U
    ¯Ê¹j)F  ã                   @   sÈ  d Z ddlZddlmZmZmZ ddlZddlmZ ddl	m
Z
mZmZ ddlmZmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZ d	d
de
efeeeeef f eeeedf eedf dœdd„Z d	dddddddddddde
eddddddfeeeeef f eeeef  eeeef  ee eeeeeedf f ee eeeee eeedf eedf eeeeeedœdd„Z!d	dddd
e
edfeeeeef f ee ee ee eeedf eedf edœdd„Z"d	ddddddddddddd
e
eddddddddddfeeeeef eeeef f eeee eeeef  eeeef  eeeeeedf f ee eeee eeedf eedf eeeeee ee ee eeedœdd„Z#dS )zŸ Transforms Factory
Factory methods for building image transforms for use with TIMM (PyTorch Image Models)

Hacked together by / Copyright 2019, Ross Wightman
é    N)ÚOptionalÚTupleÚUnion)Ú
transforms)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚDEFAULT_CROP_PCT)Úrand_augment_transformÚaugment_and_mix_transformÚauto_augment_transform)Ústr_to_interp_modeÚstr_to_pil_interpÚ!RandomResizedCropAndInterpolationÚResizeKeepRatioÚCenterCropOrPadÚRandomCropOrPadÚ
TrimBorderÚToNumpy)ÚRandomErasingéà   ÚbilinearF.)Úimg_sizeÚinterpolationÚuse_prefetcherÚmeanÚstdc                 C   sj   |dkrd}t j| t|ƒd�t  | ¡g}|r:|tƒ g7 }n&|t  ¡ t jt |¡t |¡d�g7 }t  	|¡S )aZ   No-augmentation image transforms for training.

    Args:
        img_size: Target image size.
        interpolation: Image interpolation mode.
        mean: Image normalization mean.
        std: Image normalization standard deviation.
        use_prefetcher: Prefetcher enabled. Do not convert image to tensor or normalize.

    Returns:

    Úrandomr   ©r   ©r   r   )
