
    Ng#                         d Z 	 ddlmc mZ ddlZdZdZej        j        	                                 	 	 	 	 	 dd	Z
d
 Zd Zd Zd ZedfdZedfdZddedfdZ G d d          ZdS )z Tensorflow 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       皙?      ?gHzG?g?      ?d   c           
         t          j        |d| |g          5  t           j                            |           }t           j                            ||||||d          }|\  }	}
}t          j        |	          \  }}}t          j        |
          \  }}}t          j        ||||g          }t           j                            | |d          }|cddd           S # 1 swxY w Y   dS )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                     V/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/data/tf_preprocessing.pyr   r   !   s?   > 
u;k4=P	Q	Q  ++K88(*(N(N11!%+/ )O )1 )1% $A 
Iq !#
: 6 6(A)+I)>)>&|Qh(M<PQQ--k;QR-SS%                 s   B7CC!$C!c                     t          j        | |          }t          j        |t           j                  }t          j        t          j        |          |          S )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_equalr5   U   sA    HQNNEGE28$$EBM%00!444    c           	      F    t          j        g dt           j        g d          }t           |ddddd	          t           j                                       }t          |t          j                  d
          }t          j        | fdfd          S )z!Make a random crop of image_size.)        r8   r	   r	   )   r9      )dtyper!   r   )r   gUUUUUU?)g{Gz?r	   
   N)r   r   r   r   r    r   c                  $    t                     S )N)_decode_and_center_crop)r   
image_sizes   r*   <lambda>z)_decode_and_random_crop.<locals>.<lambda>l   s    'Z@@ r6   c                  X    t           j                             gg          d         S Nr   )r   r   resize)r   r?   resize_methods   r*   r@   z)_decode_and_random_crop.<locals>.<lambda>m   s&    *j)A=QQRST r6   )	r   constantfloat32r   r   r   r5   r!   cond)r   r?   rD   r   original_shapebadr   s   ```   @r*   _decode_and_random_croprJ   \   s    ;+++2:YYYOOOD',  E X00==N

C
CCG@@@@@TTTTTTV VE
 Lr6   c           	         t           j                            |           }|d         }|d         }t          j        ||t          z   z  t          j        t          j        ||          t           j                  z  t           j                  }||z
  dz   dz  }||z
  dz   dz  }t          j        ||||g          }	t           j        	                    | |	d          }
t           j        
                    |
g||g|          d         }
|
S )z=Crops to center of image with padding then scales image_size.r   r9      r   r   )r   r   r   r-   CROP_PADDINGminimumrF   r.   r   r   rC   )r   r?   rD   r!   image_heightimage_widthpadded_center_crop_sizeoffset_heightoffset_widthr)   r   s              r*   r>   r>   r   s   H''44E8L(K g

\1
2	L+66
	C	C
D
 
 #%<<AaGM #::a?AEL(M<35LN O OKH))+{Q)OOEHOOUGj*%=}MMaPELr6   c                 D    t           j                            |           } | S )zRandom horizontal image flip.)r   r   random_flip_left_right)r   s    r*   _fliprV      s    H++E22ELr6   bicubicc                 L   |dk    rt           j        j        j        nt           j        j        j        }t          | ||          }t          |          }t          j        |||dg          }t           j                            ||rt           j	        nt           j
                  }|S aA  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`.
    rW   r   )r;   )r   r   ResizeMethodBICUBICBILINEARrJ   rV   reshapeconvert_image_dtypebfloat16rF   r   use_bfloat16r?   interpolationrD   r   s         r*   preprocess_for_trainrc      s     6Ci5O5OBH)11UWU]UjUsM#K]KKE%LLEJuz:q9::EH((L@R[[bj ) B BELr6   c                 .   |dk    rt           j        j        j        nt           j        j        j        }t          | ||          }t          j        |||dg          }t           j                            ||rt           j        nt           j	                  }|S rY   )
r   r   rZ   r[   r\   r>   r]   r^   r_   rF   r`   s         r*   preprocess_for_evalre      s     6Ci5O5OBH)11UWU]UjUsM#K]KKEJuz:q9::EH((L@R[[bj ) B BELr6   Fc                 N    |rt          | |||          S t          | |||          S )a  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].
    )rc   re   )r   is_trainingra   r?   rb   s        r*   preprocess_imagerh      s5    "  Y#Kz=YYY";j-XXXr6   c                   "    e Zd ZddZd Zd ZdS )	TfPreprocessTransformFr   rW   c                     || _         t          |t                    r|d         n|| _        || _        d | _        |                                 | _        d | _        d S rB   )	rg   
isinstancetuplesizerb   _image_bytes_build_tf_graphprocess_imagesess)selfrg   rn   rb   s       r*   __init__zTfPreprocessTransform.__init__   sW    &)$66@DGGD	* !1133			r6   c                     t          j        d          5  t          j        g t           j                  | _        t          | j        | j        d| j        | j                  }d d d            n# 1 swxY w Y   |S )Nz/cpu:0)r!   r;   F)	r   deviceplaceholderstringro   rh   rg   rn   rb   )rs   imgs     r*   rp   z%TfPreprocessTransform._build_tf_graph   s    Yx   	[ 	[ "i! ! !D #!4#3UDItGY[ [C	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 
s   AA..A25A2c                    | j         t          j                    | _         | j                             | j        | j        |i          }|                                                    dd                              t          j
                  }|j        dk     rt          j        |d          }t          j        |d          }|S )N)	feed_dictr      r   )axisrL   )rr   r   Sessionrunrq   ro   roundclipastypenpuint8ndimexpand_dimsrollaxis)rs   r   ry   s      r*   __call__zTfPreprocessTransform.__call__   s    9
DIimmD.4;Lk:Zm[[iikkq#&&--bh778a<<.2...Ck#q!!
r6   N)Fr   rW   )__name__
__module____qualname__rt   rp   r    r6   r*   rj   rj      sF                 r6   rj   )r   r   r   r
   N)__doc__tensorflow.compat.v1compatv1r   numpyr   
IMAGE_SIZErM   disable_eager_executionr   r5   rJ   r>   rV   rc   re   rh   rj   r   r6   r*   <module>r      sP   . * ! ! ! ! ! ! ! ! !    
 	 $ $ & & & 473?+6-0&*1 1 1 1h5 5 5  ,  *   @JYb    * ?IXa    * "'"' *#,	Y Y Y Y.         r6   