§
    ’©ºjš%  ã                   óØ   — d dl mZ 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mZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZmZ esdgZesddgZ G d„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnion)ÚTensor)ÚLiteral)Ú"_spectral_distortion_index_computeÚ!_spectral_distortion_index_update)Ú!_spatial_distortion_index_computeÚ _spatial_distortion_index_update)ÚMetric)Úrank_zero_warn)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLEÚ_TORCHVISION_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzQualityWithNoReference.plotÚQualityWithNoReferencec                   ót  ‡ — 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<   	 	 	 	 	 d"de
de
dededed         deddfˆ fd„Zdedeeef         ddfd„Zdefd„Z	 d#deeeee         f                  d ee         defd!„Zˆ xZS )$r   aì  Compute Quality with No Reference (QualityWithNoReference_) also now as QNR.

    The metric is used to compare the joint spectral and spatial distortion between two images.

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

    - ``preds`` (:class:`~torch.Tensor`): High resolution multispectral image of shape ``(N,C,H,W)``.
    - ``target`` (:class:`~Dict`): A dictionary containing the following keys:

      - ``ms`` (:class:`~torch.Tensor`): Low resolution multispectral image of shape ``(N,C,H',W')``.
      - ``pan`` (:class:`~torch.Tensor`): High resolution panchromatic image of shape ``(N,C,H,W)``.
      - ``pan_lr`` (:class:`~torch.Tensor`): (optional) Low resolution panchromatic image of shape ``(N,C,H',W')``.

    where H and W must be multiple of H' and W'.

    When ``pan_lr`` is ``None``, a uniform filter will be applied on ``pan`` to produce a degraded image. The degraded
    image is then resized to match the size of ``ms`` and served as ``pan_lr`` in the calculation.

    As output of `forward` and `compute` the metric returns the following output

    - ``qnr`` (:class:`~torch.Tensor`): if ``reduction!='none'`` returns float scalar tensor with average QNR value
      over sample else returns tensor of shape ``(N,)`` with QNR values per sample

    Args:
        alpha: Relevance of spectral distortion.
        beta: Relevance of spatial distortion.
        norm_order: Order of the norm applied on the difference.
        window_size: Window size of the filter applied to degrade the high resolution panchromatic image.
        reduction: a method to reduce metric score over labels.

            - ``'elementwise_mean'``: takes the mean (default)
            - ``'sum'``: takes the sum
            - ``'none'``: no reduction will be applied

        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torch import rand
        >>> from torchmetrics.image import QualityWithNoReference
        >>> preds = rand([16, 3, 32, 32])
        >>> target = {
        ...     'ms': rand([16, 3, 16, 16]),
        ...     'pan': rand([16, 3, 32, 32]),
        ... }
        >>> qnr = QualityWithNoReference()
        >>> qnr(preds, target)
        tensor(0.9694)

    TÚhigher_is_betterÚis_differentiableFÚfull_state_updateg        Úplot_lower_boundg      ð?Úplot_upper_boundÚpredsÚmsÚpanÚpan_lré   é   Úelementwise_meanÚalphaÚbetaÚ
norm_orderÚwindow_sizeÚ	reduction©r"   ÚsumÚnoneÚkwargsÚreturnNc                 ó   •—  t          ¦   «         j        di |¤Ž t          d¦  «         t          |t          t
          f¦  «        r|dk     rt          d|› d�¦  «        ‚|| _        t          |t          t
          f¦  «        r|dk     rt          d|› d�¦  «        ‚|| _        t          |t          ¦  «        r|dk    rt          d|› d�¦  «        ‚|| _	        t          |t          ¦  «        r|dk    rt          d|› d�¦  «        ‚|| _
        d}||vrt          d	|› d
