
    sgRo                       d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZ ddlZddlmZmZmZ ddlmZmZmZmZmZmZmZmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ dd	l0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:  e7       r
ddl;Z;dd
l;m<Z<  e9       rddl=Z= e4       rddl>Z?ddl@Z? e:j                  eB      ZCe"j                  e"j                  fZFdJdeeGeGf   fdZH	 dJdej                  deGdeGdeeeJe$f      deeGeGf   f
dZK	 	 dKdej                  deeGeeGeGf   e
eG   f   deeG   deeeJe$f      deeGeGf   f
dZLdefdZMdJdej                  deeG   dej                  fdZNdedeeGeGf   defdZOde	e   de
e   fdZP	 dJde
ej                     deeeJe$f      de
eG   fdZQ	 dJd ej                  d!eeGeGf   deeeJe$f      dej                  fd"ZRd#eGd$eGdej                  fd%ZS	 	 dLd&eTdeee$eJf      fd'ZUd(ej                  dej                  fd)ZV	 	 dMd ej                  d*ed+eeJej                  f   d,eTdee$eJf   defd-ZX	 dNd(ej                  d.ed/efd0ZYd1ej                  d/eeGeGf   d2eGdej                  fd3ZZd4ej                  deej                  ej                  f   fd5Z[	 dOd6ej                  d(ej                  d7ej                  d8eeGeGf   d/eeGeGf   d9edefd:Z\d;e&j                  fdeeJef   d<eeGeGf   d/eeGeGf   d=e^d>e&f
d?Z_d@ Z`dA ZadB ZbdPdCZc	 	 	 	 dQdDe^dEe^dFeeeG      d/eeGeGf   fdGZd G dH dIe      ZedIgZfy)RzImage processor class for DETR.    N)defaultdict)	AnyCallableDictIterableListOptionalSetTupleUnion   )BaseImageProcessorBatchFeatureget_size_dict)	PaddingModecenter_to_corners_formatcorners_to_center_format	id_to_rgbpadrescaleresize	rgb_to_idto_channel_dimension_format)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDAnnotationFormatAnnotationTypeChannelDimension
ImageInputPILImageResamplingget_image_sizeinfer_channel_dimension_formatis_scaled_imagemake_list_of_imagesto_numpy_arrayvalid_imagesvalidate_annotationsvalidate_kwargsvalidate_preprocess_arguments)

TensorTypeis_flax_availableis_jax_tensoris_scipy_availableis_tf_availableis_tf_tensoris_torch_availableis_torch_tensoris_vision_availablelogging)nnreturnc                    | \  }}d}|St        t        ||f            }t        t        ||f            }||z  |z  |kD  r||z  |z  }t        t	        |            }||k  r||k(  s
||k  r||k(  r||}	}||	fS ||k  r0|}	||t        ||z  |z        }||	fS t        ||z  |z        }||	fS |}||t        ||z  |z        }	||	fS t        ||z  |z        }	||	fS )aC  
    Computes the output image size given the input image size and the desired output size.

    Args:
        image_size (`Tuple[int, int]`):
            The input image size.
        size (`int`):
            The desired output size.
        max_size (`int`, *optional*):
            The maximum allowed output size.
    N)floatminmaxintround)

image_sizesizemax_sizeheightwidthraw_sizemin_original_sizemax_original_sizeohows
             a/var/www/html/venv/lib/python3.12/site-packages/transformers/models/detr/image_processing_detr.pyget_size_with_aspect_ratiorG   X   sF    MFEH!#vuo"67!#vuo"670047(B"336GGHuX'D%FdNETMB 8O 
H$8X&./B 8O TF]U*+B 8O H$8X%./B 8O TE\F*+B8O    input_image
max_height	max_widthinput_data_formatc                     t        | |      }|\  }}||z  }||z  }t        ||      }	t        ||	z        }
t        ||	z        }|
|fS )a  
    Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio.
    Important, even if image_height < max_height and image_width < max_width, the image will be resized
    to at least one of the edges be equal to max_height or max_width.

    For example:
        - input_size: (100, 200), max_height: 50, max_width: 50 -> output_size: (25, 50)
        - input_size: (100, 200), max_height: 200, max_width: 500 -> output_size: (200, 400)

    Args:
        input_image (`np.ndarray`):
            The image to resize.
        max_height (`int`):
            The maximum allowed height.
        max_width (`int`):
            The maximum allowed width.
        input_data_format (`ChannelDimension` or `str`, *optional*):
            The channel dimension format of the input image. If not provided, it will be inferred from the input image.
    )r!   r8   r:   )rI   rJ   rK   rL   r<   r?   r@   height_scalewidth_scale	min_scale
new_height	new_widths               rF   #get_image_size_for_max_height_widthrS      sc    2  ->?JMFE&Le#KL+.IVi'(JEI%&Iy  rH   r=   r>   c                 d    t        | |      }t        |t        t        f      r|S t	        |||      S )a   
    Computes the output image size given the input image size and the desired output size. If the desired output size
    is a tuple or list, the output image size is returned as is. If the desired output size is an integer, the output
    image size is computed by keeping the aspect ratio of the input image size.

    Args:
        input_image (`np.ndarray`):
            The image to resize.
        size (`int` or `Tuple[int, int]` or `List[int]`):
            The desired output size.
        max_size (`int`, *optional*):
            The maximum allowed output size.
        input_data_format (`ChannelDimension` or `str`, *optional*):
            The channel dimension format of the input image. If not provided, it will be inferred from the input image.
    )r!   
isinstancelisttuplerG   )rI   r=   r>   rL   r<   s        rF   get_resize_output_image_sizerX      s3    *  ->?J$u&%j$AArH   c                 f   t        | t        j                        rt        j                  S t	               rt        |       rddl}|j                  S t               rt        |       rddl
}|j                  S t               rt        |       rddlm} |j                  S t!        dt#        |              )z
    Returns a function that converts a numpy array to the framework of the input array.

    Args:
        arr (`np.ndarray`): The array to convert.
    r   NzCannot convert arrays of type )rU   npndarrayarrayr.   r/   
tensorflowconvert_to_tensorr0   r1   torchtensorr+   r,   	jax.numpynumpy
ValueErrortype)arrtfr_   jnps       rF   get_numpy_to_framework_fnrh      s     #rzz"xx\#.### 4||}S1yy
5d3i[A
BBrH   re   axisc                 n    || j                         S 	 | j                  |      S # t        $ r | cY S w xY w)zF
    Squeezes an array, but only if the axis specified has dim 1.
    ri   )squeezerc   )re   ri   s     rF   safe_squeezerm      s?     |{{}{{{%% 
s   & 44
annotationr<   c                     |\  }}i }| j                         D ]N  \  }}|dk(  r?|}t        |      }|t        j                  ||||gt        j                        z  }|||<   J|||<   P |S )Nboxesdtype)itemsr   rZ   asarrayfloat32)rn   r<   image_heightimage_widthnorm_annotationkeyvaluerp   s           rF   normalize_annotationr{      s     *L+O &&( )
U'>E,U3ERZZlK V^`^h^hiiE#(OC #(OC ) rH   valuesc                 J    t        |  D cg c]  }t        |       c}S c c}w )zO
    Return the maximum value across all indices of an iterable of values.
    )zipr9   )r|   values_is     rF   max_across_indicesr      s      +.v,7hCM777s    imagesc                 J   |t        | d         }|t        j                  k(  r+t        | D cg c]  }|j                   c}      \  }}}||fS |t        j
                  k(  r+t        | D cg c]  }|j                   c}      \  }}}||fS t        d|       c c}w c c}w )zH
    Get the maximum height and width across all images in a batch.
    r   z"Invalid channel dimension format: )r"   r   FIRSTr   shapeLASTrc   )r   rL   img_rJ   rK   s         rF   get_max_height_widthr      s      :6!9E,222#5F6SSsyy6S#T :y
 	""	 
