
    Z j3                        S SK Jr  S SKrS SKrS SKJr  S SKJr  S SK	J
r
  S SKJr  SSKJr  SS	KJrJrJr  SS
KJrJrJr  \R.                  " \5      r SS\\\4   S\S\S-  S\S\\\4   4
S jjr " S S\
5      r " S S\5      rSS/rg)    )OptionalN)nn)
functional)DetrImageProcessor)DetrImageProcessorPil   )center_to_corners_format)PILImageResamplingSizeDict#get_image_size_for_max_height_width)
TensorTypeloggingrequires_backends
image_sizesizemax_sizemod_sizereturnc                    U u  pESnUbP  [        [        XE45      5      n[        [        XE45      5      nX-  U-  U:  a  X'-  U-  n[        [	        U5      5      nXT:  a*  Un	Ub  Ub  [        Xd-  U-  5      n
OQ[        X-  U-  5      n
O@XE::  a  XA:X  d
  XT::  a  XQ:X  a  XEpO)Un
Ub  Ub  [        Xe-  U-  5      n	O[        X-  U-  5      n	Ub  XU-  -
  n	XU-  -
  n
X4$ )a_  
Computes the output image size given the input image size and the desired output size, while ensuring that both
height and width are multiples of `mod_size`.

This mirrors the YOLOS-specific behavior used in the torch/fast backends and is required so that all YOLOS
image processing backends (PIL, torchvision, fast) produce identical output shapes.
N)floatminmaxintround)r   r   r   r   heightwidthraw_sizemin_original_sizemax_original_sizeowohs              x/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/yolos/modular_yolos.py get_size_with_aspect_ratio_yolosr#      s    MFH!#vo"67!#vo"67047(B36GGHuX'D~H$8X&./BT]U*+B
/fn%/emBH$8X%./BT\F*+B=!=!8O    c            	          ^  \ rS rSr SS\R
                  S\S\S   S\R
                  4U 4S jjjr SS\	S	\
\\   -  4S
 jjrS rS rS rSrU =r$ )YolosImageProcessor<   imager   resamplez0PILImageResampling | tvF.InterpolationMode | intr   c                 $  > UR                   (       a@  UR                  (       a/  [        UR                  SS UR                   UR                  5      nOUR                  (       a@  UR
                  (       a/  [        UR                  SS UR                  UR
                  5      nOJUR                  (       a*  UR                  (       a  UR                  UR                  4nO[        SU S35      e[        TU ],  " U4[        US   US   S9US.UD6nU$ )	a  
Resize the image to the given size. Size can be `min_size` (scalar) or `(height, width)` tuple. If size is an
int, smaller edge of the image will be matched to this number.

Args:
    image (`torch.Tensor`):
        Image to resize.
    size (`SizeDict`):
        Size of the image's `(height, width)` dimensions after resizing. Available options are:
            - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`.
                Do NOT keep the aspect ratio.
            - `{"shortest_edge": int, "longest_edge": int}`: The image will be resized to a maximum size respecting
                the aspect ratio and keeping the shortest edge less or equal to `shortest_edge` and the longest edge
                less or equal to `longest_edge`.
            - `{"max_height": int, "max_width": int}`: The image will be resized to the maximum size respecting the
                aspect ratio and keeping the height less or equal to `max_height` and the width less or equal to
                `max_width`.
    resample (`PILImageResampling | tvF.InterpolationMode | int`, *optional*, defaults to `PILImageResampling.BILINEAR`):
        Resampling filter to use if resizing the image.
N\Size must contain 'height' and 'width' keys or 'shortest_edge' and 'longest_edge' keys. Got .r      r   r   r   r)   )shortest_edgelongest_edger#   shape
max_height	max_widthr   r   r   
ValueErrorsuperresizer   selfr(   r   r)   kwargsnew_size	__class__s         r"   r8   YolosImageProcessor.resize=   s    6 $"3"3 8BC8H$J\J\^b^o^opH__:5;;rs;KT__^b^l^lmH[[TZZTZZ0Hnosnttuv  
 8A;GRZ
^d
 r$   	thresholdtarget_sizesc                    UR                   UR                  pTUb#  [        U5      [        U5      :w  a  [        S5      e[        R
                  R                  US5      nUSSS24   R                  S5      u  px[        U5      n	Ub  [        U[        5      (       aS  [        R                  " U V
s/ s H  oS   PM	     sn
5      n[        R                  " U V
s/ s H  oS   PM	     sn
5      nOUR                  S5      u  p[        R                  " XX/SS9R                  U	R                   5      nXSS2SSS24   -  n	/ n[#        XxU	5       H1  u  nnnXU:     nUX:     nUX:     nUR%                  UUUS.5        M3     U$ s  sn
f s  sn
f )	!  
Converts the raw output of [`YolosForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
bottom_right_x, bottom_right_y) format. Only supports PyTorch.

Args:
    outputs ([`YolosObjectDetectionOutput`]):
        Raw outputs of the model.
    threshold (`float`, *optional*):
        Score threshold to keep object detection predictions.
    target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*):
        Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size
        `(height, width)` of each image in the batch. If unset, predictions will not be resized.
Returns:
    `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image
    in the batch as predicted by the model.
NTMake sure that you pass in as many target sizes as the batch dimension of the logits.r   r.   dimscoreslabelsboxes)logits
pred_boxeslenr6   r   r   softmaxr   r	   
isinstancelisttorchTensorunbindstacktodevicezipappendr:   outputsr?   r@   
out_logitsout_bboxprobrH   rI   rJ   iimg_himg_w	scale_fctresultsslbscorelabelboxs                        r"   post_process_object_detection1YolosImageProcessor.post_process_object_detectionj   s}   &  '~~w/A/AH#:#l"33 j  }}$$Z4c3B3h++B/ )2 #,--L%ALqdL%ABL%ALqdL%AB+2215U5$@aHKKELLYIaqj11E651GAq!)m$Eam$EAM"CNNeusKL	 2  &B%As   /FF	c                     [        S5      eNzHSegmentation post-processing is not implemented for Deformable DETR yet.NotImplementedErrorr:   s    r"   "post_process_instance_segmentation6YolosImageProcessor.post_process_instance_segmentation       !"lmmr$   c                     [        S5      eNzQSemantic segmentation post-processing is not implemented for Deformable DETR yet.rm   ro   s    r"   "post_process_semantic_segmentation6YolosImageProcessor.post_process_semantic_segmentation       !"uvvr$   c                     [        S5      eNzQPanoptic segmentation post-processing is not implemented for Deformable DETR yet.rm   ro   s    r"   "post_process_panoptic_segmentation6YolosImageProcessor.post_process_panoptic_segmentation   rw   r$    Ng      ?N)__name__
__module____qualname____firstlineno__rQ   rR   r   r   r8   r   r   rP   tupleri   rp   ru   rz   __static_attributes____classcell__r=   s   @r"   r&   r&   <   s    
 RV	+||+ + MN	+ 
+ +\ Y]3"'3=G$u+=U3jnww wr$   r&   c            	          ^  \ rS rSr SS\R
                  S\S\S   S\R
                  4U 4S jjjr SS\	S	\
\\   -  4S
 jjrS rS rS rSrU =r$ )YolosImageProcessorPil   r(   r   r)   r
   r   c                 l  > Ub  UOU R                   nUR                  (       aS  UR                  (       aB  [        UR                  SS UR                  UR                  =(       d    UR                  5      nOUR
                  (       a@  UR                  (       a/  [        UR                  SS UR
                  UR                  5      nOJUR                  (       a*  UR                  (       a  UR                  UR                  4nO[        SU S35      e[        TU ]0  " U4[        US   US   S9US.UD6nU$ )	a  
Resize the image to the given size. Size can be `min_size` (scalar) or `(height, width)` tuple. If size is an
int, smaller edge of the image will be matched to this number.

Args:
    image (`np.ndarray`):
        Image to resize.
    size (`SizeDict`):
        Size of the image's `(height, width)` dimensions after resizing. Available options are:
            - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`.
                Do NOT keep the aspect ratio.
            - `{"shortest_edge": int, "longest_edge": int}`: The image will be resized to a maximum size respecting
                the aspect ratio and keeping the shortest edge less or equal to `shortest_edge` and the longest edge
                less or equal to `longest_edge`.
            - `{"max_height": int, "max_width": int}`: The image will be resized to the maximum size respecting the
                aspect ratio and keeping the height less or equal to `max_height` and the width less or equal to
                `max_width`.
    resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`):
        Resampling filter to use if resizing the image.
Nr+   r,   r-   r   r.   r/   r0   )r)   r1   r2   r#   r3   r4   r5   r   r   r   r6   r7   r8   r   r9   s         r"   r8   YolosImageProcessorPil.resize   s	   6  (38$"3"3 8BC ""!!7T%7%7H
 __:5;;rs;KT__^b^l^lmH[[TZZTZZ0Hnosnttuv  
!HQK@
 	
 r$   r?   r@   c                 6   [        U S/5        UR                  UR                  pTUb#  [        U5      [        U5      :w  a  [	        S5      e[
        R                  R                  US5      nUSSS24   R                  S5      u  px[        U5      n	Ub  [        U[        5      (       aS  [        R                  " U V
s/ s H  oS   PM	     sn
5      n[        R                  " U V
s/ s H  oS   PM	     sn
5      nOUR                  S5      u  p[        R                  " XX/SS9R!                  U	R"                  5      nXSS2SSS24   -  n	/ n[%        XxU	5       H1  u  nnnXU:     nUX:     nUX:     nUR'                  UUUS	.5        M3     U$ s  sn
f s  sn
f )
rB   rQ   NrC   rD   .r   r.   rE   rG   )r   rK   rL   rM   r6   r   r   rN   r   r	   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   s                        r"   ri   4YolosImageProcessorPil.post_process_object_detection   s   & 	$	*&~~w/A/AH#:#l"33 j  }}$$Z4c3B3h++B/ )2 #,--L%ALqdL%ABL%ALqdL%AB+2215U5$@aHKKELLYIaqj11E651GAq!)m$Eam$EAM"CNNeusKL	 2  &B%As   <F%Fc                     [        S5      erl   rm   ro   s    r"   rp   9YolosImageProcessorPil.post_process_instance_segmentation  rr   r$   c                     [        S5      ert   rm   ro   s    r"   ru   9YolosImageProcessorPil.post_process_semantic_segmentation  rw   r$   c                     [        S5      ery   rm   ro   s    r"   rz   9YolosImageProcessorPil.post_process_panoptic_segmentation  rw   r$   r|   r}   r~   )r   r   r   r   npndarrayr   r   r8   r   r   rP   r   ri   rp   ru   rz   r   r   r   s   @r"   r   r      s    
 48	4zz4 4 /0	4 
4 4n Y]4"'4=G$u+=U4lnww wr$   r   )N   ) typingr   numpyr   rQ   r   torchvision.transforms.v2r   tvF.transformers.models.detr.image_processing_detrr   2transformers.models.detr.image_processing_pil_detrr   image_transformsr	   image_utilsr
   r   r   utilsr   r   r   
get_loggerr   loggerr   r   r#   r&   r   __all__r|   r$   r"   <module>r      s        7 M T 8 \ \ ; ; 
		H	% Z\&c3h&'*&69Dj&SV&
38_&Rjw, jwZtw2 twn !":
;r$   