
    khN                        d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	Z	d dl
mZ d dlmc mZ 	 d dlmZ dZd dlmZ d dlZg dZ G d d	      Z G d
 d      Z G d dej0                        Z G d d      Z eed      rej8                  j:                  dej8                  j<                  dej8                  j>                  dej8                  j@                  dej8                  jB                  dej8                  jD                  diZ#nJej:                  dej<                  dej>                  dej@                  dejB                  dejD                  diZ#e#jI                         D  ci c]  \  } }|| 
 c}} Z%eroej:                  dej<                  dej>                  dej@                  dejB                  dejD                  diZ&e&jI                         D  ci c]  \  } }|| 
 c}} Z'ni Z(i Z&d Z)d Z*d Z+ e*d       e*d      fZ,d2dZ- G d d      Z.	 	 d3de	j^                  dee0ee0   f   dee0ee0e0e0f   f   d e1d!e	j^                  f
d"Z2 G d# d$e	jf                  jh                        Z5	 	 d3de	j^                  d%e0d&e0d'e0d(e0dee0ee0e0e0f   f   d e1d!e	j^                  fd)Z6 G d* d+e	jf                  jh                        Z7 G d, d-      Z8 G d. d/      Z9 G d0 d1e	jf                  jh                        Z:y# e$ r dZY w xY wc c}} w c c}} w )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                       e Zd Zd Zy)r	   c                     t        j                  |t         j                        }|j                  dk  rt        j                  |d      }t        j
                  |d      }|S )Ndtype   )axis   )nparrayuint8ndimexpand_dimsrollaxis)selfpil_imgnp_imgs      P/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/data/transforms.py__call__zToNumpy.__call__   sF    '2;;?^^F4FVQ'    N)__name__
__module____qualname__r*    r+   r)   r	   r	      s    r+   r	   c                   4    e Zd ZdZej
                  fdZd Zy)r
   z% ToTensor with no rescaling of valuesc                     || _         y Nr   )r&   r   s     r)   __init__zToTensor.__init__%   s	    
r+   c                 `    t        j                  |      j                  | j                        S )Nr   )Fpil_to_tensortor   )r&   r'   s     r)   r*   zToTensor.__call__(   s#    w'***<<r+   N)r,   r-   r.   __doc__torchfloat32r3   r*   r/   r+   r)   r
   r
   #   s    /"]] =r+   r
   c                   P     e Zd ZdZd fdZdej                  fdZdefdZ	 xZ
S )r   zFConvert a PIL Image or ndarray to tensor if it's not already one.
    returnc                 "    t         |           y r2   superr3   r&   	__class__s    r)   r3   zMaybeToTensor.__init__0       r+   c                 d    t        |t        j                        r|S t        j                  |      S )z
        Args:
            pic (PIL Image or numpy.ndarray): Image to be converted to tensor.

        Returns:
            Tensor: Converted image.
        )
isinstancer9   Tensorr5   	to_tensorr&   pics     r)   r*   zMaybeToTensor.__call__3   s&     c5<<(J{{3r+   c                 4    | j                   j                   dS Nz()rA   r,   r&   s    r)   __repr__zMaybeToTensor.__repr__?       ..))*"--r+   r<   N)r,   r-   r.   r8   r3   r9   rE   r*   strrM   __classcell__rA   s   @r)   r   r   ,   s)    
 u|| 
 .# .r+   r   c                   6     e Zd ZdZd fdZd ZdefdZ xZS )r   zSConvert a PIL Image to a tensor of the same type - this does not scale values.
    r<   c                 "    t         |           y r2   r>   r@   s    r)   r3   zMaybePILToTensor.__init__G   rB   r+   c                 d    t        |t        j                        r|S t        j                  |      S )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.
        )rD   r9   rE   r5   r6   rG   s     r)   r*   zMaybePILToTensor.__call__J   s&     c5<<(Js##r+   c                 4    | j                   j                   dS rJ   rK   rL   s    r)   rM   zMaybePILToTensor.__repr__X   rN   r+   rO   )	r,   r-   r.   r8   r3   r*   rP   rM   rQ   rR   s   @r)   r   r   C   s    $.# .r+   r   
Resamplingnearestbilinearbicubicboxhamminglanczosc                     t         |    S r2   )_str_to_pil_interpolationmode_strs    r)   r   r      s    $X..r+   c                 2    t         r	t        |    S t        |    S r2   )has_interpolation_mode_str_to_torch_interpolationr_   r`   s    r)   r   r      s    *844(22r+   c                 2    t         r	t        |    S t        |    S r2   )rc   _torch_interpolation_to_str_pil_interpolation_to_str)modes    r)   r   r      s    *400(..r+   c                     t        | t        j                        rt        |       t        |       fS t        | t              rt        |       dk(  r
| d   | d   fS t        |       dk7  rt        |      | S )N   r   r   )rD   numbersNumberintr   len
ValueError)size	error_msgs     r)   _setup_sizerr      se    $'4y#d)##$!c$i1nAwQ
4yA~##Kr+   c                   :    e Zd ZdZ	 	 	 ddZed        Zd Zd Zy)r   aY  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                    t        |t        t        f      rt        |      | _        n	||f| _        |d   |d   kD  s|d   |d   kD  rt	        j
                  d       |dk(  rt        | _        nt        |      | _        || _	        || _
        y )Nr   rj   z"range should be of kind (min, max)random)rD   listtuplerp   warningswarn_RANDOM_INTERPOLATIONinterpolationr   scaleratio)r&   rp   r|   r}   r{   s        r)   r3   z*RandomResizedCropAndInterpolation.__init__   s     dT5M*dDItDI!HuQxU1Xa%8MM>?H$!6D!3M!BD

