§
    ’©ºj '  ã                   ó€   — d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ esdgZ G d„ de
¦  «        Zd	S )
é    )ÚAnyÚDictÚListÚUnion)ÚTensor)Ú$video_multi_method_assessment_fusion)ÚMetric)Údim_zero_cat)Ú_TORCH_VMAF_AVAILABLEÚ VideoMultiMethodAssessmentFusionc                   óÂ  ‡ — e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   d	Ze
ed
<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   ee         ed<   d dededdfˆ fd„Zdededdfd„Zdeeeeef         f         fd„Zˆ xZS )!r   aY  Calculates Video Multi-Method Assessment Fusion (VMAF) metric.

    VMAF is a full-reference video quality assessment algorithm that combines multiple quality assessment features
    such as detail loss, motion, and contrast using a machine learning model to predict human perception of video
    quality more accurately than traditional metrics like PSNR or SSIM.

    The metric works by:

       1. Converting input videos to luma component (grayscale)
       2. Computing multiple elementary features:
          - Additive Detail Measure (ADM): Evaluates detail preservation at different scales
          - Visual Information Fidelity (VIF): Measures preservation of visual information across frequency bands
          - Motion: Quantifies the amount of motion in the video
       3. Combining these features using a trained SVM model to predict quality

    .. note::
       This implementation requires you to have vmaf-torch installed: https://github.com/alvitrioliks/VMAF-torch.
       Install either by cloning the repository and running ``pip install .``
       or with ``pip install torchmetrics[video]``.

    As input to ``forward`` and ``update`` the metric accepts the following input:

        - ``preds`` (:class:`~torch.Tensor`): Video tensor of shape ``(batch, channels, frames, height, width)``.
          Expected to be in RGB format with values in range [0, 1].
        - ``target`` (:class:`~torch.Tensor`): Video tensor of shape ``(batch, channels, frames, height, width)``.
          Expected to be in RGB format with values in range [0, 1].

    As output of ``forward`` and ``compute`` the metric returns the following output ``vmaf`` (:class:`~torch.Tensor`):

        - If ``features`` is False, returns a tensor with shape (batch, frame)
          of VMAF score for each frame in each video. Higher scores indicate better quality, with typical values
          ranging from 0 to 100.
        - If ``features`` is True, returns a dictionary where each value is a (batch, frame) tensor of the
          corresponding feature. The keys are:

            - 'integer_motion2': Integer motion feature
            - 'integer_motion': Integer motion feature
            - 'integer_adm2': Integer ADM feature
            - 'integer_adm_scale0': Integer ADM feature at scale 0
            - 'integer_adm_scale1': Integer ADM feature at scale 1
            - 'integer_adm_scale2': Integer ADM feature at scale 2
            - 'integer_adm_scale3': Integer ADM feature at scale 3
            - 'integer_vif_scale0': Integer VIF feature at scale 0
            - 'integer_vif_scale1': Integer VIF feature at scale 1
            - 'integer_vif_scale2': Integer VIF feature at scale 2
            - 'integer_vif_scale3': Integer VIF feature at scale 3
            - 'vmaf': VMAF score for each frame in each video

    Args:
        features: If True, all the elementary features (ADM, VIF, motion) are returned along with the VMAF score in
            a dictionary. This corresponds to the output you would get from the VMAF command line tool with
            the ``--csv`` option enabled. If False, only the VMAF score is returned as a tensor.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        RuntimeError:
            If vmaf-torch is not installed.
        ValueError:
            If ``features`` is not a boolean.

    Example:
        >>> import torch
        >>> from torchmetrics.video import VideoMultiMethodAssessmentFusion
        >>> # 2 videos, 3 channels, 10 frames, 32x32 resolution
        >>> preds = torch.rand(2, 3, 10, 32, 32, generator=torch.manual_seed(42))
        >>> target = torch.rand(2, 3, 10, 32, 32, generator=torch.manual_seed(43))
        >>> vmaf = VideoMultiMethodAssessmentFusion()
        >>> torch.round(vmaf(preds, target), decimals=2)
        tensor([[ 9.9900, 15.9000, 14.2600, 16.6100, 15.9100, 14.3000, 13.5800, 13.4900, 15.4700, 20.2800],
                [ 6.2500, 11.3000, 17.3000, 11.4600, 19.0600, 14.9300, 14.0500, 14.4100, 12.4700, 14.8200]])
        >>> vmaf = VideoMultiMethodAssessmentFusion(features=True)
        >>> vmaf_dict = vmaf(preds, target)
        >>> vmaf_dict['vmaf'].round(decimals=2)
        tensor([[ 9.9900, 15.9000, 14.2600, 16.6100, 15.9100, 14.3000, 13.5800, 13.4900, 15.4700, 20.2800],
                [ 6.2500, 11.3000, 17.3000, 11.4600, 19.0600, 14.9300, 14.0500, 14.4100, 12.4700, 14.8200]])
        >>> vmaf_dict['integer_adm2'].round(decimals=2)
        tensor([[0.4500, 0.4500, 0.3600, 0.4700, 0.4300, 0.3600, 0.3900, 0.4100, 0.3700, 0.4700],
                [0.4200, 0.3900, 0.4400, 0.3700, 0.4500, 0.3900, 0.3800, 0.4800, 0.3900, 0.3900]])

    FÚis_differentiableTÚhigher_is_betterÚfull_state_updateg        Úplot_lower_boundg      Y@Úplot_upper_boundÚ
