
    ^jsR                     8   U d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	Z	ddl
mZ ddlmZ dd	l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mZ ddlm Z m!Z! eZ"eZ#g dZ$g dZ%dZ&e'e(d<    G d ded      Z) G d d      Z*e! G d de             Z+dgZ,y)z Image processor class for Flava.    N)Iterable)	lru_cache)Any)
functional   )TorchvisionBackend)BatchFeatureget_size_dict)group_images_by_shapereorder_images)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimension
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstring)        r   r   )      ?r   r   g?LOGIT_LAPLACE_EPSc                      e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   eed<   eed	<   eed
<   ee	ef   ed<   eed<   eed<   ee	ef   ed<   eed<   eez  ed<   eed<   eed<   ee
e   z  ed<   ee
e   z  ed<   y)FlavaImageProcessorKwargsa  
    return_image_mask (`bool`, *optional*, defaults to `False`):
        Whether to return the image mask. Can be overridden by the `return_image_mask` parameter in `preprocess`.
    input_size_patches (`int`, *optional*, defaults to `14`):
        Number of patches in the image in height and width direction. 14x14 = 196 total patches. Can be overridden
        by the `input_size_patches` parameter in `preprocess`.
    total_mask_patches (`int`, *optional*, defaults to `75`):
        Total number of patches that should be masked. Can be overridden by the `total_mask_patches` parameter in
        `preprocess`.
    mask_group_min_patches (`int`, *optional*, defaults to `16`):
        Minimum number of patches that should be masked. Can be overridden by the `mask_group_min_patches`
        parameter in `preprocess`.
    mask_group_max_patches (`int`, *optional*):
        Maximum number of patches that should be masked. Can be overridden by the `mask_group_max_patches`
        parameter in `preprocess`.
    mask_group_min_aspect_ratio (`float`, *optional*, defaults to `0.3`):
        Minimum aspect ratio of the mask window. Can be overridden by the `mask_group_min_aspect_ratio` parameter
        in `preprocess`.
    mask_group_max_aspect_ratio (`float`, *optional*):
        Maximum aspect ratio of the mask window. Can be overridden by the `mask_group_max_aspect_ratio` parameter
        in `preprocess`.
    return_codebook_pixels (`bool`, *optional*, defaults to `False`):
        Whether to return the codebook pixel values.
    codebook_do_resize (`bool`, *optional*, defaults to `True`):
        Whether to resize the input for codebook to a certain. Can be overridden by the `codebook_do_resize`
        parameter in `preprocess`. `codebook_size`.
    codebook_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`):
        Resize the input for codebook to the given size. Can be overridden by the `codebook_size` parameter in
        `preprocess`.
    codebook_resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`):
        Resampling filter to use if resizing the codebook image. With torchvision < 0.27, LANCZOS is not
        supported for torch Tensors and BICUBIC is used as the closest alternative. Can be overridden by the
        `codebook_resample` parameter in `preprocess`.
    codebook_do_center_crop (`bool`, *optional*, defaults to `True`):
        Whether to crop the input for codebook at the center. If the input size is smaller than
        `codebook_crop_size` along any edge, the image is padded with 0's and then center cropped. Can be
        overridden by the `codebook_do_center_crop` parameter in `preprocess`.
    codebook_crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`):
        Desired output size for codebook input when applying center-cropping. Can be overridden by the
        `codebook_crop_size` parameter in `preprocess`.
    codebook_do_rescale (`bool`, *optional*, defaults to `True`):
        Whether to rescale the input for codebook by the specified scale `codebook_rescale_factor`. Can be
        overridden by the `codebook_do_rescale` parameter in `preprocess`.
    codebook_rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
        Defines the scale factor to use if rescaling the codebook image. Can be overridden by the
        `codebook_rescale_factor` parameter in `preprocess`.
    codebook_do_map_pixels (`bool`, *optional*, defaults to `True`):
        Whether to map the pixel values of the codebook input to (1 - 2e)x + e. Can be overridden by the
        `codebook_do_map_pixels` parameter in `preprocess`.
    codebook_do_normalize (`bool`, *optional*, defaults to `True`):
        Whether or not to normalize the input for codebook with `codebook_image_mean` and `codebook_image_std`. Can
        be overridden by the `codebook_do_normalize` parameter in `preprocess`.
    codebook_image_mean (`Optional[Union[float, Iterable[float]]]`, *optional*, defaults to `[0, 0, 0]`):
        The sequence of means for each channel, to be used when normalizing images for codebook. Can be overridden
        by the `codebook_image_mean` parameter in `preprocess`.
    codebook_image_std (`Optional[Union[float, Iterable[float]]]`, *optional*, defaults to `[0.5, 0.5, 0.5]`):
        The sequence of standard deviations for each channel, to be used when normalizing images for codebook. Can
        be overridden by the `codebook_image_std` parameter in `preprocess`.
    return_image_maskinput_size_patchestotal_mask_patchesmask_group_min_patchesmask_group_max_patchesmask_group_min_aspect_ratiomask_group_max_aspect_ratioreturn_codebook_pixelscodebook_do_resizecodebook_sizecodebook_resamplecodebook_do_center_cropcodebook_crop_sizecodebook_do_rescalecodebook_rescale_factorcodebook_do_map_pixelscodebook_do_normalizecodebook_image_meancodebook_image_stdN)__name__
__module____qualname____doc__bool__annotations__intfloatdictstrr        {/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/flava/image_processing_flava.pyr   r   3   s    :z !&&!&&  S>!!!S#X& 5[(  %00//r:   r   F)totalc                   t    e Zd Z	 	 	 	 	 	 ddeeeef   z  dededz  dededz  dedz  fdZd	 Zd
 Zd Z	d Z
y)FlavaMaskingGeneratorN
input_sizer   r    r   r!   r"   c                 ,   t        |t              s|fdz  }|\  | _        | _        | j                  | j                  z  | _        || _        || _        ||n|| _        |xs d|z  }t        j                  |      t        j                  |      f| _
        y )N      )
isinstancetupleheightwidthnum_patchesr   r   r    mathloglog_aspect_ratio)selfr?   r   r    r   r!   r"   s          r;   __init__zFlavaMaskingGenerator.__init__   s     *e,$*J",TZ;;3"4&<#<R<Z&8`v#&A&dQIdEd#!%*E!FQlHm nr:   c           	          d| j                   | j                  | j                  | j                  | j                  | j
                  d   | j
                  d   fz  }|S )Nz<MaskingGenerator(%d, %d -> [%d ~ %d], max = %d, %.3f ~ %.3f)r   rB   )rE   rF   r   r    r   rJ   )rK   repr_strs     r;   __repr__zFlavaMaskingGenerator.__repr__   s^    QKKJJ''''##!!!$!!!$U
 
 r:   c                 2    | j                   | j                  fS NrE   rF   )rK   s    r;   	get_shapezFlavaMaskingGenerator.get_shape   s    {{DJJ&&r:   c           	      ,   d}t        d      D ]  }t        j                  | j                  |      }t	        j
                  t        j                  | j                         }t        t        t	        j                  ||z                    }t        t        t	        j                  ||z                    }|| j                  k  s|| j                  k  st        j                  d| j                  |z
        }	t        j                  d| j                  |z
        }
||	|	|z   |
|
|z   f   j                         }d||z  |z
  cxk  r|k  r>n n;||	|	|z   |
|
|z   f   dk(  }d||	|	|z   |
|
|z   f   |<   ||j                         z  }|dkD  s |S  |S )Nr   
   rB   )rangerandomuniformr   rH   exprJ   r5   roundsqrtrF   rE   randintsum)rK   maskmax_mask_patchesdelta_attempttarget_areaaspect_ratiorE   rF   topleft
num_masked	zeros_poss                r;   _maskzFlavaMaskingGenerator._mask   s   b	 	H ..)D)DFVWK88FNND4I4I$JKLtyy|)CDEFFdiil(BCDEEtzz!ft{{&:nnQf(<=~~ae);<!#f"4dTE\6I"IJNNP
v~
2F6FF $S3<%7u9L%L MQR RIOPDsV|+TD5L-@@A)LY]]_,E19%	$ r:   c                 6   t        j                  | j                         t         j                        }d}|| j                  k  rT| j                  |z
  }t        || j                        }| j                  ||      }|dk(  r	 |S ||z  }|| j                  k  rT|S )N)dtyper   )torchzerosrS   r5   r   minr    rh   )rK   r^   
mask_countr_   r`   s        r;   __call__zFlavaMaskingGenerator.__call__   s    {{4>>+599=
4222#66C"#3T5P5PQJJt%56Ez  e#
 4222 r:   )   K   N   333333?N)r/   r0   r1   r5   rD   r6   rL   rO   rS   rh   ro   r9   r:   r;   r>   r>      s     -/"$-1&(4748o%S/)o  o !$d
	o
 !$o &+T\o &+T\o,
',r:   r>   c            B       \    e Zd ZeZej                  ZeZ	e
ZdddZdddZdZdZdZdZdZdZdZdZdZd	ZdZdZdZd
d
dZej8                  ZdZd
d
dZdZ dZ!dZ"dZ#e$Z%e&Z'de(e   f fdZ)e*de+de(e   de,f fd       Z-e.de/e0e1f   f fd       Z2e3de4fd       Z5d<dZ6	 	 	 	 	 	 	 	 	 	 	 d=de7e8e7   z  e/e0e7f   z  e9z  dz  de7e8e7   z  e/e0e7f   z  e9z  dz  de:dz  de;e<e;   z  dz  de;e<e;   z  dz  de7e8e7   z  e/e0e7f   z  e9z  dz  de7e8e7   z  e/e0e7f   z  e9z  dz  de;e<e;   z  dz  de;e<e;   z  dz  dd d!e=dz  de/f fd"Z>de<d   d#e:de9d$d d%e:de9d&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+e0e?z  dz  ddfd,Z@de<d   d#e:de9d$d d%e:de9d&e:d'e;d(e:de;e<e;   z  dz  de;e<e;   z  dz  d-e:dz  d.e7dz  d/e7dz  d0e7dz  d1e7dz  d2e;dz  d3e;dz  d4e:dz  d5e:dz  de9dz  dd d6e:dz  de9dz  d7e:dz  d8e;dz  d9e:dz  d:e:dz  de;e<e;   z  dz  de;e<e;   z  dz  d*e:dz  d+e0e?z  dz  de,fBd;ZA xZBS )>FlavaImageProcessor   rR   TFrp   rq   rr   Nrs   p   gp?kwargsc                 $    t        |   di | y )Nr9   )superrL   )rK   rx   	__class__s     r;   rL   zFlavaImageProcessor.__init__   s    "6"r:   imagesreturnc                 $    t        |   |fi |S rQ   )rz   
preprocess)rK   r|   rx   r{   s      r;   r   zFlavaImageProcessor.preprocess   s    w!&3F33r:   image_processor_dictc                     |j                         }d|v r|j                  d      |d<   d|v r|j                  d      |d<   t        |   |fi |S )z
        Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is
        created using from_dict and kwargs e.g. `FlavaImageProcessor.from_pretrained(checkpoint, codebook_size=600)`
        r%   r(   )copypoprz   	from_dict)clsr   rx   r{   s      r;   r   zFlavaImageProcessor.from_dict   sd      488:f$4:JJ4O 16)9?DX9Y !56w !5@@@r:   c                 $    t        ||||||      S )N)r?   r   r   r    r!   r"   )r>   )rK   r   r   r   r    r!   r"   s          r;   masking_generatorz%FlavaImageProcessor.masking_generator  s#     %)1#9#9(C(C
 	
r:   torch.Tensorc                 .    ddt         z  z
  |z  t         z   S )NrB   rA   )r   )rK   images     r;   
map_pixelszFlavaImageProcessor.map_pixels  s    A)))U25FFFr:   size	crop_sizedefault_to_square
image_mean	image_stdr%   r(   r-   r.   r&   z7PILImageResampling | tvF.InterpolationMode | int | Nonedata_formatc           
      r   t        |   d	||||||d|}|&t        |t              st        d	i t	        ||      }|&t        |t              st        d	i t	        |d      }t        |t
              rt        |      }t        |	t
              rt        |	      }	||d<   ||d<   ||d<   |	|d<   |
|d<   |S )
z
        Update kwargs that need further processing before being validated
        Can be overridden by subclasses to customize the processing of kwargs.
        )r   r   r   r   r   r   )r   r   r(   )
param_namer%   r-   r.   r&   r9   )rz   _standardize_kwargsrC   r   r
   listrD   )rK   r   r   r   r   r   r%   r(   r-   r.   r&   r   rx   r{   s                r;   r   z'FlavaImageProcessor._standardize_kwargs!  s    & , 
/!#
 
 $Zx-P$n}-[l'mnM)*=OQY2Z!)!oM:LYm,n!o)40"'(;"<($/!&'9!:"/'9#$(;$%'9#$&7"#r:   	do_resizeresampledo_center_crop
do_rescalerescale_factordo_normalizedo_map_pixelsdisable_groupingreturn_tensorsc           	         t        ||      \  }}i }|j                         D ]   \  }}|r| j                  |||      }|||<   " t        ||      }t        ||      \  }}i }|j                         D ]H  \  }}|r| j	                  ||      }| j                  ||||	||      }|
r| j                  |      }|||<   J t        ||      }|S )N)r   )r   r   r   )r   )r   itemsresizer   center_croprescale_and_normalizer   )rK   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   grouped_imagesgrouped_images_indexresized_images_groupedshapestacked_imagesresized_imagesprocessed_images_groupedprocessed_imagess                          r;   _preprocess_imagez%FlavaImageProcessor._preprocess_imageO  s   $ 0EV^n/o,,!#%3%9%9%; 	;!E>!%>W_!`,:"5)	; ((>@TU 0E^fv/w,,#% %3%9%9%; 		=!E>!%!1!1.)!L!77
NL*V_N !%~!F.<$U+		= **BDXYr:   r   r   r   r   r    r!   r"   r#   r$   r'   r)   r*   r+   r,   c!                 @   | j                  |||||||||	d|
|||       }"d|"i}#|r$| j                  ||||||||||||||       }$|$|#d<   |rB| j                  ||||||      }%t        t        |            D &cg c]	  }& |%        }'}&|'|#d<   t	        |#|       S c c}&w )NF)r|   r   r   r   r   r   r   r   r   r   r   r   r   r   pixel_valuescodebook_pixel_values)r   r   r   r    r!   r"   bool_masked_pos)datatensor_type)r   r   rV   lenr	   )(rK   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r   r   rx   r   r   codebook_processed_imagesmask_generator_maskss(                                           r;   _preprocesszFlavaImageProcessor._preprocess|  s   L  11)!)%!-) 2 
" ,
 "(,(>(>,"*6,.624.,!1- )? )%  -FD()!33#5#5'='=,G,G 4 N 05S[/AB!^%BEB&+D"#>BB Cs   9B)r   r   r}   r   )NNNNNNNNNNN)Cr/   r0   r1   r   valid_kwargsr   BICUBICr   FLAVA_IMAGE_MEANr   FLAVA_IMAGE_STDr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   LANCZOSr&   r'   r(   r)   r*   r+   r,   FLAVA_CODEBOOK_MEANr-   FLAVA_CODEBOOK_STDr.   r   rL   r   r   r	   r   classmethodr7   r8   r   r   r   r>   r   r   r5   r   r   r3   r6   r   r   r   r   r   r   __classcell__)r{   s   @r;   ru   ru      s   ,L!))H!JIC(D-IINJL !"%"&""S1M*22"$'#6%! -+#(A!B # 4 4v>W7X 4]i 4 4 
AT#s(^ 
A 
A 
 

 
$G
 HLLP)-1504PTUY:>9=W[/3,HSM!DcN2X=D, #&c3h7(BTI,  $;	,
 DK'$., 4;&-, Xc]*T#s(^;hFM,  (3-/$sCx.@8KdR, #T%[047, "DK/$6, U, &,, 
,\+ ^$+  +  	+ 
 L+  +  +  +  +  +  +  DK'$.+  4;&-+  ++  j(4/+   
!+ ZYC^$YC YC 	YC
 LYC YC YC YC YC YC DK'$.YC 4;&-YC  $;YC  $JYC   $J!YC" !$d
#YC$ !$d
%YC& &+T\'YC( &+T\)YC, !%t-YC. !4K/YC0  $1YC2 U3YC4 "&5YC6 %tO7YC8 "D[9YC: "';YC< !%t=YC>  $d{?YC@ #T%[047AYCB "DK/$6CYCD +EYCF j(4/GYCJ 
KYCr:   ru   )-r2   rH   rW   collections.abcr   	functoolsr   typingr   rk   torchvision.transforms.v2r   tvFimage_processing_backendsr   image_processing_utilsr	   r
   image_transformsr   r   image_utilsr   r   r   r   r   r   processing_utilsr   r   utilsr   r   r   r   r   r   r   r6   r4   r   r>   ru   __all__r9   r:   r;   <module>r      s    '   $    7 ; A E  5 $ !% $  5 Q0E Q0jI IX @C, @C @CF !
!r:   