r+   c           	      f   t        j                  |       \  }}||z  }t        d      D ]  }t        j                  | |z  }t        j                  |d         t        j                  |d         f}t        j                  t        j                  |       }	t        t        t        j                  ||	z                    }
t        t        t        j                  ||	z                    }|
|k  s||k  st        j                  d||z
        }t        j                  d||
z
        }||||
fc S  ||z  }|t        |      k  r#|}
t        t        |
t        |      z              }n5|t        |      kD  r#|}t        t        |t        |      z              }
n|}
|}||z
  dz  }||
z
  dz  }||||
fS )a  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   rj   r   )r5   get_image_sizerangeru   uniformmathlogexprm   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_paramsz,RandomResizedCropAndInterpolation.get_params   s    '',uu}Ry 
	0G ..%047K%(+TXXeAh-?@I88FNNI$>?L5;+E!FGHH5;+E!FGHH5 X%6NN1eh&67NN1eh&67!Xx//
	0 5=c%j H5CJ!678HE
"H5CJ!678HHHX!#X!#!Xx''r+   c           	      <   | j                  || j                  | j                        \  }}}}t        | j                  t
        t        f      r t        j                  | j                        }n| j                  }t        j                  |||||| j                  |      S )z
        Args:
            img (PIL Image): Image to be cropped and resized.

        Returns:
            PIL Image: Randomly cropped and resized image.
        )r   r|   r}   rD   r{   rw   rv   ru   choicer5   resized_croprp   )r&   r   r   r   hwr{   s          r)   r*   z*RandomResizedCropAndInterpolation.__call__   sx     __S$**djjA
