
    khS              ?       6   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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$d/ee!ee!e!f   f   d0e!d1e$f4d2Z&d
dddde
eddddddfdee!ee!e!f   f   d3ee#   d4ee"   d5ee!   de"dee#df   dee#df   de$de$d.e$d/ee!ee!e!f   f   d0e!d1e$fd6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d7ee!ee!e!f   ee!e!e!f   f   d8e$d9e$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!d3ee#   d4ee"   d5ee!   d:e$de$de$d-e$d.e$d/ee!ee!e!f   f   d0e!d1e$f>d;Z(y)<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                 |   |dk(  rd}t        j                  | t        |            t        j                  |       g}|r|t	               gz  }n[|s|t	               gz  }nJ|t               t        j                  t        j                  |      t        j                  |            gz  }t        j                  |      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!   tfls          X/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/data/transforms_factory.pytransforms_noaug_trainr.      s    *  "(2D]2STh'C  "## "##O  \\$'LL%
 	
 c""          ?        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                    |xs d}|dv sJ g }|r3t        |xs d      }t        |xs d      }|t        |||||      gz  }n|dv rSt        |xs d      }t        |xs d      }|t        | |d	|d
d	|      |dk(  rt        | d      nt	        | d      gz  }n1t        |xs d      }t        |xs d      }|t        | |||      gz  }|dkD  r|t        j                  |      gz  }|dkD  r|t        j                  |      gz  }g }d}|rt        |t              sJ |xs d|v  }t        | t         t        f      rt        |       }n| }t        t        |dz        t        |D cg c]  }t        dt        d|z               c}            }|r|dk7  rt!        |      |d<   |j#                  d      r|t%        ||      gz  }n7|j#                  d      rd|d<   |t'        ||      gz  }n|t)        ||      gz  }|{|syt        |t        t         f      rt+        |      dv sJ t-        |      fdz  }|-|t        j.                  t        j0                  | g|      gz  }n|t        j0                  | gz  }|	r|t        j2                  |	      gz  }|
r0|t        j.                  t        j4                  d       g|
      gz  }g } |r| t7               gz  } nt|s| t7               gz  } nc| t9               t        j:                  t=        j>                  |      t=        j>                  |      !      gz  } |dkD  r| tA        ||||d"#      gz  } |r| tC        |$      gz  } |r>t        jD                  |      t        jD                  |      t        jD                  |       fS t        jD                  ||z   | z         S c c}w )%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>   rJ   rkrcrkrr)g{Gz?      ?)g      ?gUUUUUU?)rF   rG   r5   r6   r   )rK   rL   )g?rM   )g?grq?r0   T)r   random_scale_probrandom_scale_rangerandom_scale_arearandom_aspect_probrandom_aspect_rangerK   reflect)padding_mode)r5   r6   r   r1   )pF3ag?   )translate_constimg_meanr#   r   randaugmixg333333?translate_pct)      r]      )kernel_sizer%   cpu)mode	max_count
