
    NgN                        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n# e$ r dZY nw xY wd dlmZ d dlZg dZ G d d	          Z G d
 d          Z G d dej                  Z G d d          Z eed          rKej        j        dej        j        dej        j        dej        j         dej        j!        dej        j"        diZ#n,ej        dej        dej        dej         dej!        dej"        diZ#d e#$                                D             Z%erKej        dej        dej        dej         dej!        dej"        diZ&d e&$                                D             Z'ni Z(i Z&d Z)d Z*d Z+ e*d           e*d          fZ,d6dZ- G d d          Z.	 	 d7d!e	j/        d"ee0ee0         f         d#ee0ee0e0e0f         f         d$e1d%e	j/        f
d&Z2 G d' d(e	j3        j4                  Z5	 	 d7d!e	j/        d)e0d*e0d+e0d,e0d#ee0ee0e0e0f         f         d$e1d%e	j/        fd-Z6 G d. d/e	j3        j4                  Z7 G d0 d1          Z8 G d2 d3          Z9 G d4 d5e	j3        j4                  Z:dS )8    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dS )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/html/ai-engine/env/lib/python3.11/site-packages/timm/data/transforms.py__call__zToNumpy.__call__   sM    '222;??^F444FVQ''    N)__name__
__module____qualname__r*    r+   r)   r	   r	      s#            r+   r	   c                   ,    e Zd ZdZej        fdZd ZdS )r
   z% ToTensor with no rescaling of valuesc                     || _         d S Nr   )r&   r   s     r)   __init__zToTensor.__init__%   s    


r+   c                 \    t          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
   #   sB        //"]    = = = = =r+   r
   c                   F     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.
    returnNc                 H    t                                                       d S r2   superr3   r&   	__class__s    r)   r3   zMaybeToTensor.__init__0       r+   c                 b    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                      | 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<   Nc                 H    t                                                       d S r2   r>   r@   s    r)   r3   zMaybePILToTensor.__init__G   rB   r+   c                 b    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                      | 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   sq              $ $ $.# . . . . . . . .r+   r   
