
    Z j{                        S 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	J
r
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Jr  S	SKJrJr  S	SKJrJ r J!r!J"r"J#r#J$r$  S	SK%J&r&J'r'  S	SK(J)r)J*r*J+r+  \+" 5       (       a  SSK,r, " S S\&SS9r-\* " S S\5      5       r.SSS\/S\04S jr1S3S jr2S4S jr3S\4\0   S\0S\04S  jr5    S5S!\0S"\0S#\/S$\0S-  S%\4\0   S-  S&\6\4\4\0      \4\0   4   4S' jjr7S( r8S)\0S&\Rr                  4S* jr: S6S+ jr; S7S!\0S,\Rr                  S-\6\0\04   S&\Rr                  4S. jjr<S/\=\>\	4   S&\Rr                  4S0 jr?S8S1 jr@S9S2 jrAS/rBg):zImage processor class for SAM.    N)Iterable)deepcopy)product)AnyOptionalUnion)
functional)batched_nms   )TorchvisionBackend)BatchFeatureget_size_dict)group_images_by_shapereorder_images)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDChannelDimension
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstringis_vision_availablec                   B    \ rS rSr% Sr\\\4   \S'   \\\4   \S'   Sr	g)SamImageProcessorKwargs/   a>  
mask_size (`dict[str, int]`, *optional*):
    The size `{"longest_edge": int}` to resize the segmentation maps to.
mask_pad_size (`dict[str, int]`, *optional*):
    The size `{"height": int, "width": int}` to pad the segmentation maps to. Must be larger than any segmentation
    map size provided for preprocessing.
	mask_sizemask_pad_size N)
__name__
__module____qualname____firstlineno____doc__dictstrint__annotations____static_attributes__r!       }/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/sam/image_processing_sam.pyr   r   /   s%     CH~S>!r,   r   F)totalc            #         ^  \ rS rSr\r\R                  r\	r
\rSS0rSS0rSrSrSrSrSrSSS.rSSS.rS\\   4U 4S jjr\ S5S
\S\S	-  S\\   S\4U 4S jjj5       r  S6S\\\   -  \\\4   -  \ -  S	-  S\\\   -  \\\4   -  \ -  S	-  S\4U 4S jjjr!S\"\\4   S\4S jr# S5SSS\ SSSS4U 4S jjjr$ S5S
\S\S	-  S\%S\&S\\'-  S	-  S\(\S4   S	-  S\4S jjr)S
\*S   S\%S\ SSS \%S!\ S"\%S#\+S$\%S%\+\*\+   -  S	-  S&\+\*\+   -  S	-  S'\%S	-  S(\ S	-  S)\%S	-  S\"\*S   \*\"\\4      4   4S* jr,     S7SS+S,\S-\+S.\S	-  S/\*\   S	-  S\-S   4S0 jjr.    S8S1 jr/   S9S2 jr0S3 r1S4r2U =r3$ ):SamImageProcessor<   longest_edgei      Theightwidthkwargsc                 &   > [         TU ]  " S0 UD6  g )Nr!   )super__init__)selfr7   	__class__s     r-   r:   SamImageProcessor.__init__L   s    "6"r,   Nimagessegmentation_mapsreturnc                 &   > [         TU ]  " X40 UD6$ )zX
segmentation_maps (`ImageInput`, *optional*):
    The segmentation maps to preprocess.
)r9   
preprocess)r;   r>   r?   r7   r<   s       r-   rB   SamImageProcessor.preprocessO   s     w!&FvFFr,   r   r    c           	         > [         TU ]  " S0 UD6nUb(  [        U[        5      (       d  [        S0 [	        USS9D6nUb(  [        U[        5      (       d  [        S0 [	        USS9D6nXS'   X#S'   U$ )z
Update kwargs that need further processing before being validated
Can be overridden by subclasses to customize the processing of kwargs.
r   )
param_namer    r!   )r9   _standardize_kwargs
isinstancer   r   )r;   r   r    r7   r<   s       r-   rF   %SamImageProcessor._standardize_kwargs\   sx     ,6v6 Ix)H)H T={#STI$Zx-P-P$`}]'_`M'{"/r,   	old_shapec                 x    Uu  p4US-  [        X45      -  nX5-  XE-  pv[        US-   5      n[        US-   5      nXg4$ )zG
Compute the output size given input size and target long side length.
      ?      ?)maxr)   )r;   rI   r2   oldholdwscalenewhnewws           r-   _get_preprocess_shape'SamImageProcessor._get_preprocess_shapeq   sK     
