U
    ¯Ê¹jÑ#  ã                   @   sœ   d Z ddlm  mZ ddlZdZdZejj 	¡  dd	d
„Z
dd„ Zdd„ Zdd„ Zdd„ Zedfdd„Zedfdd„Zddedfdd„ZG d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éà   é    çš™™™™™¹?©ç      è?gHáz®Gõ?©gš™™™™™©?ç      ð?éd   c              
   C   s¢   t  |d| |g¡�† t j | ¡}t jj||||||dd�}|\}	}
}t  |	¡\}}}t  |
¡\}}}t  ||||g¡}t jj| |dd�}|W  5 Q R £ S Q R X 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)Zbounding_boxesÚmin_object_coveredÚaspect_ratio_rangeÚ
area_rangeÚmax_attemptsZuse_image_if_no_bounding_boxesé   ©ZchannelsN)ÚtfZ
name_scopeÚimageÚextract_jpeg_shapeÚsample_distorted_bounding_boxZunstackÚstackÚdecode_and_crop_jpeg)Úimage_bytesÚbboxr   r   r   r   ÚscopeÚshaper   Z
bbox_beginZ	bbox_sizeÚ_Zoffset_yZoffset_xZtarget_heightZtarget_widthÚcrop_windowr   © r   úY/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/data/tf_preprocessing.pyr
   !   s"    ù
r
   c                 C   s,   t  | |¡}t  |t j¡}t  t  |¡|¡S )z0At least `x` of `a` and `b` `Tensors` are equal.)r   ÚequalÚcastÚint32Zgreater_equalZ
reduce_sum)ÚaÚbÚxÚmatchr   r   r   Ú_at_least_x_are_equalU   s    r&   c              	      s|   t jddddgt jdddgd�}tˆ|dddd	d
d�‰ t j ˆ¡}t|t  ˆ ¡dƒ}t  |‡‡fdd„‡ ‡‡fdd„¡‰ ˆ S )z!Make a random crop of image_size.g        r   é   é   )Údtyper   r   )r   gUUUUUUõ?)g{®Gáz´?r   é
   N)r   r   r   r   r   r   c                      s
   t ˆ ˆƒS )N)Ú_decode_and_center_cropr   )r   Ú
image_sizer   r   Ú<lambda>l   ó    z)_decode_and_random_crop.<locals>.<lambda>c                      s   t j ˆ gˆˆgˆ¡d S ©Nr   )r   r   Úresizer   )r   r,   Úresize_methodr   r   r-   m   r.   )	r   ZconstantÚfloat32r
   r   r   r&   r   Zcond)r   r,   r1   r   Zoriginal_shapeÚbadr   )r   r   r,   r1   r   Ú_decode_and_random_crop\   s$     ùýr4   c              	   C   sª   t j | ¡}|d }|d }t  ||t  t  t  ||¡t j¡ t j¡}|| d d }|| d d }t  ||||g¡}	t jj	| |	dd�}
t j 
|
g||g|¡d }
|
S )z=Crops to center of image with padding then scales image_size.r   r'   é   r   r   )r   r   r   r    ÚCROP_PADDINGÚminimumr2   r!   r   r   r0   )r   r,   r1   r   Zimage_heightZimage_widthZpadded_center_crop_sizeZoffset_heightÚoffset_widthr   r   r   r   r   r+   r   s$    
ÿý ÿr+   c                 C   s   t j | ¡} | S )zRandom horizontal image flip.)r   r   Zrandom_flip_left_right)r   r   r   r   Ú_flip‡   s    r9   Úbicubicc                 C   sb   |dkrt jjjnt jjj}t| ||ƒ}t|ƒ}t  |||dg¡}t jj||rTt j	nt j
d�}|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`.
    r:   r   )r)   )r   r   ÚResizeMethodÚBICUBICÚBILINEARr4   r9   ÚreshapeÚconvert_image_dtypeÚbfloat16r2   ©r   Úuse_bfloat16r,   Úinterpolationr1   r   r   r   r   Úpreprocess_for_train�   s     ÿrE   c                 C   sZ   |dkrt jjjnt jjj}t| ||ƒ}t  |||dg¡}t jj||rLt jnt j	d�}|S r;   )
r   r   r<   r=   r>   r+   r?   r@   rA   r2   rB   r   r   r   Úpreprocess_for_eval¢   s     ÿrF   Fc                 C   s$   |rt | |||ƒS t| |||ƒS d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].
    N)rE   rF   )r   Úis_trainingrC   r,   rD   r   r   r   Úpreprocess_image¶   s    rH   c                   @   s&   e Zd Zddd„Zdd„ Zdd	„ Zd
S )ÚTfPreprocessTransformFr   r:   c                 C   s>   || _ t|tƒr|d n|| _|| _d | _|  ¡ | _d | _d S r/   )	rG   Ú
isinstanceÚtupleÚsizerD   Ú_image_bytesÚ_build_tf_graphÚprocess_imageÚsess)ÚselfrG   rL   rD   r   r   r   Ú__init__Ï   s    
zTfPreprocessTransform.__init__c              	   C   sD   t  d¡�0 t jg t jd�| _t| j| jd| j| jƒ}W 5 Q R X |S )Nz/cpu:0)r   r)   F)	r   ZdeviceÚplaceholderÚstringrM   rH   rG   rL   rD   )rQ   Úimgr   r   r   rN   ×   s    þ    ÿz%TfPreprocessTransform._build_tf_graphc                 C   sl   | j d krt ¡ | _ | j j| j| j|id�}| ¡  dd¡ t	j
¡}|jdk r\t	j|dd�}t	 |d¡}|S )N)Z	feed_dictr   éÿ   r   éÿÿÿÿ)Zaxisr5   )rP   r   ÚSessionÚrunrO   rM   ÚroundZclipZastypeÚnpZuint8ÚndimZexpand_dimsZrollaxis)rQ   r   rU   r   r   r   Ú__call__á   s    


zTfPreprocessTransform.__call__N)Fr   r:   )Ú__name__Ú
__module__Ú__qualname__rR   rN   r]   r   r   r   r   rI   Í   s   

rI   )r   r   r   r	   N)Ú__doc__Ztensorflow.compat.v1ÚcompatZv1r   Únumpyr[   Z
IMAGE_SIZEr6   Zdisable_eager_executionr
   r&   r4   r+   r9   rE   rF   rH   rI   r   r   r   r   Ú<module>   s.        ú
4ü
