
    Z jfS                     2   % S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	r	SSK
Jr  SSKJr  SS	K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Jr  SSKJ r J!r!  \r"\r#/ SQr$/ SQr%Sr&\'\(S'    " S S\SS9r) " S S5      r*\! " S S\5      5       r+S/r,g)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                   
   \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S	'   \\S
'   \\S'   \	\
\4   \S'   \\S'   \\S'   \	\
\4   \S'   \\S'   \\-  \S'   \\S'   \\S'   \\\   -  \S'   \\\   -  \S'   Srg)FlavaImageProcessorKwargs3   a/  
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` for PIL backend,
    `PILImageResampling.BICUBIC` for torchvision backend):
    Resampling filter to use if resizing the codebook image. LANCZOS is not supported for torch Tensors;
    BICUBIC is used as the closest alternative for the torchvision backend. 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_std N)__name__
__module____qualname____firstlineno____doc__bool__annotations__intfloatdictstrr   __static_attributes__r0       ځ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/flava/image_processing_flava.pyr   r   3   s    ;| !&&!&&  S>!!!S#X& 5[(  %00//r=   r   F)totalc                       \ rS rSr      SS\\\\4   -  S\S\S-  S\S\S-  S\S-  4S	 jjrS
 rS r	S r
S rSrg)FlavaMaskingGenerator   N
input_sizer   r!   r    r"   r#   c                 B   [        U[        5      (       d  U4S-  nUu  U l        U l        U R                  U R                  -  U l        X l        X@l        Uc  UOUU l        U=(       d    SU-  n[        R                  " U5      [        R                  " U5      4U l
        g )N      )
isinstancetupleheightwidthnum_patchesr   r    r!   mathloglog_aspect_ratio)selfrC   r   r!   r    r"   r#   s          r>   __init__FlavaMaskingGenerator.__init__   s     *e,,$*J",TZ;;3"4&<#<R<Z&8`v#&A&dQIdEd#!%*E!FQlHm nr=   c           	          SU R                   U R                  U R                  U R                  U R                  U R
                  S   U R
                  S   4-  nU$ )Nz<MaskingGenerator(%d, %d -> [%d ~ %d], max = %d, %.3f ~ %.3f)r   rF   )rI   rJ   r    r!   r   rN   )rO   repr_strs     r>   __repr__FlavaMaskingGenerator.__repr__   s^    QKKJJ''''##!!!$!!!$U
 
 r=   c                 2    U R                   U R                  4$ NrI   rJ   )rO   s    r>   	get_shapeFlavaMaskingGenerator.get_shape   s    {{DJJ&&r=   c           	      .   Sn[        S5       GH  n[        R                  " U R                  U5      n[        R
                  " [        R                  " U R                  6 5      n[        [        [        R                  " XV-  5      5      5      n[        [        [        R                  " XV-  5      5      5      nXR                  :  d  M  XpR                  :  d  M  [        R                  " SU R                  U-
  5      n	[        R                  " SU R                  U-
  5      n
XX-   2XU-   24   R                  5       nSXx-  U-
  s=:  a  U::  a7  O  O4XX-   2XU-   24   S:H  nSXX-   2XU-   24   U'   X<R                  5       -  nUS:  d  GM    U$    U$ )Nr   
   rF   )rangerandomuniformr    rL   exprN   r8   roundsqrtrJ   rI   randintsum)rO   maskmax_mask_patchesdelta_attempttarget_areaaspect_ratiorI   rJ   topleft
num_masked	zeros_poss                r>   _maskFlavaMaskingGenerator._mask   sb   b	H ..)D)DFVWK88FNND4I4I$JKLtyy)CDEFFdii(BCDEEzz!f{{&:nnQf(<=~~ae);<!"4dE\6I"IJNNP
v~
2F6FF $3<%7u9L%L MQR RIOPDs|+T5L-@@A)L]]_,E19% "$ r=   c                 0   [         R                  " U R                  5       [         R                  S9nSnX R                  :  aS  U R                  U-
  n[        X0R                  5      nU R                  X5      nUS:X  a   U$ X$-  nX R                  :  a  MS  U$ )N)dtyper   )torchzerosrY   r8   r   minr!   ro   )rO   re   
mask_countrf   rg   s        r>   __call__FlavaMaskingGenerator.__call__   s    {{4>>+599=
222#66C"#35P5PQJJt6Ez  #
 222 r=   )rI   rN   r!   r    rK   r   rJ   )   K   N   333333?N)r1   r2   r3   r4   r8   rH   r9   rP   rT   rY   ro   rw   r<   r0   r=   r>   rA   rA      s     -/"$-1&(4748o%S/)o  o !$d
	o
 !$o &+T\o &+T\o,
',r=   rA   c            B       ~  ^  \ rS rSr\r\R                  r\	r
\rSSS.rSSS.rSrSrSrSrSrSrSrSrS	rS
rS	rSrSrSSS.r\R                  rSrSSS.rSr Sr!Sr"Sr#\$r%\&r'S\(\   4U 4S jjr)\*S\+S\(\   S\,4U 4S jj5       r-\.S\/\0\14   4U 4S jj5       r2\3S\44S j5       r5S>S jr6           S?S\7\8\7   -  \/\0\74   -  \9-  S	-  S\7\8\7   -  \/\0\74   -  \9-  S	-  S\:S	-  S\;\<\;   -  S	-  S\;\<\;   -  S	-  S\7\8\7   -  \/\0\74   -  \9-  S	-  S\7\8\7   -  \/\0\74   -  \9-  S	-  S\;\<\;   -  S	-  S\;\<\;   -  S	-  S S!S"\=S	-  S\/4U 4S# jjjr>S\<S   S$\:S\9S%S!S&\:S\9S'\:S(\;S)\:S*\:S\;\<\;   -  S	-  S\;\<\;   -  S	-  S+\:S	-  S,\0\?-  S	-  SS4S- jr@S\<S   S$\:S\9S%S!S&\:S\9S'\:S(\;S)\:S\;\<\;   -  S	-  S\;\<\;   -  S	-  S.\:S	-  S/\7S	-  S0\7S	-  S1\7S	-  S2\7S	-  S3\;S	-  S4\;S	-  S5\:S	-  S6\:S	-  S\9S	-  S S!S7\:S	-  S\9S	-  S8\:S	-  S9\;S	-  S:\:S	-  S;\:S	-  S\;\<\;   -  S	-  S\;\<\;   -  S	-  S+\:S	-  S,\0\?-  S	-  S\,4BS< jrAS=rBU =rC$ )@FlavaImageProcessor      rX   TFry   rz   r{   Nr|   p   gp?kwargsc                 &   > [         TU ]  " S0 UD6  g )Nr0   )superrP   )rO   r   	__class__s     r>   rP   FlavaImageProcessor.__init__   s    "6"r=   imagesreturnc                 &   > [         TU ]  " U40 UD6$ rW   )r   
preprocess)rO   r   r   r   s      r>   r   FlavaImageProcessor.preprocess   s    w!&3F33r=   image_processor_dictc                    > UR                  5       nSU;   a  UR                  S5      US'   SU;   a  UR                  S5      US'   [        TU ]  " U40 UD6$ )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popr   	from_dict)clsr   r   r   s      r>   r   FlavaImageProcessor.from_dict  sd      488:f$4:JJ4O 16)9?DX9Y !56w !5@@@r=   c           	          [        UUUUUUS9$ )N)rC   r   r    r!   r"   r#   )rA   )rO   r   r   r    r!   r"   r#   s          r>   masking_generator%FlavaImageProcessor.masking_generator  s#     %)1#9#9(C(C
 	
r=   torch.Tensorc                 .    SS[         -  -
  U-  [         -   $ )NrF   rE   )r   )rO   images     r>   
map_pixels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           
        > [         TU ]  " S	UUUUUUS.UD6nUb'  [        U[        5      (       d  [        S	0 [	        XcS9D6nUb(  [        U[        5      (       d  [        S	0 [	        USS9D6n[        U[
        5      (       a  [        U5      n[        U	[
        5      (       a  [        U	5      n	XlS'   X|S'   XS'   XS'   XS'   U$ )
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'   r0   )r   _standardize_kwargsrG   r   r
   listrH   )rO   r   r   r   r   r   r&   r)   r.   r/   r'   r   r   r   s                r>   r   'FlavaImageProcessor._standardize_kwargs$  s    & , 
/!#
 
 $Zx-P-P$n}-'mnM)*=OQY2Z2Z!)!oM:LYm,n!o)40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           	         [        XS9u  nn0 nUR                  5        H"  u  nnU(       a  U R                  UX4S9nUUU'   M$     [        UU5      n[        UUS9u  nn0 nUR                  5        HN  u  nnU(       a  U R	                  UU5      nU R                  UXxXU5      nU
(       a  U R                  US9nUUU'   MP     [        UU5      nU$ )N)r   )r   r   r   )r   )r   itemsresizer   center_croprescale_and_normalizer   )rO   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_image%FlavaImageProcessor._preprocess_imageR  s    $ 0EV/o,,!#%3%9%9%;!E>!%>!`,:"5) &< ((>@TU 0E^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N !%~!F.<$U+ &< **BDXYr=   r   r   r   r    r!   r"   r#   r$   r%   r(   r*   r+   r,   r-   c!                 B   U R                  UUUUUUUUU	SU
UUU S9n"SU"0n#U(       a!  U R                  UUUUUUUUUUUUUU S9n$U$U#S'   U(       aB  U R                  UUUUUUS9n%[        [        U5      5       V&s/ s H
  n&U%" 5       PM     n'n&U'U#S'   [	        U#U S9$ s  sn&f )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   r]   lenr	   )(rO   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   r   r   r   codebook_processed_imagesmask_generator_maskss(                                           r>   _preprocess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/A!^%/AEB&+D"#>BB Cs   :Br0   )r   r   r   r   )NNNNNNNNNNN)Dr1   r2   r3   r4   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&   r'   r(   r)   r*   r+   r,   r-   FLAVA_CODEBOOK_MEANr.   FLAVA_CODEBOOK_STDr/   r   rP   r   r   r	   r   classmethodr:   r;   r   r   r   rA   r   r   r8   r   r   r6   r9   r   r   r   r   r   r   r<   __classcell__)r   s   @r>   r~   r~      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 YCr=   r~   )-r5   rL   r^   collections.abcr   	functoolsr   typingr   rs   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   r9   r7   r   rA   r~   __all__r0   r=   r>   <module>r      s    '   $    7 ; A E  5 $ !% $  5 R0E R0lI IX BC, BC BCJ !
!r=   