§
    ’©ºj%  ã                   óœ   — d dl mZ d dlmZmZmZ d dl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mZ esd	gZ G d
„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚOptionalÚUnionN)ÚTensorÚtensor)Ú_psnrb_computeÚ_psnrb_update)ÚMetric)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEz*PeakSignalNoiseRatioWithBlockedEffect.plotc            	       ó  ‡ — e Zd ZU dZdZeed<   dZeed<   dZeed<   e	ed<   e	ed<   e	ed	<   e	ed
<   	 dd
e
eeeef         f         dededdfˆ fd„Zde	de	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 )Ú%PeakSignalNoiseRatioWithBlockedEffecta  Computes `Peak Signal to Noise Ratio With Blocked Effect`_ (PSNRB).

    .. math::
        \text{PSNRB}(I, J) = 10 * \log_{10} \left(\frac{\max(I)^2}{\text{MSE}(I, J)-\text{B}(I, J)}\right)

    Where :math:`\text{MSE}` denotes the `mean-squared-error`_ function. This metric is a modified version of PSNR that
    better supports evaluation of images with blocked artifacts, that oftens occur in compressed images.

    .. attention::
        Metric only supports grayscale images. If you have RGB images, please convert them to grayscale first.

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

    - ``preds`` (:class:`~torch.Tensor`): Predictions from model of shape ``(N,1,H,W)``
    - ``target`` (:class:`~torch.Tensor`): Ground truth values of shape ``(N,1,H,W)``

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

    - ``psnrb`` (:class:`~torch.Tensor`): float scalar tensor with aggregated PSNRB value

    Args:
        data_range: the range of the data. If a tuple is provided then the range is calculated as the difference and
            input is clamped between the values.
        block_size: integer indication the block size
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torch import rand
        >>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
        >>> preds = rand(2, 1, 10, 10)
        >>> target = rand(2, 1, 10, 10)
        >>> metric(preds, target)
        tensor(7.2893)

    TÚis_differentiableÚhigher_is_betterFÚfull_state_updateÚsum_squared_errorÚtotalÚbefÚ
data_rangeé   Ú
block_sizeÚkwargsÚreturnNc                 ó˜  •‡—  t          ¦   «         j        di |¤Ž t          |t          ¦  «        s|dk     rt	          d¦  «        ‚|| _        |                      dt          d¦  «        d¬¦  «         |                      dt          d¦  «        d¬¦  «         |                      d	t          d¦  «        d¬¦  «         t          ‰t          ¦  «        r@|                      d
t          ‰d         ‰d         z
  ¦  «        d¬¦  «         ˆfd„| _	        d S |                      d
t          t          ‰¦  «        ¦  «        d¬¦  «         d | _	        d S )Né   z4Argument ``block_size`` should be a positive integerr   g        Úsum)ÚdefaultÚdist_reduce_fxr   r   r   r   Úmeanc                 óJ   •— t          j        | ‰d         ‰d         ¬¦  «        S )Nr   r   )ÚminÚmax)ÚtorchÚclamp)Úxr   s    €úh/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/image/psnrb.pyú<lambda>z@PeakSignalNoiseRatioWithBlockedEffect.__init__.<locals>.<lambda>\   s!   ø€ ­¬°Q¸JÀq¼MÈzÐZ[Ì}Ð)]Ñ)]Ô)]€ ó    © )ÚsuperÚ__init__Ú
isinstanceÚintÚ
ValueErrorr   Ú	add_stater   ÚtupleÚclamping_fnÚfloat)Úselfr   r   r   Ú	__class__s    `  €r(   r-   z.PeakSignalNoiseRatioWithBlockedEffect.__init__K   s?  øø€ ð 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý˜*¥cÑ*Ô*ð 	U¨z¸Aª~¨~ÝÐSÑTÔTÐTØ$ˆŒà�ŠÐ*µF¸3±K´KÐPUˆÑVÔVÐVØ�Š�w­¨q©	¬	À%ˆÑHÔHÐHØ�Š�u¥f¨S¡k¤kÀ%ˆÑHÔHÐHå�j¥%Ñ(Ô(ð 	$Ø�NŠN˜<µ¸
À1¼È
ÐSTÌÑ8UÑ1VÔ1VÐgmˆNÑnÔnÐnØ]Ð]Ð]Ð]ˆDÔÐÐà�NŠN˜<µ½¸jÑ8IÔ8IÑ1JÔ1JÐ[aˆNÑbÔbÐbØ#ˆDÔÐÐr*   ÚpredsÚtargetc                 óþ   — | j         �*|                       |¦  «        }|                       |¦  «        }t          ||| j        ¬¦  «        \  }}}| xj        |z  c_        | xj        |z  c_        | xj        |z  c_        dS )z*Update state with predictions and targets.N)r   )r3   r
   r   r   r   r   )r5   r7   r8   r   r   Únum_obss         r(   Úupdatez,PeakSignalNoiseRatioWithBlockedEffect.updatea   s‰   € àÔÐ'Ø×$Ò$ UÑ+Ô+ˆEØ×%Ò% fÑ-Ô-ˆFå*7¸¸vÐRVÔRaÐ*bÑ*bÔ*bÑ'Ð˜3 ØÐÔÐ"3Ñ3ÐÔØˆŒ�C‰ˆŒØˆ
Œ
�gÑˆ
Œ
ˆ
ˆ
r*   c                 óN   — t          | j        | j        | j        | j        ¦  «        S )z.Compute peak signal-to-noise ratio over state.)r	   r   r   r   r   )r5   s    r(   Úcomputez-PeakSignalNoiseRatioWithBlockedEffect.computel   s   € å˜dÔ4°d´hÀÄ
ÈDÌOÑ\Ô\Ð\r*   Ú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
            >>> import torch
            >>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
            >>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
            >>> metric.update(torch.rand(2, 1, 10, 10), torch.rand(2, 1, 10, 10))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
            >>> metric = PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0)
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.rand(2, 1, 10, 10), torch.rand(2, 1, 10, 10)))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)r5   r>   r?   s      r(   Úplotz*PeakSignalNoiseRatioWithBlockedEffect.plotp   s   € ðP �zŠz˜#˜rÑ"Ô"Ð"r*   )r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   r   r4   r2   r/   r   r-   r;   r=   r   r   r   r   rB   Ú__classcell__)r6   s   @r(   r   r      s‡  ø€ € € € € € ð"ð "ðH #Ð�tÐ"Ð"Ñ"Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#àÐÐÑØ€M€M�MØ	€K€K�KØÐÐÑð
 ð$ð $à˜%  u¨e |Ô!4Ð4Ô5ð$ð ð$ð ð	$ð
 
ð$ð $ð $ð $ð $ð $ð,	˜Fð 	¨Fð 	°tð 	ð 	ð 	ð 	ð]˜ð ]ð ]ð ]ð ]ð
 _cð(#ð (#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð(#ØIQÐRZÔI[ð(#à	ð(#ð (#ð (#ð (#ð (#ð (#ð (#ð (#r*   r   )Úcollections.abcr   Útypingr   r   r   r%   r   r   Ú#torchmetrics.functional.image.psnrbr	   r
   Útorchmetrics.metricr   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r+   r*   r(   ú<module>rQ      sü   ðð %Ð $Ð $Ð $Ð $Ð $Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à MÐ MÐ MÐ MÐ MÐ MÐ MÐ MØ &Ð &Ð &Ð &Ð &Ð &Ø @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð FØDÐEÐð{#ð {#ð {#ð {#ð {#¨Fñ {#ô {#ð {#ð {#ð {#r*   