
    RЦiN                     @   S SK r S SKrS SKrS SKrS SKJrJrJrJr  S SK	r	S SK
Jr  S SKJs  Jr   S SKJr  SrS SKJr  S SKr/ SQr " S S	5      r " S
 S5      r " S S\R0                  5      r " S S5      r\" \S5      (       a  \R8                  R:                  S\R8                  R<                  S\R8                  R>                  S\R8                  R@                  S\R8                  RB                  S\R8                  RD                  S0r#OJ\R:                  S\R<                  S\R>                  S\R@                  S\RB                  S\RD                  S0r#\#RI                  5        V Vs0 s H  u  pX_M	     snn r%\(       ap  \R:                  S\R<                  S\R>                  S\R@                  S\RB                  S\RD                  S0r&\&RI                  5        V Vs0 s H  u  pX_M	     snn r'O0 r(0 r&S r)S r*S r+\*" S5      \*" S5      4r,S2S jr- " S S5      r.  S3S\	R^                  S\\0\\0   4   S\\0\\0\0\04   4   S \1S!\	R^                  4
S" jjr2 " S# S$\	Rf                  Rh                  5      r5  S3S\	R^                  S%\0S&\0S'\0S(\0S\\0\\0\0\04   4   S \1S!\	R^                  4S) jjr6 " S* S+\	Rf                  Rh                  5      r7 " S, S-5      r8 " S. S/5      r9 " S0 S1\	Rf                  Rh                  5      r:g! \ a    Sr GNf = fs  snn f s  snn f )4    N)ListSequenceTupleUnion)InterpolationModeTF)Image)ToNumpyToTensorstr_to_interp_modestr_to_pil_interpinterp_mode_to_str!RandomResizedCropAndInterpolationCenterCropOrPadcenter_crop_or_padcrop_or_padRandomCropOrPad	RandomPadResizeKeepRatio
TrimBorderMaybeToTensorMaybePILToTensorc                       \ rS rSrS rSrg)r	      c                     [         R                  " U[         R                  S9nUR                  S:  a  [         R                  " USS9n[         R
                  " US5      nU$ )Ndtype   )axis   )nparrayuint8ndimexpand_dimsrollaxis)selfpil_imgnp_imgs      S/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/data/transforms.py__call__ToNumpy.__call__   sF    '2;;?^^F4FVQ'     N)__name__
__module____qualname____firstlineno__r+   __static_attributes__r.   r-   r*   r	   r	      s    r-   r	   c                   >    \ rS rSrSr\R                  4S jrS rSr	g)r
   #   z$ToTensor with no rescaling of valuesc                     Xl         g Nr   )r'   r   s     r*   __init__ToTensor.__init__%   s    
r-   c                 \    [         R                  " U5      R                  U R                  S9$ )Nr   )Fpil_to_tensortor   )r'   r(   s     r*   r+   ToTensor.__call__(   s#    w'***<<r-   r   N)
r/   r0   r1   r2   __doc__torchfloat32r8   r+   r3   r.   r-   r*   r
   r
   #   s    /"]] =r-   r
   c                   `   ^  \ rS rSrSrSU 4S jjrS\R                  4S jrS\	4S jr
SrU =r$ )	r   ,   zFConvert a PIL Image or ndarray to tensor if it's not already one.
    returnc                 "   > [         TU ]  5         g r7   superr8   r'   	__class__s    r*   r8   MaybeToTensor.__init__0       r-   c                 p    [        U[        R                  5      (       a  U$ [        R                  " U5      $ )zu
Args:
    pic (PIL Image or numpy.ndarray): Image to be converted to tensor.

Returns:
    Tensor: Converted image.
)
isinstancer@   Tensorr;   	to_tensorr'   pics     r*   r+   MaybeToTensor.__call__3   s)     c5<<((J{{3r-   c                 4    U R                   R                   S3$ Nz()rI   r/   r'   s    r*   __repr__MaybeToTensor.__repr__?       ..))*"--r-   r.   rD   N)r/   r0   r1   r2   r?   r8   r@   rN   r+   strrW   r3   __classcell__rI   s   @r*   r   r   ,   s.    
 u|| 
 .# . .r-   r   c                   D   ^  \ rS rSrSrSU 4S jjrS rS\4S jrSr	U =r
$ )	r   C   zSConvert a PIL Image to a tensor of the same type - this does not scale values.
    rD   c                 "   > [         TU ]  5         g r7   rF   rH   s    r*   r8   MaybePILToTensor.__init__G   rK   r-   c                 p    [        U[        R                  5      (       a  U$ [        R                  " U5      $ )z
Note: A deep copy of the underlying array is performed.

Args:
    pic (PIL Image): Image to be converted to tensor.

Returns:
    Tensor: Converted image.
)rM   r@   rN   r;   r<   rP   s     r*   r+   MaybePILToTensor.__call__J   s)     c5<<((Js##r-   c                 4    U R                   R                   S3$ rT   rU   rV   s    r*   rW   MaybePILToTensor.__repr__X   rY   r-   r.   rZ   )r/   r0   r1   r2   r?   r8   r+   r[   rW   r3   r\   r]   s   @r*   r   r   C   s#    $.# . .r-   r   
Resamplingnearestbilinearbicubicboxhamminglanczosc                     [         U    $ r7   )_str_to_pil_interpolationmode_strs    r*   r   r      s    $X..r-   c                 <    [         (       a	  [        U    $ [        U    $ r7   )has_interpolation_mode_str_to_torch_interpolationrn   ro   s    r*   r   r      s    *844(22r-   c                 <    [         (       a	  [        U    $ [        U    $ r7   )rr   _torch_interpolation_to_str_pil_interpolation_to_str)modes    r*   r   r      s    *400(..r-   c                     [        U [        R                  5      (       a  [        U 5      [        U 5      4$ [        U [        5      (       a  [        U 5      S:X  a
  U S   U S   4$ [        U 5      S:w  a  [        U5      eU $ )N   r   r    )rM   numbersNumberintr   len
ValueError)size	error_msgs     r*   _setup_sizer      sk    $''4y#d)##$!!c$i1nAwQ
4yA~##Kr-   c                   D    \ rS rSrSr   S	S jr\S 5       rS rS r	Sr
g)
r      a1  Crop the given PIL Image to random size and aspect ratio with random interpolation.

A crop of random size (default: of 0.08 to 1.0) of the original size and a random
aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop
is finally resized to given size.
This is popularly used to train the Inception networks.

Args:
    size: expected output size of each edge
    scale: range of size of the origin size cropped
    ratio: range of aspect ratio of the origin aspect ratio cropped
    interpolation: Default: PIL.Image.BILINEAR
c                 $   [        U[        [        45      (       a  [        U5      U l        OX4U l        US   US   :  d  US   US   :  a  [        R
                  " S5        US:X  a  [        U l        O[        U5      U l        X l	        X0l
        g )Nr   ry   z"range should be of kind (min, max)random)rM   listtupler   warningswarn_RANDOM_INTERPOLATIONinterpolationr   scaleratio)r'   r   r   r   r   s        r*   r8   *RandomResizedCropAndInterpolation.__init__   s|     dT5M**dDIDI!HuQxU1Xa%8MM>?H$!6D!3M!BD

r-   c           	      j   [         R                  " U 5      u  p4X4-  n[        S5       GH
  n[        R                  " U6 U-  n[
        R                  " US   5      [
        R                  " US   5      4n[
        R                  " [        R                  " U6 5      n	[        [        [
        R                  " Xy-  5      5      5      n
[        [        [
        R                  " Xy-  5      5      5      nX::  d  M  X::  d  M  [        R                  " SXK-
  5      n[        R                  " SX:-
  5      nXX4s  $    X4-  nU[        U5      :  a#  Un
[        [        U
[        U5      -  5      5      nO6U[        U5      :  a#  Un[        [        U[        U5      -  5      5      n
OUn
UnXK-
  S-  nX:-
  S-  nXX4$ )aQ  Get parameters for ``crop`` for a random sized crop.

Args:
    img (PIL Image): Image to be cropped.
    scale (tuple): range of size of the origin size cropped
    ratio (tuple): range of aspect ratio of the origin aspect ratio cropped

Returns:
    tuple: params (i, j, h, w) to be passed to ``crop`` for a random
        sized crop.

   r   ry   r    )r;   get_image_sizeranger   uniformmathlogexpr|   roundsqrtrandintminmax)imgr   r   img_wimg_hareaattempttarget_area	log_ratioaspect_ratiotarget_wtarget_hijin_ratios                  r*   
get_params,RandomResizedCropAndInterpolation.get_params   su    '',}RyG ..%047K%(+TXXeAh-?@I88FNNI$>?L5;+E!FGHH5;+E!FGHH X%6NN1e&67NN1e&67X// ! =c%j H5CJ!678HE
"H5CJ!678HHH!#!#X''r-   c           	      >   U R                  XR                  U R                  5      u  p#pE[        U R                  [
        [        45      (       a!  [        R                  " U R                  5      nOU R                  n[        R                  " XX4XPR                  U5      $ )zz
Args:
    img (PIL Image): Image to be cropped and resized.

Returns:
    PIL Image: Randomly cropped and resized image.
)r   r   r   rM   r   r   r   r   choicer;   resized_cropr   )r'   r   r   r   hwr   s          r*   r+   *RandomResizedCropAndInterpolation.__call__   so     __S**djjA
ad((5$-88"MM$*<*<=M ..M~~caAyy-HHr-   c                 4   [        U R                  [        [        45      (       a6  SR	                  U R                   Vs/ s H  n[        U5      PM     sn5      nO[        U R                  5      nU R                  R                  SR                  U R                  5      -   nUSR                  [        S U R                   5       5      5      -  nUSR                  [        S U R                   5       5      5      -  nUSR                  U5      -  nU$ s  snf )N 	(size={0}z, scale={0}c              3   :   #    U  H  n[        US 5      v   M     g7f   Nr   ).0ss     r*   	<genexpr>=RandomResizedCropAndInterpolation.__repr__.<locals>.<genexpr>
       3TAE!QKK   z, ratio={0}c              3   :   #    U  H  n[        US 5      v   M     g7fr   r   )r   rs     r*   r   r     r   r   z, interpolation={0}))rM   r   r   r   joinr   rI   r/   formatr   r   r   r'   xinterpolate_strformat_strings       r*   rW   *RandomResizedCropAndInterpolation.__repr__  s    d((5$-88!hhtGYGY'ZGY!(:1(=GY'Z[O01C1CDO//+2D2DTYY2OO--e3T3T.TUU--e3T3T.TUU/66GG ([s   D)r   r   r   r   N))g{Gz?      ?)g      ?gUUUUUU?rh   r/   r0   r1   r2   r?   r8   staticmethodr   r+   rW   r3   r.   r-   r*   r   r      s7    " $$* (( ((TI	r-   r   r   output_sizefillpadding_moderD   c                    [        U5      nUu  pE[        R                  " U 5      u  pgnXX:  d  XG:  av  XX:  a  XX-
  S-  OSXG:  a  XG-
  S-  OSXX:  a
  XX-
  S-   S-  OSXG:  a
  XG-
  S-   S-  OS/n	[        R                  " X	X#S9n [        R                  " U 5      u  pgnXX:X  a  XG:X  a  U $ [	        [        Xt-
  S-  5      5      n
[	        [        X-
  S-  5      5      n[        R                  " X
XU5      $ )a?  Center crops and/or pads the given image.

If the image is torch Tensor, it is expected
to have [..., H, W] shape, where ... means an arbitrary number of leading dimensions.
If image size is smaller than output size along any edge, image is padded with 0 and then center cropped.

Args:
    img (PIL Image or Tensor): Image to be cropped.
    output_size (sequence or int): (height, width) of the crop box. If int or sequence with single int,
        it is used for both directions.
    fill (int, Tuple[int]): Padding color

Returns:
    PIL Image or Tensor: Cropped image.
r    r   ry   r   r   g       @)r   r;   get_dimensionspadr|   r   crop)r   r   r   r   crop_height
crop_width_image_heightimage_widthpadding_ltrbcrop_top	crop_lefts               r*   r   r     s
   * k*K)K#$#3#3C#8 A[;#=/9/GZ%!+Q1<1K['A-QR3=3KZ%)a/QR5@5O['!+1UV	
 eeCDL'('7'7'<$$)DJ5,4;<=HE;3s:;<I66#DDr-   c            
       ~   ^  \ rS rSrSr  SS\\\\   4   S\\\\\\4   4   S\	4U 4S jjjr
S rS\	4S	 jrS
rU =r$ )r   i:  a  Crops the given image at the center.
If the image is torch Tensor, it is expected
to have [..., H, W] shape, where ... means an arbitrary number of leading dimensions.
If image size is smaller than output size along any edge, image is padded with 0 and then center cropped.

Args:
    size (sequence or int): Desired output size of the crop. If size is an
        int instead of sequence like (h, w), a square crop (size, size) is
        made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).
r   r   r   c                 Z   > [         TU ]  5         [        U5      U l        X l        X0l        g r7   rG   r8   r   r   r   r   r'   r   r   r   rI   s       r*   r8   CenterCropOrPad.__init__F  '     	%		(r-   c                 T    [        XR                  U R                  U R                  S9$ )m
Args:
    img (PIL Image or Tensor): Image to be cropped.

Returns:
    PIL Image or Tensor: Cropped image.
r   )r   r   r   r   )r'   r   s     r*   forwardCenterCropOrPad.forwardQ  s!     "#yytyytO`O`aar-   rD   c                 N    U R                   R                   SU R                   S3$ Nz(size=)rI   r/   r   rV   s    r*   rW   CenterCropOrPad.__repr__[  $    ..))*&1==r-   r   r   r   r   constant)r/   r0   r1   r2   r?   r   r|   r   r   r[   r8   r   rW   r3   r\   r]   s   @r*   r   r   :  so    	 67 *		)T#Y'	) U3S=112	) 		) 	)b># > >r-   r   topleftheightwidthc           
         [         R                  " U 5      u  pxn	X$-   n
X-   nUS:  d  US:  d
  X:  d  X:  aw  [        U* [        SU
5      -   S5      [        U* [        SU5      -   S5      [        U
[        X5      -
  S5      [        U[        X5      -
  S5      /n[         R                  " XXVS9n [        US5      n[        US5      n[         R
                  " XX#U5      $ )zZCrops and/or pads image to meet target size, with control over fill and padding_mode.
    r   r   )r;   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   rightbottomr   s                r*   r   r   _  s     $%#3#3C#8 A[LE\Fax37e1V5JAu%q)s1f~%q)K..2\//3	
 eeCDL
c1+CtQ<D66#D%00r-   c            
          ^  \ rS rSrSr  SS\\\\   4   S\\\\\\4   4   S\	4U 4S jjjr
\S 5       rS rS	\	4S
 jrSrU =r$ )r   i{  zOCrop and/or pad image with random placement within the crop or pad margin.
    r   r   r   c                 Z   > [         TU ]  5         [        U5      U l        X l        X0l        g r7   r   r   s       r*   r8   RandomCropOrPad.__init__  r   r-   c           
      N   [         R                  " U 5      u  p#nX1S   -
  nXAS   -
  n[        [        R                  " [
        R                  " S[        U5      5      U5      5      n[        [        R                  " [
        R                  " S[        U5      5      U5      5      nXx4$ )Nr   ry   )r;   r   r|   r   copysignr   r   abs)	r   r   r   r   r   delta_heightdelta_widthr   r   s	            r*   r   RandomCropOrPad.get_params  s    '('7'7'<$#1g-!G+$--q#l2C DlST4==3{3C!DkRSyr-   c           
          U R                  XR                  5      u  p#[        UUUU R                  S   U R                  S   U R                  U R                  S9$ )r   r   ry   )r   r   r   r   r   r   )r   r   r   r   r   )r'   r   r   r   s       r*   r   RandomCropOrPad.forward  sT     OOC3	99Q<))A,**
 	
r-   rD   c                 N    U R                   R                   SU R                   S3$ r   r   rV   s    r*   rW   RandomCropOrPad.__repr__  r   r-   r   r   )r/   r0   r1   r2   r?   r   r|   r   r   r[   r8   r   r   r   rW   r3   r\   r]   s   @r*   r   r   {  s     67 *		)T#Y'	) U3S=112	) 		) 	)  
&># > >r-   r   c                   4    \ rS rSrSS jr\S 5       rS rSrg)r   i  c                     Xl         X l        g r7   )
input_sizer   )r'   r  r   s      r*   r8   RandomPad.__init__  s    $	r-   c                     [         R                  " U 5      u  p#[        US   U-
  S5      n[        US   U-
  S5      n[        R                  " SU5      n[        R                  " SU5      nXF-
  nXW-
  n	XgX4$ )Nry   r   )r;   r   r   r   r   )
r   r  r   r   r   r   pad_leftpad_top	pad_right
pad_bottoms
             r*   r   RandomPad.get_params  sy    ((-*Q-%/3:a=6115>>![1..L1*	!+
)77r-   c                 ~    U R                  XR                  5      n[        R                  " XU R                  5      nU$ r7   )r   r  r;   r   r   )r'   r   paddings      r*   r+   RandomPad.__call__  s-    //#7eeC$)),
r-   )r   r  N)r   )	r/   r0   r1   r2   r8   r   r   r+   r3   r.   r-   r*   r   r     s      8 8r-   r   c                   Z    \ rS rSrSr       S	S jr\     S
S j5       rS rS r	Sr
g)r   i  z!Resize and Keep Aspect Ratio
    c	                    [        U[        [        45      (       a  [        U5      U l        OX4U l        US:X  a  [        U l        O[        U5      U l        [        U5      U l        X@l	        XPl
        X`l        Xpl        Xl        g)z

Args:
    size:
    longest:
    interpolation:
    random_scale_prob:
    random_scale_range:
    random_scale_area:
    random_aspect_prob:
    random_aspect_range:
r   N)rM   r   r   r   r   r   r   floatlongestrandom_scale_probrandom_scale_rangerandom_scale_arearandom_aspect_probrandom_aspect_range)	r'   r   r  r   r  r  r  r  r  s	            r*   r8   ResizeKeepRatio.__init__  so    . dT5M**dDIDIH$!6D!3M!BDW~!2"4!2"4#6 r-   c                    [         R                  " U 5      SS =u  pn
Uu  pX-  nX-  n[        X5      U-  [        X5      SU-
  -  -   nUS:  a[  [        R                  " 5       U:  aB  [        R
                  " US   US   5      nU(       a  S[        R                  " U5      -  nUU4nOSnUS:  a  [        R                  " 5       U:  a  [        R                  " US   5      [        R                  " US   5      4n[        R                  " [        R
                  " U6 5      n[        R                  " U5      nUS   U-  US   U-  4n[        U
U5       VVs/ s H  u  nn[        UU-  U-  5      PM     nnnU$ s  snnf )zGet parameters
        ry   Nr   r   )r   r   )r;   r   r   r   r   r   r   r   r   r   zipr   )r   target_sizer  r  r  r  r  r  r   r   img_sizer   r   ratio_hratio_wr   ratio_factor
log_aspectaspect_factorr   fr   s                         r*   r   ResizeKeepRatio.get_params  sr    #$"2"23"7";;x(""G%/#g2G2PW<2XXq V]]_7H%H!>>*<Q*?ASTUAVWL   "DIIl$;;(,7L#L!fmmo8J&J((#6q#9:DHHEXYZE[<\]J HHV^^Z%@AM IIm4M )Om;\!_}=\]L14X|1LM1LAa!eem$1LM Ns   E=c           
         U R                  XR                  U R                  U R                  U R                  U R
                  U R                  U R                  5      n[        U R                  [        [        45      (       a!  [        R                  " U R                  5      nOU R                  n[        R                  " XU5      nU$ )z
Args:
    img (PIL Image): Image to be cropped and resized.

Returns:
    PIL Image: Resized, padded to at least target size, possibly cropped to exactly target size
)r   r   r  r  r  r  r  r  rM   r   r   r   r   r   r;   resize)r'   r   r   r   s       r*   r+   ResizeKeepRatio.__call__  s     DLL""D$;$;T=S=S##T%=%=

 d((5$-88"MM$*<*<=M ..Mhhs-0
r-   c                 v   [        U R                  [        [        45      (       a6  SR	                  U R                   Vs/ s H  n[        U5      PM     sn5      nO[        U R                  5      nU R                  R                  SR                  U R                  5      -   nUSU 3-  nUSU R                  S 3-  nUSU R                  S 3-  nUSU R                  S   S S	U R                  S
   S S3-  nUSU R                  S 3-  nUSU R                  S   S S	U R                  S
   S S3-  nU$ s  snf )Nr   r   z, interpolation=z
, longest=z.3fz, random_scale_prob=z, random_scale_range=(r   z, ry   r   z, random_aspect_prob=z, random_aspect_range=(z)))rM   r   r   r   r   r   rI   r/   r   r   r  r  r  r  r  r   s       r*   rW   ResizeKeepRatio.__repr__&  sd   d((5$-88!hhtGYGY'ZGY!(:1(=GY'Z[O01C1CDO//+2D2DTYY2OO+O+<==:dll3%788/0F0Fs/KLL1 33A6s;2d>U>UVW>XY\=]]^` 	`01H1H0MNN2 44Q7<Bt?W?WXY?Z[^>__ac 	c ([s   D6)r   r  r  r  r  r  r  r   N)        rh   r(  )g333333?g?Fr(  g?g(\?)r(  )r   gHzG?Fr(  r)  r   r.   r-   r*   r   r     sT     $ +#! +$7L 
 !*#! +% %N(r-   r   c                   6   ^  \ rS rSrS\4U 4S jjrS rSrU =r$ )r   i7  border_sizec                 .   > [         TU ]  5         Xl        g r7   )rG   r8   r+  )r'   r+  rI   s     r*   r8   TrimBorder.__init__9  s     	&r-   c                    [         R                  " U5      u  p#U R                  =pE[        XC5      n[        XS5      n[	        SUSU R                  -  -
  5      n[	        SUSU R                  -  -
  5      n[         R
                  " XXVU5      $ )Nr   r    )r;   r   r+  r   r   r   )r'   r   r   r   r   r   r   r   s           r*   r   TrimBorder.forward@  s    $%%%#k4|QA 0 0001Aq1t////0vvce44r-   )r+  )	r/   r0   r1   r2   r|   r8   r   r3   r\   r]   s   @r*   r   r   7  s    ''5 5r-   r   )z3Please provide only two dimensions (h, w) for size.r   );r   rz   r   r   typingr   r   r   r   r@   torchvision.transforms
transforms!torchvision.transforms.functional
functionalr;   r   rr   ImportErrorPILr   numpyr!   __all__r	   r
   r   r   hasattrrf   NEARESTBILINEARBICUBICBOXHAMMINGLANCZOSrv   itemsrn   ru   rs   _pil_interpolation_to_torchr   r   r   r   r   r   rN   r|   r[   r   nnModuler   r   r   r   r   r   )abs   00r*   <module>rF     sj       / /  + - -#C!   = =.J'' ... .8 5,  )!!:  )e  )  )! 	y
y		5yy! /H.M.M.OP.OdaQT.OP  !!9""J!!9u!!9!!9# 5P4U4U4W"X4WDA144W"X"$"$/3/ ,J79KI9VW 
g gZ 23&	'E\\'E3S	>*'E CsC}--.'E 	'E
 \\'ET">ehhoo ">V 23&1\\11 1 	1
 1 CsC}--.1 1 \\18,>ehhoo ,>^ ,t tn5 5U  #"#J Q #Ys   L ,L"LLL