U
    ¯Ê¹j¤I  ã                   @   sž  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  mZ zd dl
mZ dZW n ek
r~   dZY nX d dlmZ d dlZddd	d
dddddddddgZG dd„ dƒZG dd„ dƒZeedƒ�re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d„ e  !¡ D ƒZ"e�rŒejdejdejdejdejdejdiZ#dd„ e# !¡ D ƒZ$ni Z%i Z#d d
„ Z&d!d	„ Z'd"d„ Z(e'dƒe'dƒfZ)d1d$d%„Z*G d&d„ dƒZ+d2e	j,ee-ee- f ee-ee-e-e-f f e.e	j,d(œd)d„Z/G d*d„ de	j0j1ƒZ2d3e	j,e-e-e-e-ee-ee-e-e-f f e.e	j,d+œd,d„Z3G d-d„ de	j0j1ƒZ4G d.d„ dƒZ5G d/d„ dƒZ6G d0d„ de	j0j1ƒZ7dS )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Ú
TrimBorderc                   @   s   e Zd Zdd„ ZdS )r   c                 C   s8   t j|t jd�}|jdk r(t j|dd�}t  |d¡}|S )N©Údtypeé   éÿÿÿÿ)Zaxisé   )ÚnpÚarrayZuint8ÚndimZexpand_dimsZrollaxis)ÚselfÚpil_imgZnp_img© r   úS/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/data/transforms.pyÚ__call__   s
    
zToNumpy.__call__N)Ú__name__Ú
__module__Ú__qualname__r!   r   r   r   r    r      s   c                   @   s&   e Zd ZdZejfdd„Zdd„ ZdS )r	   z% ToTensor with no rescaling of valuesc                 C   s
   || _ d S ©Nr   )r   r   r   r   r    Ú__init__$   s    zToTensor.__init__c                 C   s   t  |¡j| jd�S )Nr   )ÚFZpil_to_tensorÚtor   )r   r   r   r   r    r!   '   s    zToTensor.__call__N)r"   r#   r$   Ú__doc__ÚtorchZfloat32r&   r!   r   r   r   r    r	   "   s   Ú
ResamplingZnearestÚbilinearZbicubicÚboxZhammingZlanczosc                 C   s   i | ]\}}||“qS r   r   ©Ú.0ÚaÚbr   r   r    Ú
<dictcomp>A   s      r2   c                 C   s   i | ]\}}||“qS r   r   r.   r   r   r    r2   M   s      c                 C   s   t |  S r%   )Ú_str_to_pil_interpolation©Zmode_strr   r   r    r   S   s    c                 C   s   t rt|  S t|  S d S r%   )Úhas_interpolation_modeÚ_str_to_torch_interpolationr3   r4   r   r   r    r
   W   s    c                 C   s   t rt|  S t|  S d S r%   )r5   Ú_torch_interpolation_to_strÚ_pil_interpolation_to_str)Úmoder   r   r    r   ^   s    ú3Please provide only two dimensions (h, w) for size.c                 C   sZ   t | tjƒrt| ƒt| ƒfS t | tƒrBt| ƒdkrB| d | d fS t| ƒdkrVt|ƒ‚| S )Né   r   r   )Ú
isinstanceÚnumbersÚNumberÚintr   ÚlenÚ
ValueError)ÚsizeÚ	error_msgr   r   r    Ú_setup_sizeh   s    rD   c                   @   s6   e Zd ZdZddd„Zedd„ ƒZd	d
„ Zd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{®Gáz´?ç      ð?©g      è?gUUUUUUõ?r,   c                 C   sx   t |ttfƒrt|ƒ| _n
||f| _|d |d ksD|d |d krNt d¡ |dkr^t| _n
t|ƒ| _|| _	|| _
d S )Nr   r;   z"range should be of kind (min, max)Úrandom)r<   ÚlistÚtuplerB   ÚwarningsÚwarnÚ_RANDOM_INTERPOLATIONÚinterpolationr
   ÚscaleÚratio)r   rB   rO   rP   rN   r   r   r    r&   „   s    
 