.33	3#5F6SSsyy6S#T 
Iq 	"" =>O=PQRR	 7T6Ss   B+B imageoutput_sizec                     t        | |      \  }}t        j                  |t        j                        }d|d|d|f<   |S )a  
    Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding.

    Args:
        image (`np.ndarray`):
            Image to make the pixel mask for.
        output_size (`Tuple[int, int]`):
            Output size of the mask.
    channel_dimrq      N)r!   rZ   zerosint64)r   r   rL   input_heightinput_widthmasks         rF   make_pixel_maskr     sF     !/uBS TL+88Krxx0D()D,	$%KrH   r?   r@   c                    	 ddl m} g }| D ]  }|j                  |||      }|j	                  |      }t        |j                        dk  r|d   }t        j                  |t        j                        }t        j                  |d      }|j                  |        |rt        j                  |d      }|S t        j                  d||ft        j                        }|S # t        $ r t        d      w xY w)	a1  
    Convert a COCO polygon annotation to a mask.

    Args:
        segmentations (`List[List[float]]`):
            List of polygons, each polygon represented by a list of x-y coordinates.
        height (`int`):
            Height of the mask.
        width (`int`):
            Width of the mask.
    r   r   z1Pycocotools is not installed in your environment.r   ).Nrq      rk   )pycocotoolsr   ImportErrorfrPyObjectsdecodelenr   rZ   rt   uint8anyappendstackr   )segmentationsr?   r@   	coco_maskmaskspolygonsrlesr   s           rF   convert_coco_poly_to_maskr      s    O1 E! $$Xvu=%tzz?Q	?Dzz$bhh/vvd#T Q' L !VU+288<L#  OMNNOs   C$ $C9return_segmentation_masksc                    t        | |      \  }}|d   }t        j                  |gt        j                        }|d   }|D cg c]  }d|vs	|d   dk(  s| }}|D cg c]  }|d   	 }	}t        j                  |	t        j                        }	t        j                  |D cg c]  }|d   	 c}t        j                        }
t        j                  |D cg c]  }d|v r|d   nd c}t        j                        }|D cg c]  }|d	   	 }}t        j                  |t        j                        j                  d
d      }|ddddfxx   |ddddf   z  cc<   |dddddf   j                  d|      |dddddf<   |dddddf   j                  d|      |dddddf<   |dddf   |dddf   kD  |dddf   |dddf   kD  z  }i }||d<   |	|   |d<   ||   |d<   |
|   |d<   ||   |d<   t        j                  t        |      t        |      gt        j                        |d<   |rld|d   v re|D cg c]  }|d   	 }}t        j                  |t        j                        }||   }|j                  d   }|r|j                  d      n|}||d<   |r'|D cg c]  }|d   	 }}t        |||      }||   |d<   |S c c}w c c}w c c}w c c}w c c}w c c}w c c}w )zM
    Convert the target in COCO format into the format expected by DETR.
    r   image_idrq   annotationsiscrowdr   category_idareabbox   Nr   )r8   r9   r   r   class_labelsrp   	orig_size	keypoints)r   r   segmentationr   )
r!   rZ   rt   r   ru   reshapeclipr:   r   r   )r   targetr   rL   rv   rw   r   r   objclassesr   r   rp   keep
new_targetr   num_keypointssegmentation_masksr   s                      rF   !prepare_coco_detection_annotationr   C  s    !/uBS TL+j!Hzz8*BHH5H 'K"-]3#1EY[\I\3]K]-89cs=!9G9jj1G ::k:ss6{:"**MDjj{[I,<#i.!C[ceckcklG$/0SS[0E0JJuBJJ/77A>E	!QR%LE!RaR%L L1add7^((QK(@E!QTT'N1add7^((QL(AE!QTT'N!Q$K%1+%%1+ad*CDDJ%Jz!(J~+JwdJv#DMJy jj#l*;S=M)NVXV^V^_J{{k!n41<=#S%=	=JJy

;	dO	!*2?I%%g.Y	"+
; =HIcc.1II)*<lKX#Dk
7S ^9 ;[0$ > Js0   KK!K'K$K)
K.K35K8r   c                 |   | j                   dk(  rt        j                  d      S | j                  dd \  }}t        j                  d|t        j
                        }t        j                  d|t        j
                        }t        j                  ||d      \  }}| t        j                  |d      z  }|j                  |j                  d   d	      j                  d	      }t        j                  j                  |t        j                  | t               
      }|j                  d      }|j                  |j                  d   d	      j                  d	      }| t        j                  |d      z  }|j                  |j                  d   d	      j                  d	      }	t        j                  j                  |t        j                  | t               
      }|j                  d      }
|
j                  |
j                  d   d	      j                  d	      }
t        j                  ||
||	gd      S )a  
    Compute the bounding boxes around the provided panoptic segmentation masks.

    Args:
        masks: masks in format `[number_masks, height, width]` where N is the number of masks

    Returns:
        boxes: bounding boxes in format `[number_masks, 4]` in xyxy format
    r   )r   r   Nrq   ij)indexingrk   r   r   g    חA)
fill_valuer   )r=   rZ   r   r   arangeru   meshgridexpand_dimsr   r9   mar\   boolfilledr8   r   )r   hwyxx_maskx_maxx_miny_masky_maxy_mins              rF   masks_to_boxesr     s    zzQxx;;rsDAq
		!Qbjj)A
		!Qbjj)A;;q!d+DAqR^^AA..FNN6<<?B/33B7E
F288E#>!?@AHHH$EMM%++a."-11"5ER^^AA..FNN6<<?B/33B7E
F288E#>!?@AHHH$EMM%++a."-11"5E88UE5%0!44rH   r   
masks_pathreturn_masksc                 z   t        | |      \  }}t        j                  |      |d   z  }i }t        j                  d|v r|d   n|d   gt        j
                        |d<   t        j                  ||gt        j
                        |d<   t        j                  ||gt        j
                        |d<   d|v rkt        j                  t        j                  j                  |      t        j                        }	t        |	      }	t        j                  |d   D 
cg c]  }
|
d   	 c}
      }|	|d	d	d	d	f   k(  }	|	j                  t        j                        }	|r|	|d
<   t        |	      |d<   t        j                  |d   D 
cg c]  }
|
d   	 c}
t        j
                        |d<   t        j                  |d   D 
cg c]  }
|
d   	 c}
t        j
                        |d<   t        j                  |d   D 