Resamplingnearestbilinearbicubicboxhamminglanczosc                     i | ]\  }}||	S r/   r/   .0abs      r)   
<dictcomp>rc   r   s    PPPdaQPPPr+   c                     i | ]\  }}||	S r/   r/   r_   s      r)   rc   rc   ~   s    "X"X"XDAq1a"X"X"Xr+   c                     t           |          S r2   )_str_to_pil_interpolationmode_strs    r)   r   r      s    $X..r+   c                 D    t           rt          |          S t          |          S r2   )has_interpolation_mode_str_to_torch_interpolationrf   rg   s    r)   r   r      s      3*844(22r+   c                 D    t           rt          |          S t          |          S r2   )rj   _torch_interpolation_to_str_pil_interpolation_to_str)modes    r)   r   r      s      /*400(..r+   3Please provide only two dimensions (h, w) for size.c                 *   t          | t          j                  rt          |           t          |           fS t          | t                    r#t          |           dk    r| d         | d         fS t          |           dk    rt          |          | S )N   r   r   )rD   numbersNumberintr   len
ValueError)size	error_msgs     r)   _setup_sizerz      s    $'' $4yy#d))##$!!  c$ii1nnAwQ
4yyA~~###Kr+   c                   B    e Zd ZdZ	 	 	 d
dZed             Zd Zd Zd	S )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
    g{Gz?      ?g      ?gUUUUUU?rY   c                 T   t          |t          t          f          rt          |          | _        n	||f| _        |d         |d         k    s|d         |d         k    rt	          j        d           |dk    rt          | _        nt          |          | _        || _	        || _
        d S )Nr   rr   z"range should be of kind (min, max)random)rD   listtuplerx   warningswarn_RANDOM_INTERPOLATIONinterpolationr   scaleratio)r&   rx   r   r   r   s        r)   r3   z*RandomResizedCropAndInterpolation.__init__   s     dT5M** 	%dDIItDI!HuQxU1Xa%8%8M>???H$$!6D!3M!B!BD



r+   c           	         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    r>||k    r8t          j        d||z
            }t          j        d||
z
            }||||
fc S ||z  }|t          |          k     r/|}
t          t          |
t          |          z                      }nF|t          |          k    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   rr   r   )r5   get_image_sizeranger   uniformmathlogexpru   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}Ryy 
	0 
	0G .%047K%(++TXeAh-?-?@I8FNI$>??L5;+E!F!FGGHHH5;+E!F!FGGHHH5  X%6%6N1eh&677N1eh&677!Xx//// 5=c%jj  H5CJJ!67788HHE

""H5CJJ!67788HHHHX!#X!#!Xx''r+   c           	         |                      || 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   r   r   r   choicer5   resized_croprx   )r&   r   r   r   hwr   s          r)   r*   z*RandomResizedCropAndInterpolation.__call__   sx     __S$*djAA
1ad(5$-88 	/"M$*<==MM .M~c1aAty-HHHr+   c                    t          | j        t          t          f          r%d                    d | j        D                       }nt          | j                  }| j        j        d                    | j	                  z   }|d                    t          d | j
        D                                 z  }|d                    t          d | j        D                                 z  }|d                    |          z  }|S )	N c                 ,    g | ]}t          |          S r/   r   r`   xs     r)   
<listcomp>z>RandomResizedCropAndInterpolation.__repr__.<locals>.<listcomp>  !    'Z'Z'Z!(:1(=(='Z'Z'Zr+   	(size={0}z, scale={0}c              3   6   K   | ]}t          |d           V  dS    Nr   )r`   ss     r)   	<genexpr>z=RandomResizedCropAndInterpolation.__repr__.<locals>.<genexpr>
  *      3T3TAE!QKK3T3T3T3T3T3Tr+   z, ratio={0}c              3   6   K   | ]}t          |d           V  dS r   r   )r`   rs     r)   r   z=RandomResizedCropAndInterpolation.__repr__.<locals>.<genexpr>  r   r+   z, interpolation={0}))rD   r   r   r   joinr   rA   r,   formatrx   r   r   r&   interpolate_strformat_strings      r)   rM   z*RandomResizedCropAndInterpolation.__repr__  s    d(5$-88 	E!hh'Z'ZtGY'Z'Z'Z[[OO01CDDO/+2D2DTY2O2OO--e3T3T3T3T3T.T.TUUU--e3T3T3T3T3T.T.TUUU/66GGGr+   N)r|   r~   rY   	r,   r-   r.   r8   r3   staticmethodr   r*   rM   r/   r+   r)   r   r      sz         " $$   * (( (( \((TI I I	 	 	 	 	r+   r   constantr   output_sizefillpadding_moder<   c                 .   t          |          }|\  }}t          j        |           \  }}}||k    s||k    r||k    r||z
  dz  nd||k    r||z
  dz  nd||k    r||z
  dz   dz  nd||k    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   rr   r   r   g       @)rz   r5   get_dimensionspadru   r   crop)r   r   r   r   crop_height
crop_width_image_heightimage_widthpadding_ltrbcrop_top	crop_lefts               r)   r   r     sl   * k**K)K#$#3C#8#8 A|[K;#=#=/9K/G/GZ+%!++Q1<|1K1K[<'A--QR3=3K3KZ+%)a//QR5@<5O5O[<'!+11UV	
 eCD|LLL'('7'<'<$<$$)D)DJ5,4;<<==HE;3s:;;<<I6#xKDDDr+   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	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]).
    r   r   rx   r   r   c                     t                                                       t          |          | _        || _        || _        d S r2   r?   r3   rz   rx   r   r   r&   rx   r   r   rA   s       r)   r3   zCenterCropOrPad.__init__F  ?     	%%		(r+   c                 F    t          || j        | j        | j                  S )
        Args:
            img (PIL Image or Tensor): Image to be cropped.

        Returns:
            PIL Image or Tensor: Cropped image.
        r   )r   rx   r   r   )r&   r   s     r)   forwardzCenterCropOrPad.forwardQ  s"     "#tytytO`aaaar+   r<   c                 0    | j         j         d| j         dS Nz(size=)rA   r,   rx   rL   s    r)   rM   zCenterCropOrPad.__repr__[       .)======r+   r   r   )r,   r-   r.   r8   r   ru   r   r   rP   r3   r   rM   rQ   rR   s   @r)   r   r   :  s        	 	 67 *		) 	)T#Y'	) U3S=112	) 		) 	) 	) 	) 	) 	)b b b># > > > > > > > >r+   r   topleftheightwidthc           
         t          j        |           \  }}}	||z   }
||z   }|dk     s|dk     s|
|	k    s||k    rt          | 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    $%#3C#8#8 A|[5LE6\Faxx377ek11Vl5J5JAu%q))s1f~~%q))K...22\3///33	
 eCD|LLL
c1++CtQ<<D6#sD&%0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.
    r   r   rx   r   r   c                     t                                                       t          |          | _        || _        || _        d S r2   r   r   s       r)   r3   zRandomCropOrPad.__init__  r   r+   c           
      n   t          j        |           \  }}}||d         z
  }||d         z
  }t          t          j        t          j        dt          |                    |                    }t          t          j        t          j        dt          |                    |                    }||fS )Nr   rr   )r5   r   ru   r   copysignr   r   abs)	r   rx   r   r   r   delta_heightdelta_widthr   r   s	            r)   r   zRandomCropOrPad.get_params  s    '('7'<'<$<#d1g-!DG+$-q#l2C2C D DlSSTT4=3{3C3C!D!DkRRSSDyr+   c           	          |                      || j                  \  }}t          |||| j        d         | j        d         | j        | j                  S )r   r   rr   )r   r   r   r   r   r   )r   rx   r   r   r   )r&   r   r   r   s       r)   r   zRandomCropOrPad.forward  sZ     OOC33	T9Q<)A,*
 
 
 	
r+   r<   c                 0    | j         j         d| j         dS r   r   rL   s    r)   rM   zRandomCropOrPad.__repr__  r   r+   r   )r,   r-   r.   r8   r   ru   r   r   rP   r3   r   r   r   rM   rQ   rR   s   @r)   r   r   {  s          67 *		) 	)T#Y'	) U3S=112	) 		) 	) 	) 	) 	) 	)   \
 
 
&># > > > > > > > >r+   r   c                   2    e Zd ZddZed             Zd ZdS )r   r   c                 "    || _         || _        d S 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 )Nrr   r   )r5   r   r   r   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3:a=61155>![11.L11(*	!G+
)Z77r+   c                 r    |                      || j                  }t          j        ||| j                  }|S r2   )r   r   r5   r   r   )r&   r   paddings      r)   r*   zRandomPad.__call__  s1    //#t77eC$),,
r+   N)r   )r,   r-   r.   r3   r   r   r*   r/   r+   r)   r   r     sR            8 8 \8    r+   r   c                   V    e Zd ZdZ	 	 	 	 	 	 	 ddZe	 	 	 	 	 dd	            Zd
 Zd ZdS )r   z" Resize and Keep Aspect Ratio
            rY   g333333?g?Fg?g(\?c	                 6   t          |t          t          f          rt          |          | _        n	||f| _        |dk    rt          | _        nt          |          | _        t          |          | _        || _	        || _
        || _        || _        || _        dS )z

        Args:
            size:
            longest:
            interpolation:
            random_scale_prob:
            random_scale_range:
            random_scale_area:
            random_aspect_prob:
            random_aspect_range:
        r   N)rD   r   r   rx   r   r   r   floatlongestrandom_scale_probrandom_scale_rangerandom_scale_arearandom_aspect_probrandom_aspect_range)	r&   rx   r  r   r  r  r  r  r  s	            r)   r3   zResizeKeepRatio.__init__  s    . dT5M** 	%dDIItDIH$$!6D!3M!B!BDW~~!2"4!2"4#6   r+   r}   gHzG?c                    t          j        |           dd         x\  }}	}
|\  }}||z  }|	|z  }t          ||          |z  t          ||          d|z
  z  z   |dk    rVt	          j                    |k     r?t	          j        |d         |d                   }|rdt          j        |          z  }||f}nd}|dk    rt	          j                    |k     r~t          j        |d                   t          j        |d                   f}t          j	        t	          j        |           }t          j        |          }|d         |z  |d         |z  f}fdt          |
|          D             }|S )zGet parameters
        rr   Nr}   r   )r}   r}   c                 @    g | ]\  }}t          ||z  z            S r/   r   )r`   r   fr   s      r)   r   z.ResizeKeepRatio.get_params.<locals>.<listcomp>  s-    MMMAa!eem$$MMMr+   )r5   r   r   r   r   r   r   r   r   r   zip)r   target_sizer  r  r  r  r  r  r   r   img_sizer   r   ratio_hratio_wratio_factor
log_aspectaspect_factorrx   r   s                      @r)   r   zResizeKeepRatio.get_params  s    #$"23"7"7";;ux((("("GW%%/#gw2G2G2PW<2XXq  V]__7H%H%H!>*<Q*?ASTUAVWWL  <  "DIl$;$;;(,7LL#L!!fmoo8J&J&J(#6q#9::DHEXYZE[<\<\]J HV^Z%@AAM Im44M )Om;\!_}=\]LMMMMX|1L1LMMMr+   c           
      4   |                      || 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   rx   r  r  r  r  r  r  rD   r   r   r   r   r   r5   resize)r&   r   rx   r   s       r)   r*   zResizeKeepRatio.__call__  s     DL"D$;T=S#T%=
 

 d(5$-88 	/"M$*<==MM .MhsD-00
r+   c                    t          | j        t          t          f          r%d                    d | j        D                       }nt          | j                  }| j        j        d                    | 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 )Nr   c                 ,    g | ]}t          |          S r/   r   r   s     r)   r   z,ResizeKeepRatio.__repr__.<locals>.<listcomp>(  r   r+   r   z, interpolation=z
, longest=z.3fz, random_scale_prob=z, random_scale_range=(r   z, rr   r   z, random_aspect_prob=z, random_aspect_range=(z)))rD   r   r   r   r   r   rA   r,   r   rx   r  r  r  r  r  r   s      r)   rM   zResizeKeepRatio.__repr__&  sk   d(5$-88 	E!hh'Z'ZtGY'Z'Z'Z[[OO01CDDO/+2D2DTY2O2OO=O===8dl8888L0FLLLL a 3A6`a a>B>VWX>Y`a a a 	aN1HNNNN c 4Q7bc c?C?WXY?Zbc c c 	cr+   N)r   rY   r   r   Fr   r   )r   r  Fr   r   r   r/   r+   r)   r   r     s          $ +#! +$7 $7 $7 $7L 
 !*#! +% % % \%N  (    r+   r   c                   *     e Zd Zdef fdZd Z xZS )r   border_sizec                 V    t                                                       || _        d S 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#qkk4||QA 00011Aq1t///00vc3fe444r+   )r,   r-   r.   ru   r3   r   rQ   rR   s   @r)   r   r   7  sT        '' ' ' ' ' '5 5 5 5 5 5 5r+   r   )rp   r   );r   rs   r   r   typingr   r   r   r   r9   torchvision.transforms
transforms!torchvision.transforms.functional
functionalr5   r   rj   ImportErrorPILr   numpyr    __all__r	   r
   r   r   hasattrrW   NEARESTBILINEARBICUBICBOXHAMMINGLANCZOSrn   itemsrf   rm   rk   _pil_interpolation_to_torchr   r   r   r   rz   r   rE   ru   rP   r   nnModuler   r   r   r   r   r   r/   r+   r)   <module>r/     sY       / / / / / / / / / / / /  + + + + + + - - - - - - - - -#CCCCCC! # # #"#                   = = = = = = = =. . . . .J' . . ... . . . . . . .8 75,  )!: )e ) )! 	y
y	5yy! QP.G.M.M.O.OPPP   %!9"J!9u!9!9# #Y"X4O4U4U4W4W"X"X"X"$"$/ / /3 3 3/ / / ,+J779K9KI9V9VW 
 
 
 
g g g g g g g gZ 23&	'E 'E\'E3S	>*'E CsC}--.'E 	'E
 \'E 'E 'E 'ET"> "> "> "> ">eho "> "> ">V 23&1 1\11 1 	1
 1 CsC}--.1 1 \1 1 1 18,> ,> ,> ,> ,>eho ,> ,> ,>^       ,t t t t t t t tn5 5 5 5 5 5 5 5 5 5s   : AA