a
    d.                     @   s  d dl Z d dlZd dlZd dlZd dl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yt   dZY n0 d dlmZ d dlZG dd dZG d	d
 d
Zeedre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rbe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&dd Z'G dd dZ(d&ej)ee* ej)dd d!Z+G d"d# d#ej,j-Z.G d$d% d%Z/dS )'    N)ListSequence)InterpolationModeTF)Imagec                   @   s   e Zd Zdd ZdS )ToNumpyc                 C   s8   t j|t jd}|jdk r(t j|dd}t |d}|S Ndtype   )Zaxis   )nparrayuint8ndimexpand_dimsrollaxisselfZpil_imgZnp_img r   ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/timm/data/transforms.py__call__   s
    
zToNumpy.__call__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   c                   @   s"   e Zd ZejfddZdd ZdS )ToTensorc                 C   s
   || _ d S Nr   )r   r	   r   r   r   __init__   s    zToTensor.__init__c                 C   sH   t j|t jd}|jdk r(t j|dd}t |d}t|j| j	dS r   )
r   r   r   r   r   r   torchZ
from_numpytor	   r   r   r   r   r   !   s
    
zToTensor.__call__N)r   r   r   r   float32r   r   r   r   r   r   r      s   r   
ResamplingZnearestbilinearZbicubicZboxZhammingZlanczosc                 C   s   i | ]\}}||qS r   r   .0abr   r   r   
<dictcomp>?       r'   c                 C   s   i | ]\}}||qS r   r   r#   r   r   r   r'   K   r(   c                 C   s   t |  S r   )_str_to_pil_interpolationZmode_strr   r   r   str_to_pil_interpQ   s    r+   c                 C   s   t rt|  S t|  S d S r   )has_interpolation_mode_str_to_torch_interpolationr)   r*   r   r   r   str_to_interp_modeU   s    r.   c                 C   s   t rt|  S t|  S d S r   )r,   _torch_interpolation_to_str_pil_interpolation_to_str)moder   r   r   interp_mode_to_str\   s    r2   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_sizef   s    r<   c                   @   s6   e Zd ZdZdddZedd Zd	d
 Zdd ZdS )!RandomResizedCropAndInterpolationaY  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?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   r3   z"range should be of kind (min, max)random)r4   listtupler:   warningswarn_RANDOM_INTERPOLATIONinterpolationr.   scaleratio)r   r:   rH   rI   rG   r   r   r   r      s    
 

z*RandomResizedCropAndInterpolation.__init__c                 C   s  | j d | j d  }tdD ]}tj| | }t|d t|d f}ttj| }ttt	|| }ttt	|| }	|| j d kr|	| j d krt
d| j d |	 }
t
d| j d | }|
||	|f  S q| j d | j d  }|t|k r"| j d }tt|t| }	nB|t|krP| j d }	tt|	t| }n| j d }| j d }	| j d |	 d }
| j 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   r3   
   r   )r:   rangerA   uniformmathlogexpr7   roundsqrtrandintminmax)imgrH   rI   areaattemptZtarget_areaZ	log_ratioZaspect_ratiowhijZin_ratior   r   r   
get_params   s.    



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.
        )r\   rH   rI   r4   rG   rC   rB   rA   choiceFZresized_cropr:   )r   rU   rZ   r[   rY   rX   rG   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   )r2   r$   xr   r   r   
<listcomp>   r(   z>RandomResizedCropAndInterpolation.__repr__.<locals>.<listcomp>	(size={0}z, scale={0}c                 s   s   | ]}t |d V  qdS    NrP   )r$   sr   r   r   	<genexpr>   r(   z=RandomResizedCropAndInterpolation.__repr__.<locals>.<genexpr>z, ratio={0}c                 s   s   | ]}t |d V  qdS rd   rf   )r$   rr   r   r   rh      r(   z, interpolation={0}))r4   rG   rC   rB   joinr2   	__class__r   formatr:   rH   rI   r   Zinterpolate_strformat_stringr   r   r   __repr__   s    
z*RandomResizedCropAndInterpolation.__repr__N)r>   r@   r"   	r   r   r   __doc__r   staticmethodr\   r   ro   r   r   r   r   r=   s   s     