cg c]  }
|
d   	 c}
t        j                        |d<   |S c c}
w c c}
w c c}
w c c}
w )z6
    Prepare a coco panoptic annotation for DETR.
    r   	file_namer   idrq   r=   r   segments_infoNr   rp   r   r   r   r   )r!   pathlibPathrZ   rt   r   PILImageopenuint32r   r\   astyper   r   ru   )r   r   r   r   rL   rv   rw   annotation_pathr   r   segment_infoidss               rF    prepare_coco_panoptic_annotationr     s    !/uBS TL+ll:.1DDOJZZzV?S
);Y_`dYe(fnpnvnvwJz\;$?rxxPJv jj,)DBHHUJ{& 

399>>/:"))L% hhvo?VW|T*WXQd]++RXX&"'Jw,U3
7%'XX=CO=TU\\-(U]_]e]e&

>" !#

9?9PQ\)$QY[YaYa!

9  ZZ6<_6MNl\&!NVXV`V`

6 ! X V R Os   H)	H.H3H8
input_sizetarget_sizec                    |\  }}|\  }}t         j                  j                  | j                  dd      d      }	|	j                  d   dk(  r(t        j                  ||ft
        j                        }	n!|	j                  d      j                  ||      }	|r'|j                         D ]  }
|
D ]  }|
d   |	|	|k(  <     t        |	      }t        |||ft        j                        }|S )Nr   r   r   rq   resample)scipyspecialsoftmax	transposer   rZ   r   r   argmaxr   r|   r   r   r    NEAREST)r   r   r   stuff_equiv_classesdeduplicater   r   final_hfinal_wm_idequiveq_idseg_imgs                rF   get_segmentation_imager     s     DAq"GW==  A!6;Dzz"~xxAbhh/{{2&&q!,(//1 	/E /&+AhTU]#/	/ oGWw0;M;U;UVGNrH   r   	n_classesc                     |\  }}| j                  t        j                        }|j                  ||d      }t	        |      }t        |      D cg c]  }||k(  j                          }}|S c c}w )Nr   )r   rZ   r   r   r   rangesum)	r   r   r   r   r   
np_seg_imgr   ir   s	            rF   get_mask_arear     si    "GW)J##GWa8JZ D',Y'78!TQYOO8D8K 9s   A.logitsc                     t         j                  j                  | d      }|j                  dd      }t	        j
                  ||d      }|j                  d      |j                  d      }}||fS )Nr   rk   T)keepdims)r   r   r   r   rZ   take_along_axisrl   )r  probslabelsscoress       rF   %score_labels_from_class_probabilitiesr    sf    MM!!&r!2E\\"t\,FvB7F^^B');FF6>rH   
out_logitsrp   processed_sizeis_thing_mapc           
         t        |       \  }}|| j                  d   dz
  k7  ||kD  z  }	||	   }
||	   }t        ||	         }t        |      t        |      k7  rt	        d      ||	   }t        |dddf   |t        j                        }t        |d      }|j                  \  }}}|j                  |d      }t        t              }t        |      D ]  \  }}||   r||   j                  |       ! t        ||||d      }t        ||t        |
            }|j!                         d	kD  rt#        j$                  |D cg c]  }|d
k  	 c}t&              }|j)                         r||    }|
|    }
||    }t        |||f||d      }t        ||t        |
            }t#        j$                  |D cg c]  }|d
k  	 c}t&              }|j)                         rxn%t#        j*                  dt"        j,                        }t        t/        ||            D cg c]  \  }\  }}|||   t1        |      |d }}}}~t3        j4                         5 }t6        j8                  j;                  |      j=                  |d       |j?                         |d}ddd       |S c c}w c c}w c c}}}w # 1 sw Y   S xY w)a  
    Converts the output of [`DetrForSegmentation`] into panoptic segmentation predictions for a single sample.

    Args:
        out_logits (`torch.Tensor`):
            The logits for this sample.
        masks (`torch.Tensor`):
            The predicted segmentation masks for this sample.
        boxes (`torch.Tensor`):
            The prediced bounding boxes for this sample. The boxes are in the normalized format `(center_x, center_y,
            width, height)` and values between `[0, 1]`, relative to the size the image (disregarding padding).
        processed_size (`Tuple[int, int]`):
            The processed size of the image `(height, width)`, as returned by the preprocessing step i.e. the size
            after data augmentation but before batching.
        target_size (`Tuple[int, int]`):
            The target size of the image, `(height, width)` corresponding to the requested final size of the
            prediction.
        is_thing_map (`Dict`):
            A dictionary mapping class indices to a boolean value indicating whether the class is a thing or not.
        threshold (`float`, *optional*, defaults to 0.85):
            The threshold used to binarize the segmentation masks.
    r   r   &Not as many boxes as there are classesNr   T)r   )r   r   r   rq   )r   r   r   isthingr   r   PNGformat
png_stringr   ) r  r   r   r   rc   r   r    BILINEARrm   r   r   rV   	enumerater   r   r   r=   rZ   r\   r   r   onesr   r~   r:   ioBytesIOr   r   	fromarraysavegetvalue)r	  r   rp   r
  r   r  	thresholdr  r  r   
cur_scorescur_classes	cur_boxes	cur_masksbr   r   r   klabelr   r   afiltered_smallr   catr   outpredictionss                                rF   post_process_panoptic_sampler*    s   @ ;:FNFFj&&r*Q..6I3EFDJ,K(t5I
9~[))ABBdIyD)>DVD_D_`IY*IooGAq! !!!R(I%d+k* 15E"&--a01 %YM`nrsGNc*oND A4"8a16"8E  "!>/2I#^O4J%~o6K,YAM`nrsG +ZQDXXt&<!qAv&<DIN   " ggfBHH5 %Sd%;< AxQ \#.s3xQRSM  		 U		G$))#e)<%(\\^mTU + #9 '=U s   J*:J/# J4AJ;;K      ?r   r  r   c                 (   t        d t        ||      D              }|\  }}i }||d<   | j                         D ]  \  }	}
|	dk(  r4|
}|t        j                  ||||gt        j
                        z  }||d<   ?|	dk(  r|
}|||z  z  }||d<   T|	dk(  ri|
dddf   }t        j                  |D cg c]  }t        |||       c}      }|j                  t        j
                        }|ddd	f   |kD  }||d<   |	dk(  r||d<   |
||	<    |S c c}w )
ax  
    Resizes an annotation to a target size.

    Args:
        annotation (`Dict[str, Any]`):
            The annotation dictionary.
        orig_size (`Tuple[int, int]`):
            The original size of the input image.
        target_size (`Tuple[int, int]`):
            The target size of the image, as returned by the preprocessing `resize` step.
        threshold (`float`, *optional*, defaults to 0.5):
            The threshold used to binarize the segmentation masks.
        resample (`PILImageResampling`, defaults to `PILImageResampling.NEAREST`):
            The resampling filter to use when resizing the masks.
    c              3   P   K   | ]  \  }}t        |      t        |      z     y wN)r7   ).0ss_origs      rF   	<genexpr>z$resize_annotation.<locals>.<genexpr>_  s!     Y	658eFm+Ys   $&r=   rp   rq   r   r   Nr   r   )	rW   r~   rs   rZ   rt   ru   r\   r   r   )rn   r   r   r  r   ratiosratio_heightratio_widthnew_annotationry   rz   rp   scaled_boxesr   scaled_arear   r   s                    rF   resize_annotationr9  I  s?   , YSi=XYYF &L+N(N6 &&( (
U'>E 2::{L+Wc.dlnlvlv#wwL&2N7#F]D+"<=K%0N6"G^!T'NEHHW\]tfT;J]^ELL,E!Q$K)+E&+N7#F]%0N6""'N3%((  ^s   .D
c                    t        |       r| j                         } | j                         }t        j                  dg|dgg      }t        j
                  |dd |dd k7        d   dz   }|dddxx   |ddd   z  cc<   t        |      S )a  
    Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format.

    Args:
        mask (`torch.Tensor` or `numpy.array`):
            A binary mask tensor of shape `(height, width)` where 0 denotes background and 1 denotes the target
            segment_id or class_id.
    Returns:
        `List`: Run-length encoded list of the binary mask. Refer to COCO API for more information about the RLE
        format.
    r   r   Nr   r   )r1   rb   flattenrZ   concatenatewhererV   )r   pixelsrunss      rF   binary_mask_to_rler@  }  s     tzz|\\^F^^aS&1#./F88F12J&"+-.q1A5DAJ$ss)J:rH   c                     t        j                  |       }g }|D ]8  }t        j                  | |k(  dd      }t        |      }|j	                  |       : |S )a  
    Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format.

    Args:
        segmentation (`torch.Tensor` or `numpy.array`):
            A segmentation map of shape `(height, width)` where each value denotes a segment or class id.
    Returns:
        `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id.
    r   r   )r_   uniquer=  r@  r   )r   segment_idsrun_length_encodingsidxr   rles         rF   convert_segmentation_to_rlerG    s^     ,,|,K ){{<3.15 &##C()
  rH   c                     | j                   d   |j                   d   cxk(  r|j                   d   k(  st        d       t        d      |j                  |      ||kD  z  }| |   ||   ||   fS )a	  
    Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and
    `labels`.

    Args:
        masks (`torch.Tensor`):
            A tensor of shape `(num_queries, height, width)`.
        scores (`torch.Tensor`):
            A tensor of shape `(num_queries)`.
        labels (`torch.Tensor`):
            A tensor of shape `(num_queries)`.
        object_mask_threshold (`float`):
            A number between 0 and 1 used to binarize the masks.
    Raises:
        `ValueError`: Raised when the first dimension doesn't match in all input tensors.
    Returns:
        `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region
        < `object_mask_threshold`.
    r   z1mask, scores and labels must have the same shape!)r   rc   ne)r   r  r  object_mask_threshold
num_labelsto_keeps         rF   remove_low_and_no_objectsrM    sy    ( KKNfll1o@a@LMM ALMMii
#v0E'EFG>6'?F7O;;rH   c                     | |k(  }|j                         }||   |k\  j                         }|dkD  xr |dkD  }|r||z  }	|	j                         |kD  sd}||fS )Nr   F)r   item)
mask_labels
mask_probsr#  mask_thresholdoverlap_mask_area_thresholdmask_kmask_k_areaoriginal_areamask_exists
area_ratios
             rF   check_segment_validityrY    sr    AF**,K  ]n499;M/7ma&7K  =0
 #>>KrH   rR  rS  label_ids_to_fusec                    || j                   d   n|d   }|| j                   d   n|d   }t        j                  ||ft        j                  | j                        }	g }
|5t
        j                  j                  | j                  d      |dd      d   } d}| |j                  ddd      z  } | j                  d      }i }t        |j                   d         D ]}  }||   j                         }||v }t        || |||      \  }}|s/||v r||   }n|dz  }||	|<   t        ||   j                         d	      }|
j                  ||||d
       |sy|||<    |	|