1ad((5$-8"MM$*<*<=M ..M~~c1aAtyy-HHr+   c                 $   t        | j                  t        t        f      r3dj	                  | j                  D cg c]  }t        |       c}      }nt        | j                        }| j                  j                  dj                  | j                        z   }|dj                  t        d | j                  D                    z  }|dj                  t        d | j                  D                    z  }|dj                  |      z  }|S c c}w )N 	(size={0}z, scale={0}c              3   4   K   | ]  }t        |d         yw   Nr   ).0ss     r)   	<genexpr>z=RandomResizedCropAndInterpolation.__repr__.<locals>.<genexpr>
       3TAE!QK3T   z, ratio={0}c              3   4   K   | ]  }t        |d         ywr   r   )r   rs     r)   r   z=RandomResizedCropAndInterpolation.__repr__.<locals>.<genexpr>  r   r   z, interpolation={0}))rD   r{   rw   rv   joinr   rA   r,   formatrp   r|   r}   r&   xinterpolate_strformat_strings       r)   rM   z*RandomResizedCropAndInterpolation.__repr__  s    d((5$-8!hhtGYGY'Z!(:1(='Z[O01C1CDO//+2D2DTYY2OO--e3T3T.TUU--e3T3T.TUU/66GG ([s   DN))g{Gz?      ?)g      ?gUUUUUU?rY   	r,   r-   r.   r8   r3   staticmethodr   r*   rM   r/   r+   r)   r   r      s7    " $$* (( ((TI	r+   r   r   output_sizefillpadding_moder<   c                    t        |      }|\  }}t        j                  |       \  }}}||kD  s||kD  r~||kD  r||z
  dz  nd||kD  r||z
  dz  nd||kD  r||z
  dz   dz  nd||kD  r||z
  dz   dz  ndg}	t        j                  | |	||      } t        j                  |       \  }}}||k(  r||k(  r| S t	        t        ||z
  dz              }
t	        t        ||z
  dz              }t        j                  | |
|||      S )ak  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   rj   r   r   g       @)rr   r5   get_dimensionspadrm   r   crop)r   r   r   r   crop_height
crop_width_image_heightimage_widthpadding_ltrbcrop_top	crop_lefts               r)   r   r     s4   * k*K)K#$#3#3C#8 A|[K;#=/9K/GZ+%!+Q1<|1K[<'A-QR3=3KZ+%)a/QR5@<5O[<'!+1UV	
 eeCD|L'('7'7'<$<$)DJ5,4;<=HE;3s:;<I66#xKDDr+   c            
       n     e Zd ZdZ	 	 d	deeee   f   deeeeeef   f   def fdZ	d Z
defdZ xZS )
r   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]).
    rp   r   r   c                 ^    t         |           t        |      | _        || _        || _        y r2   r?   r3   rr   rp   r   r   r&   rp   r   r   rA   s       r)   r3   zCenterCropOrPad.__init__F  +     	%		(r+   c                 \    t        || j                  | j                  | j                        S )
        Args:
            img (PIL Image or Tensor): Image to be cropped.

        Returns:
            PIL Image or Tensor: Cropped image.
        r   )r   rp   r   r   )r&   r   s     r)   forwardzCenterCropOrPad.forwardQ  s#     "#tyytyytO`O`aar+   r<   c                 N    | j                   j                   d| j                   dS Nz(size=)rA   r,   rp   rL   s    r)   rM   zCenterCropOrPad.__repr__[  $    ..))*&1==r+   r   constant)r,   r-   r.   r8   r   rm   r   r   rP   r3   r   rM   rQ   rR   s   @r)   r   r   :  se    	 67 *		)T#Y'	) U3S=112	) 		)b># >r+   r   topleftheightwidthc           
         t        j                  |       \  }}}	||z   }
||z   }|dk  s|dk  s
|
|	kD  s||kD  r}t        | t        d|
      z   d      t        | t        d|      z   d      t        |
t        |	|      z
  d      t        |t        ||      z
  d      g}t        j                  | |||      } t        |d      }t        |d      }t        j
                  | ||||      S )z[ Crops and/or pads image to meet target size, with control over fill and padding_mode.
    r   r   )r5   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|[5LE6\Fax37ek1Vl5JAu%q)s1f~%q)K..2\3//3	
 eeCD|L
c1+CtQ<D66#sD&%00r+   c            
       ~     e Zd ZdZ	 	 d
