
    cCi"                         S r SSKrSSKJrJr  SSKrSSKJr  SSK	J
r
Jr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  SS
KJrJr  SSKJr   " S S\5      r\ " S S\5      5       rS/r g)z%Fast Image processor class for OWLv2.    N)OptionalUnion)
functional   )BaseImageProcessorFastBatchFeatureDefaultFastImageProcessorKwargs)group_images_by_shapereorder_images)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimension
ImageInputPILImageResamplingSizeDict)Unpack)
TensorTypeauto_docstring   )OwlViTImageProcessorFastc                       \ rS rSrSrg)Owlv2FastImageProcessorKwargs-    N)__name__
__module____qualname____firstlineno____static_attributes__r       a/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/owlv2/modular_owlv2.pyr   r   -   s    cr    r   c                      \ rS rSr\R
                  r\r\	r
SSS.rSrSrSrSrSr\rSrSrS\\   4S jr\S	\S\\   4S
 j5       rS!S	SS\SS4S jjr S!S	\S   S\\   S\S\S   4S jjr  S"SSS\ S\SS4S jjr!S	\S   S\S\ S\S   S\S\S\S\S\\"\\\   4      S\\"\\\   4      S\\   S\\"\#\$4      S\%4S jr&S r'g)#Owlv2ImageProcessorFast0   i  )heightwidthgp?TNkwargsc                 2    [         R                  " U 40 UD6  g N)r   __init__)selfr'   s     r!   r*    Owlv2ImageProcessorFast.__init__?   s    ''77r    imagesc                 0    [         R                  " X40 UD6$ r)   )r   
preprocess)r+   r-   r'   s      r!   r/   "Owlv2ImageProcessorFast.preprocessB   s    %00HHHr    ztorch.Tensorconstant_valuereturnc                     UR                   SS u  p4[        X45      nXS-
  nXT-
  nSSXv4n[        R                  " XUS9n	U	$ )z,
Pad an image with zeros to the given size.
Nr   )fill)shapemaxFpad)
r+   r-   r1   r%   r&   size
pad_bottom	pad_rightpaddingpadded_images
             r!   _pad_images#Owlv2ImageProcessorFast._pad_imagesF   sP     RS)6!]
L	a/uuV>Br    disable_groupingc                     [        XS9u  pV0 nUR                  5        H  u  pU R                  U	US9n	XU'   M     [        Xv5      n
U
$ )zx
Unlike the Base class `self.pad` where all images are padded to the maximum image size,
Owlv2 pads an image to square.
rA   )r1   )r
   itemsr?   r   )r+   r-   rA   r1   r'   grouped_imagesgrouped_images_indexprocessed_images_groupedr6   stacked_imagesprocessed_imagess              r!   r9   Owlv2ImageProcessorFast.padS   sg     0EV/o,#% %3%9%9%;!E!--- . N /=U+ &< **BYr    imager:   anti_aliasingc                    UR                   UR                  4nUR                  n[        R                  " USS 5      R                  UR                  5      [        R                  " U5      R                  UR                  5      -  nU(       Ga#  Uc  US-
  S-  R                  SS9nO[        R                  " U5      [        R                  " U5      -  n[        R                  " US:  5      (       a  [        S5      e[        R                  " US:  US:*  -  5      (       a  [        R                  " S5        [        R                  " US:H  5      (       a  Un	O[S[        R                  " SU-  5      R                  5       -  S-   n
[         R"                  " XS   U
S   4UR%                  5       S	9n	OUn	[         R&                  " XR                   UR                  4S
S9nU$ )a  
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&   r6   torchtensortodeviceclamp
atleast_1d	ones_likeany
ValueErrorwarningswarnceilintr8   gaussian_blurtolistresize)r+   rK   r:   rL   anti_aliasing_sigmar'   output_shapeinput_shapefactorsfilteredkernel_sizesouts               r!   ra   Owlv2ImageProcessorFast.resizek   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5$%mnnYY 3a 7GqLIJJMMq yy,122  5::a2E.E#F#J#J#LLqP??O\!_=EXE_E_Ea
 Hhhx{{DJJ&?5Q
r    	do_resizeinterpolationzF.InterpolationModedo_pad
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsc           	      |   [        XS9u  p0 nUR                  5        H  u  nnU R                  UXgSX5      nUUU'   M!     [        UU5      nU(       a  U R	                  USUS9n[        UUS9u  p0 nUR                  5        H4  u  nnU(       d  M  U R                  UUU[        R                  S9nUUU'   M6     [        UU5      n[        UUS9u  p0 nUR                  5        H  u  nnU R                  USXxX5      nUUU'   M!     [        UU5      nU(       a  [        R                  " USS9OUn[        SU0US	9$ )
NrC   F      ?)r1   rA   )rK   r:   rk   input_data_formatr   )dimpixel_values)datatensor_type)r
   rD   rescale_and_normalizer   r9   ra   r   FIRSTrR   stackr   )r+   r-   rj   r:   rk   rl   rm   rn   ro   rp   rq   rA   rr   r'   rE   rF   rG   r6   rH   rI   resized_images_groupedresized_stackresized_imagess                          r!   _preprocess#Owlv2ImageProcessorFast._preprocess   s   " 0EV/o,#% %3%9%9%;!E>!77
E:N /=$U+ &< **BDXY#xx(8_oxp/D/?0
, "$%3%9%9%;!E>y $("/&6&<&<	 !, ! 1>&u- &< ((>@TU 0E^fv/w,#% %3%9%9%;!E>!77~ZN /=$U+ &< **BDXYCQ5;;'7Q?Wg.2B!CQ_``r    r   )rt   )TN)(r   r   r   r   r   BILINEARresampler   rp   r   rq   r:   rn   rj   rm   ro   rl   r   valid_kwargs	crop_sizedo_center_cropr   r*   r   r   r/   floatr?   listr   boolr9   r   ra   r   strr   r   r   r   r   r    r!   r#   r#   0   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	 8 # 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#   )!__doc__r[   typingr   r   rR   torchvision.transforms.v2r   r8   image_processing_utils_fastr   r   r	   image_transformsr
   r   image_utilsr   r   r   r   r   r   processing_utilsr   utilsr   r   #owlvit.image_processing_owlvit_fastr   r   r#   __all__r   r    r!   <module>r      sy    ,  "  5 
 F  ' K J$C I pa6 pa paf %
%r    