o
    .wi`                     @   sP   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 G dd deZ	dS )    )AnyN)Tensortensor)_vif_per_channel)Metricc                       sr   e Zd ZU dZdZdZdZeed< eed< dde	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`): Tensor with vif-p score

    Args:
        sigma_n_sq: variance of the visual noise
        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])
        >>> target = randn([32, 3, 41, 41])
        >>> vif = VisualInformationFidelity()
        >>> vif(preds, target)
        tensor(0.0032)

    TF	vif_scoretotal       @
sigma_n_sqkwargsreturnNc                    s|   t  jdi | t|tst|tstd| |dk r%td| | jdtddd | jdtddd || _d S )	NzIArgument `sigma_n_sq` is expected to be a positive float or int, but got r   r   g        sum)defaultdist_reduce_fxr	    )	super__init__
isinstancefloatint
ValueError	add_stater   r   )selfr   r   	__class__r   S/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/image/vif.pyr   9   s   
z"VisualInformationFidelity.__init__predstargetc                    st     d} fddt|D }|dkrtt|dnt|} jt|7  _ j j	d 7  _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>I   s    >z4VisualInformationFidelity.update.<locals>.<listcomp>r   N)
sizerangetorchmeanstackcatr   r   r	   shape)r   r   r   channelsvif_per_channelr   r"   r   updateF   s   
$z VisualInformationFidelity.updatec                 C   s   | j | j S )zCompute vif-p over state.)r   r	   )r   r   r   r   computeP   s   z!VisualInformationFidelity.compute)r
   )__name__
__module____qualname____doc__is_differentiablehigher_is_betterfull_state_updater   __annotations__r   r   r   r-   r.   __classcell__r   r   r   r   r      s   
 
r   )
typingr   r&   r   r   !torchmetrics.functional.image.vifr   torchmetrics.metricr   r   r   r   r   r   <module>   s   