§
    ·¯¸jÑ#  ã                   óÖ   — 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éà   é    çš™™™™™¹?©ç      è?gHáz®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                     új/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/timm/data/tf_preprocessing.pyr   r   !   s?  € õ> 
Œ�uÐ;¸kÈ4Ð=PÑ	QÔ	Qð ð Ý”×+Ò+¨KÑ8Ô8ˆÝ(*¬×(NÒ(NØØØ1Ø1Ø!Ø%Ø+/ð )Oñ )1ô )1Ð%ð $AÑ ˆ
�I˜qõ !#¤
¨:Ñ 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   € åŒH�Q˜‰NŒN€EÝŒG�E�2œ8Ñ$Ô$€EÝÔ�BœM¨%Ñ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{®Gáz´?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ÔQÐRSÔ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´:ÀYÀYÀYÐOÑOÔO€DÝ'ØØØØ,ØØØðñ ô €Eõ ”X×0Ò0°Ñ=Ô=€NÝ
 µ´¸±´ÀÑ
CÔ
C€CåŒ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Ô4€EØ˜”8€LØ˜”(€Kå œgØ
˜
¥\Ñ1Ñ
2Ý	Œ•”˜L¨+Ñ6Ô6½¼
Ñ	CÔ	Cñ
Då
Œñô Ðð
 #Ð%<Ñ<ÀÑAÀaÑG€MØ Ð#:Ñ:¸aÑ?ÀAÑE€LÝ”(˜M¨<Ø3Ð5LðNñ Oô O€KåŒH×)Ò)¨+°{ÈQÐ)ÑOÔO€EÝŒH�OŠO˜U˜G j°*Ð%=¸}ÑMÔMÈaÔP€Eà€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Ô2€EØ€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ÀiÒ5OÐ5O•B”HÔ)Ô1Ð1ÕUWÔU]ÔUjÔUs€MÝ# K°¸]ÑKÔK€EÝ�%‰LŒL€EÝŒJ�u˜z¨:°qÐ9Ñ:Ô:€EÝŒH×(Ò(Ø LÐ@•R”[�[µb´jð )ñ 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ÀiÒ5OÐ5O•B”HÔ)Ô1Ð1ÕUWÔU]ÔUjÔUs€MÝ# K°¸]ÑKÔK€EÝŒJ�u˜z¨:°qÐ9Ñ:Ô:€EÝŒH×(Ò(Ø LÐ@•R”[�[µb´jð )ñ 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˜”G�G¸DˆŒ	Ø*ˆÔØ ˆÔØ!×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É   € ÝŒY�xÑ Ô ð 	[ð 	[Ý "¤ØÝ”ið!ñ !ô !ˆDÔõ #ØÔ! 4Ô#3°U¸D¼IÀtÔGYñ[ô [ˆ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ÐÝœ
™œˆDŒIØŒi�mŠm˜DÔ.¸4Ô;LÈkÐ:ZˆmÑ[Ô[ˆØ�iŠi‰kŒk×Ò˜q #Ñ&Ô&×-Ò-­b¬hÑ7Ô7ˆØŒ8�aŠ<ˆ<Ý”. ¨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  ððð ð. *Ø !Ð !Ð !Ð !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð à€
Ø€à „	„× $Ò $Ñ &Ô &Ð &ð 47Ø3?Ø+6Ø-0Ø&*ð1ð 1ð 1ð 1ðh5ð 5ð 5ðð ð ð,ð ð ð*ð ð ð @JÐYbð ð ð ð ð* ?IÐXað ð ð ð ð* "'Ø"'Ø *Ø#,ð	Yð Yð Yð Yð.ð ð ð ð ñ ô ð ð ð r6   