§
    ’©ºjC  ã                   ó¶   — d dl mZmZ d dlZd dlmZ d dlmZmZ erd dlZ	d dl
mZ ndgZerd dlmZ ded	efd
„Z	 ddededed	eeeeef         f         fd„ZdS )é    )ÚDictÚUnionN)ÚTensor)Ú_EINOPS_AVAILABLEÚ_TORCH_VMAF_AVAILABLE)ÚVMAFÚ$video_multi_method_assessment_fusion)Ú	rearrangeÚvideoÚreturnc                 óÎ   — | dd…ddd…dd…dd…f         }| dd…ddd…dd…dd…f         }| dd…ddd…dd…dd…f         }d|z  d|z  z   d|z  z                         d¦  «        dz  S )	z/Calculate the luma component of a video tensor.Nr   é   é   g‰A`åÐ"Ó?gbX9´Èâ?gÉv¾Ÿ/½?éÿ   )Ú	unsqueeze)r   ÚrÚgÚbs       úr/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/video/vmaf.pyÚcalculate_lumar      s�   € àˆaˆaˆa��A�A�A�q�q�q˜!˜!˜!ˆmÔ€AØˆaˆaˆa��A�A�A�q�q�q˜!˜!˜!ˆmÔ€AØˆaˆaˆa��A�A�A�q�q�q˜!˜!˜!ˆmÔ€AØ�A‰I˜ ™	Ñ! E¨A¡IÑ-×8Ò8¸Ñ;Ô;¸cÑAÐAó    FÚpredsÚtargetÚfeaturesc                 óz  ‡‡‡‡‡	‡
‡‡— t           st          d¦  «        ‚| j        d         Š| j        | j        cŠ}t          | ¦  «        Št          |¦  «        Št          ¦   «                              |¦  «        Š|sXˆˆˆfd„t          ‰¦  «        D ¦   «         }t          j
        |d¬¦  «                             ¦   «                              ‰¦  «        S ˆˆˆfd„t          ‰¦  «        D ¦   «         Š
ˆ
fd„t          ‰¦  «        D ¦   «         Šˆˆfd„t          ‰¦  «        D ¦   «         Š	ˆˆ	fd	„‰	d         D ¦   «         S )
a¥  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]`.

    Args:
        preds: Video tensor of shape (batch, channels, frames, height, width). Expected to be in RGB format
            with values in range [0, 1].
        target: Video tensor of shape (batch, channels, frames, height, width). Expected to be in RGB format
            with values in range [0, 1].
        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.

    Returns:
        - 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

    Example:
        >>> import torch
        >>> from torchmetrics.functional.video import video_multi_method_assessment_fusion
        >>> # 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_score = video_multi_method_assessment_fusion(preds, target)
        >>> torch.round(vmaf_score, 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 = video_multi_method_assessment_fusion(preds, target, features=True)
        >>> # show a couple of features, more features are available
        >>> 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]])

    zSvmaf-torch is not installed. Please install with `pip install torchmetrics[video]`.r   c           	      óŒ   •— g | ]@}‰                      t          ‰|         d ¦  «        t          ‰|         d ¦  «        ¦  «        ‘ŒAS ©zc f h w -> f c h w)Úcompute_vmaf_scorer
   ©Ú.0r   Ú
preds_lumaÚtarget_lumaÚvmafs     €€€r   ú
<listcomp>z8video_multi_method_assessment_fusion.<locals>.<listcomp>{   sa   ø€ ð 
ð 
ð 
ð ð ×#Ò#Ý˜+ eÔ,Ð.BÑCÔCÅYÈzÐZ_ÔO`ÐbvÑEwÔEwñô ð
ð 
ð 
r   r   )Údimc           	      óŒ   •— g | ]@}‰                      t          ‰|         d ¦  «        t          ‰|         d ¦  «        ¦  «        ‘ŒAS r   )Útabler
   r   s     €€€r   r$   z8video_multi_method_assessment_fusion.<locals>.<listcomp>ƒ   s_   ø€ ð ð ð ð ð 	�
Š
Ý�k %Ô(Ð*>Ñ?Ô?ÅÈ:ÐV[ÔK\Ð^rÑAsÔAsñ	
ô 	
ðð ð r   c                 ó^   •— g | ])}‰|                               t          j        d ¬¦  «        ‘Œ*S )Úcoerce)Úerrors)ÚapplyÚpdÚ
to_numeric)r    r   Úscores_and_featuress     €r   r$   z8video_multi_method_assessment_fusion.<locals>.<listcomp>‰   s5   ø€ Ð
bÐ
bÐ
bÐPUÐ˜uÔ%×+Ò+­B¬MÀ(Ð+ÑKÔKÐ
bÐ
bÐ
br   c                 óH   •‡— g | ]Šˆˆˆfd „‰‰         j         D ¦   «         ‘ŒS )c                 ól   •— i | ]0}|d k    ¯|t          j        ‰‰         |         j        ‰¬¦  «        “Œ1S )ÚFrame)Údtype)ÚtorchÚtensorÚvalues)r    ÚcolÚdfsÚ
orig_dtyper   s     €€€r   ú
<dictcomp>zCvideo_multi_method_assessment_fusion.<locals>.<listcomp>.<dictcomp>‹   sA   ø€ ÐuÐuÐuÈÐfiÐmtÒftÐftˆ�eŒl˜3˜uœ: cœ?Ô1¸ÐDÑDÔDÐftÐftÐftr   )Úcolumns)r    r   r7   r8   s    @€€r   r$   z8video_multi_method_assessment_fusion.<locals>.<listcomp>Š   sO   øø€ ð ð ð àð 	vÐuÐuÐuÐuÐuÐPSÐTYÔPZÔPbÐuÑuÔuðð ð r   c           	      óp   •‡— i | ]1Š‰t          j        ˆˆfd „t          ‰¦  «        D ¦   «         ¦  «        “Œ2S )c                 ó,   •— g | ]}‰|         ‰         ‘ŒS © r=   )r    r   r6   Úresults     €€r   r$   zCvideo_multi_method_assessment_fusion.<locals>.<dictcomp>.<listcomp>Ž   s"   ø€ ÐGÐGÐG°U˜f Uœm¨CÔ0ÐGÐGÐGr   )r3   ÚstackÚrange)r    r6   r   r>   s    @€€r   r9   z8video_multi_method_assessment_fusion.<locals>.<dictcomp>Ž   sD   øø€ Ð^Ð^Ð^ÈSˆC•”ÐGÐGÐGÐGÐG½eÀA¹h¼hÐGÑGÔGÑHÔHÐ^Ð^Ð^r   )r   ÚRuntimeErrorÚshaper2   Údevicer   r   Útor@   r3   ÚcatÚt)r   r   r   rC   Úscoresr   r7   r8   r!   r>   r.   r"   r#   s        @@@@@@@@r   r	   r	   '   sŸ  øøøøøøøø€ õR !ð rÝÐpÑqÔqÐqØŒ�AŒ€AØœ e¤lÐ€J�Ý Ñ&Ô&€JÝ  Ñ(Ô(€Kå‰6Œ6�9Š9�VÑÔ€Dð ð ;ð
ð 
ð 
ð 
ð 
ð 
õ ˜q™œð	
ñ 
ô 
ˆõ Œy˜ QÐ'Ñ'Ô'×)Ò)Ñ+Ô+×.Ò.¨zÑ:Ô:Ð:ðð ð ð ð ð õ ˜1‘X”Xð	ñ ô Ðð cÐ
bÐ
bÐ
bÕY^Ð_`ÑYaÔYaÐ
bÑ
bÔ
b€Cðð ð ð ð å˜1‘X”Xðñ ô €Fð _Ð^Ð^Ð^Ð^ÐTZÐ[\ÔT]Ð^Ñ^Ô^Ð^r   )F)Útypingr   r   r3   r   Útorchmetrics.utilities.importsr   r   Úpandasr,   Ú
vmaf_torchr   Ú__doctest_skip__Úeinopsr
   r   ÚboolÚstrr	   r=   r   r   ú<module>rP      s<  ðð Ð Ð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð à SÐ SÐ SÐ SÐ SÐ SÐ SÐ Sàð @ØÐÐÐØÐÐÐÐÐÐà>Ð?Ðàð !Ø Ð Ð Ð Ð Ð ðB˜&ð B Vð Bð Bð Bð Bð ðg_ð g_Øðg_àðg_ð ðg_ð ˆ6�4˜˜V˜Ô$Ð$Ô%ð	g_ð g_ð g_ð g_ð g_ð g_r   