s"S_4\4<d4#:4#:|r,   imagetorch.Tensorsizeresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                    > UR                   (       d  [        SUR                  5        35      eUR                  SS nU R	                  XRR                   5      u  pg[
        TU ]  " U4[        XgS9US.UD6$ )aE  
Resize an image to `(size["height"], size["width"])`.

Args:
    image (`torch.Tensor`):
        Image to resize.
    size (`SizeDict`):
        Dictionary in the format `{"longest_edge": int}` specifying the size of the output image. The longest
        edge of the image will be resized to the specified size, while the other edge will be resized to
        maintain the aspect ratio.
    resample (`PILImageResampling | tvF.InterpolationMode | int | None`, *optional*):
        Resampling filter to use when resizing the image.

Returns:
    `torch.Tensor`: The resized image.
z?The `size` dictionary must contain the key `longest_edge`. Got Nr4   )rW   rX   )r2   
ValueErrorkeysshaperS   r9   resizer   )	r;   rU   rW   rX   r7   
input_sizeoutput_heightoutput_widthr<   s	           r-   r^   SamImageProcessor.resize|   s    .   ^_c_h_h_j^klmm[[%
&*&@&@M^M^&_#w~
 JU]
ag
 	
r,   do_convert_rgbinput_data_formatreturn_tensorsdeviceztorch.devicec           	      x   U R                  XXFS9nU Vs/ s H  oR                  SS PM     n	nUR                  5       n
U R                  " U40 U
D6u  pUU	US.nUb  U R                  USS[        R
                  S9nUR                  5       nUR                  SS[        R                  UR                  S5      UR                  S	5      S
.5        U R                  " SSU0UD6u  nnU Vs/ s H1  nUR                  S5      R                  [        R                  5      PM3     snUS'   [        XS9$ s  snf s  snf )z
Preprocess image-like inputs.
)r>   rc   rd   rf   rZ   N)pixel_valuesoriginal_sizesreshaped_input_sizes   F)r>   expected_ndimsrc   rd   r   r    )do_normalize
do_rescalerX   rW   pad_sizer>   r   labels)datatensor_typer!   )_prepare_image_like_inputsr]   copy_preprocessr   FIRSTupdater   NEARESTpopsqueezetotorchint64r   )r;   r>   r?   rc   rd   re   rf   r7   rU   ri   images_kwargsrh   rj   rq   processed_segmentation_mapssegmentation_maps_kwargs_ms                     r-   _preprocess_image_like_inputs/SamImageProcessor._preprocess_image_like_inputs   sa    00L] 1 
 9??u++bc*?-1-=-=f-V-V*(,$8
 (*.*I*I( $"2"8"8	 +J +' (.{{}$$++$)"' 2 : :488E 8 < <_ M .2-=-= .2.6N.*' E``D_qaiilooekk:D_`DNBBA @< as   D2+8D7	do_resizedo_center_crop	crop_sizern   rescale_factorrm   
image_mean	image_stddo_padro   disable_groupingc           	         [        XS9u  nn0 nUR                  5        H"  u  nnU(       a  U R                  UX4S9nUUU'   M$     [        UU5      nU Vs/ s H  nUR                  SS  PM     nn[        UUS9u  nn0 nUR                  5        H8  u  nnU(       a  U R                  UU5      nU R                  UXxXU5      nUUU'   M:     [        UU5      nU(       a  U R                  UXS9nUU4$ s  snf )N)r   )rU   rW   rX   rZ   )ro   r   )r   itemsr^   r   r]   center_croprescale_and_normalizepad)r;   r>   r   rW   rX   r   r   rn   r   rm   r   r   r   ro   r   r7   grouped_imagesgrouped_images_indexresized_images_groupedr]   stacked_imagesresized_imagesrU   rj   processed_images_groupedprocessed_imagess                             r-   ru   SamImageProcessor._preprocess   s-   & 0EV/o,,!#%3%9%9%;!E>!%>!`,:"5) &< ((>@TU>LMnUBC 0nM 0E^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N /=$U+ &< **BDXY#xx(88xo!555'  Ns   C2z+np.ndarray | PIL.Image.Image | torch.Tensorcrop_n_layersoverlap_ratiopoints_per_cropcrop_n_points_downscale_factorc                     U R                  U5      n[        UUUUUU5      u  ppUc  [        R                  " S5      nUR	                  U5      nUR	                  U5      nU
R	                  U5      n
XX4$ )an  
Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer.

Args:
    image (`torch.Tensor`):
        Input original image
    target_size (`int`):
        Target size of the resized image
    crop_n_layers (`int`, *optional*, defaults to 0):
        If >0, mask prediction will be run again on crops of the image. Sets the number of layers to run, where
        each layer has 2**i_layer number of image crops.
    overlap_ratio (`float`, *optional*, defaults to 512/1500):
        Sets the degree to which crops overlap. In the first crop layer, crops will overlap by this fraction of
        the image length. Later layers with more crops scale down this overlap.
    points_per_crop (`int`, *optional*, defaults to 32):
        Number of points to sample from each crop.
    crop_n_points_downscale_factor (`list[int]`, *optional*, defaults to 1):
        The number of points-per-side sampled in layer n is scaled down by crop_n_points_downscale_factor**n.
    device (`torch.device`, *optional*, defaults to None):
        Device to use for the computation. If None, cpu will be used.
cpu)process_image_generate_crop_boxesr|   rf   r{   )r;   rU   target_sizer   r   r   r   rf   
crop_boxescropped_imagesinput_labelss              r-   generate_crop_boxes%SamImageProcessor.generate_crop_boxes   s    > ""5)DX*E
A
^ >\\%(F]]6*
),,V4#v.NHHr,   c	                 t   Uu  pUR                  SS5      nUR                  SS5      nUR                  S   UR                  S   :w  a  [        S5      eUR                  UR                  :w  a  UR	                  UR                  5      nUR                  S   n[
        R                  " U[
        R                  UR                  S9nUS:  a  XU:  -  nUS:  a  [        XU5      nXU:  -  nX,   nX   nX:  n[        U5      n[        XSSX/5      ) nX   nX   nX   n[        XX5      n[        U5      nXU4$ )a[  
Filters the predicted masks by selecting only the ones that meets several criteria. The first criterion being
that the iou scores needs to be greater than `pred_iou_thresh`. The second criterion is that the stability
score needs to be greater than `stability_score_thresh`. The method also converts the predicted masks to
bounding boxes and pad the predicted masks if necessary.

Args:
    masks (`torch.Tensor`):
        Input masks.
    iou_scores (`torch.Tensor`):
        List of IoU scores.
    original_size (`tuple[int,int]`):
        Size of the original image.
    cropped_box_image (`torch.Tensor`):
        The cropped image.
    pred_iou_thresh (`float`, *optional*, defaults to 0.88):
        The threshold for the iou scores.
    stability_score_thresh (`float`, *optional*, defaults to 0.95):
        The threshold for the stability score.
    mask_threshold (`float`, *optional*, defaults to 0):
        The threshold for the predicted masks.
    stability_score_offset (`float`, *optional*, defaults to 1):
        The offset for the stability score used in the `_compute_stability_score` method.
r      z3masks and iou_scores must have the same batch size.dtyperf           )flattenr]   r[   rf   r{   r|   onesbool_compute_stability_score_batched_mask_to_box_is_box_near_crop_edge
_pad_masks_mask_to_rle)r;   masks
iou_scoresoriginal_sizecropped_box_imagepred_iou_threshstability_score_threshmask_thresholdstability_score_offsetoriginal_heightoriginal_width
batch_size	keep_maskstability_scoresscoresconverted_boxess                   r-   filter_masksSamImageProcessor.filter_masks-  sV   F +8'''1-
a#;;q>Z--a00RSS<<:,,,#u||4J[[^
JJzELLQ	S !/%ABI "C'7Oef!8N%NOI&  &.u5+A~0W
 
	 " )45_UU#o--r,   c                    Uc  U R                   OUnUS   US   4n[        U[        R                  [        R
                  45      (       a  UR                  5       n[        U[        R                  [        R
                  45      (       a  UR                  5       n/ n[        U5       H  u  p[        X   [        R
                  5      (       a  [        R                  " X   5      X'   O,[        X   [        R                  5      (       d  [        S5      e[        R                  " X   USSS9nUSSX9   S	   2SX9   S
   24   n[        R                  " XSSS9nU(       a  X:  nUR                  U5        M     U$ )a  
Remove padding and upscale masks to the original image size.

Args:
    masks (`Union[List[torch.Tensor], List[np.ndarray]]`):
        Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format.
    original_sizes (`Union[torch.Tensor, List[Tuple[int,int]]]`):
        The original sizes of each image before it was resized to the model's expected input shape, in (height,
        width) format.
    reshaped_input_sizes (`Union[torch.Tensor, List[Tuple[int,int]]]`):
        The size of each image as it is fed to the model, in (height, width) format. Used to remove padding.
    mask_threshold (`float`, *optional*, defaults to 0.0):
        The threshold to use for binarizing the masks.
    binarize (`bool`, *optional*, defaults to `True`):
        Whether to binarize the masks.
    pad_size (`int`, *optional*, defaults to `self.pad_size`):
        The target size the images were padded to before being passed to the model. If None, the target size is
        assumed to be the processor's `pad_size`.
Returns:
    (`torch.Tensor`): Batched masks in batch_size, num_channels, height, width) format, where (height, width)
    is given by original_size.
Nr5   r6   zIInput masks should be a list of `torch.tensors` or a list of `np.ndarray`bilinearF)modealign_corners.r   r   )ro   rG   r|   Tensornpndarraytolist	enumerate
from_numpy	TypeErrorFinterpolateappend)r;   r   ri   rj   r   binarizero   target_image_sizeoutput_masksir   interpolated_masks               r-   post_process_masks$SamImageProcessor.post_process_masks{  s]   > %-$44==(%h/'1BCnu||RZZ&@AA+224N*U\\2::,FGG#7#>#>#@  ). 9A%(BJJ// ++EH5%,,77 kll !eh8IPZjo p 1#7S9M9PQR9S7SUqWkWnopWqUq2q r !.?U_ot u$5$F! 12 !: r,   c                     [        XX45      $ )a  
Post processes mask that are generated by calling the Non Maximum Suppression algorithm on the predicted masks.

Args:
    all_masks (`torch.Tensor`):
        List of all predicted segmentation masks
    all_scores (`torch.Tensor`):
        List of all predicted iou scores
    all_boxes (`torch.Tensor`):
        List of all bounding boxes of the predicted masks
    crops_nms_thresh (`float`):
        Threshold for NMS (Non Maximum Suppression) algorithm.
)!_post_process_for_mask_generation)r;   	all_masks
all_scores	all_boxescrops_nms_threshs        r-    post_process_for_mask_generation2SamImageProcessor.post_process_for_mask_generation  s     1	ddr,   r!   N)NN)r   g?    r   N)g)\(?gffffff?r   r   )r   TN)4r"   r#   r$   r%   r   valid_kwargsr   BILINEARrX   r   r   r   r   rW   r   r   rn   rm   rc   r   ro   r    r   r:   r   r   r   rB   r)   r   r'   r(   r   rF   tuplerS   r^   r   r   r   r   r   listfloatru   r   r   r   r   r   r+   __classcell__)r<   s   @r-   r0   r0   <   sy   *L!**H&J$ID!D%IIJLNF.H"S1M#(?!@ #  04
G
G &,
G 01	
G
 

G 
G MQPT#&c3h7(BTI Xc]*T#s(^;hFM
 
 *	uS#X 	c 	 OS	

 
 L	
 

 
L 590C0C &,0C 	0C
 ,0C j(4/0C c>)*T10C 
0Cd-6^$-6 -6 	-6
 L-6 -6 -6 -6 -6 -6 DK'$.-6 4;&--6 t-6 T/-6 +-6" 
tN#T%S/%::	;#-6f )&(;<+//I</I 	/I
 /I t/I )-S	D(8/I (/In # L.f 3je er,   r0   r   rV   r   r   c                 
   XU-   :  R                  S[        R                  S9R                  S[        R                  S9nXU-
  :  R                  S[        R                  S9R                  S[        R                  S9nX4-  nU$ )Nr   )sumr|   int16int32)r   r   r   intersectionsunionsr   s         r-   r   r     s     
#99	:??%++?VZZ[]ejepepZq  (>>?DDRu{{D[__`bjojuju_vF$-r,   c                 .   [         R                  " U 5      S:X  a2  [         R                  " / U R                  SS QSP7SU R                  06$ U R                  nUSS u  p#[         R
                  " U SS9u  pEU[         R                  " X$R                  S9SSS24   -  n[         R
                  " USS9u  puXbU) -  -   n[         R                  " USS9u  p[         R
                  " U SS9u  pU	[         R                  " X9R                  S9SSS24   -  n
[         R
                  " U
SS9u  pXU	) -  -   n
[         R                  " U
SS9u  pX:  Xx:  -  n[         R                  " XX/SS9nX) R                  S5      -  nUR                  " / USS QSP76 nU$ )	a   
Computes the bounding boxes around the given input masks. The bounding boxes are in the XYXY format which
corresponds the following required indices:
    - LEFT: left hand side of the bounding box
    - TOP: top of the bounding box
    - RIGHT: right of the bounding box
    - BOTTOM: bottom of the bounding box

Return [0,0,0,0] for an empty mask. For input shape channel_1 x channel_2 x ... x height x width, the output shape
is channel_1 x channel_2 x ... x 4.

Args:
    - masks (`torch.Tensor` of shape `(batch, nb_mask, height, width)`)
r   NrZ      rf   r   dimrf   )r|   numelzerosr]   rf   rM   arangeminstack	unsqueezereshape)r   r]   r5   r6   	in_heightr   in_height_coordsbottom_edges	top_edgesin_widthin_width_coordsright_edges
left_edgesempty_filterouts                  r-   r   r     s   " {{5Q{{EEKK,EaEEE KKE"#JMF 99U+LI 5<<?O?O#PQUWXQX#YYii 0b9OL'YJ*??99-26LI ))Er*KHeOO!LTSTW!UUOYYB7NK%((;;OIIo26MJ  ,1IJL
++zkHb
QC
))"-
-C ++
%uSbz
%1
%CJr,   c                 N   [         R                  " U[         R                  U R                  S9n[         R                  " U[         R                  U R                  S9nUu  pg  n[         R                  " XgXg//U R                  S9n	[        U R                  5      S:X  a  U	R                  S5      n	X	-   R                  5       n [         R                  " XSSS24   USS9n
[         R                  " XSSS24   USS9n[         R                  " X) 5      n
[         R                  " U
SS9$ )	zNFilter masks at the edge of a crop, but not at the edge of the original image.r   r   r   r   Nr   )atolrtolr   )r|   	as_tensorr   rf   tensorlenr]   r   iscloselogical_andany)boxescrop_boxorig_boxr  crop_box_torchorig_box_torchlefttopr   offsetnear_crop_edgenear_image_edges               r-   r   r     s    __XU[[VN__XU[[VNODq!\\Dt125<<HF
5;;1!!!$^""$E]]5q*ASTUNmmE$'+BTUVO&&~7GHN99^++r,   r  orig_height
orig_widthc                     Uu  pEpgUS:X  a  US:X  a  Xc:X  a  Xr:X  a  U $ X6U-
  -
  X'U-
  -
  pXHU-
  XYU-
  4n
[         R                  R                  R                  X
SS9$ )Nr   )value)r|   nnr	   r   )r   r  r  r  r  r  rightbottompad_xpad_yr   s              r-   r   r     so    'DuqySAX%"5&:O.|0L5sCK
0C88""5Q"77r,   r   r   r   r   r   r@   c                 n   [        U [        5      (       a  [        S5      eU R                  SS n/ n[	        US-   5       H-  n[        XEU-  -  5      n	UR                  [        U	5      5        M/     [        X#U5      u  p[        XX{X5      u  p[        R                  " U
5      n
U
R                  5       n
[        R                  " U5      nUR                  S5      R                  SSSS5      n[        R                  " U5      n[        R                   " USS2SS2SS2S4   [        R"                  S9nXX4$ )	a  
Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer.

Args:
    image (`torch.Tensor`):
        Image to generate crops for.
    target_size (`int`):
        Size of the smallest crop.
    crop_n_layers (`int`, *optional*):
        If `crops_n_layers>0`, mask prediction will be run again on crops of the image. Sets the number of layers
        to run, where each layer has 2**i_layer number of image crops.
    overlap_ratio (`int`, *optional*):
        Sets the degree to which crops overlap. In the first crop layer, crops will overlap by this fraction of the
        image length. Later layers with more crops scale down this overlap.
    points_per_crop (`int`, *optional*):
        Number of points to sample per crop.
    crop_n_points_downscale_factor (`int`, *optional*):
        The number of points-per-side sampled in layer n is scaled down by crop_n_points_downscale_factor**n.
z.Only one image is allowed for crop generation.rZ   Nr   r   rk   r   r   )rG   r   r[   r]   ranger)   r   _build_point_grid_generate_per_layer_crops_generate_crop_imagesr|   r  r   r   r   permute	ones_liker}   )rU   r   r   r   r   r   r   points_gridr   n_pointsr   
layer_idxsr   point_grid_per_cropr   s                  r-   r   r     s   8 %IJJKK$MK=1$%!*KLM,X67 & 7}UbcJ*?;K+'N j)J!!#Jkk"56O%//2::1aAFO[[0N???1aA:#>ekkRLDDr,   c           	         / / pCUu  pV[        XV5      nUR                  SSXe/5        UR                  S5        [        U 5       GH  nSUS-   -  n	[        X-  SU	-  -  5      n
[        [        R
                  " XS-
  -  U-   U	-  5      5      n[        [        R
                  " XS-
  -  U-   U	-  5      5      n[        U	5       Vs/ s H  n[        X-
  U-  5      PM     nn[        U	5       Vs/ s H  n[        X-
  U-  5      PM     nn[        X5       HK  u  nnUU[        UU-   U5      [        UU-   U5      /nUR                  U5        UR                  US-   5        MM     GM"     X44$ s  snf s  snf )aU  
Generates 2 ** (layers idx + 1) crops for each crop_n_layers. Crops are in the XYWH format : The XYWH format
consists of the following required indices:
    - X: X coordinate of the top left of the bounding box
    - Y: Y coordinate of the top left of the bounding box
    - W: width of the bounding box
    - H: height of the bounding box
r   rk   r   )r   r   r'  r)   mathceilr   )r   r   r   r   r/  	im_heightim_width
short_sidei_layern_crops_per_sideoverlap
crop_widthcrop_heightr   crop_box_x0crop_box_y0r  r  boxs                      r-   r)  r)  N  s     
'IY)J q!X12a'1-m0A8H4HIJG!/C$Dx$OSc#cde
$))W10D%E	%QUe$efg@EFV@WX@W1sJ0A56@WXAFGWAXYAXAsK1Q67AXY :ID#c$"3X>C+DUW`@abCc"gk* ; ( !! YYs   E*,E/
n_per_sidec                    SSU -  -  n[         R                  " USU-
  U 5      n[         R                  " USSS24   U S45      n[         R                  " USS2S4   SU 45      n[         R                  " X4/SS9R	                  SS5      nU$ )z;Generates a 2D grid of points evenly spaced in [0,1]x[0,1].r   rk   Nr   r   )r|   linspacetiler   r   )r?  r  points_one_sidepoints_xpoints_ypointss         r-   r(  r(  p  s    !j.!FnnVQZDOzz/$'2ZODHzz/!T'2Q
ODH[[(-26>>r1EFMr,   c                 N   / n/ n[        U 5       H  u  pU
u  ppUSS2X2X24   nUR                  U5        UR                  SS n[        R                  " U5      R                  SS9R                  S5      nX#U	      U-  n[        UUU5      nUR                  U5        M     Xx4$ )z
Takes as an input bounding boxes that are used to crop the image. Based in the crops, the corresponding points are
also passed.
NrZ   )r   )dimsr   )r   r   r]   r|   r  flipr   _normalize_coordinates)r   rU   r-  r/  r   r   rd   r   total_points_per_cropr   r  r  r  r"  r#  
cropped_imcropped_im_sizepoints_scalerF  normalized_pointss                       r-   r*  r*  z  s     N ,#+ 51cj$*45
j)$**23/||O499t9DNNqQ]+l:2;V$$%67 - 00r,   coordsr   c                 B   Uu  pEU S-  [        XE5      -  nXF-  XV-  p[        US-   5      n[        US-   5      n[        U5      R                  5       nU(       a  UR	                  SSS5      nUS   X-  -  US'   US   Xt-  -  US'   U(       a  UR	                  SS5      nU$ )zw
Expects a numpy array of length 2 in the final dimension. Requires the original image size in (height, width)
format.
rK   rL   r   rk   ).r   ).r   r   )rM   r)   r   r   r   )	r   rP  r   is_bounding_box
old_height	old_widthrP   
new_height	new_widths	            r-   rJ  rJ    s     *J#J ::E&.	0A	IO$IZ#%&Jf##%FAq)F^y'<=F6NF^z'>?F6NA&Mr,   rlec                     U S   u  p[         R                  " X-  [        S9nSnSnU S    H  nXSXDU-   & XF-  nU(       + nM     UR                  X!5      nUR	                  SS5      $ )z/Compute a binary mask from an uncompressed RLE.rW   r   r   Fcountsr   )r|   emptyr   r   	transpose)rW  r5   r6   maskidxparitycounts          r-   _rle_to_maskr`    sr    KMF;;v~T2D
CFX"(S;  <<&D>>!Qr,   c                    [        UR                  5       U[        R                  " UR                  S   5      US9nX   nU Vs/ s H  oPU   PM	     n nX$   nU  Vs/ s H  n[        U5      PM     nnXqX4$ s  snf s  snf )a  
Perform NMS (Non Maximum Suppression) on the outputs.

Args:
        rle_masks (`torch.Tensor`):
            binary masks in the RLE format
        iou_scores (`torch.Tensor` of shape (nb_masks, 1)):
            iou_scores predicted by the model
        mask_boxes (`torch.Tensor`):
            The bounding boxes corresponding to segmentation masks
        amg_crops_nms_thresh (`float`, *optional*, defaults to 0.7):
            NMS threshold.
r   )r  r   idxsiou_threshold)r
   r   r|   r   r]   r`  )	rle_masksr   
mask_boxesamg_crops_nms_threshkeep_by_nmsr   rW  r   s           r-   r   r     s      [[))!,-*	K (J'23{!1{I3(J*34)3\#)E4i33	 44s   A7A<c                 ~   U R                   u  pnU R                  SSS5      R                  S5      n U SS2SS24   U SS2SS24   -  nUR                  5       n/ n[	        U5       H  nXUSS2S4   U:H  S4   S-   n[        U5      S:X  a>  XS4   S:X  a  UR                  X#/X#-  /S.5        OUR                  X#/SX#-  /S.5        Mc  USS USS -
  n	XS4   S:X  a  / OS/n
XS   R                  5       /U	R                  5       -   X#-  US   R                  5       -
  /-   -  n
UR                  X#/U
S.5        M     U$ )zV
Encodes masks the run-length encoding (RLE), in the format expected by pycoco tools.
r   rk   r   Nr   )rW   rY  )	r]   r+  r   nonzeror'  r  r   itemr   )
input_maskr   r5   r6   diffchange_indicesr	  r   cur_idxsbtw_idxsrY  s              r-   r   r     sr   
 !+ 0 0J##Aq!,44Q7J aez!SbS&11D\\^N C:!A"6!";Q">?!Cx=A Q$1$

VO?OPQ

VO6>?RSTAB<(3B-/!Q$'1,1#A;##%&)::fnxXZ|O`O`Ob>b=ccc

VOv>?  Jr,   )r   rV   )g      4@)r   r   r   r   r   )F)gffffff?)rk  rV   )Cr&   r2  collections.abcr   rt   r   	itertoolsr   typingr   r   r   numpyr   r|   torch.nnr	   r   torchvision.ops.boxesr
   torchvision.transforms.v2tvFimage_processing_backendsr   image_processing_utilsr   r   image_transformsr   r   image_utilsr   r   r   r   r   r   processing_utilsr   r   utilsr   r   r   PILr   r0   r   r)   r   r   r   r   r   r   r   r)  r   r(  r*  rJ  r'   r(   r`  r   r   __all__r!   r,   r-   <module>r     s   %  $   ' '   $ - 7 ; A E  5 D D 
"l% 
" Ae* Ae AeHN E cf .b,$8S	 8 8 8 %"$782E2E 2E 	2E
 4Z2E %)I$42E 4S	?DI%&2Ej"D# %,,  _c14 ]b#ll;@c?
\\8 d38n    4:> 
r,   