o
    Ñôjº  ã                   @   sª   d dl mZ d dlmZmZmZ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 d d	lmZ d d
lmZmZ esKdgZG dd„ deƒZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnionN)ÚTensorÚtensor)ÚLiteral)Ú_total_variation_computeÚ_total_variation_update)ÚMetric)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEúTotalVariation.plotc                       sÔ   e Zd ZU dZdZeed< dZeed< dZeed< dZ	e
ed< eed	< ee ed
< eed< dde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	ddeeeee f  dee defdd„Z‡  ZS )ÚTotalVariationa¦  Compute 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:
        >>> from torch import rand
        >>> from torchmetrics.image import TotalVariation
        >>> tv = TotalVariation()
        >>> img = torch.rand(5, 3, 28, 28)
        >>> tv(img)
        tensor(7546.8018)

    FÚfull_state_updateTÚis_differentiableÚhigher_is_betterg        Úplot_lower_boundÚnum_elementsÚ
score_listÚscoreÚsumÚ	reduction)Úmeanr   ÚnoneÚkwargsÚreturnNc                    sx   t ƒ jdi |¤Ž |d ur|dvrtdƒ‚|| _| jdg dd� | 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   Úcat)ÚdefaultÚdist_reduce_fxr   r   )Údtyper   r   © )	ÚsuperÚ__init__Ú
ValueErrorr   Ú	add_stater   ÚtorchÚfloatÚint)Úselfr   r   ©Ú	__class__r$   úh/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchmetrics/image/tv.pyr&   K   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,   r0   r   r   r$   r$   r/   ÚupdateU   s
   zTotalVariation.updatec                 C   s4   | 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$   r/   Úcompute^   s   $zTotalVariation.computeÚvalÚaxc                 C   s   |   ||¡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 TotalVariation
            >>> metric = TotalVariation()
            >>> metric.update(torch.rand(5, 3, 28, 28))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.image import TotalVariation
            >>> metric = TotalVariation()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.rand(5, 3, 28, 28)))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)r,   r4   r5   r$   r$   r/   Úplotc   s   (r   )r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   r*   r   r   r   r	   r   r&   r2   r3   r   r   r   r   r7   Ú__classcell__r$   r$   r-   r/   r      s(   
 "$
	ÿÿÿþr   )Úcollections.abcr   Útypingr   r   r   r   r)   r   r   Útyping_extensionsr	   Ú torchmetrics.functional.image.tvr
   r   Útorchmetrics.metricr   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r$   r$   r$   r/   Ú<module>   s   