
    ^ja                         d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZ ddlmZmZ dd	lmZmZmZmZ erdd
lmZ ddlZddlmZ  ej8                  e      Z G d ded      Ze G d de             Z dgZ!y)zImage processor class for GLPN.    )TYPE_CHECKING   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstringloggingrequires_backends)DepthEstimatorOutputN)
functionalc                       e Zd ZU dZeed<   y)GLPNImageProcessorKwargsz
    size_divisor (`int`, *optional*, defaults to 32):
        When `do_resize` is `True`, images are resized so their height and width are rounded down to the closest
        multiple of `size_divisor`.
    size_divisorN)__name__
__module____qualname____doc__int__annotations__     y/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/glpn/image_processing_glpn.pyr   r   (   s     r   r   F)totalc            #           e Zd ZdZeZdZdZdZe	j                  ZdZdee   f fdZ fdZededee   d	ef fd
       Z	 d$dddeddded	df
 fdZ	 d$ded   dededddedededededeee   z  dz  deee   z  dz  dedz  dedz  dedz  deez  dz  ded	ef"dZ	 d%d d!d"eeeeef      z  dz  d	eeeef      fd#Z xZ S )&GLPNImageProcessorz6Torchvision backend for GLPN with size_divisor resize.Tgp?    kwargsc                 $    t        |   di | y )Nr   )super__init__selfr$   	__class__s     r   r'   zGLPNImageProcessor.__init__>   s    "6"r   c                 F    |j                  dd        t        |   di |S )N	do_resizer   )popr&   _validate_preprocess_kwargsr(   s     r   r.   z.GLPNImageProcessor._validate_preprocess_kwargsA   s$    

;%w2<V<<r   imagesreturnc                 $    t        |   |fi |S N)r&   
preprocess)r)   r/   r$   r*   s      r   r3   zGLPNImageProcessor.preprocessF   s    w!&3F33r   imageztorch.Tensorsizeresamplez7PILImageResampling | tvF.InterpolationMode | int | Noner   c                     |j                   dd \  }}||z  |z  }||z  |z  }	t        
|   |t        ||	      fd|i|S )zTResize so height and width are rounded down to the closest multiple of size_divisor.N)heightwidthr6   )shaper&   resizer   )r)   r4   r5   r6   r   r$   r9   r:   new_hnew_wr*   s             r   r<   zGLPNImageProcessor.resizeJ   si     BC(,&5%4w~E/
 
 	
 	
r   r,   do_center_crop	crop_size
do_rescalerescale_factordo_normalize
image_meanN	image_stddo_padpad_sizedisable_groupingreturn_tensorsc           	          t        ||      \  }}i }|j                         D ]7  \  }}|r| j                  ||||      }| j                  ||||	|
|      }|||<   9 t	        ||      }t        d|i|      S )zCustom preprocessing for GLPN.)rH   )r   pixel_values)datatensor_type)r   itemsr<   rescale_and_normalizer   r   )r)   r/   r,   r5   r6   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   r   r$   grouped_imagesgrouped_images_indexprocessed_images_groupedr;   stacked_imagesprocessed_imagess                           r   _preprocesszGLPNImageProcessor._preprocess]   s    * 0EV^n/o,,#% %3%9%9%; 	=!E>!%^T8Zf!g!77
NL*V_N /=$U+	= **BDXY.2B!CQ_``r   outputsr   target_sizesc                    t        | d       |j                  }|"t        |      t        |      k7  rt        d      g }|dgt        |      z  n|}t	        ||      D ]l  \  }}|R|d   }t
        j                  j                  j                  ||dd      }|j                  d      j                  d      }|j                  d	|i       n |S )
zf
        Convert raw model outputs to final depth predictions.
        Only supports PyTorch.
        torchNz]Make sure that you pass in as many target sizes as the batch dimension of the predicted depth)NN.bicubicF)r5   modealign_cornersr   predicted_depth)r   r]   len
ValueErrorziprY   nnr   interpolatesqueezeappend)r)   rV   rW   r]   resultsdepthtarget_sizes          r   post_process_depth_estimationz0GLPNImageProcessor.post_process_depth_estimation~   s     	$(!11#O(<L@Q(Qo  8D8LvO 44R^"%o|"D 	7E;&o.++77KV_ot7ua(003NN-u56	7 r   )r#   r2   )!r   r   r   r   r   valid_kwargsr,   rA   rB   r
   BILINEARr6   r   r   r'   r.   r   r	   r   r3   r   r   r<   listboolfloatstrr   rU   tupledictrh   __classcell__)r*   s   @r   r"   r"   2   s   @+LIJN!**HL#(@!A #=
 4 4v>V7W 4\h 4 4 

 
 L	

 
 

H #a^$a a 	a
 La a a a a a DK'$.a 4;&-a ta T/a +a  j(4/!a" #a& 
'aH CG' !4c3h#884? 
d3
?#	$	r   r"   )"r   typingr   image_processing_backendsr   image_processing_utilsr   image_transformsr   r   image_utilsr	   r
   r   processing_utilsr   r   utilsr   r   r   r   modeling_outputsr   rY   torchvision.transforms.v2r   tvF
get_loggerr   loggerr   r"   __all__r   r   r   <module>r      s    &   ; 2 E 
 5 K K 8  7 
		H	%|5  b+ b bJ  
 r   