fS )Nr   r   r   rr   devicebilinearFr=   modealign_cornersr      )r   label_id	was_fusedscore)r   r_   r   int32r]  r4   
functionalinterpolate	unsqueezeviewr   r   rO  rY  r;   r   )rQ  pred_scorespred_labelsrR  rS  rZ  r   r?   r@   r   segmentscurrent_segment_idrP  stuff_memory_listr#  
pred_classshould_fuserW  rT  segment_scores                       rF   compute_segmentsrs    s    %0$7Za [^F#.#6JQKNE;;ekk*J[J[\LH]]..  #+JV[ / 


  +""2q!,,J##A&K )+;$$Q'( C ^((*
 $55 5Q8S
V ..%6z%B""a'" $6L !+a."5"5"7;MOO, *!,*	 0B!*-7C: !!rH   c            (           e Zd ZdZddgZej                  ddej                  ddddddddfde	e
ef   ded	ee
ef   d
edede	eef   dede	eee   f   de	eee   f   dee   dedeee
ef      ddf fdZedee
ef   f fd       Z	 	 	 	 d?dej,                  dedee   dedee	e
ej0                  f      dee	e
ef      defdZej                  ddfdej,                  d	ee
ef   d
edee   dee	e
ef      dej,                  fdZej8                  fd
edefdZ	 	 d@dej,                  dedee	e
ef      dee	e
ef      dej,                  f
d Zd!ed"eeef   defd#Z d!ed$eeef   d%eeef   defd&Z!	 	 	 	 	 dAdej,                  d'eeef   d!eee
ef      d(e	ee"e   f   dee   dee	e
ef      d)edej,                  fd*Z#	 	 	 	 	 	 	 	 dBd+eej,                     d,ee	e$ee$   f      d(e	ee"e   f   d-ed.ee	e
e%f      dee   dee	e
ef      d)edeee
ef      de&fd/Z'dddddddddddddddejP                  ddfd+e)d,ee	e$ee$   f      dedee	e
ej0                  f      dee   d	eee
ef      dee   dee	eef      dee   dee   dee	eee   f      dee	eee   f      dee   dee	e
ef      d.ee	e%e
f      de	e
ef   dee	e
ef      deee
ef      de&f&d0Z*d1 Z+dCd2Z,dDd3Z-dEd4Z.	 dFd5ed6e	e%ee   f   fd7Z/dGd6eeeef      fd8Z0	 	 	 	 	 dHd5ed9ed:ed6eeeeef         d;ee   dee   fd<Z1	 	 	 	 	 dId5ed9ed:ed=ee2e      d6eeeeef         dee   fd>Z3 xZ4S )JDetrImageProcessora  
    Constructs a Detr image processor.

    Args:
        format (`str`, *optional*, defaults to `"coco_detection"`):
            Data format of the annotations. One of "coco_detection" or "coco_panoptic".
        do_resize (`bool`, *optional*, defaults to `True`):
            Controls whether to resize the image's `(height, width)` dimensions to the specified `size`. Can be
            overridden by the `do_resize` parameter in the `preprocess` method.
        size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`):
            Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter
            in the `preprocess` method. 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.
        do_rescale (`bool`, *optional*, defaults to `True`):
            Controls whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by the
            `do_rescale` parameter in the `preprocess` method.
        rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
            Scale factor to use if rescaling the image. Can be overridden by the `rescale_factor` parameter in the
            `preprocess` method.
        do_normalize (`bool`, *optional*, defaults to True):
            Controls whether to normalize the image. Can be overridden by the `do_normalize` parameter in the
            `preprocess` method.
        image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`):
            Mean values to use when normalizing the image. Can be a single value or a list of values, one for each
            channel. Can be overridden by the `image_mean` parameter in the `preprocess` method.
        image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`):
            Standard deviation values to use when normalizing the image. Can be a single value or a list of values, one
            for each channel. Can be overridden by the `image_std` parameter in the `preprocess` method.
        do_convert_annotations (`bool`, *optional*, defaults to `True`):
            Controls whether to convert the annotations to the format expected by the DETR model. Converts the
            bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`.
            Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method.
        do_pad (`bool`, *optional*, defaults to `True`):
            Controls whether to pad the image. Can be overridden by the `do_pad` parameter in the `preprocess`
            method. If `True`, padding will be applied to the bottom and right of the image with zeros.
            If `pad_size` is provided, the image will be padded to the specified dimensions.
            Otherwise, the image will be padded to the maximum height and width of the batch.
        pad_size (`Dict[str, int]`, *optional*):
            The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size
            provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest
            height and width in the batch.
    pixel_values
pixel_maskTNgp?r  	do_resizer=   r   
do_rescalerescale_factordo_normalize
image_mean	image_stddo_convert_annotationsdo_padpad_sizer5   c                    d|v r|j                  d      }d|v r't        j                  d       |j                  d      }n|d nd}||nddd}t        ||d      }|
|}
t	        |   d
i | || _        || _        || _        || _	        || _
        || _        || _        |
| _        ||nt        | _        |	|	nt         | _        || _        || _        g d	| _        y )Npad_and_return_pixel_maskr>   vThe `max_size` parameter is deprecated and will be removed in v4.26. Please specify in `size['longest_edge'] instead`.i5  i   )shortest_edgelongest_edgeFr>   default_to_square)r   r   r   r   rx  r=   r   ry  rz  r{  r~  r|  r}  r  r  r  return_tensorsdata_formatrL    )poploggerwarning_oncer   super__init__r  rx  r=   r   ry  rz  r{  r~  r   r|  r   r}  r  r  _valid_processor_keys)selfr  rx  r=   r   ry  rz  r{  r|  r}  r~  r  r  kwargsr>   	__class__s                  rF   r  zDetrImageProcessor.__init__J  s     '&0ZZ ;<FD zz*-H#|tH'tsTX-YTHN ")%1""6""	 $,(&<#(2(>*DY&/&;AU &
"rH   image_processor_dictc                     |j                         }d|v r|j                  d      |d<   d|v r|j                  d      |d<   t        |   |fi |S )a  
        Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is
        created using from_dict and kwargs e.g. `DetrImageProcessor.from_pretrained(checkpoint, size=600,
        max_size=800)`
        r>   r  )copyr  r  	from_dict)clsr  r  r  s      rF   r  zDetrImageProcessor.from_dict  sd      488:/5zz*/E ,&&0@F

Kf@g !<=w !5@@@rH   r   r   r   r   rL   c                     ||n| j                   }|t        j                  k(  r|dn|}t        ||||      }|S |t        j                  k(  r|dn|}t        |||||      }|S t        d| d      )zD
        Prepare an annotation for feeding into DETR model.
        FrL   T)r   r   rL   zFormat z is not supported.)r  r   COCO_DETECTIONr   COCO_PANOPTICr   rc   )r  r   r   r  r   r   rL   s          rF   prepare_annotationz%DetrImageProcessor.prepare_annotation  s     "-4;;%4441J1RXq%6v8L]F  '5550I0QWp%5%6"3F  wvh.@ABBrH   r  c                 v   d|v r't         j                  d       |j                  d      }nd}t        ||d      }d|v rd|v rt	        ||d   |d   |      }nNd	|v rd
|v rt        ||d	   |d
   |      }n0d|v rd|v r|d   |d   f}nt        d|j                          d      t        |f||||d|}|S )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 (`Dict[str, int]`):
                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.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for the output image. If unset, the channel dimension format of the input
                image is used.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the input image. If not provided, it will be inferred.
        r>   r  NFr  r  r  r  rJ   rK   r?   r@   z\Size must contain 'height' and 'width' keys or 'shortest_edge' and 'longest_edge' keys. Got .)r=   r   r  rL   )	r  r  r  r   rX   rS   rc   keysr   )	r  r   r=   r   r  rL   r  r>   new_sizes	            rF   r   zDetrImageProcessor.resize  s   D D zz*-HHTHNd"~'=3tO,d>.BVgH T!kT&9:tL)4+<PaH 'T/XW6HIIK=#  
#/
 
 rH   c                      t        ||||      S )z
        Resize the annotation to match the resized image. If size is an int, smaller edge of the mask will be matched
        to this number.
        )r   r   r   )r9  )r  rn   r   r=   r   s        rF   r9  z$DetrImageProcessor.resize_annotation  s     !yd]effrH   c                      t        ||||      S )a  
        Rescale the image by the given factor. image = image * rescale_factor.

        Args:
            image (`np.ndarray`):
                Image to rescale.
            rescale_factor (`float`):
                The value to use for rescaling.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for the output image. If unset, the channel dimension format of the input
                image is used. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            input_data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for the input image. If unset, is inferred from the input image. Can be
                one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        )r  rL   )r   )r  r   rz  r  rL   s        rF   r   zDetrImageProcessor.rescale  s    4 un+YjkkrH   rn   r<   c                     t        ||      S )z
        Normalize the boxes in the annotation from `[top_left_x, top_left_y, bottom_right_x, bottom_right_y]` to
        `[center_x, center_y, width, height]` format and from absolute to relative pixel values.
        )r<   )r{   )r  rn   r<   s      rF   r{   z'DetrImageProcessor.normalize_annotation,  s    
 $J:FFrH   input_image_sizeoutput_image_sizec           
         i }||d<   |j                         D ]  \  }}|dk(  r@|}	t        |	|t        j                  dt        j
                        }	t        |	d      }	|	|d<   K|dk(  rJ|rH|}
