
    hx#                         d Z ddl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mZmZ ddlmZ dd	lmZmZmZmZmZ d
dlmZ  e       rddlZ e       rddlmZ  n e       rddl!mZ   G d de      Z"e G d de             Z#dgZ$y)z%Fast Image processor class for OWLv2.    N)OptionalUnion   )BaseImageProcessorFastBatchFeatureDefaultFastImageProcessorKwargs)group_images_by_shapereorder_images)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimension
ImageInputPILImageResamplingSizeDict)Unpack)
TensorTypeauto_docstringis_torch_availableis_torchvision_availableis_torchvision_v2_available   )OwlViTImageProcessorFast)
functionalc                   "    e Zd ZU dZee   ed<   y)Owlv2FastImageProcessorKwargsa  
    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 grey pixels.
    do_padN)__name__
__module____qualname____doc__r   bool__annotations__     e/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/transformers/models/owlv2/modular_owlv2.pyr   r   7   s     TNr$   r   c                   t   e Zd Zej                  ZeZeZ	dddZ
dZdZdZdZdZeZdZdZdee   fdZededee   fd	       Zddd
dedd
fdZ	 dded
   dee   deded
   fdZ	 	 d dd
dededd
fdZ ded
   dededed   dededed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$fdZ%y)!Owlv2ImageProcessorFasti  )heightwidthgp?TNkwargsc                 0    t        j                  | fi | y N)r   __init__)selfr*   s     r%   r-   z Owlv2ImageProcessorFast.__init__P   s    ''77r$   imagesc                 0    t        j                  | |fi |S r,   )r   
preprocess)r.   r/   r*   s      r%   r1   z"Owlv2ImageProcessorFast.preprocessS   s    %00vHHHr$   ztorch.Tensorconstant_valuereturnc                     |j                   dd \  }}t        ||      }||z
  }||z
  }dd||f}t        j                  |||      }	|	S )z<
        Pad an image with zeros to the given size.
        Nr   )fill)shapemaxFpad)
r.   r/   r2   r(   r)   size
pad_bottom	pad_rightpaddingpadded_images
             r%   _pad_imagesz#Owlv2ImageProcessorFast._pad_imagesW   s\     RS)65!F]
5L	aJ/uuVW>Br$   disable_groupingc                     t        ||      \  }}i }|j                         D ]  \  }}| j                  ||      }|||<    t        ||      }	|	S )NrA   )r2   )r	   itemsr@   r
   )
r.   r/   rA   r2   grouped_imagesgrouped_images_indexprocessed_images_groupedr7   stacked_imagesprocessed_imagess
             r%   r:   zOwlv2ImageProcessorFast.padd   sz     0EV^n/o,,#% %3%9%9%; 	=!E>!--- . N /=$U+	= **BDXYr$   imager;   anti_aliasingc                    |j                   |j                  f}|j                  }t        j                  |dd       j                  |j                        t        j                  |      j                  |j                        z  }|r||dz
  dz  j                  d      }nt        j                  |      t        j                  |      z  }t        j                  |dk        rt        d      t        j                  |dkD  |dk  z        rt        j                  d       t        j                  |dk(        r|}	n]dt        j                  d|z        j                         z  dz   }
t!        j"                  ||
d   |
d   f|j%                         	      }	n|}	t!        j&                  |	|j                   |j                  fd
      }|S )az  
        Resize an image as per the original implementation.

        Args:
            image (`Tensor`):
                Image to resize.
            size (`dict[str, int]`):
                Dictionary containing the height and width to resize the image to.
            anti_aliasing (`bool`, *optional*, defaults to `True`):
                Whether to apply anti-aliasing when downsampling the image.
            anti_aliasing_sigma (`float`, *optional*, defaults to `None`):
                Standard deviation for Gaussian kernel when downsampling the image. If `None`, it will be calculated
                automatically.
        r   N   r   )minzFAnti-aliasing standard deviation must be greater than or equal to zerozWAnti-aliasing standard deviation greater than zero but not down-sampling along all axesr   )sigmaF)r;   	antialias)r(   r)   r7   torchtensortodeviceclamp
atleast_1d	ones_likeany
ValueErrorwarningswarnceilintr9   gaussian_blurtolistresize)r.   rJ   r;   rK   anti_aliasing_sigmar*   output_shapeinput_shapefactorsfilteredkernel_sizesouts               r%   r`   zOwlv2ImageProcessorFast.resizew   s   , TZZ0kk ,,{12/225<<@5<<P\C]C`C`afamamCnn"*(/!q'8&?&?A&?&F#&+&6&67J&Keoo^eNf&f#990145$%mnnYY 3a 7GqLIJMMq yy,12  5::a2E.E#F#J#J#LLqP??LO\!_=EXE_E_Ea
 Hhhxt{{DJJ&?5Q
r$   	do_resizeinterpolationzF.InterpolationModer   
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsc           	         t        ||      \  }}i }|j                         D ]   \  }}| j                  |||d|	|
      }|||<   " t        ||      }|r| j	                  ||      }t        ||      \  }}i }|j                         D ]0  \  }}|s	| j                  |||t        j                        }|||<   2 t        ||      }t        ||      \  }}i }|j                         D ]   \  }}| j                  |d|||	|
      }|||<   " t        ||      }|rt        j                  |d      n|}t        d|i|      S )NrC   F)rJ   r;   ri   input_data_formatr   )dimpixel_values)datatensor_type)r	   rD   rescale_and_normalizer
   r:   r`   r   FIRSTrQ   stackr   )r.   r/   rh   r;   ri   r   rj   rk   rl   rm   rn   rA   ro   r*   rE   rF   rG   r7   rH   rI   resized_images_groupedresized_stackresized_imagess                          r%   _preprocessz#Owlv2ImageProcessorFast._preprocess   s   " 0EV^n/o,,#% %3%9%9%; 	=!E>!77
NE:yN /=$U+	= **BDXY#xx(8K[x\/D/?0
,, "$%3%9%9%; 	>!E> $("/&6&<&<	 !, ! 1>&u-	> ((>@TU 0E^fv/w,,#% %3%9%9%; 	=!E>!77~|ZQZN /=$U+	= **BDXYCQ5;;'7Q?Wg.2B!CQ_``r$   )g      ?)TN)&r   r   r   r   BILINEARresampler   rm   r   rn   r;   rk   rh   rj   rl   r   r   valid_kwargs	crop_sizedo_center_cropr   r-   r   r   r1   floatr@   listr   r!   r:   r   r`   r   strr   r   r|   r#   r$   r%   r'   r'   A   s   !**H!JIC(DNIJLF0LIN8(E!F 8 I Iv>[7\ I I. % R` " !$	 ^$  #4.  	 
 
n	 . # 66 6 	6 
6p>a^$>a >a 	>a
   56>a >a >a >a >a U5$u+#567>a E%e"456>a #4.>a !sJ!78>a 
>ar$   r'   )%r    rZ   typingr   r   image_processing_utils_fastr   r   r   image_transformsr	   r
   image_utilsr   r   r   r   r   r   processing_utilsr   utilsr   r   r   r   r   #owlvit.image_processing_owlvit_fastr   rQ   torchvision.transforms.v2r   r9   torchvision.transformsr   r'   __all__r#   r$   r%   <module>r      s    ,  " 
 F  '  K   96$C  ka6 ka ka\ %
%r$   