§
    ’©ºj¼/  ã                   óœ  — d dl Z d dlmZ d dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZ erd dlmZ d dlmZ d	d
dœZe
d         ZdZeresdgZ G d„ dej        ¦  «        Z G d„ de¦  «        Z	 d#dedej        dededeeeeef         f         f
d„Z	 d$dedeeef         de
d         defd„Z	 	 	 	 d%ded!ede
d         dededefd"„ZdS )&é    N)ÚUnion)ÚTensorÚnn©Ú	normalize)ÚLiteral)Ú_TORCH_GREATER_EQUAL_2_2Ú_TORCHVISION_AVAILABLE)Ú
transforms)Úresnet50)é   é   )r   éd   )Úkadid10kÚkoniq10kz=https://github.com/miccunifi/ARNIQA/releases/download/weightsÚarniqac            	       ó‚   ‡ — e Zd ZdZddeddfˆ fd„Zdd„Zdd	ed
ede	eef         fd„Z
dedefd„Zdd	ed
edefd„Zˆ xZS )Ú_ARNIQAzÌInitializes a No-Reference Image Quality Assessment ARNIQA torch.nn.Module.

    Args:
        regressor_dataset: dataset used for training the regressor, choose between [``koniq10k``, ``kadid10k``]

    r   Úregressor_datasetÚreturnNc                 ó¾  •— t          ¦   «                              ¦   «          t          st          d¦  «        ‚t          st          d¦  «        ‚t                               ¦   «         }||vrt          d|› d|› d�¦  «        ‚|| _	        g d¢| _
        g d¢| _        t          ¦   «         }|j        j        | _        t!          j        t%          |                     ¦   «         ¦  «        d d…         Ž }|| _        t!          j        | j        d	z  d
