
    Ph"                         d Z ddlZddlmZmZ ddlZddl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 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)
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                       e Zd Zy)Owlv2FastImageProcessorKwargsN)__name__
__module____qualname__     a/var/www/html/saasai/venv/lib/python3.12/site-packages/transformers/models/owlv2/modular_owlv2.pyr   r   -   s    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__?   s    ''77r   imagesc                 0    t        j                  | |fi |S r%   )r   
preprocess)r'   r(   r#   s      r   r*   z"Owlv2ImageProcessorFast.preprocessB   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(   r+   r!   r"   size
pad_bottom	pad_rightpaddingpadded_images
             r   _pad_imagesz#Owlv2ImageProcessorFast._pad_imagesF   s\     RS)65!F]
5L	aJ/uuVW>Br   disable_groupingc                     t        ||      \  }}i }|j                         D ]  \  }}	| j                  |	|      }	|	||<    t        ||      }
|
S )z
        Unlike the Base class `self.pad` where all images are padded to the maximum image size,
        Owlv2 pads an image to square.
        r:   )r+   )r
   itemsr9   r   )r'   r(   r:   r+   r#   grouped_imagesgrouped_images_indexprocessed_images_groupedr0   stacked_imagesprocessed_imagess              r   r3   zOwlv2ImageProcessorFast.padS   su     0EV^n/o,,#% %3%9%9%;!E>!--- . N /=$U+ &< **BDXYr   imager4   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)r4   	antialias)r!   r"   r0   torchtensortodeviceclamp
atleast_1d	ones_likeany
ValueErrorwarningswarnceilintr2   gaussian_blurtolistresize)r'   rC   r4   rD   anti_aliasing_sigmar#   output_shapeinput_shapefactorsfilteredkernel_sizesouts               r   rY   z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$%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.InterpolationModedo_pad
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsc           	         t        ||      \  }}i }|j                         D ]   \  }}| j                  |||d|	|
      }|||<   " t        ||      }|r| j	                  |d|      }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 )
Nr<   F      ?)r+   r:   )rC   r4   rb   input_data_formatr   )dimpixel_values)datatensor_type)r
   r=   rescale_and_normalizer   r3   rY   r   FIRSTrJ   stackr   )r'   r(   ra   r4   rb   rc   rd   re   rf   rg   rh   r:   ri   r#   r>   r?   r@   r0   rA   rB   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(8_oxp/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   )rk   )TN)&r   r   r   r   BILINEARresampler   rg   r   rh   r4   re   ra   rd   rf   rc   r   valid_kwargs	crop_sizedo_center_cropr   r&   r   r   r*   floatr9   listr   boolr3   r   rY   r   strr   r   rw   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__rS   typingr   r   rJ   torchvision.transforms.v2r   r2   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   