a
    d&                     @   s   d Z ddlmZmZmZ ddlZddlmZ g dZG dd dej	Z
ejejejejddd	ZG d
d dej	Zejeejef eejef ejdddZdejeeeejdddZdS )z:Module containing functionals for intensity normalisation.    )ListTupleUnionN)	normalizenormalize_min_maxdenormalize	NormalizeDenormalizec                       sr   e Zd ZdZeejee e	e ef eejee e	e ef dd fddZ
ejejdddZd	d
 Z  ZS )r   ac  Normalize a tensor image with mean and standard deviation.

    .. math::
        \text{input[channel] = (input[channel] - mean[channel]) / std[channel]}

    Where `mean` is :math:`(M_1, ..., M_n)` and `std` :math:`(S_1, ..., S_n)` for `n` channels,

    Args:
        mean: Mean for each channel.
        std: Standard deviations for each channel.

    Shape:
        - Input: Image tensor of size :math:`(*, C, ...)`.
        - Output: Normalised tensor with same size as input :math:`(*, C, ...)`.

    Examples:
        >>> x = torch.rand(1, 4, 3, 3)
        >>> out = Normalize(0.0, 255.)(x)
        >>> out.shape
        torch.Size([1, 4, 3, 3])

        >>> x = torch.rand(1, 4, 3, 3)
        >>> mean = torch.zeros(4)
        >>> std = 255. * torch.ones(4)
        >>> out = Normalize(mean, std)(x)
        >>> out.shape
        torch.Size([1, 4, 3, 3])
    Nmeanstdreturnc                    sv   t    t|tr t|g}t|tr6t|g}t|ttfrNt|}t|ttfrft|}|| _|| _	d S N)
super__init__
isinstancefloattorchtensortuplelistr   r   selfr   r   	__class__ a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/enhance/normalize.pyr   )   s    




zNormalize.__init__inputr   c                 C   s   t || j| jS r   )r   r   r   r   r   r   r   r   forward?   s    zNormalize.forwardc                 C   s"   d| j  d| j d}| jj| S Nz(mean=z, std=)r   r   r   __name__r   reprr   r   r   __repr__B   s    zNormalize.__repr__)r$   
__module____qualname____doc__r   r   Tensorr   r   r   r   r    r'   __classcell__r   r   r   r   r      s   r   )datar   r   r   c                 C   s  | j }t|j dks"|j d dkr0||d }t|j dksL|j d dkrZ||d }|j r|j d dkr|j d | j d kr|j dd | j dd krtd|j  d| j  d|j r|j d dkr|j d | j d kr|j dd | j dd krtd|j  d| j  dtj|| j| jd	}tj|| j| jd	}|j r`|d
dddf }|j rz|d
dddf }| |d |d d| | }||S )az  Normalize an image/video tensor with mean and standard deviation.

    .. math::
        \text{input[channel] = (input[channel] - mean[channel]) / std[channel]}

    Where `mean` is :math:`(M_1, ..., M_n)` and `std` :math:`(S_1, ..., S_n)` for `n` channels,

    Args:
        data: Image tensor of size :math:`(B, C, *)`.
        mean: Mean for each channel.
        std: Standard deviations for each channel.

    Return:
        Normalised tensor with same size as input :math:`(B, C, *)`.

    Examples:
        >>> x = torch.rand(1, 4, 3, 3)
        >>> out = normalize(x, torch.tensor([0.0]), torch.tensor([255.]))
        >>> out.shape
        torch.Size([1, 4, 3, 3])

        >>> x = torch.rand(1, 4, 3, 3)
        >>> mean = torch.zeros(4)
        >>> std = 255. * torch.ones(4)
        >>> out = normalize(x, mean, std)
        >>> out.shape
        torch.Size([1, 4, 3, 3])
    r      N   5mean length and number of channels do not match. Got  and .4std length and number of channels do not match. Got devicedtype.)	shapelenexpand
ValueErrorr   	as_tensorr5   r6   viewr-   r   r   r8   outr   r   r   r   G   s&    04r   c                       sZ   e Zd ZdZeejef eejef dd fddZejejdddZ	d	d
 Z
  ZS )r	   aw  Denormalize a tensor image with mean and standard deviation.

    .. math::
        \text{input[channel] = (input[channel] * std[channel]) + mean[channel]}

    Where `mean` is :math:`(M_1, ..., M_n)` and `std` :math:`(S_1, ..., S_n)` for `n` channels,

    Args:
        mean: Mean for each channel.
        std: Standard deviations for each channel.

    Shape:
        - Input: Image tensor of size :math:`(*, C, ...)`.
        - Output: Denormalised tensor with same size as input :math:`(*, C, ...)`.

    Examples:
        >>> x = torch.rand(1, 4, 3, 3)
        >>> out = Denormalize(0.0, 255.)(x)
        >>> out.shape
        torch.Size([1, 4, 3, 3])

        >>> x = torch.rand(1, 4, 3, 3, 3)
        >>> mean = torch.zeros(1, 4)
        >>> std = 255. * torch.ones(1, 4)
        >>> out = Denormalize(mean, std)(x)
        >>> out.shape
        torch.Size([1, 4, 3, 3, 3])
    Nr
   c                    s   t    || _|| _d S r   )r   r   r   r   r   r   r   r   r      s    