vmaf_scoreÚinteger_motion2Úinteger_motionÚinteger_adm2Úinteger_adm_scale0Úinteger_adm_scale1Úinteger_adm_scale2Úinteger_adm_scale3Úinteger_vif_scale0Úinteger_vif_scale1Úinteger_vif_scale2Úinteger_vif_scale3ÚfeaturesÚkwargsÚreturnNc                 ó  •—  t          ¦   «         j        di |¤Ž t          st          d¦  «        ‚t	          |t
          ¦  «        st          d¦  «        ‚|| _        |                      dg d¬¦  «         | j        �r
|                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         |                      d	g d¬¦  «         |                      d
g d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         d S d S )NzSvmaf-torch is not installed. Please install with `pip install torchmetrics[video]`.zGArgument `elementary_features` should be a boolean, but got {features}.r   Úcat)ÚdefaultÚdist_reduce_fxr   r   r   r   r   r   r   r   r   r   r   © )	ÚsuperÚ__init__r   ÚRuntimeErrorÚ
isinstanceÚboolÚ
ValueErrorr   Ú	add_state)Úselfr   r    Ú	__class__s      €úg/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/video/vmaf.pyr(   z)VideoMultiMethodAssessmentFusion.__init__€   s¨  ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý$ð 	vÝÐtÑuÔuÐuå˜(¥DÑ)Ô)ð 	hÝÐfÑgÔgÐgØ ˆŒà�Š�|¨RÀˆÑFÔFÐFØŒ=ñ 	SØ�NŠNÐ,°bÈˆNÑOÔOÐOØ�NŠNÐ+°RÈˆNÑNÔNÐNØ�NŠN˜>°2ÀeˆNÑLÔLÐLØ�NŠNÐ/¸ÈEˆNÑRÔRÐRØ�NŠNÐ/¸ÈEˆNÑRÔRÐRØ�NŠNÐ/¸ÈEˆNÑRÔRÐRØ�NŠNÐ/¸ÈEˆNÑRÔRÐRØ�NŠNÐ/¸ÈEˆNÑRÔRÐRØ�NŠNÐ/¸ÈEˆNÑRÔRÐRØ�NŠNÐ/¸ÈEˆNÑRÔRÐRØ�NŠNÐ/¸ÈEˆNÑRÔRÐRÐRÐRð	Sð 	Só    ÚpredsÚtargetc                 óÔ  — t          ||| j        ¦  «        }| j        �r˜t          |t          ¦  «        �r‚| j                             |d         ¦  «         | j                             |d         ¦  «         | j                             |d         ¦  «         | j                             |d         ¦  «         | j	                             |d         ¦  «         | j
                             |d         ¦  «         | j                             |d         ¦  «         | j                             |d         ¦  «         | j                             |d	         ¦  «         | j                             |d
         ¦  «         | j                             |d         ¦  «         | j                             |d         ¦  «         dS t          |t"          ¦  «        r| j                             |¦  «         dS dS )z*Update state with predictions and targets.Úvmafr   r   r   r   r   r   r   r   r   r   r   N)r   r   r*   Údictr   Úappendr   r   r   r   r   r   r   r   r   r   r   r   )r.   r2   r3   Úscores       r0   Úupdatez'VideoMultiMethodAssessmentFusion.update—   sÆ  € å4°U¸FÀDÄMÑRÔRˆØŒ=ñ 	*�Z¨­tÑ4Ô4ñ 	*ØŒO×"Ò" 5¨¤=Ñ1Ô1Ð1ØÔ ×'Ò'¨Ð.?Ô(@ÑAÔAÐAØÔ×&Ò& uÐ-=Ô'>Ñ?Ô?Ð?ØÔ×$Ò$ U¨>Ô%:Ñ;Ô;Ð;ØÔ#×*Ò*¨5Ð1EÔ+FÑGÔGÐGØÔ#×*Ò*¨5Ð1EÔ+FÑGÔGÐGØÔ#×*Ò*¨5Ð1EÔ+FÑGÔGÐGØÔ#×*Ò*¨5Ð1EÔ+FÑGÔGÐGØÔ#×*Ò*¨5Ð1EÔ+FÑGÔGÐGØÔ#×*Ò*¨5Ð1EÔ+FÑGÔGÐGØÔ#×*Ò*¨5Ð1EÔ+FÑGÔGÐGØÔ#×*Ò*¨5Ð1EÔ+FÑGÔGÐGÐGÐGÝ˜�vÑ&Ô&ð 	*ØŒO×"Ò" 5Ñ)Ô)Ð)Ð)Ð)ð	*ð 	*r1   c                 ó  — | j         rçt          | j        ¦  «        t          | j        ¦  «        t          | j        ¦  «        t          | j        ¦  «        t          | j        ¦  «        t          | j        ¦  «        t          | j        ¦  «        t          | j	        ¦  «        t          | j
        ¦  «        t          | j        ¦  «        t          | j        ¦  «        t          | j        ¦  «        dœS t          | j        ¦  «        S )zCompute final VMAF score.)r5   r   r   r   r   r   r   r   r   r   r   r   )r   r
   r   r   r   r   r   r   r   r   r   r   r   r   )r.   s    r0   Úcomputez(VideoMultiMethodAssessmentFusion.computeª   sÔ   € àŒ=ð 	å$ T¤_Ñ5Ô5Ý#/°Ô0DÑ#EÔ#EÝ".¨tÔ/BÑ"CÔ"CÝ ,¨TÔ->Ñ ?Ô ?Ý&2°4Ô3JÑ&KÔ&KÝ&2°4Ô3JÑ&KÔ&KÝ&2°4Ô3JÑ&KÔ&KÝ&2°4Ô3JÑ&KÔ&KÝ&2°4Ô3JÑ&KÔ&KÝ&2°4Ô3JÑ&KÔ&KÝ&2°4Ô3JÑ&KÔ&KÝ&2°4Ô3JÑ&KÔ&Kðð ð õ ˜DœOÑ,Ô,Ð,r1   )F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r+   Ú__annotations__r   r   r   Úfloatr   r   r   r   r(   r9   r   r   Ústrr;   Ú__classcell__)r/   s   @r0   r   r      sÞ  ø€ € € € € € ðOð Oðb $Ð�tÐ#Ð#Ñ#Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�eÐ!Ð!Ñ!Ø#Ð�eÐ#Ð#Ñ#à�V”ÐÐÑØ˜&”\Ð!Ð!Ñ!Ø˜”LÐ Ð Ñ Ø�v”,ÐÐÑØ˜VœÐ$Ð$Ñ$Ø˜VœÐ$Ð$Ñ$Ø˜VœÐ$Ð$Ñ$Ø˜VœÐ$Ð$Ñ$Ø˜VœÐ$Ð$Ñ$Ø˜VœÐ$Ð$Ñ$Ø˜VœÐ$Ð$Ñ$Ø˜VœÐ$Ð$Ñ$ðSð S ð S¸ð SÀð Sð Sð Sð Sð Sð Sð.*˜Fð *¨Fð *°tð *ð *ð *ð *ð&-˜˜v t¨C°¨KÔ'8Ð8Ô9ð -ð -ð -ð -ð -ð -ð -ð -r1   N)Útypingr   r   r   r   Útorchr   Ú"torchmetrics.functional.video.vmafr   Útorchmetrics.metricr	   Útorchmetrics.utilities.datar
   Útorchmetrics.utilities.importsr   Ú__doctest_skip__r   r&   r1   r0   ú<module>rK      sÔ   ðð *Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )à Ð Ð Ð Ð Ð à SÐ SÐ SÐ SÐ SÐ SØ &Ð &Ð &Ð &Ð &Ð &Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø @Ð @Ð @Ð @Ð @Ð @àð <Ø:Ð;Ðð`-ð `-ð `-ð `-ð `- vñ `-ô `-ð `-ð `-ð `-r1   