o
    yi2                     @   sl   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m	Z	 d dl
mZ d dlmZ G dd	 d	eZdS )
    )AnyN)Tensortensor)Literal)_total_variation_compute_total_variation_update)Metric)dim_zero_catc                       sz   e Zd ZU dZdZeed< dZeed< dZeed< dde	d	 d
e
ddf fddZdeddfddZdefddZ  ZS )TotalVariationa  Computes Total Variation loss (`TV`_).

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

    - ``img`` (:class:`~torch.Tensor`): A tensor of shape ``(N, C, H, W)`` consisting of images

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

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

    Args:
        reduction: a method to reduce metric score over samples

            - ``'mean'``: takes the mean over samples
            - ``'sum'``: takes the sum over samples
            - ``None`` or ``'none'``: return the score per sample

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

    Raises:
        ValueError:
            If ``reduction`` is not one of ``'sum'``, ``'mean'``, ``'none'`` or ``None``

    Example:
        >>> import torch
        >>> from torchmetrics import TotalVariation
        >>> _ = torch.manual_seed(42)
        >>> tv = TotalVariation()
        >>> img = torch.rand(5, 3, 28, 28)
        >>> tv(img)
        tensor(7546.8018)
    Ffull_state_updateTis_differentiablehigher_is_bettersum	reduction)meanr   noneNkwargsreturnNc                    s   t  jdi | |d ur|dvrtd|| _| jd u s"| jdkr+| jdg dd n| jdtdtjdd	d | jd
tdtjdd	d d S )N)r   r   r   zHExpected argument `reduction` to either be 'sum', 'mean', 'none' or Noner   scorecat)defaultdist_reduce_fxr   )dtyper   num_elements )	super__init__
ValueErrorr   	add_stater   torchfloatint)selfr   r   	__class__r   I/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/image/tv.pyr   @   s   zTotalVariation.__init__imgc                 C   sR   t |\}}| jdu s| jdkr| j| n	|  j| 7  _|  j|7  _dS )z0Update current score with batch of input images.Nr   )r   r   r   appendr   r   )r"   r&   r   r   r   r   r%   updateL   s
   zTotalVariation.updatec                 C   s6   | j du s
| j dkrt| j}n| j}t|| j| j S )zCompute final total variation.Nr   )r   r	   r   r   r   )r"   r   r   r   r%   computeU   s   zTotalVariation.compute)r   )__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r   r   r   r(   r)   __classcell__r   r   r#   r%   r
      s   
 " 	r
   )typingr   r   r   r   typing_extensionsr    torchmetrics.functional.image.tvr   r   torchmetrics.metricr   torchmetrics.utilities.datar	   r
   r   r   r   r%   <module>   s   