z*RandomResizedCropAndInterpolation.__init__c                 C   sL  t  | ¡\}}|| }tdƒD ]ª}tj|Ž | }t |d ¡t |d ¡f}t tj|Ž ¡}	tt	t 
||	 ¡ƒƒ}
tt	t 
||	 ¡ƒƒ}|
|kr||krt d|| ¡}t d||
 ¡}||||
f  S q|| }|t|ƒk rø|}
tt	|
t|ƒ ƒƒ}n0|t|ƒk�r |}tt	|t|ƒ ƒƒ}
n|}
|}|| d }||
 d }||||
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   r;   r   )r'   Úget_image_sizeÚrangerH   ÚuniformÚmathÚlogÚexpr?   ÚroundÚsqrtÚrandintÚminÚmax)ÚimgrO   rP   Úimg_wÚimg_hZareaÚattemptZtarget_areaZ	log_ratioZaspect_ratioÚtarget_wÚtarget_hÚiÚjZin_ratior   r   r    Ú
get_params™   s0    z,RandomResizedCropAndInterpolation.get_paramsc              	   C   sV   |   || j| j¡\}}}}t| jttfƒr8t | j¡}n| j}t	 
|||||| j|¡S )z¢
        Args:
            img (PIL Image): Image to be cropped and resized.

        Returns:
            PIL Image: Randomly cropped and resized image.
        )re   rO   rP   r<   rN   rJ   rI   rH   Úchoicer'   Zresized_croprB   )r   r]   rc   rd   ÚhÚwrN   r   r   r    r!   Ä   s
    z*RandomResizedCropAndInterpolation.__call__c                 C   s”   t | jttfƒr(d dd„ | jD ƒ¡}n
t| jƒ}| jjd | j	¡ }|d tdd„ | j
D ƒƒ¡7 }|d td	d„ | jD ƒƒ¡7 }|d
 |¡7 }|S )Nú c                 S   s   g | ]}t |ƒ‘qS r   ©r   ©r/   Úxr   r   r    Ú
<listcomp>Õ   s     z>RandomResizedCropAndInterpolation.__repr__.<locals>.<listcomp>ú	(size={0}z, scale={0}c                 s   s   | ]}t |d ƒV  qdS ©é   N©rX   )r/   Úsr   r   r    Ú	<genexpr>Ù   s     z=RandomResizedCropAndInterpolation.__repr__.<locals>.<genexpr>z, ratio={0}c                 s   s   | ]}t |d ƒV  qdS ro   rq   )r/   Úrr   r   r    rs   Ú   s     z, interpolation={0}))r<   rN   rJ   rI   Újoinr   Ú	__class__r"   ÚformatrB   rO   rP   ©r   Zinterpolate_strÚformat_stringr   r   r    Ú__repr__Ó   s    
z*RandomResizedCropAndInterpolation.__repr__N)rE   rG   r,   ©	r"   r#   r$   r)   r&   Ústaticmethodre   r!   rz   r   r   r   r    r   u   s      û