r   ÚResizer   Ú
CenterCropr   ÚToTensorÚ	NormalizeÚtorchÚtensorÚCompose)r   r   r   r   r   Útfl© r'   ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/data/transforms_factory.pyÚtransforms_noaug_train   s    þþþr)   ç      à?ç        gš™™™™™Ù?r   Úconsté   )r   ÚscaleÚratioÚtrain_crop_modeÚhflipÚvflipÚcolor_jitterÚcolor_jitter_probÚforce_color_jitterÚgrayscale_probÚgaussian_blur_probÚauto_augmentr   r   r   Úre_probÚre_modeÚre_countÚre_num_splitsr   Úseparatec              	   C   sò  |pd}|dkst ‚|dkrlt|p$dƒ}t|p0dƒ}t| |d|dd|d�|d	kr\t| d
d�n
t| d
d�g}n*t|ptdƒ}t|p€dƒ}t| |||d�g}|dkr°|tj|d�g7 }|dkrÊ|tj|d�g7 }g }d}|�r¤t	|t
ƒsæt ‚|pðd|k }t	| ttfƒ�rt| ƒ}n| }tt|d ƒtdd„ |D ƒƒd�}|�rP|dk�rPt|ƒ|d< | d¡�rn|t||ƒg7 }n6| d¡�r”d|d< |t||ƒg7 }n|t||ƒg7 }|dk	�r|�st	|ttfƒ�rØt|ƒdk�sæt ‚nt|ƒfd }|dk	�r|tjtj|Ž g|d�g7 }n|tj|Ž g7 }|	�r6|tj|	d�g7 }|
�rZ|tjtjd d!�g|
d�g7 }g }|�rr|tƒ g7 }nH|t ¡ tjt |¡t |¡d"�g7 }|dk�rº|t||||d#d$�g7 }|�rÜt  |¡t  |¡t  |¡fS t  || | ¡S dS )%a—   ImageNet-oriented image transforms for training.

    Args:
        img_size: Target image size.
        train_crop_mode: Training random crop mode ('rrc', 'rkrc', 'rkrr').
        scale: Random resize scale range (crop area, < 1.0 => zoom in).
        ratio: Random aspect ratio range (crop ratio for RRC, ratio adjustment factor for RKR).
        hflip: Horizontal flip probability.
        vflip: Vertical flip probability.
        color_jitter: Random color jitter component factors (brightness, contrast, saturation, hue).
            Scalar is applied as (scalar,) * 3 (no hue).
        color_jitter_prob: Apply color jitter with this probability if not None (for SimlCLR-like aug).
        force_color_jitter: Force color jitter where it is normally disabled (ie with RandAugment on).
        grayscale_prob: Probability of converting image to grayscale (for SimCLR-like aug).
        gaussian_blur_prob: Probability of applying gaussian blur (for SimCLR-like aug).
        auto_augment: Auto augment configuration string (see auto_augment.py).
        interpolation: Image interpolation mode.
        mean: Image normalization mean.
        std: Image normalization standard deviation.
        re_prob: Random erasing probability.
        re_mode: Random erasing fill mode.
        re_count: Number of random erasing regions.
        re_num_splits: Control split of random erasing across batch size.
        use_prefetcher: Prefetcher enabled. Do not convert image to tensor or normalize.
        separate: Output transforms in 3-stage tuple.

    Returns:
        If separate==True, the transforms are returned as a tuple of 3 separate transforms
        for use in a mixing dataset that passes
         * all data through the first (primary) transform, called the 'clean' data
         * a portion of the data through the secondary transform
         * normalizes and converts the branches above with the third, final transform
    Úrrc>   Úrkrrr>   Úrkrc)r@   r?   )gš™™™™™é?ç      ð?)gÍÌÌÌÌÌì?grÇqÇñ?r*   T)r   Zrandom_scale_probZrandom_scale_rangeZrandom_scale_areaZrandom_aspect_probZrandom_aspect_ranger@   Zreflect)Zpadding_mode)g{®Gáz´?rA   )g      è?gUUUUUUõ?)r.   r/   r   r+   )ÚpFZ3agÍÌÌÌÌÌÜ?c                 S   s   g | ]}t d td | ƒƒ‘qS ©éÿ   )ÚminÚround©Ú.0Úxr'   r'   r(   Ú
<listcomp>¤   s     z-transforms_imagenet_train.<locals>.<listcomp>)Ztranslate_constZimg_meanr   r   ZrandZaugmixg333333Ó?Ztranslate_pctN)é   é   rK   é   )Zkernel_sizer   Úcpu)ÚmodeÚ	max_countZ
num_splitsZdevice)!ÚAssertionErrorÚtupler   r   r   r   r   ZRandomHorizontalFlipZRandomVerticalFlipÚ
isinstanceÚstrÚlistrE   ÚdictÚintr   Ú
startswithr	   r
   r   ÚlenÚfloatZRandomApplyZColorJitterZRandomGrayscaleZGaussianBlurr   r!   r"   r#   r$   r   r%   )r   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r   r   r   r9   r:   r;   r<   r   r=   Zprimary_tflZsecondary_tflZdisable_color_jitterZimg_size_minZ	aa_paramsZ	final_tflr'   r'   r(   Útransforms_imagenet_train;   sÂ    8ù
ÿ
ôüÿ