deeee   f   deeeeeef   f   def fdZ	e
d        Zd Zdefd	Z xZS )r   zP Crop and/or pad image with random placement within the crop or pad margin.
    rp   r   r   c                 ^    t         |           t        |      | _        || _        || _        y r2   r   r   s       r)   r3   zRandomCropOrPad.__init__  r   r+   c           
      L   t        j                  |       \  }}}||d   z
  }||d   z
  }t        t        j                  t        j                  dt        |            |            }t        t        j                  t        j                  dt        |            |            }||fS )Nr   rj   )r5   r   rm   r   copysignru   r   abs)	r   rp   r   r   r   delta_heightdelta_widthr   r   s	            r)   r   zRandomCropOrPad.get_params  s    '('7'7'<$<#d1g-!DG+$--q#l2C DlST4==3{3C!DkRSDyr+   c           	          | j                  || j                        \  }}t        |||| j                  d   | j                  d   | j                  | j                        S )r   r   rj   )r   r   r   r   r   r   )r   rp   r   r   r   )r&   r   r   r   s       r)   r   zRandomCropOrPad.forward  sX     OOC3	T99Q<))A,**
 	
r+   r<   c                 N    | j                   j                   d| j                   dS r   r   rL   s    r)   rM   zRandomCropOrPad.__repr__  r   r+   r   )r,   r-   r.   r8   r   rm   r   r   rP   r3   r   r   r   rM   rQ   rR   s   @r)   r   r   {  sx     67 *		)T#Y'	) U3S=112	) 		)  
&># >r+   r   c                   *    e Zd ZddZed        Zd Zy)r   c                      || _         || _        y r2   )
input_sizer   )r&   r   r   s      r)   r3   zRandomPad.__init__  s    $	r+   c                     t        j                  |       \  }}t        |d   |z
  d      }t        |d   |z
  d      }t        j                  d|      }t        j                  d|      }||z
  }||z
  }	||||	fS )Nrj   r   )r5   r   r   ru   r   )
r   r   r   r   r   r   pad_leftpad_top	pad_right
pad_bottoms
             r)   r   zRandomPad.get_params  s    ((-v*Q-%/3:a=6115>>![1..L1(*	!G+
)Z77r+   c                     | j                  || j                        }t        j                  ||| j                        }|S r2   )r   r   r5   r   r   )r&   r   paddings      r)   r*   zRandomPad.__call__  s1    //#t7eeC$)),
r+   N)r   )r,   r-   r.   r3   r   r   r*   r/   r+   r)   r   r     s      8 8r+   r   c                   N    e Zd ZdZ	 	 	 	 	 	 	 ddZe	 	 	 	 	 dd       Zd Zd Zy)	r   z" Resize and Keep Aspect Ratio
    c	                    t        |t        t        f      rt        |      | _        n	||f| _        |dk(  rt        | _        nt        |      | _        t        |      | _        || _	        || _
        || _        || _        || _        y)z

        Args:
            size:
            longest:
            interpolation:
            random_scale_prob:
            random_scale_range:
            random_scale_area:
            random_aspect_prob:
            random_aspect_range:
        ru   N)rD   rv   rw   rp   rz   r{   r   floatlongestrandom_scale_probrandom_scale_rangerandom_scale_arearandom_aspect_probrandom_aspect_range)	r&   rp   r   r{   r   r   r   r   r   s	            r)   r3   zResizeKeepRatio.__init__  sx    . dT5M*dDItDIH$!6D!3M!BDW~!2"4!2"4#6 r+   c                    t        j                  |       dd x\  }}	}
|\  }}||z  }|	|z  }t        ||      |z  t        ||      d|z
  z  z   }|dkD  rRt	        j                         |k  r;t	        j
                  |d   |d         }|rdt        j                  |      z  }||f}nd}|dkD  rt	        j                         |k  rzt        j                  |d         t        j                  |d         f}t        j                  t	        j
                  |       }t        j                  |      }|d   |z  |d   |z  f}t        |
