o
    eiF                     @   s   d Z ddlZddlmZ ddlZddlm  m  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 deZdgZdS )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                   @   s  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dZ	d!ded dedB d	ed
ed fddZ		d"dddeded
dfddZded dededed dededededeee B dB deee B dB dedB deeB dB d
efdd ZdS )#Owlv2ImageProcessorFasti  )heightwidthgp?TN        imagesztorch.Tensorconstant_valuereturnc           
      C   sL   |j dd \}}t||}|| }|| }dd||f}tj|||d}	|	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_image r#   e/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/models/owlv2/modular_owlv2.py_pad_images6   s   
z#Owlv2ImageProcessorFast._pad_imagesdisable_groupingc                 K   sJ   t ||d\}}i }| D ]\}}	| j|	|d}	|	||< q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_imagesr#   r#   r$   r   C   s   

zOwlv2ImageProcessorFast.padimager   anti_aliasingc                 K   s  |j |jf}|j}t|dd |jt||j }|r}|du r0|d d jdd}n%t|t	| }t
|dk rEtdt
|dk|dk@ rUtd t
|dkr_|}	n dtd|   d }
tj||
d |
d f| d	}	n|}	tj|	|j |jfd
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   r/   r   r0   anti_aliasing_sigmar)   output_shapeinput_shapefactorsfilteredkernel_sizesoutr#   r#   r$   rD   [   s,   ,zOwlv2ImageProcessorFast.resize	do_resizeinterpolationztvF.InterpolationModedo_pad
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsc              	   K   s  t ||d\}}i }| D ]\}}| |||d|	|
}|||< qt||}|r0| j|d|d}t ||d\}}i }| D ]\}}|rR| j|||tjd}|||< q>t||}t ||d\}}i }| D ]\}}| |d|||	|
}|||< qft||}td|i|dS )Nr'   Fr   )r   r&   )r/   r   rM   input_data_formatpixel_values)datatensor_type)	r   r(   rescale_and_normalizer   r   rD   r	   FIRSTr   )r   r   rL   r   rM   rN   rO   rP   rQ   rR   rS   r&   rT   r)   r*   r+   r,   r   r-   r.   resized_images_groupedresized_stackresized_imagesr#   r#   r$   _preprocess   sD   





z#Owlv2ImageProcessorFast._preprocess)r   )TN)__name__
__module____qualname__r
   BILINEARresampler   rR   r   rS   r   rP   rL   rO   rQ   rN   	crop_sizedo_center_cropfloatr%   listboolr   r   rD   r   strr   r   r^   r#   r#   r#   r$   r   (   sz    


8	

r   )__doc__r>   typingr   r5   $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__r#   r#   r#   r$   <module>   s    
*