þ
ÿýÿ
ÿýÿþþ
ûÿ
r[   )r   Úcrop_pctÚ	crop_modeÚcrop_border_pixelsr   r   r   r   c                    sZ  ˆ pt ‰ t| ttfƒr>t| ƒdks&t‚t‡ fdd„| D ƒƒ}nt | ˆ  ¡}||f}g }	|rj|	t|ƒg7 }	|dkr”|	t	j
|t|ƒd�t	 | ¡g7 }	n‚|dkrÊdd„ |D ƒ}
|	t||dd	�t| |
d
�g7 }	nL|d |d krø|	t	j
|d t|ƒd�g7 }	n|	t|ƒg7 }	|	t	 | ¡g7 }	|�r*|	tƒ g7 }	n&|	t	 ¡ t	jt |¡t |¡d�g7 }	t	 |	¡S )a’   ImageNet-oriented image transform for evaluation and inference.

    Args:
        img_size: Target image size.
        crop_pct: Crop percentage. Defaults to 0.875 when None.
        crop_mode: Crop mode. One of ['squash', 'border', 'center']. Defaults to 'center' when None.
        crop_border_pixels: Trim a border of specified # pixels around edge of original image.
        interpolation: Image interpolation mode.
        mean: Image normalization mean.
        std: Image normalization standard deviation.
        use_prefetcher: Prefetcher enabled. Do not convert image to tensor or normalize.

    Returns:
        Composed transform pipeline
    é   c                    s   g | ]}t  |ˆ  ¡‘qS r'   )ÚmathÚfloorrG   ©r\   r'   r(   rJ   
  s     z,transforms_imagenet_eval.<locals>.<listcomp>Zsquashr   Úborderc                 S   s   g | ]}t d | ƒ‘qS rC   )rF   )rH   Úvr'   r'   r(   rJ     s     rA   )r   Zlongest)Úfillr   r-   r   )r   rS   rR   rU   rY   rQ   r`   ra   r   r   r   r   r    r   r   r   r!   r"   r#   r$   r%   )r   r\   r]   r^   r   r   r   r   Z
scale_sizer&   re   r'   rb   r(   Útransforms_imagenet_evalí   sH    þ
þÿþþrf   )Ú
input_sizeÚis_trainingÚno_augr0   r.   r/   r1   r2   r3   r4   r6   r7   r8   r   r   r   r9   r:   r;   r<   r\   r]   r^   Útf_preprocessingr   r=   c                 C   sÖ   t | ttfƒr| dd… }n| }|rP|rP|r4tdƒ‚ddlm} ||||d�}n‚|rx|rx|rdtdƒ‚t|||||d�}nZ|r®t||||||||	|
|||||||||||d	�}n$|rºtd
ƒ‚t||||||||d�}|S )aÝ  

    Args:
        input_size: Target input size (channels, height, width) tuple or size scalar.
        is_training: Return training (random) transforms.
        no_aug: Disable augmentation for training (useful for debug).
        train_crop_mode: Training random crop mode ('rrc', 'rkrc', 'rkrr').
        scale: Random resize scale range (crop area, < 1.0 => zoom in).
        ratio: Random aspect ratio range (crop ratio for RRC, ratio adjustment factor for RKR).
        hflip: Horizontal flip probability.
        vflip: Vertical flip probability.
        color_jitter: Random color jitter component factors (brightness, contrast, saturation, hue).
            Scalar is applied as (scalar,) * 3 (no hue).
        color_jitter_prob: Apply color jitter with this probability if not None (for SimlCLR-like aug).
        grayscale_prob: Probability of converting image to grayscale (for SimCLR-like aug).
        gaussian_blur_prob: Probability of applying gaussian blur (for SimCLR-like aug).
        auto_augment: Auto augment configuration string (see auto_augment.py).
        interpolation: Image interpolation mode.
        mean: Image normalization mean.
        std: Image normalization standard deviation.
        re_prob: Random erasing probability.
        re_mode: Random erasing fill mode.
        re_count: Number of random erasing regions.
        re_num_splits: Control split of random erasing across batch size.
        crop_pct: Inference crop percentage (output size / resize size).
        crop_mode: Inference crop mode. One of ['squash', 'border', 'center']. Defaults to 'center' when None.
        crop_border_pixels: Inference crop border of specified # pixels around edge of original image.
        tf_preprocessing: Use TF 1.0 inference preprocessing for testing model ports
        use_prefetcher: Pre-fetcher enabled. Do not convert image to tensor or normalize.
        separate: Output transforms in 3-stage tuple.

    Returns:
        Composed transforms or tuple thereof
    éþÿÿÿNz6Separate transforms not supported for TF preprocessingr   )ÚTfPreprocessTransform)rh   Úsizer   z-Cannot perform split augmentation with no_aug)r   r   r   r   )r0   r.   r/   r1   r2   r3   r4   r6   r7   r8   r   r   r   r   r9   r:   r;   r<   r=   z>Separate transforms not supported for validation preprocessing)r   r   r   r   r\   r]   r^   )	rS   rR   rU   rQ   Ztimm.data.tf_preprocessingrl   r)   r[   rf   )rg   rh   ri   r0   r.   r/   r1   r2   r3   r4   r6   r7   r8   r   r   r   r9   r:   r;   r<   r\   r]   r^   rj   r   r=   r   rl   Z	transformr'   r'   r(   Úcreate_transform?  sn    >ýûìørn   )$Ú__doc__r`   Útypingr   r   r   r#   Ztorchvisionr   Ztimm.data.constantsr   r   r   Ztimm.data.auto_augmentr	   r
   r   Ztimm.data.transformsr   r   r   r   r   r   r   r   Ztimm.data.random_erasingr   rW   rT   ÚboolrZ   r)   r[   rf   rn   r'   r'   r'   r(   Ú<module>   s  (û

û)ë

ë 4ø

øSæ

æ