num_splitsdevicerF   )#tupler   r   r   r   r   r   RandomHorizontalFlipRandomVerticalFlip
isinstancestrlistmindictintroundr   
startswithr
   r   r   lenfloatRandomApplyColorJitterRandomGrayscaleGaussianBlurr   r   r(   r)   r*   r   r   r+   )!r   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r   r   r   r@   rA   rB   rC   r    r!   rD   rE   rF   rG   rH   primary_tflsecondary_tfldisable_color_jitterimg_size_minx	aa_params	final_tfls!                                    r-   transforms_imagenet_trainr   A   sX   B &.O5555Ke*{+e1123!#'
  	 ..%.;/E%.;/E"/&)',&*'*(- #f,  yAyA K %.;/E%5#56E1"/	 K rz
77%@AArz
55>??M ,,,, %7$N$,:NOh.x=L#Lt 34dCCU37^4CD
	 ]h6):=)IIo&""6*4\9MNNM$$X.),Io&7iPQQM4\9MNNM(<lT5M2 |$... ",/1A5L(&&"..=( ( M j44lCDDM*44~FGG""++;$ %
 	
 I&())	&())	O  \\$'LL%
 	
	 R< &,  I h*566	!!+.
0B0B=0QS]SeSefoSppp!!+"=	"IJJY Ds   *Ocrop_pct	crop_modecrop_border_pixelsc           	      4   |xs t         }t        | t        t        f      r@t	        |       dk(  sJ t        | D cg c]  }t        j                  ||z         c}      }nt        j                  | |z        }||f}g }|r|t        |      gz  }|	r|t        |
||      gz  }n|dk(  r9|t        j                  |t        |            t        j                  |       gz  }n|dk(  r:|D cg c]  }t        d|z         }}|t        ||d      t        | |	      gz  }n[|d
   |d   k(  r(|t        j                  |d
   t        |            gz  }n|t        |      gz  }|t        j                  |       gz  }|r|t!               gz  }n[|s|t!               gz  }nJ|t#               t        j$                  t'        j(                  |      t'        j(                  |            gz  }|r|t+        |
      gz  }t        j,                  |      S c c}w c c}w )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
       )rF   rG   r   squashr$   borderrW   rM   )r   longest)fillr   r3   r%   rf   )r	   rj   rg   rl   rr   mathfloorr   r   r   r&   r   r'   rp   r   r   r   r   r(   r)   r*   r   r+   )r   r   r   r   r   r   r   r    r!   rE   rF   rG   rH   r|   
scale_sizer,   vr   s                     r-   transforms_imagenet_evalr     s7   F ++H(UDM*8}!!!hGDJJq8|4GH
ZZ8 34
 *-

C
-.// !#'
  	   !!*<N}<]^%%h/ C (" -11qE#'N1D1
-QTUt4 C !}
1-%%jmCUVcCde 
 
344J))(344C "## "##O  \\$'LL%
 	
 J/00c"" H6 2s   H,H
input_sizeis_trainingno_augtf_preprocessingc                    t        | t        t        f      r| dd }n| }|r|r|rJ d       ddlm}   | |||      }!|!S |r|r|rJ d       t        ||||||      }!|!S |rTt        |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 |rJ d!       t        |||||||||||||"      }!|!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!   r7   r5   r6   r8   r9   r:   r;   r=   r>   r?   r   r   r   r@   rA   rB   rC   r    r!   rD   rE   rF   rG   rH   z>Separate transforms not supported for validation preprocessing)r   r   r   r   r   r   r    r!   rE   rF   rG   rH   )rj   rg   rl   timm.data.tf_preprocessingr   r.   r   r   )"r   r   r   r7   r5   r6   r8   r9   r:   r;   r=   r>   r?   r   r   r   r@   rA   rB   rC   r   r   r   r   r    r!   rD   rE   rF   rG   rH   r   r   	transforms"                                     r-   create_transformr   y  s   H *udm,bc?NUUU|D)#'
	| q 6P!PP<.+-#Il ] 1 /  	
   * #4  . $6 * ,       !" "#$ ,%&  .'( $)* "+, -. &/0 (12 "3IZ #  a!aa<0+!##5-#%'!I  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   ro   rk   rs   boolr.   r   r   r    r/   r-   <module>r      s  
  ) )  " ] ] l lc c c _ _ 2 14'"7!5$*#U38_,-*#*# E3J*# 5#:	*#
 *# *#\ 14/3/3)-8;-1#( "$&&*%"7!5$245LKU38_,-LKeUl+,LK eUl+,LK "#	LK
 LK LK E5#445LK $E?LK !LK LK "LK smLK LK E3JLK 5#:LK  !LK" #LK$ %LK& 'LK( )LK* +LK, -LK. /LK0 #uS#X./1LK2 3LK4 5LK` 14$(#',0'"7!5$24f#U38_,-f#5/f# C=f# %SM	f#
 f# E3Jf# 5#:f# f# f# f# #uS#X./f# f# f#T IL!)-/3/38;-1 "$&&*'"7!5$(#',0!&$24?J#uS#Xc3m0DDEJJ J "#	J
 eUl+,J eUl+,J J J E5#445J $E?J J "J smJ J E3JJ  5#:!J" #J$ %J& 'J( )J* 5/+J, C=-J. %SM/J0 1J2 3J4 5J6 7J8 9J: #uS#X./;J< =J> ?Jr/   