¦  «        | _        |                      ¦   «          dt           j        dd fd„} || j        ¦  «          || j        ¦  «         d S )Nz'ARNIQA metric requires PyTorch >= 2.2.0z‡ARNIQA metric requires that torchvision is installed. Either install as `pip install torchmetrics[image]` or `pip install torchvision`.z,Argument `regressor_dataset` must be one of ú
, but got ú.)g
×£p=
ß?gÉv¾Ÿ/Ý?g–C‹lçûÙ?)gZd;ßOÍ?gyé&1¬Ì?gÍÌÌÌÌÌÌ?éÿÿÿÿé   r   Úmoduler   c                 ój   — |                       ¦   «          |                      ¦   «         D ]	}d|_        Œ
d S )NF)ÚevalÚ
parametersÚrequires_grad)r   Úps     út/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/image/arniqa.pyÚ_freezez!_ARNIQA.__init__.<locals>._freezeU   s=   € Ø�KŠK‰MŒMˆMØ×&Ò&Ñ(Ô(ð (ð (�Ø"'�”�ð(ð (ó    )ÚsuperÚ__init__r	   ÚRuntimeErrorr
   ÚModuleNotFoundErrorÚ_AVAILABLE_REGRESSOR_DATASETSÚkeysÚ
ValueErrorr   Úimagenet_norm_meanÚimagenet_norm_stdr   ÚfcÚin_featuresÚfeat_dimr   Ú
SequentialÚlistÚchildrenÚencoderÚLinearÚ	regressorÚ_load_weightsÚModule)Úselfr   Úvalid_regressor_datasetsr4   r#   Ú	__class__s        €r"   r&   z_ARNIQA.__init__8   sƒ  ø€ Ý‰Œ×ÒÑÔÐå'ð 	JÝÐHÑIÔIÐIå%ð 	Ý%ðeñô ð õ
 $A×#EÒ#EÑ#GÔ#GÐ ØÐ$<Ð<Ð<ÝØwÐ?WÐwÐwÐctÐwÐwÐwñô ð ð "3ˆÔØ"7Ð"7Ð"7ˆÔØ!6Ð!6Ð!6ˆÔå‘*”*ˆØœ
Ô.ˆŒÝ”-¥ g×&6Ò&6Ñ&8Ô&8Ñ!9Ô!9¸#¸2¸#Ô!>Ð?ˆØˆŒÝœ 4¤=°1Ñ#4°aÑ8Ô8ˆŒØ×ÒÑÔÐð	(�BœIð 	(¨$ð 	(ð 	(ð 	(ð 	(ð
 	ˆ�”ÑÔÐØˆ�”ÑÔÐÐÐr$   c                 ó¤  — t           j                             t          › d�dd¬¦  «        }d„ |                     ¦   «         D ¦   «         }| j                             |d¬¦  «         t          j        ¦   «         5  t          j	        dt          d¬	¦  «         t           j                             t          › d
| j        › d�dd¬¦  «                             ¦   «         }|                     d¦  «        |d<   |                     d¦  «                             d¦  «        |d<   | j                             |d¬¦  «         ddd¦  «         dS # 1 swxY w Y   dS )z/Loads the weights of the encoder and regressor.z/ARNIQA.pthTÚcpu)ÚprogressÚmap_locationc                 óJ   — i | ] \  }}d |v¯	|                      dd¦  «        |“Œ!S )Ú	projectorzmodel.Ú )Úreplace)Ú.0ÚkÚvs      r"   ú
<dictcomp>z)_ARNIQA._load_weights.<locals>.<dictcomp>b   s?   € ð '
ð '
ð '
Ù+/¨1¨aÐQ\ÐdeÐQeÐQeˆA�IŠI�h Ñ#Ô# QÐQeÐQeÐQer$   )ÚstrictÚignoreztorch.serialization)Úcategoryr   z/regressor_z.pthÚweightsÚweightÚbiasesr   ÚbiasN)ÚtorchÚhubÚload_state_dict_from_urlÚ	_base_urlÚitemsr4   Úload_state_dictÚwarningsÚcatch_warningsÚfilterwarningsÚUserWarningr   Ú
state_dictÚpopÚ	unsqueezer6   )r9   Úencoder_state_dictÚfiltered_encoder_state_dictÚregressor_state_dicts       r"   r7   z_ARNIQA._load_weights]   s¿  € å"œY×?Ò?ÝÐ%Ð%Ð%°À5ð @ñ 
ô 
Ðð'
ð '
Ø3E×3KÒ3KÑ3MÔ3Mð'
ñ '
ô '
Ð#ð 	Œ×$Ò$Ð%@ÈÐ$ÑNÔNÐNåÔ$Ñ&Ô&ð 	Nð 	NÝÔ# Hµ{ÐK`ÐaÑaÔaÐaÝ#(¤9×#EÒ#EÝÐEÐE¨Ô)?ÐEÐEÐEÐPTÐchð $Fñ $ô $çŠj‰lŒlð !ð .B×-EÒ-EÀiÑ-PÔ-PÐ  Ñ*Ø+?×+CÒ+CÀHÑ+MÔ+M×+WÒ+WÐXYÑ+ZÔ+ZÐ  Ñ(ØŒN×*Ò*Ð+?ÈÐ*ÑMÔMÐMð	Nð 	Nð 	Nñ 	Nô 	Nð 	Nð 	Nð 	Nð 	Nð 	Nð 	Nð 	Nøøøð 	Nð 	Nð 	Nð 	Nð 	Nð 	Ns   Á8C EÅE	ÅE	FÚimgr   c                 ó   — |j         dd…         \  }} t          j        |dz  |dz  f¦  «        |¦  «        }|rR t          j        | j        | j        ¬¦  «        |¦  «        } t          j        | j        | j        ¬¦  «        |¦  «        }||fS )zŽPreprocesses the input to the model.

        Obtains the half-scale version of the input image and applies normalization if needed.

        éþÿÿÿNr   )ÚmeanÚstd)Úshaper   ÚResizeÚ	Normalizer,   r-   )r9   r_   r   ÚhÚwÚimg_dss         r"   Ú_preprocess_inputz_ARNIQA._preprocess_inputq   s�   € ð Œy˜˜˜Œ~‰ˆˆ1Ø4•Ô" A¨¡F¨A°©FÐ#3Ñ4Ô4°SÑ9Ô9ˆØð 	lØ`•*Ô&¨DÔ,CÈÔI_Ð`Ñ`Ô`ÐadÑeÔeˆCØc•ZÔ)¨tÔ/FÈDÔLbÐcÑcÔcÐdjÑkÔkˆFØ�Fˆ{Ðr$   Úscorec                 óB   — t           | j                 \  }}||z
  ||z
  z  S )zKScales the quality score to be in the [0, 1] range, where higher is better.)r)   r   )r9   rk   Ú	min_scoreÚ	max_scores       r"   Ú_scale_scorez_ARNIQA._scale_score~   s)   € å<¸TÔ=SÔTÑˆ	�9Ø˜	Ñ! i°)Ñ&;Ñ<Ð<r$   c                 ó¸  — |                       ||¦  «        \  }}|                      |¦  «        }|                     d| j        ¦  «        }t	          |d¬¦  «        }|                      |¦  «        }|                     d| j        ¦  «        }t	          |d¬¦  «        }t          j        ||f¦  «        }|                      |¦  «        }|                      |¦  «        S )Nr   r   )Údim)	rj   r4   Úviewr0   Únormalize_fnrO   Úhstackr6   ro   )r9   r_   r   ri   Úimg_fÚimg_ds_fÚfrk   s           r"   Úforwardz_ARNIQA.forwardƒ   sÈ   € à×,Ò,¨S°)Ñ<Ô<‰ˆˆVð —’˜SÑ!Ô!ˆØ—
