
    RЦi#                         S r  SSKJs  Jr  SSKrSrSr\R                  R                  R                  5              SS jr
S rS rS rS	 r\S
4S jr\S
4S jrSS\S
4S jr " S S5      rg)zTensorflow Preprocessing Adapter

Allows use of Tensorflow preprocessing pipeline in PyTorch Transform

Copyright of original Tensorflow code below.

Hacked together by / Copyright 2020 Ross Wightman
    N       c                    [         R                  " USX/5         [         R                  R                  U 5      n[         R                  R	                  UUUUUUSS9nUu  pn[         R
                  " U	5      u  pn[         R
                  " U
5      u  pn[         R                  " XX/5      n[         R                  R                  U USS9nUsSSS5        $ ! , (       d  f       g= f)a  Generates cropped_image using one of the bboxes randomly distorted.

See `tf.image.sample_distorted_bounding_box` for more documentation.

Args:
  image_bytes: `Tensor` of binary image data.
  bbox: `Tensor` of bounding boxes arranged `[1, num_boxes, coords]`
      where each coordinate is [0, 1) and the coordinates are arranged
      as `[ymin, xmin, ymax, xmax]`. If num_boxes is 0 then use the whole
      image.
  min_object_covered: An optional `float`. Defaults to `0.1`. The cropped
      area of the image must contain at least this fraction of any bounding
      box supplied.
  aspect_ratio_range: An optional list of `float`s. The cropped area of the
      image must have an aspect ratio = width / height within this range.
  area_range: An optional list of `float`s. The cropped area of the image
      must contain a fraction of the supplied image within in this range.
  max_attempts: An optional `int`. Number of attempts at generating a cropped
      region of the image of the specified constraints. After `max_attempts`
      failures, return the entire image.
  scope: Optional `str` for name scope.
Returns:
  cropped image `Tensor`
distorted_bounding_box_cropT)bounding_boxesmin_object_coveredaspect_ratio_range
area_rangemax_attemptsuse_image_if_no_bounding_boxes   channelsN)tf
name_scopeimageextract_jpeg_shapesample_distorted_bounding_boxunstackstackdecode_and_crop_jpeg)image_bytesbboxr   r	   r
   r   scopeshaper   
bbox_begin	bbox_size_offset_yoffset_xtarget_heighttarget_widthcrop_windowr   s                     Y/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/data/tf_preprocessing.pyr   r   !   s    > 
u;k=P	Q++K8(*(N(N11!%+/ )O )1% $A 
q !#

: 6A)+I)>&QhhMPQ--k;QR-S% 
R	Q	Qs   B2C
C$c                     [         R                  " X5      n[         R                  " U[         R                  5      n[         R                  " [         R
                  " U5      U5      $ )z0At least `x` of `a` and `b` `Tensors` are equal.)r   equalcastint32greater_equal
reduce_sum)abxmatchs       r$   _at_least_x_are_equalr/   U   s?    HHQNEGGE288$EBMM%0!44    c           
      B  ^ ^^^ [         R                  " / SQ[         R                  / SQS9n[        T USSSSSS	9m[         R                  R                  T 5      n[        U[         R                  " T5      S
5      n[         R                  " UU U4S jUUU4S j5      mT$ )z!Make a random crop of image_size.)        r2         ?r3   )   r4      )dtyper   皙?)      ?gUUUUUU?)g{Gz?r3   
   N)r   r	   r
   r   r   r   c                     > [        T T5      $ )N)_decode_and_center_crop)r   
image_sizes   r$   <lambda>)_decode_and_random_crop.<locals>.<lambda>l   s    'Z@r0   c                  R   > [         R                  R                  T /TT/T5      S   $ Nr   )r   r   resize)r   r<   resize_methods   r$   r=   r>   m   s#    *j)A=QRSTr0   )	r   constantfloat32r   r   r   r/   r   cond)r   r<   rB   r   original_shapebadr   s   ```   @r$   _decode_and_random_croprH   \   s    ;;+2::YOD',E XX00=N

