o
    Ðôjn  ã                   @   sh   d dl mZmZ d dl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 G dd	„ d	e
ƒZdS )
é    )ÚAnyÚListN)ÚTensor)ÚLiteral)Ú_vif_per_channel)ÚMetric)Údim_zero_catc                	       s~   e Zd ZU dZdZdZdZee e	d< ee	d< dde
d	ed
 deddf‡ fdd„Zdededdfdd„Zdefdd„Z‡  ZS )ÚVisualInformationFidelityu<  Compute Pixel Based Visual Information Fidelity (VIF_).

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

    - ``preds`` (:class:`~torch.Tensor`): Predictions from model of shape ``(N,C,H,W)`` with H,W â‰¥ 41
    - ``target`` (:class:`~torch.Tensor`): Ground truth values of shape ``(N,C,H,W)`` with H,W â‰¥ 41

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

    - ``vif-p`` (:class:`~torch.Tensor`):
        - If ``reduction='mean'`` (default), returns a Tensor mean VIF score.
        - If ``reduction='none'``, returns a tensor of shape ``(N,)`` with VIF values per sample.

    Args:
        sigma_n_sq: variance of the visual noise
        reduction: The reduction method for aggregating scores.

            - ``'mean'``: return the average VIF across the batch.
            - ``'none'``: return a VIF score for each sample in the batch.

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

    Example:
        >>> from torch import randn
        >>> from torchmetrics.image import VisualInformationFidelity
        >>> preds = randn([32, 3, 41, 41], generator=torch.Generator().manual_seed(42))
        >>> target = randn([32, 3, 41, 41], generator=torch.Generator().manual_seed(43))
        >>> vif_mean = VisualInformationFidelity(reduction='mean')
        >>> vif_mean(preds, target)
        tensor(0.0032)
        >>> vif_none = VisualInformationFidelity(reduction='none')
        >>> vif_none(preds, target)
        tensor([0.0040, 0.0049, 0.0017, 0.0039, 0.0041, 0.0043, 0.0030, 0.0028, 0.0012,
                0.0067, 0.0010, 0.0014, 0.0030, 0.0048, 0.0050, 0.0038, 0.0037, 0.0025,
                0.0041, 0.0019, 0.0007, 0.0034, 0.0037, 0.0016, 0.0026, 0.0021, 0.0038,
                0.0033, 0.0031, 0.0020, 0.0036, 0.0057])

    TFÚ	vif_scoreÚtotalç       @ÚmeanÚ
sigma_n_sqÚ	reduction©r   ÚnoneÚkwargsÚreturnNc                    sl   t ƒ jdi |¤Ž t|ttfƒr|dk rtd|› �ƒ‚|dvr&td|› �ƒ‚|| _|| _| jdg d d� d S )Nr   zIArgument `sigma_n_sq` is expected to be a positive float or int, but got r   z7Argument `reduction` must be 'mean' or 'none', but got r
   )ÚdefaultÚdist_reduce_fx© )	ÚsuperÚ__init__Ú
isinstanceÚfloatÚintÚ
ValueErrorr   r   Ú	add_state)Úselfr   r   r   ©Ú	__class__r   úi/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchmetrics/image/vif.pyr   H   s   z"VisualInformationFidelity.__init__ÚpredsÚtargetc                    sX   ˆ   d¡}‡ ‡‡fdd„t|ƒD ƒ}|dkrt t |¡d¡nt |¡}ˆj |¡ dS )z*Update state with predictions and targets.é   c              
      sJ   g | ]!}t ˆ d d …|d d …d d …f ˆd d …|d d …d d …f ˆjƒ‘qS )N)r   r   )Ú.0Úi©r"   r   r#   r   r!   Ú
<listcomp>X   s    >ÿz4VisualInformationFidelity.update.<locals>.<listcomp>r   N)ÚsizeÚrangeÚtorchr   ÚstackÚcatr
   Úappend)r   r"   r#   ÚchannelsZvif_per_channelr   r'   r!   ÚupdateU   s   
ÿ$z VisualInformationFidelity.updatec                 C   s    t | jƒ}| jdkr| ¡ S |S )zCompute VIF over state.r   )r   r
   r   r   )r   r
   r   r   r!   Úcompute^   s   

z!VisualInformationFidelity.compute)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úis_differentiableÚhigher_is_betterÚfull_state_updater   r   Ú__annotations__r   r   r   r   r0   r1   Ú__classcell__r   r   r   r!   r	      s   
 '$	r	   )Útypingr   r   r+   r   Útyping_extensionsr   Ú!torchmetrics.functional.image.vifr   Útorchmetrics.metricr   Útorchmetrics.utilities.datar   r	   r   r   r   r!   Ú<module>   s   