Ë
    éÓ^jÑ#  ã                   óÚ   — 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                     úe/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/timm/data/tf_preprocessing.pyr   r   !   sä   € ô> 
�‰�uÐ;¸kÈ4Ð=PÓ	Qñ Ü—‘×+Ñ+¨KÓ8ˆÜ(*¯©×(NÑ(NØØØ1Ø1Ø!Ø%Ø+/ð )Oó )1Ð%ð $AÑ ˆ
�I˜qô !#§
¡
¨:Ó 6Ñˆ�(˜AÜ)+¯©°IÓ)>Ñ&ˆ�| QÜ—h‘h ¨(°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   € ä�H‰H�Q˜‹N€EÜ�G‰G�Eœ2Ÿ8™8Ó$€EÜ×ÑœBŸM™M¨%Ó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{®Gáz´?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À=ÓQÐRSÑ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ÔO€DÜ'ØØØØ,ØØØô€Eô —X‘X×0Ñ0°Ó=€NÜ
 ´·±¸³ÀÓ
C€Cä�G‰GØÜ@Ý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û   € ä�H‰H×'Ñ'¨Ó4€EØ˜‘8€LØ˜‘(€Kä Ÿg™gØ
˜
¤\Ñ1Ñ
2Ü	�‰”—‘˜L¨+Ó6¼¿
¹
Ó	Cñ
Dä
�‰óÐð
 #Ð%<Ñ<ÀÑAÀaÑG€MØ Ð#:Ñ:¸aÑ?ÀAÑE€LÜ—(‘(˜M¨<Ø3Ð5LðNó O€Kä�H‰H×)Ñ)¨+°{ÈQÐ)ÓO€EÜ�H‰H�O‰O˜U˜G j°*Ð%=¸}ÓMÈaÑP€Eà€Lr0   c                 óD   — t         j                  j                  | «      } | S )zRandom horizontal image flip.)r   r   Úrandom_flip_left_right)r   s    r$   Ú_fliprS   ‡   s   € ä�H‰H×+Ñ+¨EÓ2€EØ€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ÀiÒ5O”B—H‘H×)Ñ)×1Ò1ÔUW×U]ÑU]×UjÑUj×UsÑUs€MÜ# K°¸]ÓK€EÜ�%‹L€EÜ�J‰J�u˜z¨:°qÐ9Ó:€EÜ�H‰H×(Ñ(Ø¡L”R—[’[´b·j±jð )ó 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ÀiÒ5O”B—H‘H×)Ñ)×1Ò1ÔUW×U]ÑU]×UjÑUj×UsÑUs€MÜ# K°¸]ÓK€EÜ�J‰J�u˜z¨:°qÐ9Ó:€EÜ�H‰H×(Ñ(Ø¡L”R—[’[´b·j±jð )ó 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   € Ø&ˆÔÜ)¨$´Ô6�D˜’G¸DˆŒ	Ø*ˆÔØ ˆÔØ!×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   € Ü�Y‰Y�xÓ ñ 	[Ü "§¡ØÜ—i‘iô!ˆDÔô #Ø×!Ñ! 4×#3Ñ#3°U¸D¿I¹IÀt×GYÑGYó[ˆ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—   € Ø�9‰9ÐÜŸ
™
›ˆDŒIØ�i‰i�m‰m˜D×.Ñ.¸4×;LÑ;LÈkÐ:ZˆmÓ[ˆØ�i‰i‹k×Ñ˜q #Ó&×-Ñ-¬b¯h©hÓ7ˆØ�8‰8�aŠ<Ü—.‘. ¨2Ô.ˆCÜ�k‰k˜#˜qÓ!ˆØˆ
r0   N)Fr   rT   )Ú__name__Ú
__module__Ú__qualname__rq   rm   r†   © r0   r$   rg   rg   Í   s   „ óòór0   rg   )r7   )r8   gHáz®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œ   ðñð. *ß !Ð !Û à€
Ø€à ‡	�	‡�× $Ñ $Ô &ð 47Ø3?Ø+6Ø-0Ø&*ó1òh5òò,ò*ð @JÐYbó ð* ?IÐXaó ð* "'Ø"'Ø *Ø#,ó	Y÷.ò r0   