o
    پiS                  ?   @   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mZ ddlmZmZmZ dd	lm Z  d
de
eddfdee!ee!e!f f de"dee#df dee#df de$de$fddZ%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dfdee!ee!e!f f deee#e#f  d eee#e#f  d!ee" d"e#d#e#d$ee#ee#df f d%ee# d&e$d'e#d(e#d)ee" de"dee#df dee#df d*e#d+e"d,e!d-e!de$de$d.e$d/e$d0ee!ee!e!f f d1e!d2e$f4d3d4Z&d
dddde
eddddddfdee!ee!e!f f d5ee# d6ee" d7ee! de"dee#df dee#df de$de$d/e$d0ee!ee!e!f f d1e!d2e$fd8d9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dddddfd:ee!ee!e!f ee!e!e!f f d;e$d<e$d!ee" deee#e#f  d eee#e#f  d"e#d#e#d$ee#ee#df f d%ee# d'e#d(e#d)ee" de"dee#df dee#df d*e#d+e"d,e!d-e!d5ee# d6ee" d7ee! d=e$de$de$d.e$d/e$d0ee!ee!e!f f d1e!d2e$f>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MaybeToTensorMaybePILToTensor)RandomResizedCropToSequenceResizeToSequencePatchify)RandomErasing   bilinearFTimg_sizeinterpolationmean.stduse_prefetcher	normalizec                 C   sz   |dkrd}t j| t|dt | g}|r|t g7 }n|s&|t g7 }n|t t jt|t|dg7 }t 	|S )a   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.
        normalize: Normalization tensor output w/ provided mean/std (if prefetcher not used).

    Returns:

    randomr   r   r   r   )
r   Resizer   
CenterCropr   r   	NormalizetorchtensorCompose)r   r   r   r   r   r    tfl r+   P/home/ubuntu/.local/lib/python3.10/site-packages/timm/data/transforms_factory.pytransforms_noaug_train   s"   
r-         ?        g?r!   const      i@  scaleratiotrain_crop_modehflipvflipcolor_jittercolor_jitter_probforce_color_jittergrayscale_probgaussian_blur_probauto_augmentre_probre_modere_countre_num_splitsseparatenaflex
patch_sizemax_seq_lenpatchifyc               
   C   sL  |pd}|dv s
J g }|r't |pd}t |pd}|t|||||dg7 }nE|dv rUt |p/d}t |p5d}|t| |d	|d
d	|d|dkrLt| ddnt| ddg7 }nt |pYd}t |p_d}|t| |||dg7 }|dkry|tj|dg7 }|dkr|tj|dg7 }g }d}|rt	|t
sJ |pd|v  }t	| t tfrt| }n| }tt|d t dd |D d}|r|dkrt||d< |dr|t||g7 }n|drd|d< |t||g7 }n|t||g7 }|dur*|s*t	|tt frt|dv sJ nt|fd  }|dur"|tjtj| g|dg7 }n|tj| g7 }|	r6|tj|	dg7 }|
rH|tjtjd!d"g|
dg7 }g }|rT|t g7 }n-|s^|t g7 }n#|t tjt|t|d#g7 }|dkr|t||||d$d%g7 }|r|t |d&g7 }|rt!|t!|t!|fS t!|| | 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.
        normalize: Normalize tensor output w/ provided mean/std (if prefetcher not used).
        separate: Output transforms in 3-stage tuple.
        naflex: Enable NaFlex mode, sequence constrained patch output
        patch_size: Patch size for NaFlex mode.
        max_seq_len: Max sequence length for NaFlex mode.

    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>   rG   rkrcrkrr)g{Gz?      ?)g      ?gUUUUUU?)rD   rE   r3   r4   r   )rH   rI   )g?rJ   )g?grq?r.   T)r   random_scale_probrandom_scale_rangerandom_scale_arearandom_aspect_probrandom_aspect_rangerH   reflect)padding_mode)r3   r4   r   r/   )pF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>)translate_constimg_meanr!   r   randaugmixg333333?translate_pctN)      ra      )kernel_sizer#   cpu)mode	max_count