*r=   )rU   output_sizereturnc                 C   sH  t |tjrt|t|f}n*t |ttfrHt|dkrH|d |d f}t| \}}}|\}}||ksr||kr||kr|| d nd||kr|| d nd||kr|| 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 )aj  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.
    r3   r   r   fillg       @)r4   r5   r6   r7   rC   rB   r8   r^   Zget_dimensionspadrP   Zcrop)rU   rs   rv   _Zimage_heightZimage_widthZcrop_heightZ
crop_widthZpadding_ltrbZcrop_topZ	crop_leftr   r   r   center_crop_or_pad   s&    ry   c                       s8   e Zd ZdZd
 fdd	Zdd Zeddd	Z  ZS )CenterCropOrPada/  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   c                    s"   t    t|dd| _|| _d S )Nz3Please provide only two dimensions (h, w) for size.)r;   )superr   r<   r:   rv   )r   r:   rv   rk   r   r   r     s    
zCenterCropOrPad.__init__c                 C   s   t || j| jdS )z
        Args:
            img (PIL Image or Tensor): Image to be cropped.

        Returns:
            PIL Image or Tensor: Cropped image.
        ru   )ry   r:   rv   )r   rU   r   r   r   forward  s    zCenterCropOrPad.forward)rt   c                 C   s   | j j d| j dS )Nz(size=))rk   r   r:   )r   r   r   r   ro     s    zCenterCropOrPad.__repr__)r   )	r   r   r   rq   r   r}   strro   __classcell__r   r   r|   r   rz      s   
rz   c                   @   s6   e Zd ZdZdddZedd Zd	d
 Zdd ZdS )ResizeKeepRatioz Resize and Keep Ratio
            r"   r   c                 C   sB   t |ttfrt|| _n
||f| _t|| _t|| _|| _d S r   )	r4   rB   rC   r:   r.   rG   floatlongestrv   )r   r:   r   rG   rv   r   r   r   r   #  s    


zResizeKeepRatio.__init__c                    sf   | j ddd }|\}}|\}}|| }|| }	t||	| t||	d|     fdd|D }
|
S )a	  Get parameters

        Args:
            img (PIL Image): Image to be cropped.
            target_size (Tuple[int, int]): Size of output
        Returns:
            tuple: params (h, w) and (l, r, t, b) to be passed to ``resize`` and ``pad`` respectively
        Nr   r?   c                    s   g | ]}t |  qS r   rf   r`   rI   r   r   rb   B  r(   z.ResizeKeepRatio.get_params.<locals>.<listcomp>)r:   rT   rS   )rU   Ztarget_sizer   source_sizerY   rX   Ztarget_hZtarget_wZratio_hZratio_wr:   r   r   r   r\   2  s    
 zResizeKeepRatio.get_paramsc                 C   s&   |  || j| 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\   r:   r   r^   resizerG   )r   rU   r:   r   r   r   r   E  s    zResizeKeepRatio.__call__c                 C   sF   t | j}| jjd| j }|d| d7 }|d| jdd7 }|S )Nrc   z, interpolation=r~   z
, longest=z.3f)r2   rG   rk   r   rl   r:   r   rm   r   r   r   ro   Q  s
    
zResizeKeepRatio.__repr__N)r   r"   r   rp   r   r   r   r   r     s      

r   )r   )0rM   r5   rA   rD   typingr   r   r   Z!torchvision.transforms.functionalZ
transformsZ
functionalr^   r   r,   ImportErrorZPILr   numpyr   r   r   hasattrr!   ZNEARESTZBILINEARZBICUBICZBOXZHAMMINGZLANCZOSr0   itemsr)   r/   r-   Z_pil_interpolation_to_torchr+   r.   r2   rF   r<   r=   ZTensorr7   ry   nnModulerz   r   r   r   r   r   <module>   sf   



	e(