
    iF                         d Z ddlZddlmZ ddlZddlmc mc mZ	 ddl
mZ ddlmZmZ ddlmZmZmZmZmZ ddlmZmZ d	d
lmZ e G d de             ZdgZy)z%Fast Image processor class for OWLv2.    N)Optional   )BatchFeature)group_images_by_shapereorder_images)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimensionPILImageResamplingSizeDict)
TensorTypeauto_docstring   )OwlViTImageProcessorFastc                   2   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dZdZdddded	dfd
Z	 dded   dedz  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   z  dz  deee   z  dz  dedz  deez  dz  d	efdZy)Owlv2ImageProcessorFasti  )heightwidthgp?TNimagesz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tvFpad)
selfr   r   r   r   size
pad_bottom	pad_rightpaddingpadded_images
             c/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/models/owlv2/modular_owlv2.py_pad_imagesz#Owlv2ImageProcessorFast._pad_images6   s\     RS)65!F]
5L	aJ/wwvw^D    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   itemsr&   r   )r   r   r(   r   kwargsgrouped_imagesgrouped_images_indexprocessed_images_groupedr   stacked_imagesprocessed_imagess              r%   r   zOwlv2ImageProcessorFast.padC   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   r   torchtensortodeviceclamp
atleast_1d	ones_likeany
ValueErrorwarningswarnceilintr   gaussian_blurtolistresize)r   r2   r    r3   anti_aliasing_sigmar,   output_shapeinput_shapefactorsfilteredkernel_sizesouts               r%   rH   zOwlv2ImageProcessorFast.resize[   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jjdjj(AUS
r'   	do_resizeinterpolationztvF.InterpolationModedo_pad
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsc           	      R   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        ||      }t        d|i|      S )Nr*   F        )r   r(   )r2   r    rQ   input_data_formatpixel_values)datatensor_type)	r   r+   rescale_and_normalizer   r   rH   r
   FIRSTr   )r   r   rP   r    rQ   rR   rS   rT   rU   rV   rW   r(   rX   r,   r-   r.   r/   r   r0   r1   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.2B!CQ_``r'   )rZ   )TN)__name__
__module____qualname__r   BILINEARresampler   rV   r	   rW   r    rT   rP   rS   rU   rR   	crop_sizedo_center_cropfloatr&   listboolr   r   rH   r   strr   r   rd    r'   r%   r   r   (   s   !**H!JIC(DNIJLFIN. % R` " !$	 ^$  +  	  
n	 8 # 66 6 	6 
6p<a^$<a <a 	<a
   78<a <a <a <a <a DK'$.<a 4;&-<a +<a j(4/<a 
<ar'   r   )__doc__rB   typingr   r9   $torchvision.transforms.v2.functional
transformsv2
functionalr   image_processing_utils_fastr   image_transformsr   r   image_utilsr   r	   r
   r   r   utilsr   r   #owlvit.image_processing_owlvit_fastr   r   __all__rp   r'   r%   <module>r}      sf    ,    2 2 F  K fa6 fa faR %
%r'   