
    Z jfC              
       "   S SK r S SKJr  S SKJr  S SKrS SKJs  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JrJr  SS
KJr  SSKJrJr  SSKJrJrJ r J!r!   " S S\SS9r"SSS\#\\#   -  S\$S\#S\4
S jr%\ " S S\5      5       r&S/r'g)    N)Iterable)Union)
functional   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)ChannelDimension
ImageInputPILImageResamplingSizeDict)DepthEstimatorOutput)ImagesKwargsUnpack)
TensorTypeauto_docstringis_torch_availablerequires_backendsc                   B    \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   Srg)	CHMv2ImageProcessorKwargs&   a  
ensure_multiple_of (`int`, *optional*, defaults to 1):
    If `do_resize` is `True`, the image is resized to a size that is a multiple of this value. Can be overridden
    by `ensure_multiple_of` in `preprocess`.
keep_aspect_ratio (`bool`, *optional*, defaults to `False`):
    If `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. Can
    be overridden by `keep_aspect_ratio` in `preprocess`.
do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`):
    Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0
    is used for background, and background itself is not included in all classes of a dataset (e.g.
    ADE20k). The background label will be replaced by 255.
ensure_multiple_ofsize_divisorkeep_aspect_ratiodo_reduce_labels N)	__name__
__module____qualname____firstlineno____doc__int__annotations__bool__static_attributes__r       ځ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/chmv2/image_processing_chmv2.pyr   r   &   s!     r'   r   F)totalinput_imagetorch.Tensoroutput_sizer   multiplereturnc                     SS jnU R                   SS  u  pVUu  pxXu-  n	X-  n
U(       a#  [        SU
-
  5      [        SU	-
  5      :  a  U
n	OU	n
U" X-  US9nU" X-  US9n[        XS9$ )Nc                     [        X-  5      U-  nUb   XC:  a  [        R                  " X-  5      U-  nXB:  a  [        R                  " X-  5      U-  nU$ N)roundmathfloorceil)valr-   min_valmax_valxs        r(   constrain_to_multiple_of>get_resize_output_image_size.<locals>.constrain_to_multiple_of@   sQ    #.!H,1;

3>*X5A;		#.)H4Ar'      )r-   heightwidth)r   N)shapeabsr   )r*   r,   r   r-   r:   input_heightinput_widthoutput_heightoutput_widthscale_heightscale_width
new_height	new_widths                r(   get_resize_output_image_sizerK   :   s    	 !, 1 1"# 6L"-M !/L,Kq;#a,&6"77&L 'K),*EPXYJ()BXVI:77r'   c            $         ^  \ rS rSrSr\r\R                  r	/ SQr
/ SQrSSS.rSrSrS	rSrSrSrSrSrS
rSrSrSrS\\   4U 4S jjr\ S7S\S\S-  S\\   S\4U 4S jjj5       r S7S\S\S-  S\S\ S\!\"-  S-  S\#\!S4   S-  S\4S jjr$S\%S   S\%S   4S 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+\S-  S\4$S, jr*S7S-\%\+   S-  4S. jjr,   S8S/SS\'SSS0\S(\)S-  S'\SS4U 4S1 jjjr- S9S/SS*\)SS4S2 jjr. S7S3S4S-\"\%\+\)\)4      -  S-  S-  S\%\/\!\"4      4S5 jjr0S6r1U =r2$ ):CHMv2ImageProcessora   z0PIL backend for CHMV2 with reduce_label support.)gzG?gM?gl?)g$C?g+?gM?i  r>   TNFgp?   kwargsc                 &   > [         TU ]  " S0 UD6  g )Nr   )super__init__)selfrP   	__class__s     r(   rS   CHMv2ImageProcessor.__init__y   s    "6"r'   imagessegmentation_mapsr.   c                 &   > [         TU ]  " X40 UD6$ )zX
segmentation_maps (`ImageInput`, *optional*):
    The segmentation maps to preprocess.
)rR   
preprocess)rT   rW   rX   rP   rU   s       r(   rZ   CHMv2ImageProcessor.preprocess|   s     w!&FvFFr'   do_convert_rgbinput_data_formatreturn_tensorsdeviceztorch.devicec                    U R                  XXFS9nUR                  5       nSUS'   0 n	U R                  " U40 UD6U	S'   Ub  U R                  USS[        R                  S9n
UR                  5       nUR                  SSS.5        U R                  " SSU
0UD6n
U
 Vs/ s H1  nUR                  S	5      R                  [        R                  5      PM3     n
nXS
'   [        XS9$ s  snf )z"Handle extra inputs beyond images.)rW   r\   r]   r_   Fr   pixel_values   )rW   expected_ndimsr\   r]   )do_normalize
do_rescalerW   r   labels)datatensor_typer   )_prepare_image_like_inputscopy_preprocessr   FIRSTupdatesqueezetotorchint64r   )rT   rW   rX   r\   r]   r^   r_   rP   images_kwargsrg   processed_segmentation_mapssegmentation_maps_kwargsprocessed_segmentation_maps                r(   _preprocess_image_like_inputs1CHMv2ImageProcessor._preprocess_image_like_inputs   s!    00L] 1 
 ,1()#//H-H^ (*.*I*I( $"2"8"8	 +J +' (.{{}$$++URW,XY*.*:*: +2+6N+' 3N+2M. +221588E2M ( + 9NBB+s   8C$rf   r+   c           
      b   [        [        U5      5       H  nX   n[        R                  " US:H  [        R                  " SUR
                  UR                  S9U5      nUS-
  n[        R                  " US:H  [        R                  " SUR
                  UR                  S9U5      nX1U'   M     U$ )z/Reduce label values by 1, replacing 0 with 255.r      )dtyper_   r=      )rangelenrp   wheretensorrz   r_   )rT   rf   idxlabels       r(   reduce_label CHMv2ImageProcessor.reduce_label   s    V%CKEKK
ELLEKKX]XdXd,eglmEAIEKKell3ekkZ_ZfZf.ginoE3K & r'   r   	do_resizesizeresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonedo_center_crop	crop_sizere   rescale_factorrd   
image_mean	image_stdr   r   do_padr   disable_groupingc           	         U(       a  U R                  U5      n[        UUS9u  nn0 nUR                  5        H%  u  nnU(       a  U R                  UUUUUS9nUUU'   M'     [	        UU5      n[        UUS9u  nn0 nUR                  5        HQ  u  nnU(       a  U R                  UU5      nU R                  UXXU5      nU(       a  U R                  UU5      nUUU'   MS     [	        UU5      nU$ )zCustom preprocessing for CHMV2.)r   )imager   r   r   r   )r   r	   itemsresizer
   center_croprescale_and_normalize	pad_image)rT   rW   r   r   r   r   r   r   re   r   rd   r   r   r   r   r   r   r   rP   grouped_imagesgrouped_images_indexresized_images_groupedrA   stacked_imagesresized_imagesprocessed_images_groupedprocessed_imagess                              r(   rk   CHMv2ImageProcessor._preprocess   s'   , &&v.F 0EV^n/o,,!#%3%9%9%;!E>!%(%'9&7 "- " -;"5) &< ((>@TU 0E^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N !%!M.<$U+ &< **BDXYr'   target_sizesc                 L   [        5       (       d  [        S5      eUR                  nUb  [        U5      [        U5      :w  a  [	        S5      e[        U[        R                  5      (       a  UR                  5       n/ n[        [        U5      5       HN  n[        R                  " X5   R                  SS9X%   SSS9nUS   R                  SS9nUR                  U5        MP     U$ UR                  SS9n[        UR                  S   5       Vs/ s H  oU   PM	     nnU$ s  snf )	a  
Converts the output of [`CHMv2ForSemanticSegmentation`] into semantic segmentation maps.

Args:
    outputs ([`CHMv2ForSemanticSegmentation`]):
        Raw outputs of the model.
    target_sizes (`list[Tuple]` of length `batch_size`, *optional*):
        List of tuples corresponding to the requested final size (height, width) of each prediction. If unset,
        predictions will not be resized.

Returns:
    semantic_segmentation: `list[torch.Tensor]` 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.
z:PyTorch is required for post_process_semantic_segmentationzTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   )dimbilinearFr   modealign_cornersr=   )r   ImportErrorlogitsr}   
ValueError
isinstancerp   Tensornumpyr|   Finterpolate	unsqueezeargmaxappendrA   )	rT   outputsr   r   semantic_segmentationr   resized_logitssemantic_mapis	            r(   "post_process_semantic_segmentation6CHMv2ImageProcessor.post_process_semantic_segmentation   s+     "##Z[[ #6{c,// j  ,55+113$&!S[)!"K))a)0|7Hzin"  .a077A7>%,,\: * %$ %+MMaM$8!GLMbMhMhijMkGl$mGl!1%=Gl!$m$$ %ns   D!r   	antialiasc                    > UR                   (       a  UR                  (       d  [        SUR                  5        35      e[	        UUR                   UR                  4UUS9n[
        TU ]  XX4S9$ )a  
Resize an image to `(size["height"], size["width"])`.

Args:
    image (`torch.Tensor`):
        Image to resize.
    size (`SizeDict`):
        Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
    interpolation (`InterpolationMode`, *optional*, defaults to `InterpolationMode.BILINEAR`):
        `InterpolationMode` filter to use when resizing the image e.g. `InterpolationMode.BICUBIC`.
    antialias (`bool`, *optional*, defaults to `True`):
        Whether to use antialiasing when resizing the image
    ensure_multiple_of (`int`, *optional*):
        If `do_resize` is `True`, the image is resized to a size that is a multiple of this value
    keep_aspect_ratio (`bool`, *optional*, defaults to `False`):
        If `True`, and `do_resize` is `True`, the image is resized to the largest possible size such that the aspect ratio is preserved.

Returns:
    `torch.Tensor`: The resized image.
zDThe size dictionary must contain the keys 'height' and 'width'. Got )r,   r   r-   )r   r   )r?   r@   r   keysrK   rR   r   )	rT   r   r   r   r   r   r   r,   rU   s	           r(   r   CHMv2ImageProcessor.resize'  sh    : {{$**cdhdmdmdocpqrr2djj1/'	
 w~e8~YYr'   c                     UR                   SS u  p4S nU" X25      u  pgU" XB5      u  pXX4n
[        R                  " X5      $ )aL  
Center pad a batch of images to be a multiple of `size_divisor`.

Args:
    image (`torch.Tensor`):
        Image to pad.  Can be a batch of images of dimensions (N, C, H, W) or a single image of dimensions (C, H, W).
    size_divisor (`int`):
        The width and height of the image will be padded to a multiple of this number.
r<   Nc                 X    [         R                  " X-  5      U-  nX -
  nUS-  nX4-
  nXE4$ )Nrb   )r3   r5   )r   r   new_sizepad_sizepad_size_leftpad_size_rights         r(   _get_pad/CHMv2ImageProcessor.pad_image.<locals>._get_pad_  s9    yy!45DHH$MM%5N 00r'   )rA   tvFpad)rT   r   r   r?   r@   r   pad_top
pad_bottompad_left	pad_rightpaddings              r(   r   CHMv2ImageProcessor.pad_imageO  sP     BC(	1 'v<&u;i<wwu&&r'   r   r   c                 x   [        U S5        UR                  nUb#  [        U5      [        U5      :w  a  [        S5      e/ nUc  S/[        U5      -  OUn[	        X25       HV  u  pVUb;  [
        R                  R                  R                  US   USSS9R                  5       nUR                  SU05        MX     U$ )	aj  
Converts the raw output of [`DepthEstimatorOutput`] into final depth predictions and depth PIL images.
Only supports PyTorch.

Args:
    outputs ([`DepthEstimatorOutput`]):
        Raw outputs of the model.
    target_sizes (`TensorType` 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 left to None, predictions will not be resized.

Returns:
    `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth
    predictions.
rp   Nz]Make sure that you pass in as many target sizes as the batch dimension of the predicted depth)NN.r   Tr   predicted_depth)r   r   r}   r   ziprp   nnr   r   rn   r   )rT   r   r   r   resultsdepthtarget_sizes          r(   post_process_depth_estimation1CHMv2ImageProcessor.post_process_depth_estimationk  s    ( 	$(!11$3+?3|CT+To  8D8LvO 44R^"%o"DE&++77/*:]a 8 ')  NN-u56 #E r'   r   r1   )Tr=   F)r=   )3r   r   r    r!   r"   r   valid_kwargsr   BICUBICr   r   r   r   default_to_squarer   r   r   re   rd   r   r   r   r   r   r   r   rS   r   r   r   rZ   r%   r   strr   r   rv   listr   r   floatr#   rk   tupler   r   r   dictr   r&   __classcell__)rU   s   @r(   rM   rM   a   sB   :,L!))H&J%IC(D IINJLFNL#(A!B #  04
G
G &,
G 23	
G
 

G 
G& 59*C*C &,*C 	*C
 ,*C j(4/*C c>)*T1*C 
*CX4#7 D<P 9 ^$9  9  	9 
 9  L9  9  9  9  9  9  DK'$.9  4;&-9   9   $J9   !9 " Dj#9 $ +%9 ( 
)9 v+%UVZHZ +%d )*"'&Z&Z &Z L	&Z
 &Z  $J&Z  &Z 
&Z &ZV '' ' 
	'> JN''' !4c3h#884?$F' 
d3
?#	$	' 'r'   rM   )(r3   collections.abcr   typingr   rp   torch.nn.functionalr   r   r   torchvision.transforms.v2r   image_processing_backendsr   image_processing_baser   image_transformsr	   r
   image_utilsr   r   r   r   modeling_outputsr   processing_utilsr   r   utilsr   r   r   r   r   r#   r%   rK   rM   __all__r   r'   r(   <module>r      s   *  $     7 ; 1 E U U 4 4 V VE ($8$8x}$$8 $8 	$8
 $8N p, p pf	 !
!r'   