|
t        j                  |d   |d   z  |d   |d   z  |d   |d   z  |d   |d   z  g      z  }
|
|d<   |dk(  r||d<   |||<    |S )z;
        Update the annotation for a padded image.
        r=   r   r   )r`  constant_valuesrL   r   rp   )	rs   r   r   CONSTANTr   r   rm   rZ   rt   )r  rn   r  r  paddingupdate_bboxesr6  ry   rz   r   rp   s              rF   #_update_annotation_for_padded_imagez6DetrImageProcessor._update_annotation_for_padded_image3  s    !2v$**, 	,JCg~$--$%&6&<&< %UA.*/w'M(+.?.BB(+.?.BB(+.?.BB(+.?.BB	  +0w'):v&&+s#5	,6 rH   r   r  r  c                     t        ||      \  }}	|\  }
}|
|z
  }||	z
  }d|fd|ff}t        ||t        j                  |||      }|| j	                  |||	f|
|f||      }||fS )z<
        Pad an image with zeros to the given size.
        r   r   )r`  r  r  rL   )r!   r   r   r  r  )r  r   r   rn   r  r  rL   r  r   r   output_heightoutput_width
pad_bottom	pad_rightr  padded_images                   rF   
_pad_imagezDetrImageProcessor._pad_image^  s     %35FW$X!k&1#|"\1
 ;.	z?Q	N3%%+#/
 !AA\;7-9VX_anJ Z''rH   r   r   return_pixel_maskr  c
           
         |	|	n| j                   }	|	|	d   |	d   f}
nt        ||      }
||ndgt        |      z  }g }g }t        ||      D ]B  \  }}| j	                  ||
|||||      \  }}|j                  |       |j                  |       D d|i}|r |D cg c]  }t        ||
|       }}||d<   t        ||	      }||D cg c]  }t        ||
       c}|d<   |S c c}w c c}w )a"	  
        Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width
        in the batch and optionally returns their corresponding pixel mask.

        Args:
            images (List[`np.ndarray`]):
                Images to pad.
            annotations (`AnnotationType` or `List[AnnotationType]`, *optional*):
                Annotations to transform according to the padding that is applied to the images.
            constant_values (`float` or `Iterable[float]`, *optional*):
                The value to use for the padding if `mode` is `"constant"`.
            return_pixel_mask (`bool`, *optional*, defaults to `True`):
                Whether to return a pixel mask.
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                    - Unset: Return a list of `np.ndarray`.
                    - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                    - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                    - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                    - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format of the image. If not provided, it will be the same as the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the input image. If not provided, it will be inferred.
            update_bboxes (`bool`, *optional*, defaults to `True`):
                Whether to update the bounding boxes in the annotations to match the padded images. If the
                bounding boxes have not been converted to relative coordinates and `(centre_x, centre_y, width, height)`
                format, the bounding boxes will not be updated.
            pad_size (`Dict[str, int]`, *optional*):
                The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size
                provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest
                height and width in the batch.
        Nr?   r@   r  )r  r  rL   r  rv  )r   r   rL   rw  datatensor_typer  r  )r  r   r   r~   r  r   r   r   )r  r   r   r  r  r  r  rL   r  r  padded_sizeannotation_listpadded_imagespadded_annotationsr   rn   r  padded_annotationr  r   encoded_inputss                        rF   r   zDetrImageProcessor.pad  sP   Z  (38#H-x/@AK.vIZ[K)4)@+tfsSY{FZ!$V_!= 	9E:.2oo /'"3+ /> /+L+   .%%&78	9 . $  eXijE  "'D%4^L"Wi(ISZ^D(N8$ (s   C(C-c                 .	   d|v r&t         j                  d       |j                  d      }d}d|v r&t         j                  d       |j                  d      }|| j                  n|}|| j                  n|}t        ||d      }|| j                  n|}|| j                  n|}|	| j                  n|	}	|
| j                  n|
}
|| j                  n|}|| j                  n|}|| j                  n|}|| j                  n|}|| j                  n|}|| j                  n|}t!        |      }t#        |      st%        d      t'        |j)                         | j*                  	       t-        ||	|
|||||
       |t/        |t0              r|g}|;t3        |      t3        |      k7  r$t%        dt3        |       dt3        |       d      t5        |      }|t7        |t8        |       |K|t4        j:                  k(  r8t/        |t<        j>                  t@        f      st%        dtC        |       d      |D cg c]  }tE        |       }}tG        |d         r|rt         j                  d       |tI        |d         }|Wg }g }tK        ||      D ]>  \  }}| jM                  ||||||      }|jO                  |       |jO                  |       @ |}|}~~|r|g g }}tK        ||      D ]f  \  }}tQ        ||      }| jS                  |||||      }| jU                  ||tQ        ||            }|jO                  |       |jO                  |       h |}|}~~n"|D cg c]  }| jS                  ||||       }}|r!|D cg c]  }| jW                  ||	|       }}|
r"|D cg c]  }| jY                  ||||       }}|r:|8tK        ||      D cg c]!  \  }}| j[                  |tQ        ||            # }}}|r| j]                  ||d|||||      } | S |D cg c]  }t_        |||       }}ta        d|i|      } ||D cg c]  }ta        ||       c}| d<   | S c c}w c c}w c c}w c c}w c c}}w c c}w c c}w )at  
        Preprocess an image or a batch of images so that it can be used by the model.

        Args:
            images (`ImageInput`):
                Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging
                from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`.
            annotations (`AnnotationType` or `List[AnnotationType]`, *optional*):
                List of annotations associated with the image or batch of images. If annotation is for object
                detection, the annotations should be a dictionary with the following keys:
                - "image_id" (`int`): The image id.
                - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a
                  dictionary. An image can have no annotations, in which case the list should be empty.
                If annotation is for segmentation, the annotations should be a dictionary with the following keys:
                - "image_id" (`int`): The image id.
                - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary.
                  An image can have no segments, in which case the list should be empty.
                - "file_name" (`str`): The file name of the image.
            return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks):
                Whether to return segmentation masks.
            masks_path (`str` or `pathlib.Path`, *optional*):
                Path to the directory containing the segmentation masks.
            do_resize (`bool`, *optional*, defaults to self.do_resize):
                Whether to resize the image.
            size (`Dict[str, int]`, *optional*, defaults to self.size):
                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 self.resample):
                Resampling filter to use when resizing the image.
            do_rescale (`bool`, *optional*, defaults to self.do_rescale):
                Whether to rescale the image.
            rescale_factor (`float`, *optional*, defaults to self.rescale_factor):
                Rescale factor to use when rescaling the image.
            do_normalize (`bool`, *optional*, defaults to self.do_normalize):
                Whether to normalize the image.
            do_convert_annotations (`bool`, *optional*, defaults to self.do_convert_annotations):
                Whether to convert the annotations to the format expected by the model. Converts the bounding
                boxes from the format `(top_left_x, top_left_y, width, height)` to `(center_x, center_y, width, height)`
                and in relative coordinates.
            image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean):
                Mean to use when normalizing the image.
            image_std (`float` or `List[float]`, *optional*, defaults to self.image_std):
                Standard deviation to use when normalizing the image.
            do_pad (`bool`, *optional*, defaults to self.do_pad):
                Whether to pad the image. If `True`, padding will be applied to the bottom and right of
                the image with zeros. If `pad_size` is provided, the image will be padded to the specified
                dimensions. Otherwise, the image will be padded to the maximum height and width of the batch.
            format (`str` or `AnnotationFormat`, *optional*, defaults to self.format):
                Format of the annotations.
            return_tensors (`str` or `TensorType`, *optional*, defaults to self.return_tensors):
                Type of tensors to return. If `None`, will return the list of images.
            data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use the channel dimension format of the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
            pad_size (`Dict[str, int]`, *optional*):
                The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size
                provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest
                height and width in the batch.
        r  zuThe `pad_and_return_pixel_mask` argument is deprecated and will be removed in a future version, use `do_pad` instead.Nr>   zrThe `max_size` argument is deprecated and will be removed in a future version, use `size['longest_edge']` instead.F)r=   r>   r  zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)captured_kwargsvalid_processor_keys)ry  rz  r{  r|  r}  rx  r=   r   zThe number of images (z) and annotations (z) do not match.zxThe path to the directory containing the mask PNG files should be provided as a `pathlib.Path` or string object, but is z	 instead.r   zIt looks like you are trying to rescale already rescaled images. If the input images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again.)r   r   rL   )r=   r>   r   rL   )r=   r   rL   r  T)r   r  r  rL   r  r  r  )input_channel_dimrv  r  r  r  )1r  r  r  rx  r=   r   r   ry  rz  r{  r|  r}  r~  r  r  r  r$   r&   rc   r(   r  r  r)   rU   dictr   r   r'   SUPPORTED_ANNOTATION_FORMATSr  r   r   strrd   r%   r#   r"   r~   r  r   r!   r   r9  r   	normalizer{   r   r   r   )!r  r   r   r   r   rx  r=   r   ry  rz  r{  r~  r|  r}  r  r  r  r  rL   r  r  r>   r   prepared_imagesprepared_annotationsr   resized_imagesresized_annotationsr   resized_imageresized_annotationrn   r  s!                                    rF   
preprocesszDetrImageProcessor.preprocess  s   B '&0( ZZ ;<F3 ::j)D&/&7DNNY	 Ltyyd$US$,$44==((2(:T__

0>0F,,N,8,@t((l(2(:T__

&/&7DNNY	+A+ID''Oe 	 !'F$,$44==( &F$V,F#:  	DLfLfg 	&!)%!		
 "z+t'D&-K"s6{c+6F'F(V5H[IYHZZij  "&)" )E{S "*888zGLL#+>?<<@<L;MYX  6<<E.'<<6!9%*s
 $ >vay I " O#% !$V[!9 
4v00.G)&7 1   &&u-$++F3
4 %F.K!5 &68" 3%(%= 	CME6 .u6G HI$(KKD8hbs %0 %M *.)?)?	>-IZ+[*& #))-8'../AB	C (1"$7 "( KKD8WhKi 
 lrschdll5.L]l^sFsoufkuj)O`aF  "k&= *-[&)A%J ))*nUL]6^_K 
 !XX'"&'"34-! & 	N*  $ ,E;RcdF  */GUcdN&[f,MWLH,x( w =b t