|› �¦  «        ‚|| _        |                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         |                      dg d¬¦  «         d S )NzŒMetric `QualityWithNoReference` will save all targets and predictions in buffer. For large datasets this may lead to large memory footprint.r   z>Expected `alpha` to be a non-negative real number. Got alpha: ú.z<Expected `beta` to be a non-negative real number. Got beta: z@Expected `norm_order` to be a positive integer. Got norm_order: zBExpected `window_size` to be a positive integer. Got window_size: r(   z(Expected argument `reduction` be one of z	 but got r   Úcat)ÚdefaultÚdist_reduce_fxr   r   r   © )ÚsuperÚ__init__r   Ú
isinstanceÚintÚfloatÚ
ValueErrorr#   r$   r%   r&   r'   Ú	add_state)	Úselfr#   r$   r%   r&   r'   r+   Úallowed_reductionsÚ	__class__s	           €úf/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/image/qnr.pyr4   zQualityWithNoReference.__init__b   sÛ  ø€ ð 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"ÝðKñ	
ô 	
ð 	
õ
 ˜%¥#¥u Ñ.Ô.ð 	h°%¸!²)°)ÝÐfÐ^cÐfÐfÐfÑgÔgÐgØˆŒ
Ý˜$¥¥e Ñ-Ô-ð 	e°¸²°ÝÐcÐ\`ÐcÐcÐcÑdÔdÐdØˆŒ	Ý˜*¥cÑ*Ô*ð 	o¨j¸Aªo¨oÝÐmÐ`jÐmÐmÐmÑnÔnÐnØ$ˆŒÝ˜+¥sÑ+Ô+ð 	r¨{¸aÒ/?Ð/?ÝÐpÐbmÐpÐpÐpÑqÔqÐqØ&ˆÔØ@ÐØÐ.Ð.Ð.ÝÐpÐHZÐpÐpÐenÐpÐpÑqÔqÐqØ"ˆŒØ�Š�w¨¸5ˆÑAÔAÐAØ�Š�t R¸ˆÑ>Ô>Ð>Ø�Š�u b¸ˆÑ?Ô?Ð?Ø�Š�x¨¸EˆÑBÔBÐBÐBÐBó    Útargetc                 óH  — d|vr%t          d|                     ¦   «         › d�¦  «        ‚d|vr%t          d|                     ¦   «         › d�¦  «        ‚|d         }|d         }|                     d¦  «        }t          ||¦  «        \  }}t	          ||||¦  «        \  }}}}| j                             |¦  «         | j                             |d         ¦  «         | j                             |d         ¦  «         d|v r"| j	                             |d         ¦  «         dS dS )aì  Update state with preds and target.

        Args:
            preds: High resolution multispectral image.
            target: A dictionary containing the following keys:

                - ``'ms'``: low resolution multispectral image.
                - ``'pan'``: high resolution panchromatic image.
                - ``'pan_lr'``: (optional) low resolution panchromatic image.

        Raises:
            ValueError:
                If ``target`` doesn't have ``ms`` and ``pan``.

        r   z0Expected `target` to have key `ms`. Got target: r.   r   z1Expected `target` to have key `pan`. Got target: r   N)