|      D cg c]  \  }}t        ||z  |z         }}}|S c c}}w )zGet parameters
        rj   Nr   r   )r   r   )r5   r   r   r   ru   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   frp   s                         r)   r   zResizeKeepRatio.get_params  s{    #$"2"23"7";;ux((("("GW%/#gw2G2PW<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MAa!eem$MM Ns   E-c           
         | j                  || j                  | j                  | j                  | j                  | j
                  | j                  | j                        }t        | j                  t        t        f      r t        j                  | j                        }n| j                  }t        j                  |||      }|S )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   rp   r   r   r   r   r   r   rD   r{   rw   rv   ru   r   r5   resize)r&   r   rp   r{   s       r)   r*   zResizeKeepRatio.__call__  s     DLL""D$;$;T=S=S##T%=%=

 d((5$-8"MM$*<*<=M ..MhhsD-0
r+   c                 f   t        | j                  t        t        f      r3dj	                  | j                  D cg c]  }t        |       c}      }nt        | j                        }| j                  j                  dj                  | j                        z   }|d| z  }|d| j                  dz  }|d| j                  dz  }|d| j                  d   dd	| j                  d
   ddz  }|d| j                  dz  }|d| j                  d   dd	| j                  d
   ddz  }|S c c}w )Nr   r   z, interpolation=z
, longest=z.3fz, random_scale_prob=z, random_scale_range=(r   z, rj   r   z, random_aspect_prob=z, random_aspect_range=(z)))rD   r{   rw   rv   r   r   rA   r,   r   rp   r   r   r   r   r   r   s       r)   rM   zResizeKeepRatio.__repr__&  s[   d((5$-8!hhtGYGY'Z!(:1(='Z[O01C1CDO//+2D2DTYY2OO+O+<==:dll3%788/0F0Fs/KLL1 33A6s;2d>V>VWX>YZ]=^^_a 	a01H1H0MNN2 44Q7<Bt?W?WXY?Z[^>__ac 	c ([s   D.N)        rY   r  )g333333?g?Fr  g?g(\?)r  )r   gHzG?Fr  r  r   r/   r+   r)   r   r     sT     $ +#! +$7L 
 !*#! +% %N(r+   r   c                   *     e Zd Zdef fdZd Z xZS )r   border_sizec                 0    t         |           || _        y r2   )r?   r3   r  )r&   r  rA   s     r)   r3   zTrimBorder.__init__9  s     	&r+   c                     t        j                  |      \  }}| j                  x}}t        ||      }t        ||      }t	        d|d| j                  z  z
        }t	        d|d| j                  z  z
        }t        j
                  |||||      S )Nr   r   )r5   r   r  r   r   r   )r&   r   r   r   r   r   r   r   s           r)   r   zTrimBorder.forward@  s    $1%%%d#qk4|QA 0 0001Aq1t////0vvc3fe44r+   )r,   r-   r.   rm   r3   r   rQ   rR   s   @r)   r   r   7  s    ''5r+   r   )z3Please provide only two dimensions (h, w) for size.r   );r   rk   ru   rx   typingr   r   r   r   r9   torchvision.transforms
transforms!torchvision.transforms.functional
functionalr5   r   rc   ImportErrorPILr   numpyr    __all__r	   r
   r   r   hasattrrW   NEARESTBILINEARBICUBICBOXHAMMINGLANCZOSrg   itemsr_   rf   rd   _pil_interpolation_to_torchr   r   r   rz   rr   r   rE   rm   rP   r   nnModuler   r   r   r   r   r   )abs   00r)   <module>r      sc       / /  + - -#C!   = =.J'' ... .8 5,  )!!:  )e  )  )! 	y
y		5yy! /H.M.M.OPdaQTP  !!9""J!!9u!!9!!9# 5P4U4U4W"XDAq1a4"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   K4 'LL4K?>K?