num_splitsdevicerD   )"tupler   r   r   r   r   r   RandomHorizontalFlipRandomVerticalFlip
isinstancestrlistrV   dictintr   
startswithr	   r
   r   lenfloatRandomApplyColorJitterRandomGrayscaleGaussianBlurr   r   r&   r'   r(   r   r   r)   ) r   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r   r   r   r>   r?   r@   rA   r   r    rB   rC   rD   rE   rF   primary_tflsecondary_tfldisable_color_jitterimg_size_min	aa_params	final_tflr+   r+   r,   transforms_imagenet_trainA   s   A

	







r   crop_pct	crop_modecrop_border_pixelsc                    s   pt  t| ttfrt| dksJ t fdd| D }nt|   }||f}g }|r5|t|g7 }|	rB|t|
||dg7 }nV|dkrW|t	j
|t|dt	| g7 }nA|dkrrdd |D }|t||d	d
t| |dg7 }n&|d |d kr|t	j
|d t|dg7 }n|t|g7 }|t	| g7 }|r|t g7 }n|s|t g7 }n|t t	jt|t|dg7 }|r|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.
        normalize: Normalize tensor output w/ provided mean/std (if prefetcher not used).
        naflex: Enable NaFlex mode, sequence constrained patch output
        patch_size: Patch size for NaFlex mode.
        max_seq_len: Max sequence length for NaFlex mode.
        patchify: Patchify the output instead of relying on prefetcher

    Returns:
        Composed transform pipeline
       c                    s   g | ]	}t |  qS r+   )mathfloorrX   r   r+   r,   r[   7  s    z,transforms_imagenet_eval.<locals>.<listcomp>)rD   rE   r   squashr"   borderc                 S   s   g | ]}t d | qS rT   )rW   )rY   vr+   r+   r,   r[   R  s    rJ   )r   longest)fillr   r1   r#   rj   )r   rn   rk   rp   rt   r   r   r   r   r   r$   r   r%   r   r   r   r   r&   r'   r(   r   r)   )r   r   r   r   r   r   r   r   r    rC   rD   rE   rF   
scale_sizer*   r   r+   r   r,   transforms_imagenet_eval  s\   #

r   
input_sizeis_trainingno_augtf_preprocessingc           "      C   sT  t | ttfr| dd }n| }|r)|r)|rJ dddlm}  | |||d}!|!S |r?|r?|r3J dt||||||d}!|!S |rt|fi d	|d
|d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d |}!|!S |rJ 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.
        normalize: Normalization tensor output w/ provided mean/std (if prefetcher not used).
        separate: Output transforms in 3-stage tuple.

    Returns:
        Composed transforms or tuple thereof
    Nz6Separate transforms not supported for TF preprocessingr   )TfPreprocessTransform)r   sizer   z-Cannot perform split augmentation with no_aug)r   r   r   r   r    r5   r3   r4   r6   r7   r8   r9   r;   r<   r=   r   r   r   r>   r?   r@   rA   r   r    rB   rC   rD   rE   rF   z>Separate transforms not supported for validation preprocessing)r   r   r   r   r   r   r   r    rC   rD   rE   rF   )rn   rk   rp   timm.data.tf_preprocessingr   r-   r   r   )"r   r   r   r5   r3   r4   r6   r7   r8   r9   r;   r<   r=   r   r   r   r>   r?   r@   rA   r   r   r   r   r   r    rB   rC   rD   rE   rF   r   r   	transformr+   r+   r,   create_transformy  s   D>6	
-r   ))__doc__r   typingr   r   r   r'   torchvisionr   timm.data.constantsr   r   r   timm.data.auto_augmentr	   r
   r   timm.data.transformsr   r   r   r   r   r   r   r   r   timm.data.naflex_transformsr   r   r   timm.data.random_erasingr   rr   ro   ru   boolr-   r   r   r   r+   r+   r+   r,   <module>   s   ,


.	



 Q

	

j	