r8   ÚkeysÚgetr   r   r   Úappendr   r   r   )r:   r   r?   r   r   r   s         r=   ÚupdatezQualityWithNoReference.update†   s5  € ð  �vÐÐÝÐ`ÐPV×P[ÒP[ÑP]ÔP]Ð`Ð`Ð`ÑaÔaÐaØ˜ÐÐÝÐaÐQW×Q\ÒQ\ÑQ^ÔQ^ÐaÐaÐaÑbÔbÐbØ�DŒ\ˆØ�UŒmˆØ—’˜HÑ%Ô%ˆÝ5°e¸RÑ@Ô@‰	ˆˆrÝ!AÀ%ÈÈSÐRXÑ!YÔ!YÑˆˆr�3˜ØŒ
×Ò˜%Ñ Ô Ð ØŒ�Š�v˜d”|Ñ$Ô$Ð$ØŒ�Š˜˜uœÑ&Ô&Ð&Ø�vÐÐØŒK×Ò˜v hÔ/Ñ0Ô0Ð0Ð0Ð0ð Ðr>   c           	      óŒ  — t          | j        ¦  «        }t          | j        ¦  «        }t          | j        ¦  «        }t	          | j        ¦  «        dk    rt          | j        ¦  «        nd}t          ||| j        | j        ¦  «        }t          ||||| j        | j
        | j        ¦  «        }d|z
  | j        z  d|z
  | j        z  z  S )z.Compute and returns quality with no reference.r   Nr    )r   r   r   r   Úlenr   r
   r%   r'   r   r&   r#   r$   )r:   r   r   r   r   Úd_lambdaÚd_ss          r=   ÚcomputezQualityWithNoReference.compute¥   s¸   € å˜TœZÑ(Ô(ˆÝ˜$œ'Ñ"Ô"ˆÝ˜4œ8Ñ$Ô$ˆÝ.1°$´+Ñ.>Ô.>ÀÒ.BÐ.B•˜dœkÑ*Ô*Ð*ÈˆÝ5°e¸RÀÄÐRVÔR`ÑaÔaˆÝ/Ø�2�s˜F D¤O°TÔ5EÀtÄ~ñ
ô 
ˆð �H‘ ¤Ñ+¨q°3©w¸4¼9Ñ.DÑDÐDr>   ÚvalÚaxc                 ó.   — |                       ||¦  «        S )aˆ  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> from torch import rand
            >>> from torchmetrics.image import QualityWithNoReference
            >>> preds = rand([16, 3, 32, 32])
            >>> target = {
            ...     'ms': rand([16, 3, 16, 16]),
            ...     'pan': rand([16, 3, 32, 32]),
            ... }
            >>> metric = QualityWithNoReference()
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> from torch import rand
            >>> from torchmetrics.image import QualityWithNoReference
            >>> preds = rand([16, 3, 32, 32])
            >>> target = {
            ...     'ms': rand([16, 3, 16, 16]),
            ...     'pan': rand([16, 3, 32, 32]),
            ... }
            >>> metric = QualityWithNoReference()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(preds, target))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)r:   rJ   rK   s      r=   ÚplotzQualityWithNoReference.plot±   s   € ðd �zŠz˜#˜rÑ"Ô"Ð"r>   )r    r    r    r!   r"   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   r7   r   r   r   r6   r	   r   r4   ÚdictÚstrrD   rI   r   r   r   r   r   rN   Ú__classcell__)r<   s   @r=   r   r   $   sô  ø€ € € € € € ð0ð 0ðd "Ð�dÐ!Ð!Ñ!Ø"Ð�tÐ"Ð"Ñ"Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!à�Œ<ÐÐÑØˆVŒÐÐÑØ	ˆfŒÐÐÑØ�ŒLÐÐÑð ØØØØ@Rð"Cð "Càð"Cð ð"Cð ð	"Cð
 ð"Cð Ð<Ô=ð"Cð ð"Cð 
ð"Cð "Cð "Cð "Cð "Cð "CðH1˜Fð 1¨D°°f°Ô,=ð 1À$ð 1ð 1ð 1ð 1ð>
E˜ð 
Eð 
Eð 
Eð 
Eð _cð2#ð 2#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð2#ØIQÐRZÔI[ð2#à	ð2#ð 2#ð 2#ð 2#ð 2#ð 2#ð 2#ð 2#r>   N)Úcollections.abcr   Útypingr   r   r   r   Útorchr   Útyping_extensionsr	   Ú&torchmetrics.functional.image.d_lambdar
   r   Ú!torchmetrics.functional.image.d_sr   r   Útorchmetrics.metricr   Útorchmetrics.utilitiesr   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   r   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r2   r>   r=   ú<module>rd      s[  ðð %Ð $Ð $Ð $Ð $Ð $Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à xÐ xÐ xÐ xÐ xÐ xÐ xÐ xØ qÐ qÐ qÐ qÐ qÐ qÐ qÐ qØ &Ð &Ð &Ð &Ð &Ð &Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø XÐ XÐ XÐ XÐ XÐ XÐ XÐ XØ @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð 7Ø5Ð6Ðàð QØ0Ð2OÐPÐð#ð #ð #ð #ð #˜Vñ #ô #ð #ð #ð #r>   