
    Z j8                     Z   S r SSKrSSKJr  SSKJrJr  SSKrSSK	r	SSK
Jr  SSKJr  SSKJrJr  SS	KJrJr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   \(       a  SSK!J"r"  SSK#J$r%   " S S\SS9r& SSSS\'\\'   -  S\(S\'S\)\-  S-  S\*\'\'4   4S jjr+\ " S S\5      5       r,S/r-g)z#Image processor class for ZoeDepth.    N)Iterable)TYPE_CHECKINGUnion   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)IMAGENET_STANDARD_MEANIMAGENET_STANDARD_STDChannelDimension
ImageInputPILImageResamplingSizeDictget_image_sizepil_torch_interpolation_mapping)ImagesKwargsUnpack)
TensorTypeauto_docstringrequires_backends   )ZoeDepthDepthEstimatorOutput)
functionalc                   .    \ rS rSr% Sr\\S'   \\S'   Srg)ZoeDepthImageProcessorKwargs.   a  
keep_aspect_ratio (`bool`, *optional*, defaults to `self.keep_aspect_ratio`):
    If `True`, the image is resized by choosing the smaller of the height and width scaling factors and using it
    for both dimensions. This ensures that the image is scaled down as little as possible while still fitting
    within the desired output size. In case `ensure_multiple_of` is also set, the image is further resized to a
    size that is a multiple of this value by flooring the height and width to the nearest multiple of this value.
    Can be overridden by `keep_aspect_ratio` in `preprocess`.
ensure_multiple_of (`int`, *optional*, defaults to `self.ensure_multiple_of`):
    If `do_resize` is `True`, the image is resized to a size that is a multiple of this value. Works by flooring
    the height and width to the nearest multiple of this value.
    Works both with and without `keep_aspect_ratio` being set to `True`.
    Can be overridden by `ensure_multiple_of` in `preprocess`.
