
    3j,                        S r SSKJrJrJr  SSKrSSKJr  SSKJr  / SQr	 " S S\R                  5      rS	\R                  S
\R                  S\R                  S\R                  4S jr " S S\R                  5      rS	\R                  S
\\R                  \4   S\\R                  \4   S\R                  4S jr\SS\R                  S\S\S\S\R                  4
S jj5       rg)z=nn.Module containing functionals for intensity normalisation.    )ListTupleUnionN)nn)perform_keep_shape_image)Denormalize	Normalizedenormalize	normalizenormalize_min_maxc                      ^  \ rS rSrSrS\\R                  \\	   \
\	   \	4   S\\R                  \\	   \
\	   \	4   SS4U 4S jjrS\R                  S\R                  4S	 jrS\4S
 jrSrU =r$ )r	      a"  Normalize a torch.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 torch.Tensor of size :math:`(*, C, ...)`.
    - Output: Normalised torch.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])

meanstdreturnNc                   > [         TU ]  5         [        U[        [        45      (       a  [
        R                  " U/5      n[        U[        [        45      (       a  [
        R                  " U/5      n[        U[        [        45      (       a  [
        R                  " U5      S    n[        U[        [        45      (       a  [
        R                  " U5      S    nXl	        X l
        g N)super__init__
isinstanceintfloattorchtensortuplelistr   r   selfr   r   	__class__s      R/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/enhance/normalize.pyr   Normalize.__init__=   s    
 	dS%L))<<'DcC<((,,u%CdUDM**<<%d+DcE4=)),,s#D)C	    inputc                 B    [        XR                  U R                  5      $ r   )r   r   r   r   r#   s     r    forwardNormalize.forwardS   s    		48844r"   c                 n    SU R                    SU R                   S3nU R                  R                  U-   $ Nz(mean=z, std=)r   r   r   __name__r   reprs     r    __repr__Normalize.__repr__V   3    		{&
!4~~&&--r"   r   r   )r,   
__module____qualname____firstlineno____doc__r   r   Tensorr   r   r   r   r&   strr/   __static_attributes____classcell__r   s   @r    r	   r	      s    <ELL%,UUBC 5<<utE{EAB 
	,5U\\ 5ell 5.# . .r"   r	   datar   r   r   c                    U R                   n[        R                  R                  5       (       a  [	        U[        R
                  5      (       a  [	        U[        R
                  5      (       d  [        S5      eUR                   S   S:w  d  UR                   S   S:w  a&  [        SUR                    SUR                    S35      eGO[	        U[        5      (       a1  [        R                  " U/US   -  U R                  U R                  S9n[	        U[        5      (       a1  [        R                  " U/US   -  U R                  U R                  S9nUR                   (       ay  UR                   S   S:w  af  UR                   S   U R                   S   :w  aF  UR                   SS	 U R                   SS	 :w  a&  [        S
UR                    SU R                    S35      eUR                   (       ay  UR                   S   S:w  af  UR                   S   U R                   S   :w  aF  UR                   SS	 U R                   SS	 :w  a&  [        SUR                    SU R                    S35      e[        R                  " XR                  U R                  S9n[        R                  " X R                  U R                  S9nUS   nUS   nU R                  US   US   S5      U-
  U-  nUR                  U5      $ )a9  Normalize an image/video torch.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 torch.Tensor of size :math:`(B, C, *)`.
    mean: Mean for each channel.
    std: Standard deviations for each channel.

Return:
    Normalised torch.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])

6Only torch.Tensor is accepted when converting to ONNX.r      zqBatch dimension must be one for broadcasting when converting to ONNX.Try changing mean shape and std shape from (z, z) to (1, C) or (1, C, 1, 1).devicedtypeN   5mean length and number of channels do not match. Got  and .4std length and number of channels do not match. Got ).N)shaper   onnxis_in_onnx_exportr   r7   
ValueErrorr   r   rA   rB   	as_tensorview)r<   r   r   rI   outs        r    r   r   [   sj   < JJEzz##%%$--ZU\\5R5RUVV::a=A1!2??Czzl"SYYKWsu  "3 dE""<<q 1$++TZZXDc5!!,,uuQx/4::VC ::$**Q-1,zz!}

1-$**Ra.DJJrPQN2R #XY]YcYcXddijnjtjtiuuv!wxx 991*yy|tzz!},2A$**Ra.1P #WX[XaXaWbbghlhrhrgsst!uvvtKKtzzJooc++TZZH	?D
i.C58U1Xr:TASHC88E?r"   c                      ^  \ rS rSrSrS\\R                  \4   S\\R                  \4   SS4U 4S jjr	S\R                  S\R                  4S	 jr
S\4S
 jrSrU =r$ )r      a6  Denormalize a torch.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 torch.Tensor of size :math:`(*, C, ...)`.
    - Output: Denormalised torch.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])

r   r   r   Nc                 :   > [         TU ]  5         Xl        X l        g r   )r   r   r   r   r   s      r    r   Denormalize.__init__   s    	r"   r#   c                 B    [        XR                  U R                  5      $ r   )r
   r   r   r%   s     r    r&   Denormalize.forward   s    5))TXX66r"   c                 n    SU R                    SU R                   S3nU R                  R                  U-   $ r)   r+   r-   s     r    r/   Denormalize.__repr__   r1   r"   r2   )r,   r3   r4   r5   r6   r   r   r7   r   r   r&   r8   r/   r9   r:   r;   s   @r    r   r      sl    <U5<<#67 eELLRWDW>X ]a 7U\\ 7ell 7.# . .r"   r   c                    U R                   n[        R                  R                  5       (       a|  [	        U[        R
                  5      (       a  [	        U[        R
                  5      (       d  [        S5      eUR                   S   S:w  d  UR                   S   S:w  a  [        S5      eGO[	        U[        5      (       a1  [        R                  " U/US   -  U R                  U R                  S9n[	        U[        5      (       a1  [        R                  " U/US   -  U R                  U R                  S9nUR                   (       ay  UR                   S   S:w  af  UR                   S   U R                   S   :w  aF  UR                   SS U R                   SS :w  a&  [        S	UR                    S
U R                    S35      eUR                   (       ay  UR                   S   S:w  af  UR                   S   U R                   S   :w  aF  UR                   SS U R                   SS :w  a&  [        SUR                    S
U R                    S35      e[        R                  " XR                  U R                  S9n[        R                  " X R                  U R                  S9nUR                  5       S:X  a*  UR                  " SS/S/U R                  5       S-
  -  Q76 nOa[        UR                   5      U R                  5       :  a:  UR                  S5      n[        UR                   5      U R                  5       :  a  M:  UR                  5       S:X  a*  UR                  " SS/S/U R                  5       S-
  -  Q76 nOa[        UR                   5      U R                  5       :  a:  UR                  S5      n[        UR                   5      U R                  5       :  a  M:  [        R                   " XU5      $ )a-  Denormalize an image/video torch.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:
    data: Image torch.Tensor of size :math:`(B, C, *)`.
    mean: Mean for each channel.
    std: Standard deviations for each channel.

Return:
    Denormalised torch.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>   r   r?   zEBatch dimension must be one for broadcasting when converting to ONNX.r@   NrC   rD   rE   rF   rG   rH   )rI   r   rJ   rK   r   r7   rL   r   r   rA   rB   rM   dimrN   len	unsqueezeaddcmul)r<   r   r   rI   s       r    r
   r
      s   < JJEzz##%%$--ZU\\5R5RUVV::a=A1!2dee "3 dE""<<q 1$++TZZXDc5!!,,uuQx/4::VC ::$**Q-1,zz!}

2.4::bq>TZZPRQR^3S #XY]YcYcXddijnjtjtiuuv!wxx 991*yy|tzz"~-#))BQ-4::bq>2Q #WX[XaXaWbbghlhrhrgsst!uvvtKKtzzJooc++TZZHxxzQyyB:1#a"8: $**o
*>>"%D $**o
* wwyA~hhq"8txxzA~ 68#))ntxxz)--#C #))ntxxz) ==S))r"   xmin_valmax_valepsc                    [        U [        R                  5      (       d  [        S[	        U 5       S35      e[        U[
        5      (       d  [        S[	        U5       S35      e[        U[
        5      (       d  [        S[	        U5       S35      eU R                  nUS   US   peU R                  XVS5      nUR                  SSS	9S   nUR                  SSS	9S   n	X!-
  Xx-
  -  X-
  U-   -  U-   n
U
R                  U5      $ )
aE  Normalise an image/video torch.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 torch.Tensor to be normalised with shape :math:`(*, C, H, W)`.
    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 torch.Tensor with same shape as input :math:`(*, C, H, W)`.

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 torch.Tensor. Got: rF   z"'min_val' should be a float. Got: z"'max_val' should be a float. Got: r   r?   rH   T)keepdim)
r   r   r7   	TypeErrortyper   rI   rN   minmax)r^   r_   r`   ra   rI   BC
x_reshapedx_minx_maxx_outs              r    r   r     s   : a&&>tAwiqIJJgu%%<T']O1MNNgu%%<T']O1MNNGGE8U1Xqb!JNN2tN,Q/ENN2tN,Q/E:#56%-#:MNQXXE::er"   )g        g      ?gư>)r6   typingr   r   r   r   r   kornia.image.utilsr   __all__Moduler	   r7   r   r   r   r
   r    r"   r    <module>rs      s  $ D % %   7
W:.		 :.zAELL A A5<< AELL AH*.")) *.ZF*ell F*%e0C*D F*5QVQ]Q]_dQdKe F*jojvjv F*R - - -e -X] -iniuiu - -r"   