CCGG@TVE
 Lr0   c           	         [         R                  R                  U 5      nUS   nUS   n[         R                  " X[        -   -  [         R                  " [         R
                  " XE5      [         R                  5      -  [         R                  5      nXF-
  S-   S-  nXV-
  S-   S-  n[         R                  " XxXf/5      n	[         R                  R                  X	SS9n
[         R                  R                  U
/X/U5      S   n
U
$ )z=Crops to center of image with padding then scales image_size.r   r4      r   r   )r   r   r   r'   CROP_PADDINGminimumrD   r(   r   r   rA   )r   r<   rB   r   image_heightimage_widthpadded_center_crop_sizeoffset_heightoffset_widthr#   r   s              r$   r;   r;   r   s    HH''4E8L(K gg
\1
2	L6

	C
D

 #<AaGM :a?AEL((M3N OKHH))+Q)OEHHOOUGj%=}MaPELr0   c                 D    [         R                  R                  U 5      n U $ )zRandom horizontal image flip.)r   r   random_flip_left_right)r   s    r$   _fliprT      s    HH++E2ELr0   bicubicc                    US:X  a$  [         R                  R                  R                  O#[         R                  R                  R                  n[        XU5      n[        U5      n[         R                  " XRUS/5      n[         R                  R                  XQ(       a  [         R                  O[         R                  S9nU$ a!  Preprocesses the given image for evaluation.

Args:
  image_bytes: `Tensor` representing an image binary of arbitrary size.
  use_bfloat16: `bool` for whether to use bfloat16.
  image_size: image size.
  interpolation: image interpolation method

Returns:
  A preprocessed image `Tensor`.
rU   r   )r6   )r   r   ResizeMethodBICUBICBILINEARrH   rT   reshapeconvert_image_dtypebfloat16rD   r   use_bfloat16r<   interpolationrB   r   s         r$   preprocess_for_trainra      s     6Ci5OBHH))11UWU]U]UjUjUsUsM#K]KE%LEJJu:q9:EHH((LR[[bjj ) BELr0   c                 p   US:X  a$  [         R                  R                  R                  O#[         R                  R                  R                  n[        XU5      n[         R                  " XRUS/5      n[         R                  R                  XQ(       a  [         R                  O[         R                  S9nU$ rW   )
r   r   rX   rY   rZ   r;   r[   r\   r]   rD   r^   s         r$   preprocess_for_evalrc      s     6Ci5OBHH))11UWU]U]UjUjUsUsM#K]KEJJu:q9:EHH((LR[[bjj ) BELr0   Fc                 @    U(       a  [        XX45      $ [        XX45      $ )at  Preprocesses the given image.

Args:
  image_bytes: `Tensor` representing an image binary of arbitrary size.
  is_training: `bool` for whether the preprocessing is for training.
  use_bfloat16: `bool` for whether to use bfloat16.
  image_size: image size.
  interpolation: image interpolation method

Returns:
  A preprocessed image `Tensor` with value range of [0, 255].
)ra   rc   )r   is_trainingr_   r<   r`   s        r$   preprocess_imagerf      s!    " #KzYY";jXXr0   c                   *    \ rS rSrSS jrS rS rSrg)TfPreprocessTransform   c                     Xl         [        U[        5      (       a  US   OUU l        X0l        S U l        U R                  5       U l        S U l        g r@   )	re   
isinstancetuplesizer`   _image_bytes_build_tf_graphprocess_imagesess)selfre   rm   r`   s       r$   __init__TfPreprocessTransform.__init__   sG    &)$66DGD	* !113	r0   c                 &   [         R                  " S5         [         R                  " / [         R                  S9U l        [        U R                  U R                  SU R                  U R                  5      nS S S 5        U$ ! , (       d  f       W$ = f)Nz/cpu:0)r   r6   F)	r   deviceplaceholderstringrn   rf   re   rm   r`   )rr   imgs     r$   ro   %TfPreprocessTransform._build_tf_graph   ss    YYx  "ii!D #!!4#3#3UDIItGYGY[C ! 
 !  
s   A B
Bc                    U R                   c  [        R                  " 5       U l         U R                   R                  U R                  U R
                  U0S9nUR                  5       R                  SS5      R                  [        R                  5      nUR                  S:  a  [        R                  " USS9n[        R                  " US5      nU$ )N)	feed_dictr      r   )axisrJ   )rq   r   Sessionrunrp   rn   roundclipastypenpuint8ndimexpand_dimsrollaxis)rr   r   ry   s      r$   __call__TfPreprocessTransform.__call__   s    99

DIiimmD..4;L;Lk:Zm[iikq#&--bhh788a<..2.Ckk#q!
r0   )rn   r`   re   rp   rq   rm   N)Fr   rU   )__name__
__module____qualname____firstlineno__rs   ro   r   __static_attributes__ r0   r$   rh   rh      s    r0   rh   )r7   )r8   gHzG?)g?r3   d   N)__doc__tensorflow.compat.v1compatv1r   numpyr   
IMAGE_SIZErK   disable_eager_executionr   r/   rH   r;   rT   ra   rc   rf   rh   r   r0   r$   <module>r      s   . * ! ! 
 		 $ $ & 473?+6-0&*1h5,* @JYb * ?IXa * "'"' *#,	Y. r0   