
    kh#                         d Z 	 ddlmc mZ ddlZdZdZej                  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y)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       c           
         t        j                  |d| |g      5  t         j                  j                  |       }t         j                  j	                  ||||||d      }|\  }	}
}t        j
                  |	      \  }}}t        j
                  |
      \  }}}t        j                  ||||g      }t         j                  j                  | |d      }|cddd       S # 1 sw Y   yxY w)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/teggl/fontify/venv/lib/python3.12/site-packages/timm/data/tf_preprocessing.pyr   r   !   s    > 
u;k4=P	Q ++K8(*(N(N11!%+/ )O )1% $A 
Iq !#

: 6(A)+I)>&|Qhh(M<PQ--k;QR-S%  s   B: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_equalr/   U   sA    HHQNEGGE288$EBMM%0!44    c           	      D    t        j                  g dt         j                  g d      }t         |ddddd	      t         j                  j                         }t        |t        j                        d
      }t        j                  | fdfd      S )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               S )N)_decode_and_center_crop)r   
image_sizes   r$   <lambda>z)_decode_and_random_crop.<locals>.<lambda>l   s    'Z@ r0   c                  R    t         j                  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=QRST r0   )	r   constantfloat32r   r   r   r/   r   cond)r   r<   rA   r   original_shapebadr   s   ```   @r$   _decode_and_random_croprG   \   s    ;;+2::YOD',E XX00=N

CCGG@TVE
 Lr0   c           	         t         j                  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                  j                  | |	d      }
t         j                  j                  |
g||g|      d   }
|
S )z=Crops to center of image with padding then scales image_size.r   r4      r   r   )r   r   r   r'   CROP_PADDINGminimumrC   r(   r   r   r@   )r   r<   rA   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<35LN OKHH))+{Q)OEHHOOUGj*%=}MaPELr0   c                 D    t         j                  j                  |       } | S )zRandom horizontal image flip.)r   r   random_flip_left_right)r   s    r$   _fliprS      s    HH++E2ELr0   bicubicc                    |dk(  r$t         j                  j                  j                  n#t         j                  j                  j                  }t        | ||      }t        |      }t        j                  |||dg      }t         j                  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`.
    rT   r   )r6   )r   r   ResizeMethodBICUBICBILINEARrG   rS   reshapeconvert_image_dtypebfloat16rC   r   use_bfloat16r<   interpolationrA   r   s         r$   preprocess_for_trainr`      s     6Ci5OBHH))11UWU]U]UjUjUsUsM#K]KE%LEJJuz:q9:EHH((LR[[bjj ) BELr0   c                 n   |dk(  r$t         j                  j                  j                  n#t         j                  j                  j                  }t        | ||      }t        j                  |||dg      }t         j                  j                  ||rt         j                  nt         j                        }|S rV   )
r   r   rW   rX   rY   r;   rZ   r[   r\   rC   r]   s         r$   preprocess_for_evalrb      s     6Ci5OBHH))11UWU]U]UjUjUsUsM#K]KEJJuz:q9:EHH((LR[[bjj ) BELr0   Fc                 >    |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].
    )r`   rb   )r   is_trainingr^   r<   r_   s        r$   preprocess_imagere      s)    " #Kz=YY";j-XXr0   c                        e Zd ZddZd Zd Zy)TfPreprocessTransformc                     || _         t        |t              r|d   n|| _        || _        d | _        | j                         | _        d | _        y r?   )	rd   
isinstancetuplesizer_   _image_bytes_build_tf_graphprocess_imagesess)selfrd   rk   r_   s       r$   __init__zTfPreprocessTransform.__init__   sH    &)$6DGD	* !113	r0   c                    t        j                  d      5  t        j                  g t         j                        | _        t        | j                  | j                  d| j                  | j                        }d d d        |S # 1 sw Y   S xY w)Nz/cpu:0)r   r6   F)	r   deviceplaceholderstringrl   re   rd   rk   r_   )rp   imgs     r$   rm   z%TfPreprocessTransform._build_tf_graph   sx    YYx  	[ "ii!D #!!4#3#3UDIItGYGY[C	[ 
	[ 
s   A"BBc                    | j                   t        j                         | _         | j                   j                  | j                  | j
                  |i      }|j                         j                  dd      j                  t        j                        }|j                  dk  rt        j                  |d      }t        j                  |d      }|S )N)	feed_dictr      r   )axisrI   )ro   r   Sessionrunrn   rl   roundclipastypenpuint8ndimexpand_dimsrollaxis)rp   r   rv   s      r$   __call__zTfPreprocessTransform.__call__   s    99

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