zDenormalize.__init__r   c                 C   s   t || j| jS r   )r   r   r   r   r   r   r   r       s    zDenormalize.forwardc                 C   s"   d| j  d| j d}| jj| S r!   r#   r%   r   r   r   r'      s    zDenormalize.__repr__)r$   r(   r)   r*   r   r   r+   r   r   r    r'   r,   r   r   r   r   r	      s   *r	   c                 C   s   | j }t|tr.tj|g|d  | j| jd}t|trVtj|g|d  | j| jd}t| tjsttdt	|  t|tjstdt	| t|tjstdt	| |j r|j d dkr|j d | j d kr|j dd	 | j dd	 krt
d
|j  d| j  d|j r||j d dkr||j d | j d kr||j dd	 | j dd	 kr|t
d|j  d| j  dtj|| j| jd}tj|| j| jd}|j r|ddddf }|j r|ddddf }| |d |d d| | }||S )ao  Denormalize an image/video tensor with mean and standard deviation.

    .. math::
        \text{input[channel] = (input[channel] * std[channel]) + mean[channel]}

    Where `mean` is :math:`(M_1, ..., M_n)` and `std` :math:`(S_1, ..., S_n)` for `n` channels,

    Args:
        input: Image tensor of size :math:`(B, C, *)`.
        mean: Mean for each channel.
        std: Standard deviations for each channel.

    Return:
        Denormalised tensor with same size as input :math:`(B, C, *)`.

    Examples:
        >>> x = torch.rand(1, 4, 3, 3)
        >>> out = denormalize(x, 0.0, 255.)
        >>> out.shape
        torch.Size([1, 4, 3, 3])

        >>> x = torch.rand(1, 4, 3, 3, 3)
        >>> mean = torch.zeros(1, 4)
        >>> std = 255. * torch.ones(1, 4)
        >>> out = denormalize(x, mean, std)
        >>> out.shape
        torch.Size([1, 4, 3, 3, 3])
    r.   r4   zdata should be a tensor. Got z(mean should be a tensor or a float. Got z%std should be a tensor or float. Got r   Nr/   r0   r1   r2   r3   .r7   )r8   r   r   r   r   r5   r6   r+   	TypeErrortyper;   r<   r=   r>   r   r   r   r      s2    

44r                 ?ư>)xmin_valmax_valepsr   c           
      C   s  t | tjs tdt|  dt |ts>tdt| dt |ts\tdt| dt| jdk r|td| j d| j}|d |d  }}| 	||d	
d	d 	||d}| 	||d	d	d 	||d}|| | 	||d	|  || |  | }	|		|S )
ax  Normalise an image/video tensor by MinMax and re-scales the value between a range.

    The data is normalised using the following formulation:

    .. math::
        y_i = (b - a) * \frac{x_i - \text{min}(x)}{\text{max}(x) - \text{min}(x)} + a

    where :math:`a` is :math:`\text{min_val}` and :math:`b` is :math:`\text{max_val}`.

    Args:
        x: The image tensor to be normalised with shape :math:`(B, C, *)`.
        min_val: The minimum value for the new range.
        max_val: The maximum value for the new range.
        eps: Float number to avoid zero division.

    Returns:
        The normalised image tensor with same shape as input :math:`(B, C, *)`.

    Example:
        >>> x = torch.rand(1, 5, 3, 3)
        >>> x_norm = normalize_min_max(x, min_val=-1., max_val=1.)
        >>> x_norm.min()
        tensor(-1.)
        >>> x_norm.max()
        tensor(1.0000)
    zdata should be a tensor. Got: r2   z"'min_val' should be a float. Got: z'b' should be a float. Got:    z/Input shape must be at least a 3d tensor. Got: r   r.   r7   )r   r   r+   rA   rB   r   r9   r8   r;   r=   minmax)
rF   rG   rH   rI   r8   BCZx_minZx_maxZx_outr   r   r   r      s    

""*r   )rC   rD   rE   )r*   typingr   r   r   r   Ztorch.nnnn__all__Moduler   r+   r   r	   r   r   r   r   r   r   r   <module>   s   <:-,E