a
    d                     @   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                       sj   e Zd ZU dZdZdZdZeed< eed< de	e
dd fd	d
ZeeddddZed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:
        >>> import torch
        >>> _ = torch.manual_seed(42)
        >>> from torchmetrics.image import VisualInformationFidelity
        >>> preds = torch.randn([32, 3, 41, 41])
        >>> target = torch.randn([32, 3, 41, 41])
        >>> vif = VisualInformationFidelity()
        >>> vif(preds, target)
        tensor(0.0032)

    TF	vif_scoretotal       @N)
sigma_n_sqkwargsreturnc                    s|   t  jf i | t|ts4t|ts4td| |dk rJt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)defaultZdist_reduce_fxr	   )	super__init__
isinstancefloatint
ValueErrorZ	add_stater   r   )selfr   r   	__class__ _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/image/vif.pyr   :   s    z"VisualInformationFidelity.__init__)predstargetr   c                    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 | ]B}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>J   s   z4VisualInformationFidelity.update.<locals>.<listcomp>r   N)
sizerangetorchmeanstackcatr   r   r	   shape)r   r   r   ZchannelsZvif_per_channelr   r    r   updateG   s    
$z VisualInformationFidelity.update)r   c                 C   s   | j | j S )zCompute vif-p over state.)r   r	   )r   r   r   r   computeQ   s    z!VisualInformationFidelity.compute)r
   )__name__
__module____qualname____doc__Zis_differentiableZhigher_is_betterZfull_state_updater   __annotations__r   r   r   r)   r*   __classcell__r   r   r   r   r      s   

r   )
typingr   r$   r   r   Z!torchmetrics.functional.image.vifr   Ztorchmetrics.metricr   r   r   r   r   r   <module>   s
   