$,s*   ?Q3)Q8Q=0R!&R+RRc           	      r   t         j                  d       |j                  |j                  }}t	        |      t	        |      k7  rt        d      |j                  d   dk7  rt        d      t        j                  j                  |d      }|dddf   j                  d      \  }}t        |      }|j                  d      \  }	}
t        j                  |
|	|
|	gd	      j                  |j                         }||dddddf   z  }t#        |||      D cg c]  \  }}}|||d
 }}}}|S c c}}}w )aY  
        Converts the raw output of [`DetrForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
        bottom_right_x, bottom_right_y) format. Only supports PyTorch.

        Args:
            outputs ([`DetrObjectDetectionOutput`]):
                Raw outputs of the model.
            target_sizes (`torch.Tensor` of shape `(batch_size, 2)`):
                Tensor containing the size (height, width) of each image of the batch. For evaluation, this must be the
                original image size (before any data augmentation). For visualization, this should be the image size
                after data augment, but before padding.
        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.
        z`post_process` is deprecated and will be removed in v5 of Transformers, please use `post_process_object_detection` instead, with `threshold=0.` for equivalent results.TMake sure that you pass in as many target sizes as the batch dimension of the logitsr   r   zTEach element of target_sizes must contain the size (h, w) of each image of the batchr   .Ndimr  r  rp   )r  r  r  
pred_boxesr   rc   r   r4   rg  r   r9   r   unbindr_   r   tor]  r~   )r  outputstarget_sizesr	  out_bboxprobr  r  rp   img_himg_w	scale_fctr0  lr"  resultss                   rF   post_processzDetrImageProcessor.post_process  s5     	d	

  '~~w/A/AH
z?c,//stta A%stt}}$$Z4c3B3h++B/ )2#**1-uKKue <!DGGU		!T1*--ILVU[]bIcddgaAa1q9dd es   D2c                 "   t         j                  d       |j                  |j                  }}|j                  d   dz
  }g }d }	t        |||      D ]  \  }
}}|
j                  d      j                  d      \  }}|j                  |      ||kD  z  }||   }||   }||   }t        j                  j                  |dddf    |	|      d      j                  d      }|j                         |kD  dz  }|||d}|j                  |        |S )	a  
        Converts the output of [`DetrForSegmentation`] into image segmentation predictions. Only supports PyTorch.

        Args:
            outputs ([`DetrSegmentationOutput`]):
                Raw outputs of the model.
            target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`):
                Torch Tensor (or list) corresponding to the requested final size (h, w) of each prediction.
            threshold (`float`, *optional*, defaults to 0.9):
                Threshold to use to filter out queries.
            mask_threshold (`float`, *optional*, defaults to 0.5):
                Threshold to use when turning the predicted masks into binary values.
        Returns:
            `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, and masks for an image
            in the batch as predicted by the model.
        z`post_process_segmentation` is deprecated and will be removed in v5 of Transformers, please use `post_process_semantic_segmentation`.r   r   c                 t    t        | t              r| S t        | j                         j                               S r.  rU   rW   cputolisttups    rF   to_tuplez>DetrImageProcessor.post_process_segmentation.<locals>.to_tuple!  +    #u%
))+,,rH   Nr^  r`  )r  r  r   )r  r  r  
pred_masksr   r~   r   r9   rI  r4   rg  rh  rl   sigmoidr   )r  r  r  r  rR  r	  	raw_masksempty_labelpredsr  
cur_logitsr!  r=   r  
cur_labelsr   r)  s                    rF   post_process_segmentationz,DetrImageProcessor.post_process_segmentation  s.   " 	5	
 !(0B0BI
 &&r*Q.	-
 ,/z9l+S 	&'J	4%/%7%7%;%?%?%C"J
==-i1GHD#D)J#D)J!$I11)AtG2Dhtn[e1fnnopqI"**,~=BI%/:PYZKLL%	& rH   c                    t         j                  d       t        |      t        |      k7  rt        d      |j	                  d      d   j                         \  }}|j                  j                  d      }t        j                  j                  |||fdd      }|j                         |kD  j                         }t        t        |||            D ]  \  }	\  }
}}|d   |d   }}|
d	d	d	|d	|f   j                  d      ||	   d
<   t        j                  j                  ||	   d
   j!                         t#        |j                               d      j%                         ||	   d
<    |S )a  
        Converts the output of [`DetrForSegmentation`] into actual instance segmentation predictions. Only supports
        PyTorch.

        Args:
            results (`List[Dict]`):
                Results list obtained by [`~DetrImageProcessor.post_process`], to which "masks" results will be added.
            outputs ([`DetrSegmentationOutput`]):
                Raw outputs of the model.
            orig_target_sizes (`torch.Tensor` of shape `(batch_size, 2)`):
                Tensor containing the size (h, w) of each image of the batch. For evaluation, this must be the original
                image size (before any data augmentation).
            max_target_sizes (`torch.Tensor` of shape `(batch_size, 2)`):
                Tensor containing the maximum size (h, w) of each image of the batch. For evaluation, this must be the
                original image size (before any data augmentation).
            threshold (`float`, *optional*, defaults to 0.5):
                Threshold to use when turning the predicted masks into binary values.
        Returns:
            `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, boxes and masks for an
            image in the batch as predicted by the model.
        z`post_process_instance` is deprecated and will be removed in v5 of Transformers, please use `post_process_instance_segmentation`.zBMake sure to pass in as many orig_target_sizes as max_target_sizesr   r   r^  Fr_  r   Nr   nearest)r=   r`  )r  r  r   rc   r9   r  r  rl   r4   rg  rh  r  r  r  r~   ri  r7   rW   byte)r  r  r  orig_target_sizesmax_target_sizesr  max_hmax_woutputs_masksr   cur_masktttr  r  s                  rF   post_process_instancez(DetrImageProcessor.post_process_instance5  su   , 	5	

  !S)9%::abb'++A.q188:u**221511Zu 2 
 '..09<AAC$-c-AQSd.e$f 	 A !RQ415E"*1fuffuf+<"="G"G"JGAJw"$--";";