keep_aspect_ratioensure_multiple_of N)	__name__
__module____qualname____firstlineno____doc__bool__annotations__int__static_attributes__r        ڇ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/zoedepth/image_processing_zoedepth.pyr   r   .   s     r*   r   F)totalinput_imageztorch.Tensor | np.ndarrayoutput_sizer   multipleinput_data_formatreturnc                     SS jn[        U[        5      (       a  X4OUn[        X5      u  pgUu  pX-  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X4$ )Nc                     [         R                  " X-  5      U-  R                  [        5      nX2:  a  [        R
                  " X-  5      U-  nU$ N)nproundastyper(   mathceil)valr/   min_valxs       r+   constrain_to_multiple_of>get_resize_output_image_size.<locals>.constrain_to_multiple_ofH   sA    XXcn%088=;		#.)H4Ar*   r   )r/   )r   )
isinstancer(   r   abs)r-   r.   r   r/   r0   r=   input_heightinput_widthoutput_heightoutput_widthscale_heightscale_width
new_height	new_widths                 r+   get_resize_output_image_sizerI   A   s     1;;0L0L;,R]K .{ NL"-M !/L,Kq;#a,&6"77&L 'K),*EPXYJ()BXVI""r*   c                     ^  \ rS rSr\rSrSrSr\	r
\rSrSSS.r\R                   rSrSrS\\   SS	4U 4S
 jjr\S\S\\   S\4U 4S jj5       r   S(SSS\S\S\SSSS4S jjr  S)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jr#    S+SS S!\"\\$\\4      -  S	-  S	-  S"\"\\$\\4      -  S	-  S	-  S#\%S$   S	-  S%\S	-  S	-  S\\&\!\"4      4S& jjr'S'r(U =r)$ ),ZoeDepthImageProcessorh   Ti  i   )heightwidthg      ?kwargsr1   Nc                 &   > [         TU ]  " S0 UD6  g )Nr    )super__init__)selfrO   	__class__s     r+   rR   ZoeDepthImageProcessor.__init__v   s    "6"r*   imagesc                 &   > [         TU ]  " U40 UD6$ r4   )rQ   
preprocess)rS   rV   rO   rT   s      r+   rX   !ZoeDepthImageProcessor.preprocessy   s    w!&3F33r*   torch.Tensorsizer   r   resamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                    UR                   (       a  UR                  (       d  [        SU 35      e[        UUR                   UR                  4UU[        R
                  S9nUu  px[        U[        R                  5      (       d	  [        U   n	[        R                  R                  R                  U[        U5      [        U5      4W	R                  SS9n
U
$ )a  
Resize an image or batched images to target size `(size.height, size.width)`. If `keep_aspect_ratio` is `True`, the image
is resized to the largest possible size such that the aspect ratio is preserved. If `ensure_multiple_of` is
set, the image is resized to a size that is a multiple of this value.

Args:
    images (`torch.Tensor`):
        Images to resize.
    size (`SizeDict`):
        Target size of the output image.
    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.
    ensure_multiple_of (`int`, *optional*, defaults to 1):
        The image is resized to a size that is a multiple of this value.
    resample (`tvF.InterpolationMode`, *optional*, defaults to `InterpolationMode.BILINEAR`):
        Defines the resampling filter to use if resizing the image. Otherwise, the image is resized to size
        specified in `size`.
zDThe size dictionary must contain the keys 'height' and 'width'. Got )r.   r   r/   r0   T)modealign_corners)rM   rN   
ValueErrorrI   r   FIRSTr?   tvFInterpolationModer   torchnnr   interpolater(   value)rS   rV   r[   r   r   r\   r.   rM   rN   interpolationresized_imagess              r+   resizeZoeDepthImageProcessor.resize}   s    4 {{$**cdhcijkk2djj1/'.44
 $ (C$9$9::;HEM,,88S[#e*-M4G4GW[ 9 
 r*   c                     [        U[        R                  S9u  p#[        [        R
                  " US-  5      S-  5      n[        [        R
                  " US-  5      S-  5      n[        R                  " XU4SS9$ )z:
Args:
    images (`torch.Tensor`):
        Image to pad.
)channel_dim   r   reflect)paddingpadding_mode)r   r   ra   r(   r5   sqrtrb   pad)rS   rV   rM   rN   
pad_height	pad_widths         r+   _pad_images"ZoeDepthImageProcessor._pad_images   sg     'v;K;Q;QR!,q01
	*Q./	wwv:'>YWWr*   	do_resizedo_pad
do_rescalerescale_factordo_normalize
image_mean	image_stddisable_groupingreturn_tensorsc                 P   [        XS9u  nn0 nUR                  5        Hm  u  nnU(       a  U R                  UU	5      nU(       a  U R                  US9nU(       a  U R	                  UX4XV5      nU
(       a  U R                  UX5      nUUU'   Mo     [        UU5      n[        SU0US9$ )N)r   )rV   pixel_values)datatensor_type)r	   itemsrescalerv   rj   	normalizer
   r   )rS   rV   rx   r[   r   r   r\   ry   rz   r{   r|   r}   r~   r   r   rO   grouped_imagesgrouped_images_indexresized_images_groupedshapestacked_imagesprocessed_imagess                         r+   _preprocess"ZoeDepthImageProcessor._preprocess   s    & 0EV/o,,!#%3%9%9%;!E>!%nn!M!%!1!1!1!H!%^TVh!s!%
!V,:"5) &< **@BVW.2B!CQ_``r*   outputsr   source_sizestarget_sizesoutputs_flipped)r   Ndo_remove_paddingc                    [        U S5        UR                  nUb/  UR                  UR                  R                  :w  a  [        S5      eUb#  [	        U5      [	        U5      :w  a  [        S5      eUc  U R
                  nUc  U(       a  [        S5      eUb#  [	        U5      [	        U5      :w  a  [        S5      eUb&  U[        R                  " UR                  S/S9-   S	-  nUR                  S
5      nS=px/ n	Uc  S/[	        U5      -  OUnUc  S/[	        U5      -  OUn[        XcU5       GH/  u  pnUb  S=pU(       aP  [        [        R                  " US   S	-  5      U-  5      n[        [        R                  " US
   S	-  5      U-  5      n[        R                  " U
US   S	U-  -   US
   S	U-  -   /[        R                  R                   SS9n
US:  a  U
SS2X* 2SS24   n
US:  a  U
SS2SS2X* 24   n
Ub9  US   US
   /n[        R                  " U
U[        R                  R                   SS9n
U
R#                  S5      n
U	R%                  SU
05        GM2     U	$ )u  
Converts the raw output of [`ZoeDepthDepthEstimatorOutput`] into final depth predictions and depth PIL images.
Only supports PyTorch.

Args:
    outputs ([`ZoeDepthDepthEstimatorOutput`]):
        Raw outputs of the model.
    source_sizes (`TensorType` or `list[tuple[int, int]]`, *optional*):
        Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the source size
        (height, width) of each image in the batch before preprocessing. This argument should be dealt as
        "required" unless the user passes `do_remove_padding=False` as input to this function.
    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.
    outputs_flipped ([`ZoeDepthDepthEstimatorOutput`], *optional*):
        Raw outputs of the model from flipped input (averaged out in the end).
    do_remove_padding (`bool`, *optional*):
        By default ZoeDepth adds padding equal to `int(√(height / 2) * 3)` (and similarly for width) to fix the
        boundary artifacts in the output depth map, so we need remove this padding during post_processing. The
        parameter exists here in case the user changed the image preprocessing to not include padding.

Returns:
    `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth
    predictions.
rd   NzBMake sure that `outputs` and `outputs_flipped` have the same shapez]Make sure that you pass in as many target sizes as the batch dimension of the predicted depthzXEither `source_sizes` should be passed in, or `do_remove_padding` should be set to FalsezZMake sure that you pass in as many source image sizes as the batch dimension of the logits)dimsrn   r   r   r   F)r[   rh   	antialiaspredicted_depth)r   r   r   r`   lenry   rd   flip	unsqueezezipr(   r5   rr   rb   rj   rc   BICUBICsqueezeappend)rS   r   r   r   r   r   r   padding_factor_hpadding_factor_wresultsdepthtarget_sizesource_sizepad_hpad_ws                  r+   post_process_depth_estimation4ZoeDepthImageProcessor.post_process_depth_estimation   s   B 	$(!11'o.C.CGfGfGlGl.labb$3+?3|CT+To  $ $$5j  $3+?3|CT+Tl  &.O<[<[cebf1ggkllO)33A6 /0/8D8LvO 44R^8D8LvO 44R^/2?R^/_+E& !!$A(: ;>N NOEA(: ;>N NOE

%a.1u94k!nq5y6PQ"%"7"7"?"?#	 19!!U6\1"45E19!!Qf"45E&*1~{1~>

$"%"7"7"?"?#	 MM!$ENN-u56? 0`B r*   r    )Fr   N)rV   rZ   r4   )NNNN)*r!   r"   r#   r$   r   valid_kwargsry   rz   r|   r   r}   r   r~   rx   r[   r   BILINEARr\   r   r   r   rR   r   r   r   rX   r   r&   r(   rj   rv   listfloatstrr   r   tupler   dictr   r)   __classcell__)rT   s   @r+   rK   rK   h   s   /LFJL'J%IIC(D!**H#(D!E #$ # 4 4v>Z7[ 4`l 4 4 #("#NR-- -  	-
  - L- 
-^XX> 37!a^$!a !a 	!a
  $;!a  $J!a L!a !a !a !a !a DK'$.!a 4;&-!a +!a j(4/!a" 
#!aL JNIMNR04k/k !4c3h#884?$Fk !4c3h#884?$F	k
 CDtKk  $;-k 
d3
?#	$k kr*   rK   r4   ).r%   r8   collections.abcr   typingr   r   numpyr5   rd   image_processing_backendsr   image_processing_utilsr   image_transformsr	   r
   image_utilsr   r   r   r   r   r   r   r   processing_utilsr   r   utilsr   r   r   modeling_zoedepthr   torchvision.transforms.v2r   rb   r   r(   r&   r   r   rI   rK   __all__r    r*   r+   <module>r      s    *  $ '   ; 2 E	 	 	 5 B B ? 7<u 0 8<$#,$#x}$$# $# 	$#
 --4$# 38_$#N a/ a aH $
$r*   