
    Z j%$                         S 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JrJr  SS
KJrJr  SSKJrJrJr   " S S\SS9r\ " S S\5      5       r S/r!g)zImage processor class for BEiT.    )UnionN)
functional   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)IMAGENET_STANDARD_MEANIMAGENET_STANDARD_STDChannelDimension
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstringis_torch_availablec                   $    \ rS rSr% Sr\\S'   Srg)BeitImageProcessorKwargs%   aW  
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.
do_reduce_labels N)__name__
__module____qualname____firstlineno____doc__bool__annotations____static_attributes__r       /root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/beit/image_processing_beit.pyr   r   %   s     r"   r   F)totalc                     ^  \ rS rSrSr\r\R                  r	\
r\rSSS.rSrSSS.rSrSrSrSrSrS\\   4U 4S jjr\ S(S
\S\S	-  S\\   S\4U 4S jjj5       r S(S
\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   4S$ jjr&S(S%\"\'   S	-  4S& jjr(S'r)U =r*$ )*BeitImageProcessor0   z/PIL backend for BEiT with reduce_label support.   )heightwidthTFkwargsc                 &   > [         TU ]  " S0 UD6  g )Nr   )super__init__)selfr+   	__class__s     r#   r.   BeitImageProcessor.__init__B   s    "6"r"   Nimagessegmentation_mapsreturnc                 &   > [         TU ]  " X40 UD6$ )zX
segmentation_maps (`ImageInput`, *optional*):
    The segmentation maps to preprocess.
)r-   
preprocess)r/   r2   r3   r+   r0   s       r#   r6   BeitImageProcessor.preprocessE   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.)r2   r8   r9   r;   Fr   pixel_values   )r2   expected_ndimsr8   r9   )do_normalize
do_rescaler2   r   labels)datatensor_typer   )_prepare_image_like_inputscopy_preprocessr   FIRSTupdatesqueezetotorchint64r   )r/   r2   r3   r8   r9   r:   r;   r+   images_kwargsrC   processed_segmentation_mapssegmentation_maps_kwargsprocessed_segmentation_maps                r#   _preprocess_image_like_inputs0BeitImageProcessor._preprocess_image_like_inputsR   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$rB   ztorch.Tensorc           
      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;         )rangelenrL   wheretensorrV   r;   )r/   rB   idxlabels       r#   reduce_labelBeitImageProcessor.reduce_label~   s    V%CKEKK
ELLEKKX]XdXd,eglmEAIEKKell3ekkZ_ZfZf.ginoE3K & r"   	do_resizesizeresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonedo_center_crop	crop_sizerA   rescale_factorr@   
image_mean	image_stddisable_groupingr   c           	         U(       a  U R                  U5      n[        XS9u  nn0 nUR                  5        H$  u  nnU(       a  U R                  UX45      nUUU'   M&     [	        UU5      n[        UUS9u  nn0 nUR                  5        H8  u  nnU(       a  U R                  UU5      nU R                  UXxXU5      nUUU'   M:     [	        UU5      nU$ )zCustom preprocessing for BEiT.)ri   )r_   r   itemsresizer	   center_croprescale_and_normalize)r/   r2   ra   rb   rc   rd   re   rA   rf   r@   rg   rh   ri   r   r+   grouped_imagesgrouped_images_indexresized_images_groupedshapestacked_imagesresized_imagesprocessed_images_groupedprocessed_imagess                          r#   rG   BeitImageProcessor._preprocess   s    $ &&v.F 0EV/o,,!#%3%9%9%;!E>!%^T!L,:"5) &< ((>@TU 0E^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N /=$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 [`BeitForSemanticSegmentation`] into semantic segmentation maps.

Args:
    outputs ([`BeitForSemanticSegmentation`]):
        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)rb   modealign_cornersrW   )r   ImportErrorlogitsrZ   
ValueError
isinstancerL   TensornumpyrY   Finterpolate	unsqueezeargmaxappendrr   )	r/   outputsrx   r   semantic_segmentationr]   resized_logitssemantic_mapis	            r#   "post_process_semantic_segmentation5BeitImageProcessor.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   )N)F)+r   r   r   r   r   r   valid_kwargsr   BICUBICrc   r
   rg   r   rh   rb   default_to_squarere   ra   rd   rA   r@   r   r   r.   r   r   r   r6   r   r   strr   r   rR   listr_   r   floatrG   tupler   r!   __classcell__)r0   s   @r#   r&   r&   0   s#   9+L!))H'J%IC(D-IINJL#(@!A #  04
G
G &,
G 12	
G
 

G 
G& 59*C*C &,*C 	*C
 ,*C j(4/*C c>)*T1*C 
*CX4#7 D<P 0 "', ^$,  ,  	, 
 L,  ,  ,  ,  ,  ,  DK'$.,  4;&-,  +,  ,   
n	!, \+%UVZHZ +% +%r"   r&   )"r   typingr   rL   torch.nn.functionalnnr   r   torchvision.transforms.v2tvFimage_processing_backendsr   image_processing_utilsr   image_transformsr   r	   image_utilsr
   r   r   r   r   r   processing_utilsr   r   utilsr   r   r   r   r&   __all__r   r"   r#   <module>r      so    &     7 ; 2 E  5 C C|5  p%+ p% p%f  
 r"   