7#))+%		2D9 #< #df AJw	 rH   c           	      |   !"# t         j                  d       ||}t        |      t        |      k7  rt        d      |t	        d      D ci c]  }||dk  
 }}|j
                  |j                  |j                  }	}}t        |      t        |      cxk(  rt        |      k(  st        d       t        d      |j                  d   dz
  }
g }d	 "t        |||	||      D ]  \  }}}}!|j                  d      j                  d      \  }}|j                  |
      ||kD  z  }||   }||   }||   }t        j                  j                  |dddf    "|      d
      j!                  d      }t#        ||         }|j                  dd \  #t        |      t        |      k7  rt        d      |j%                  d      }t'        d        t)        |      D ];  \  }}||j+                            r |j+                            j-                  |       = d !"#fd	} |||d      \  }}|j/                         dkD  r	 t1        j2                  t)        |      D cg c]  \  }}||   dk   c}}t0        j4                  |j6                        }|j9                         j+                         r||    }||    }||    } |||      \  }}nn1t1        j:                  dt0        j<                  |j6                        }g }t)        |      D ]1  \  }}||   j+                         }|j-                  |||   ||d       3 ~t?        j@                         5 }|jC                  |d       |jE                         |d}ddd       |j-                          |S c c}w c c}}w # 1 sw Y   *xY w)a  
        Converts the output of [`DetrForSegmentation`] into actual panoptic predictions. Only supports PyTorch.

        Args:
            outputs ([`DetrSegmentationOutput`]):
                Raw outputs of the model.
            processed_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`):
                Torch Tensor (or list) containing the size (h, w) of each image of the batch, i.e. the size after data
                augmentation but before batching.
            target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`, *optional*):
                Torch Tensor (or list) corresponding to the requested final size `(height, width)` of each prediction.
                If left to None, it will default to the `processed_sizes`.
            is_thing_map (`torch.Tensor` of shape `(batch_size, 2)`, *optional*):
                Dictionary mapping class indices to either True or False, depending on whether or not they are a thing.
                If not set, defaults to the `is_thing_map` of COCO panoptic.
            threshold (`float`, *optional*, defaults to 0.85):
                Threshold to use to filter out queries.
        Returns:
            `List[Dict]`: A list of dictionaries, each dictionary containing a PNG string and segments_info values for
            an image in the batch as predicted by the model.
        z`post_process_panoptic is deprecated and will be removed in v5 of Transformers, please use `post_process_panoptic_segmentation`.Nz<Make sure to pass in as many processed_sizes as target_sizes   Z   z^Make sure that you pass in as many target sizes as the batch dimension of the logits and masksr   r   c                 t    t        | t              r| S t        | j                         j                               S r.  r  r  s    rF   r  z:DetrImageProcessor.post_process_panoptic.<locals>.to_tuple  r  rH   r^  r  r   r  c                      g S r.  r  r  rH   rF   <lambda>z:DetrImageProcessor.post_process_panoptic.<locals>.<lambda>  s    b rH   c                 p   | j                  dd      j                  d      }|j                  d   dk(  r3t        j                  ft        j
                  |j                        }n!|j                  d      j                        }|rOj                         D ]<  }t        |      dkD  s|D ]&  }|j                  |j                  |      |d          ( >        \  }}t        j                  j                  t!        |j                        j#                         j%                                     }|j'                  ||ft(        j*                        }t        j,                  t        j.                  j1                  |j3                                     }	|	j                  ||d      }	|	j%                         }	t        j4                  t7        |	            }g }
t9        t        |            D ]>  }|
j;                  |j                  |      j=                         j?                                @ |
|fS )Nr   r   r   r\  )r=   r   r   ) r   r   r   r_   r   longr]  r   rj  r|   r   masked_fill_eqr   r   r  r   r  rb   r   r    r   
ByteTensorByteStoragefrom_buffertobytes
from_numpyr   r   r   r   rO  )r   r  dedupr   r   r   r   r   r   r   r   r   r   r   r   r  r   s               rF   get_ids_areaz>DetrImageProcessor.post_process_panoptic.<locals>.get_ids_area  s    q!,44R8::b>Q& ;;1vUZZTD;;r?//15D!4!;!;!= Lu:>). L $ 1 1$''%.%( KLL
 $,K#8 ))--i		!Q8K8K8M8S8S8U.VW!..w.@K]KeKe.f"--e.?.?.K.KGOOL].^_
'__WgqA
'--/
''	*(=>s6{+ 9AKK
 0 5 5 789W}$rH   T)r  r   r   r\  r  r  r  r  F)#r  r  r   rc   r   r  r  r  r   r~   r   r9   rI  r4   rg  rh  rl   r   r;  r   r  rO  r   numelr_   	as_tensorr   r]  r   r  r  r  r  r  r  )$r  r  processed_sizesr  r  r  r   r	  r  	raw_boxesr  r  r  r!  r   r=   r  r  r   r#  r$  r  r   r   cr&  r   r%  r'  r(  r)  r   r   r   r  r   s$                                  @@@@@rF   post_process_panopticz(DetrImageProcessor.post_process_panopticc  s   , 	5	
 *L3|#44[\\05c
;1AqBwJ;L;+2>>7;M;MwOaOayI
:#i.EC4EEp  Fp  !&&r*Q.	-
 DG	9o|D
 V	&?J	9dK &0%7%7%;%?%?%C"J
==-i1GHD#D)J#D)J!$I11)AtG2Dhtn[e1fnnopqI04AI??23'DAq9~Z0 !IJJ "))!,I"-j"9%j1 @5#EJJL1'

5<<Q?@!% !%F )JdKMD'!A%%*__2;J2GH$!QaAHPUPZPZcgcncn&N &))+002%/%@
%/%@
$-~o$>	(4Y
(Kg  #ZZJDUDUV
M!$ m1 m((*$$A,s:K\_ij%klm  ]S/-0\\^m\] LL%mV	&n M <^ I&] ]s   N'(N,"'N22N;	r  r  c                    |j                   |j                  }}|"t        |      t        |      k7  rt        d      t        j
                  j                  |d      }|dddf   j                  d      \  }}t        |      }	|t        |t              rMt        j                  |D 
cg c]  }
|
d   	 c}
      }t        j                  |D 
cg c]  }
|
d   	 c}
      }n|j                  d      \  }}t        j                  ||||gd      j                  |	j                         }|	|dddddf   z  }	g }t#        |||	      D ]3  \  }}}|||kD     }|||kD     }|||kD     }|j%                  |||d       5 |S c c}
w c c}
w )	a  
        Converts the raw output of [`DetrForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
        bottom_right_x, bottom_right_y) format. Only supports PyTorch.

        Args:
            outputs ([`DetrObjectDetectionOutput`]):
                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.
        Nr  r   .r   r   r  r  )r  r  r   rc   r4   rg  r   r9   r   rU   r   r_   Tensorr  r   r  r]  r~   r   )r  r  r  r  r	  r  r  r  r  rp   r   r  r  r  r  r0  r  r"  re  r$  boxs                        rF   post_process_object_detectionz0DetrImageProcessor.post_process_object_detection  s   &  '~~w/A/AH
#:#l"33 j  }}$$Z4c3B3h++B/ )2 #,-L%Aqad%ABL%Aqad%AB+2215uUE5%$@aHKKELLYIIaqj11E6651 	MGAq!a)m$Ea)m$EA	M"CNNeusKL		M  &B%As   (F Fc                 n   |j                   }|j                  }|j                  d      dddf   }|j                         }t	        j
                  d||      }|j                  d   }||t        |      k7  rt        d      g }	t        |      D ]a  }
t        j                  j                  ||
   j                  d      ||
   dd	
      }|d   j                  d      }|	j                  |       c |	S |j                  d      }	t        |	j                  d         D cg c]  }|	|   	 }	}|	S c c}w )a0  
        Converts the output of [`DetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch.

        Args:
            outputs ([`DetrForSegmentation`]):
                Raw outputs of the model.
            target_sizes (`List[Tuple[int, int]]`, *optional*):
                A 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[torch.Tensor]`:
                A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width)
                corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each
                `torch.Tensor` correspond to a semantic class id.
        r   r  .Nzbqc, bqhw -> bchwr   r  r^  Fr_  r   )r  r  r   r  r_   einsumr   r   rc   r   r4   rg  rh  ri  r   r   )r  r  r  class_queries_logitsmasks_queries_logitsmasks_classesmasks_probsr   
batch_sizesemantic_segmentationrE  resized_logitssemantic_mapr   s                 rF   "post_process_semantic_segmentationz5DetrImageProcessor.post_process_semantic_segmentation"  sj      '~~&11 -444<S#2#XF*224 ||$7T)//2
 #S.. j  %'!Z( ;!#!:!: %//A/6\#=NU_ot "; "  .a077A7>%,,\:; %$ %1$7$7A$7$>!GLMbMhMhijMkGl$m!%:1%=$m!$m$$ %ns   "D2rR  rS  return_coco_annotationc           
         |j                   }|j                  }|j                  d   }	|j                  d   dz
  }