’
˜2˜tœ}Ñ-Ô-ˆÝ˜U¨Ð*Ñ*Ô*ˆØ—<’< Ñ'Ô'ˆØ—=’=  T¤]Ñ3Ô3ˆÝ ¨aÐ0Ñ0Ô0ˆÝŒL˜% Ð*Ñ+Ô+ˆð —’˜qÑ!Ô!ˆØ× Ò  Ñ'Ô'Ð'r$   )r   )r   N©F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_TYPE_REGRESSOR_DATASETr&   r7   r   ÚboolÚtuplerj   ro   rx   Ú__classcell__©r;   s   @r"   r   r   0   sý   ø€ € € € € ðð ð# ð # Ð*Að # ÐSWð # ð # ð # ð # ð # ð # ðJNð Nð Nð Nð(ð  Vð ¸ð ÈÈvÐW]È~ÔI^ð ð ð ð ð= &ð =¨Vð =ð =ð =ð =ð
(ð (˜6ð (¨dð (¸vð (ð (ð (ð (ð (ð (ð (ð (r$   r   c                   ó,   ‡ — e Zd ZdZdedd fˆ fd„Zˆ xZS )Ú_NoTrainArniqaz9Wrapper to make sure ARNIQA never leaves evaluation mode.Úmoder   c                 óF   •— t          ¦   «                              d¦  «        S )z.Force network to always be in evaluation mode.F)r%   Útrain)r9   r…   r;   s     €r"   r‡   z_NoTrainArniqa.train˜   s   ø€ å‰wŒw�}Š}˜UÑ#Ô#Ð#r$   )rz   r{   r|   r}   r   r‡   r�   r‚   s   @r"   r„   r„   •   sR   ø€ € € € € ØCÐCð$˜$ð $Ð#3ð $ð $ð $ð $ð $ð $ð $ð $ð $ð $r$   r„   Fr_   Úmodelr   Úautocastr   c                 ó€  — | j         dk    r| j        d         dk    st          d| j        › d�¦  «        ‚|                      ¦   «         dk    r|                      ¦   «         dk    s<|r:t          d|                      ¦   «         › d	|                      ¦   «         › d�¦  «        ‚|rVt
          j                             | j        j	        | j
        ¬
¦  «        5   || |¬¦  «        }ddd¦  «         n# 1 swxY w Y   n& |                     | j
        ¬¦  «        | |¬¦  «        }|                     ¦   «         | j        d         fS )a  Update step for ARNIQA metric.

    Args:
        img: the input image
        model: the pre-trained model
        normalize: boolean indicating whether the input image is normalized
        autocast: boolean indicating whether to use automatic mixed precision

    é   r   é   z?Input image must have shape [N, 3, H, W]. Got input with shape r   g      ð?g        zdInput image values must be in the [0, 1] range when normalize==True. Got input with values in range z and )Údevice_typeÚdtyper   N)rŽ   r   )Úndimrd   r+   ÚmaxÚminrO   Úampr‰   ÚdeviceÚtyperŽ   ÚtoÚsqueeze)r_   rˆ   r   r‰   Úlosss        r"   Ú_arniqa_updater˜   �   s‚  € ð ŒH˜ŠMˆM˜cœi¨œl¨aÒ/Ð/ÝÐgÐ[^Ô[dÐgÐgÐgÑhÔhÐhØ�GŠG‰IŒI˜ÒÐ §¢¡¤¨cÒ!1Ð!1°yÐ!1Ýð6ØŸš™œð6ð 6Ø),¯ª©¬ð6ð 6ð 6ñ
ô 
ð 	
ð
 ð CÝŒY×Ò¨C¬J¬OÀ3Ä9ÐÑMÔMð 	3ð 	3Ø�5˜¨	Ð2Ñ2Ô2ˆDð	3ð 	3ð 	3ñ 	3ô 	3ð 	3ð 	3ð 	3ð 	3ð 	3ð 	3øøøð 	3ð 	3ð 	3ð 	3øð )ˆu�xŠx˜cœiˆxÑ(Ô(¨¸	ÐBÑBÔBˆØ�<Š<‰>Œ>˜3œ9 Qœ<Ð'Ð's   ÃC-Ã-C1Ã4C1rb   ÚscoresÚ
num_scoresÚ	reduction)Úsumrb   Únonec                 óT   — |                       ¦   «         }|dk    r| S |dk    r||z  S |S )zCompute step for ARNIQA metric.r�   rb   )rœ   )r™   rš   r›   Ú
sum_scoress       r"   Ú_arniqa_computer    º   s=   € ð —’‘”€JØ�FÒÐØˆØ�FÒÐØ˜JÑ&Ð&ØÐr$   r   Tr   c                 ó2  — d}||vrt          d|› d|› �¦  «        ‚t          |t          ¦  «        st          d|› �¦  «        ‚t          |¬¦  «                             | j        | j        ¬¦  «        }t          | |||¬¦  «        \  }}t          |||¦  «        S )a  ARNIQA: leArning distoRtion maNifold for Image Quality Assessment metric.

    `ARNIQA`_ is a No-Reference Image Quality Assessment metric that predicts the technical quality of an image with
    a high correlation with human opinions. ARNIQA consists of an encoder and a regressor. The encoder is a ResNet-50
    model trained in a self-supervised way to model the image distortion manifold to generate similar representation for
    images with similar distortions, regardless of the image content. The regressor is a linear model trained on IQA
    datasets using the ground-truth quality scores. ARNIQA extracts the features from the full- and half-scale versions
    of the input image and then outputs a quality score in the [0, 1] range, where higher is better.

    The input image is expected to have shape ``(N, 3, H, W)``. The image should be in the [0, 1] range if `normalize`
    is set to ``True``, otherwise it should be normalized with the ImageNet mean and standard deviation.

    .. note::
        Using this metric requires you to have ``torchvision`` package installed. Either install as
        ``pip install torchmetrics[image]`` or ``pip install torchvision``.

    Args:
        img: the input image
        regressor_dataset: dataset used for training the regressor. Choose between [``koniq10k``, ``kadid10k``].
            ``koniq10k`` corresponds to the `KonIQ-10k`_ dataset, which consists of real-world images with authentic
            distortions. ``kadid10k`` corresponds to the `KADID-10k`_ dataset, which consists of images with
            synthetically generated distortions.
        reduction: indicates how to reduce over the batch dimension. Choose between [``sum``, ``mean``, ``none``].
        normalize: by default this is ``True`` meaning that the input is expected to be in the [0, 1] range. If set
            to ``False`` will instead expect input to be already normalized with the ImageNet mean and standard
            deviation.
        autocast: boolean indicating whether to use automatic mixed precision

    Returns:
        A tensor in the [0, 1] range, where higher is better, representing the ARNIQA score of the input image. If
        `reduction` is set to ``none``, the output will have shape ``(N,)``, otherwise it will be a scalar tensor.

    Raises:
        ModuleNotFoundError:
            If ``torchvision`` package is not installed
        ValueError:
            If ``regressor_dataset`` is not in [``"kadid10k"``, ``"koniq10k"``]
        ValueError:
            If ``reduction`` is not in [``"sum"``, ``"mean"``, ``"none"``]
        ValueError:
            If ``normalize`` is not a bool
        ValueError:
            If the input image is not a valid image tensor with shape [N, 3, H, W].
        ValueError:
            If the input image values are not in the [0, 1] range when ``normalize`` is set to ``True``

    Examples:
        >>> from torch import rand
        >>> from torchmetrics.functional.image.arniqa import arniqa
        >>> img = rand(8, 3, 224, 224)
        >>> # Non-normalized input
        >>> arniqa(img, regressor_dataset='koniq10k', normalize=True)
        tensor(0.5308)


        >>> from torch import rand
        >>> from torchmetrics.functional.image.arniqa import arniqa
        >>> from torchvision.transforms import Normalize
        >>> img = rand(8, 3, 224, 224)
        >>> img = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(img)
        >>> # Normalized input
        >>> arniqa(img, regressor_dataset='koniq10k', normalize=False)
        tensor(0.5065)

    )rb   rœ   r�   z$Argument `reduction` must be one of r   z.Argument `normalize` should be a bool but got )r   )r“   rŽ   )r   r‰   )	r+   Ú