*Úconstant)r]   Úoutput_sizeÚfillÚpadding_modeÚreturnc                 C   s  t |ƒ}|\}}t | ¡\}}}||ks0||krÊ||krD|| d nd||krZ|| d nd||krt|| d d nd||krŽ|| d d ndg}	tj| |	||d�} t | ¡\}}}||krÊ||krÊ| S tt|| d ƒƒ}
tt|| d ƒƒ}t | |
|||¡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   r;   ©r   r€   g       @)rD   r'   Úget_dimensionsÚpadr?   rX   Úcrop)r]   r~   r   r€   Zcrop_heightZ
crop_widthÚ_Úimage_heightÚimage_widthÚpadding_ltrbZcrop_topZ	crop_leftr   r   r    r   ß   s     üc                       s`   e Zd ZdZdeeee f eeeeeef f edœ‡ fdd„Z	dd„ Z
ed	œd
d„Z‡  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}   ©rB   r   r€   c                    s$   t ƒ  ¡  t|ƒ| _|| _|| _d S r%   ©Úsuperr&   rD   rB   r   r€   ©r   rB   r   r€   ©rv   r   r    r&     s    

zCenterCropOrPad.__init__c                 C   s   t || j| j| jd�S )ú•
        Args:
            img (PIL Image or Tensor): Image to be cropped.

        Returns:
            PIL Image or Tensor: Cropped image.
        r‚   )r   rB   r   r€   )r   r]   r   r   r    Úforward   s    zCenterCropOrPad.forward©r�   c                 C   s   | j j› d| j› d�S ©Nz(size=ú)©rv   r"   rB   ©r   r   r   r    rz   *  s    zCenterCropOrPad.__repr__)r   r}   )r"   r#   r$   r)   r   r?   r   r   Ústrr&   r�   rz   Ú__classcell__r   r   rŽ   r    r   	  s     üü
)r]   ÚtopÚleftÚheightÚwidthr   r€   r�   c                 C   sÈ   t  | ¡\}}}	|| }
|| }|dk s@|dk s@|
|	ks@||kr¢t| td|
ƒ dƒt| td|ƒ dƒt|
t|	|ƒ dƒt|t||ƒ dƒg}t j| |||d�} t|dƒ}t|dƒ}t  | ||||¡S )z[ Crops 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‰   r   r   r    r   .  s     ü

c                       sl   e Zd ZdZdeeee f eeeeeef f edœ‡ fdd„Z	e
dd„ ƒZd	d
„ Zedœdd„Z‡  ZS )r   zP Crop and/or pad image with random placement within the crop or pad margin.
    r   r}   rŠ   c                    s$   t ƒ  ¡  t|ƒ| _|| _|| _d S r%   r‹   r�   rŽ   r   r    r&   N  s    

zRandomCropOrPad.__init__c           	      C   sh   t  | ¡\}}}||d  }||d  }tt t dt|ƒ¡|¡ƒ}tt t dt|ƒ¡|¡ƒ}||fS )Nr   r;   )r'   rƒ   r?   rU   ÚcopysignrH   rZ   Úabs)	r]   rB   r†   r‡   rˆ   Údelta_heightÚdelta_widthr˜   r™   r   r   r    re   Y  s    zRandomCropOrPad.get_paramsc              	   C   s8   |   || j¡\}}t|||| jd | jd | j| jd�S )r�   r   r;   )r˜   r™   rš   r›   r   r€   )re   rB   r   r   r€   )r   r]   r˜   r™   r   r   r    r�   b  s    ùzRandomCropOrPad.forwardr‘   c                 C   s   | j j› d| j› d�S r’   r”   r•   r   r   r    rz   u  s    zRandomCropOrPad.__repr__)r   r}   )r"   r#   r$   r)   r   r?   r   r   r–   r&   r|   re   r�   rz   r—   r   r   rŽ   r    r   J  s     üü
c                   @   s*   e Zd Zd	dd„Zedd„ ƒZdd„ ZdS )
r   r   c                 C   s   || _ || _d S r%   )Ú
input_sizer   )r   r¢   r   r   r   r    r&   z  s    zRandomPad.__init__c           
      C   sf   t  | ¡\}}t|d | dƒ}t|d | dƒ}t d|¡}t d|¡}|| }|| }	||||	fS )Nr;   r   )r'   rR   r\   rH   rZ   )
r]   r¢   r›   rš   r¡   r    Úpad_leftÚpad_topÚ	pad_rightZ
pad_bottomr   r   r    re   ~  s    zRandomPad.get_paramsc                 C   s"   |   || j¡}t ||| j¡}|S r%   )re   r¢   r'   r„   r   )r   r]   Úpaddingr   r   r    r!   ‰  s    zRandomPad.__call__N)r   )r"   r#   r$   r&   r|   re   r!   r   r   r   r    r   y  s   


c                   @   s8   e Zd ZdZddd„Zedd
d„ƒZdd„ Zdd„ ZdS )r   z" Resize and Keep Aspect Ratio
    ç        r,   ©g333333ë?gÍÌÌÌÌÌð?F©gÍÌÌÌÌÌì?gÃõ(\�Âñ?c	           	      C   sj   t |ttfƒrt|ƒ| _n
||f| _|dkr4t| _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:
        rH   N)r<   rI   rJ   rB   rM   rN   r
   ÚfloatÚlongestÚrandom_scale_probÚrandom_scale_rangeÚrandom_scale_areaÚrandom_aspect_probÚrandom_aspect_range)	r   rB   r«   rN   r¬   r­   r®   r¯   r°   r   r   r    r&   “  s    


zResizeKeepRatio.__init__©rF   gHáz®Gõ?c                    s  t  | ¡dd…  \}}	}
|\}}|| }|	| }t||ƒ| t||ƒd|   ‰ |dkr–t ¡ |k r–t |d |d ¡}|rŒdt |¡ }||f}nd}|dkrüt ¡ |k rüt |d ¡t |d ¡f}t 	tj|Ž ¡}t |¡}|d | |d | f}‡ fdd„t
|
|ƒD ƒ}|S )zGet parameters
        r;   NrF   r   )rF   rF   c                    s    g | ]\}}t || ˆ  ƒ‘qS r   rq   )r/   rl   Úf©rP   r   r    rm   Þ  s     z.ResizeKeepRatio.get_params.<locals>.<listcomp>)r'   rƒ   r\   r[   rH   rT   rU   rY   rV   rW   Úzip)r]   Ztarget_sizer«   r¬   r­   r®   r¯   r°   r_   r^   Zimg_sizerb   ra   Zratio_hZratio_wZratio_factorZ
log_aspectZaspect_factorrB   r   r³   r    re   ¹  s$     

zResizeKeepRatio.get_paramsc              
   C   s\   |   || j| j| j| j| j| j| j¡}t| j	t
tfƒrDt | j	¡}n| j	}t |||¡}|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
        )re   rB   r«   r¬   r­   r®   r¯   r°   r<   rN   rJ   rI   rH   rf   r'   Úresize)r   r]   rB   rN   r   r   r    r!   á  s         ýzResizeKeepRatio.__call__c                 C   sÚ   t | jttfƒr(d dd„ | jD ƒ¡}n
t| jƒ}| jjd | j	¡ }|d|› �7 }|d| j
d›�7 }|d| jd›�7 }|d	| jd
 d›d| jd d›d�7 }|d| jd›�7 }|d| jd
 d›d| jd d›d�7 }|S )Nri   c                 S   s   g | ]}t |ƒ‘qS r   rj   rk   r   r   r    rm   ÷  s     z,ResizeKeepRatio.__repr__.<locals>.<listcomp>rn   z, interpolation=z
, longest=z.3fz, random_scale_prob=z, random_scale_range=(r   z, r;   r“   z, random_aspect_prob=z, random_aspect_range=(z)))r<   rN   rJ   rI   ru   r   rv   r"   rw   rB   r«   r¬   r­   r°   r¯   rx   r   r   r    rz   õ  s    
&&zResizeKeepRatio.__repr__N)r§   r,   r§   r¨   Fr§   r©   )r§   r±   Fr§   r©   r{   r   r   r   r    r   �  s$          ÷
&     ø'c                       s*   e Zd Zedœ‡ fdd„Zdd„ Z‡  ZS )r   )Úborder_sizec                    s   t ƒ  ¡  || _d S r%   )rŒ   r&   r¶   )r   r¶   rŽ   r   r    r&     s    
zTrimBorder.__init__c                 C   sf   t  |¡\}}| j }}t||ƒ}t||ƒ}td|d| j  ƒ}td|d| j  ƒ}t  |||||¡S )Nr   r   )r'   rR   r¶   r[   r\   r…   )r   r]   rh   rg   r˜   r™   rš   r›   r   r   r    r�     s    


zTrimBorder.forward)r"   r#   r$   r?   r&   r�   r—   r   r   rŽ   r    r     s   þ)r:   )r   r}   )r   r}   )8rU   r=   rH   rK   Útypingr   r   r   r   r*   Z!torchvision.transforms.functionalZ
transformsZ
functionalr'   r   r5   ÚImportErrorZPILr   Únumpyr   Ú__all__r   r	   Úhasattrr+   ZNEARESTZBILINEARZBICUBICZBOXZHAMMINGZLANCZOSr8   Úitemsr3   r7   r6   Z_pil_interpolation_to_torchr   r
   r   rM   rD   r   ZTensorr?   r–   r   ÚnnÚModuler   r   r   r   r   r   r   r   r   r    Ú<module>   sÔ   
          ý
      ú
      ú	      ú
m  üû*+  ùø/w