|j                         }t        j
                  j                  |d      j                  d      \  }}g }t        |	      D ]  }t        ||   ||   ||   ||
      \  }}}|j                  d   dk  rH|||   n|j                  dd \  }}t        j                  ||f      dz
  }|j                  |g d       y|||   nd}t        |||||g |      \  }}|rt        |      }|j                  ||d        |S )a  
        Converts the output of [`DetrForSegmentation`] into instance segmentation predictions. Only supports PyTorch.

        Args:
            outputs ([`DetrForSegmentation`]):
                Raw outputs of the model.
            threshold (`float`, *optional*, defaults to 0.5):
                The probability score threshold to keep predicted instance masks.
            mask_threshold (`float`, *optional*, defaults to 0.5):
                Threshold to use when turning the predicted masks into binary values.
            overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8):
                The overlap mask area threshold to merge or discard small disconnected parts within each binary
                instance mask.
            target_sizes (`List[Tuple]`, *optional*):
                List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested
                final size (height, width) of each prediction. If unset, predictions will not be resized.
            return_coco_annotation (`bool`, *optional*):
                Defaults to `False`. If set to `True`, segmentation maps are returned in COCO run-length encoding (RLE)
                format.
        Returns:
            `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys:
            - **segmentation** -- A tensor of shape `(height, width)` where each pixel represents a `segment_id` or
              `List[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to
              `True`. Set to `None` if no mask if found above `threshold`.
            - **segments_info** -- A dictionary that contains additional information on each segment.
                - **id** -- An integer representing the `segment_id`.
                - **label_id** -- An integer representing the label / semantic class id corresponding to `segment_id`.
                - **score** -- Prediction score of segment with `segment_id`.
        r   r   r   r  Nr   r   rQ  rk  rl  rR  rS  rZ  r   )r  r  r   r  r4   rg  r   r9   r   rM  r_   r   r   rs  rG  )r  r  r  rR  rS  r  r&  r  r  r!  rK  rQ  rk  rl  r  r   mask_probs_itempred_scores_itempred_labels_itemr?   r@   r   r   rm  s                           rF   "post_process_instance_segmentationz5DetrImageProcessor.post_process_instance_segmentationR  s   L  '~~&11)//2
)//3a7
)113
 $&==#8#89MSU#8#V#Z#Z[]#^ [ 02z" 	VAB[1{1~{1~y*C?O-/?
 $$Q'1,3?3KQQ`QfQfghgiQj${{FE?;a?rRS .:-E,q/4K%5*,,-,G"$'&"L( &:<HNNL8TU9	V: rH   rZ  c           
         |t         j                  d       t               }|j                  }|j                  }|j
                  d   }	|j
                  d   dz
  }
|j                         }t        j                  j                  |d      j                  d      \  }}g }t        |	      D ]  }t        ||   ||   ||   ||
      \  }}}|j
                  d   dk  rH|||   n|j
                  dd \  }}t        j                  ||f      dz
  }|j                  |g d       y|||   nd}t!        |||||||      \  }}|j                  ||d        |S )	ae	  
        Converts the output of [`DetrForSegmentation`] into image panoptic segmentation predictions. Only supports
        PyTorch.

        Args:
            outputs ([`DetrForSegmentation`]):
                The outputs from [`DetrForSegmentation`].
            threshold (`float`, *optional*, defaults to 0.5):
                The probability score threshold to keep predicted instance masks.
            mask_threshold (`float`, *optional*, defaults to 0.5):
                Threshold to use when turning the predicted masks into binary values.
            overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8):
                The overlap mask area threshold to merge or discard small disconnected parts within each binary
                instance mask.
            label_ids_to_fuse (`Set[int]`, *optional*):
                The labels in this state will have all their instances be fused together. For instance we could say
                there can only be one sky in an image, but several persons, so the label ID for sky would be in that
                set, but not the one for person.
            target_sizes (`List[Tuple]`, *optional*):
                List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested
                final size (height, width) of each prediction in batch. If unset, predictions will not be resized.
        Returns:
            `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys:
            - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id` or
              `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to
              the corresponding `target_sizes` entry.
            - **segments_info** -- A dictionary that contains additional information on each segment.
                - **id** -- an integer representing the `segment_id`.
                - **label_id** -- An integer representing the label / semantic class id corresponding to `segment_id`.
                - **was_fused** -- a boolean, `True` if `label_id` was in `label_ids_to_fuse`, `False` otherwise.
                  Multiple instances of the same class / label were fused and assigned a single `segment_id`.
                - **score** -- Prediction score of segment with `segment_id`.
        Nz5`label_ids_to_fuse` unset. No instance will be fused.r   r   r   r  r(  r)  )r  r  setr  r  r   r  r4   rg  r   r9   r   rM  r_   r   r   rs  )r  r  r  rR  rS  rZ  r  r  r  r!  rK  rQ  rk  rl  r  r   r*  r+  r,  r?   r@   r   r   rm  s                           rF   "post_process_panoptic_segmentationz5DetrImageProcessor.post_process_panoptic_segmentation  s   V $ WX #&~~&11)//2
)//3a7
)113
 $&==#8#89MSU#8#V#Z#Z[]#^ [ 02z" 	VAB[1{1~{1~y*C?O-/?
 $$Q'1,3?3KQQ`QfQfghgiQj${{FE?;a?rRS .:-E,q/4K%5*,,-,G"3'&"L( NNL8TU1	V2 rH   )NNNNNN)Nr   NNT)Nr   TNNNTN)g?r+  )r+  )NN333333?)r+  Nr.  )r+  r+  皙?NF)r+  r+  r3  NN)5__name__
__module____qualname____doc__model_input_namesr   r  r    r  r   r  r   r   r:   r7   r   r	   r  classmethodr   r  rZ   r[   r   r   r   r  r   r   r9  r   r   r{   r  r   r  r   r*   r   r   r   r   r  r  r  r  r  r  r%  r-  r
   r0  __classcell__)r  s   @rF   ru  ru    s   2h (6 0@/N/N#'9'B'B,3!04/315-1D
c++,D
 D
 38n	D

 %D
 D
 c5j)D
 D
 %e,-D
 U+,D
 !)D
 D
 4S>*D
 
D
L AT#s(^ A A" .2*.9=DHzz  )*	
 $( U3#456 $E#/?*?$@A 
H (:'B'B26DHBzzB 38nB %	B
 ./B $E#/?*?$@AB 
BR (:'A'Ag
 %g 
g$ ?CDHlzzl l eC)9$9:;	l
 $E#/?*?$@Al 
l8Gt GsCx GUY G))  S/) !c?	) 
)^ 049:26DH"(zz( 38_( T#s(^,	(
 uhuo56( ./( $E#/?*?$@A( ( 
(H NR9:"&;?26DH"-1SRZZ S eND4H$HIJS uhuo56	S
  S !sJ!78S ./S $E#/?*?$@AS S 4S>*S 
Sp NR*.9=$()-%)6:'+15:>9=!%9=;?4D4J4JDH-1)GG eND4H$HIJG $(	G
 U3#456G D>G tCH~&G TNG !sEz!23G tnG !)G U5$u+#567G E%e"456G G  s$4456!G" !z3!78#G$ 3 001%G& $E#/?*?$@A'G( 4S>*)G, 
-GV'R*Z+\GV _c3"'3=B:tTY{CZ=[3j-%USVX[S[_H] -%f  #-08<16Q Q 	Q
 &+Q tE#s(O45Q !)Q 
dQn  #-0048<V V 	V
 &+V $CH-V tE#s(O45V 
dVrH   ru  r.  r1  )FN)TNr  )r2  )r+  r3  )r+  r3  NN)gr7  r  r   collectionsr   typingr   r   r   r   r   r	   r
   r   r   rb   rZ   image_processing_utilsr   r   r   image_transformsr   r   r   r   r   r   r   r   r   image_utilsr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   utilsr*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r_   r4   r   scipy.specialr   scipy.stats
get_loggerr4  r  r  r  r  r:   rG   r[   r  rS   rX   rh   rm   r{   r   r   r   r   r   r   r   r   r   r   r   r  r*  r   r7   r9  r@  rG  rM  rY  rs  ru  __all__r  rH   rF   <module>rE     s3   & 	  # S S S  U U
 
 
    $      
		H	% 0 ? ?AQA_A_` $5c? $V AE	 ! ! !  !  c+;&; <=	 !
 38_ !L #@D	BB
U38_d3i/
0B smB  c+;&; <=	B
 38_B8Ch C0
bjj 
 
 
T uS#X 4 8x} 8c 8 [_##19%EU@U:V1W#	#Y#( rv::$)#s(OHPQVWZ\lWlQmHnZZ&S   L ',@D	9  $9  &6&; <=	9x5"** 5 5L 6:&::&& c7<<'(& 	&
 -s23& 
&T `e::#(7<22:: E#s(O PS XZXbXb "** rzzSUS]S]G]A^  R

R::R ::R #s(O	R
 sCxR R 
Rr #5#=#=0S#X0S#X0 sCx0 	0
 !0h. *<8,  ),,0#'9" 	9"
 "'9"  C)9" sCx9"xi+ iX'  
 rH   