isinstancer   r„   r•   r“   rŽ   r˜   r    )	r_   r   r›   r   r‰   Úvalid_reductionrˆ   r—   rš   s	            r"   r   r   Æ   s·   € ðP .€OØ˜Ð'Ð'ÝÐfÀÐfÐfÐ[dÐfÐfÑgÔgÐgå�i¥Ñ&Ô&ð WÝÐUÈ)ÐUÐUÑVÔVÐVåÐ->Ð?Ñ?Ô?×BÒBÈ#Ì*Ð\_Ô\eÐBÑfÔf€EÝ% c¨5¸IÐPXÐYÑYÔYÑ€Dˆ*Ý˜4 ¨YÑ7Ô7Ð7r$   ry   )rb   )r   rb   TF)rU   Útypingr   rO   r   r   Útorch.nn.functionalr   rs   Útyping_extensionsr   Útorchmetrics.utilities.importsr	   r
   Útorchvisionr   Útorchvision.modelsr   r)   r~   rR   Ú__doctest_skip__r8   r   r„   r   r€   Úintr˜   r    r   © r$   r"   ú<module>r­      st  ðð( €€€Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø %Ð %Ð %Ð %Ð %Ð %à [Ð [Ð [Ð [Ð [Ð [Ð [Ð [àð ,Ø&Ð&Ð&Ð&Ð&Ð&Ø+Ð+Ð+Ð+Ð+Ð+ð Øð!ð !Ð ð
 "Ð"8Ô9Ð àK€	ð 	!ð "Ð%;ð "Ø �zÐðb(ð b(ð b(ð b(ð b(ˆbŒiñ b(ô b(ð b(ðJ$ð $ð $ð $ð $�Wñ $ô $ð $ð FKð(ð (Ø	ð(Øœ	ð(Ø.2ð(Ø>Bð(à
ˆ6�5˜˜f˜Ô%Ð%Ô&ð(ð (ð (ð (ð< agð	ð 	Øð	Ø % f¨c kÔ 2ð	Ø?FÐG\Ô?]ð	àð	ð 	ð 	ð 	ð 2<Ø06ØØðQ8ð Q8Ø	ðQ8à.ðQ8ð Ð,Ô-ðQ8ð ð	Q8ð
 ðQ8ð ðQ8ð Q8ð Q8ð Q8ð Q8ð Q8r$   