
    ^j)                         d Z ddl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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mZ  G d ded      Ze G d de             ZdgZy)z!Image processor class for Gemma3.    N)
functional   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)IMAGENET_STANDARD_MEANIMAGENET_STANDARD_STD
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstringc                   :    e Zd ZU dZeed<   eed<   eed<   eed<   y)Gemma3ImageProcessorKwargsa  
    do_pan_and_scan (`bool`, *optional*):
        Whether to apply `pan_and_scan` to images.
    pan_and_scan_min_crop_size (`int`, *optional*):
        Minimum size of each crop in pan and scan.
    pan_and_scan_max_num_crops (`int`, *optional*):
        Maximum number of crops per image in pan and scan.
    pan_and_scan_min_ratio_to_activate (`float`, *optional*):
        Minimum aspect ratio to activate pan and scan.
    do_pan_and_scanpan_and_scan_min_crop_sizepan_and_scan_max_num_crops"pan_and_scan_min_ratio_to_activateN)__name__
__module____qualname____doc__bool__annotations__intfloat     }/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/gemma3/image_processing_gemma3.pyr   r   '   s!    	  ## ##(--r!   r   F)totalc            !           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ZdZeZddgZdee   f fdZed	edee   d
ef fd       Zd	ddededefdZd	e d   de!dededef
dZ"	 	 	 	 dd	e d   de!de#dd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!dz  dedz  dedz  dedz  d
ef dZ& xZ'S ) Gemma3ImageProcessor   )heightwidthTNpixel_values	num_cropskwargsc                 $    t        |   di | y )Nr    )super__init__)selfr+   	__class__s     r"   r.   zGemma3ImageProcessor.__init__K   s    "6"r!   imagesreturnc                 $    t        |   |fi |S )N)r-   
preprocess)r/   r1   r+   r0   s      r"   r4   zGemma3ImageProcessor.preprocessN   s    w!&3F33r!   ztorch.Tensorr   r   r   c           
         |j                   dd \  }}||k\  rt||z  |k  rg S t        t        j                  ||z  dz               }t	        t        t        j                  ||z              |      }t        d|      }t	        ||      }d}ns||z  |k  rg S t        t        j                  ||z  dz               }t	        t        t        j                  ||z              |      }t        d|      }t	        ||      }d}t        t        j                  ||z              }	t        t        j                  ||z              }
t	        |	|
      |k  rg S t        |      D cg c]  }|	|z  	 }}t        |      D cg c]  }|
|z  	 }}t        j                  ||      D cg c]  \  }}|d|||
z   |||	z   f    c}}S c c}w c c}w c c}}w )aU  
        Pan and Scan an image, by cropping into smaller images when the aspect ratio exceeds
        minimum allowed ratio.

        Args:
            images (`torch.Tensor`):
                Image to resize.
            pan_and_scan_min_crop_size (`int`, *optional*):
                Minimum size of each crop in pan and scan.
            pan_and_scan_max_num_crops (`int`, *optional*):
                Maximum number of crops per image in pan and scan.
            pan_and_scan_min_ratio_to_activate (`float`, *optional*):
                Minimum aspect ratio to activate pan and scan.
        Ng      ?      .)
shaper   mathfloorminmaxceilrange	itertoolsproduct)r/   r1   r   r   r   r'   r(   num_crops_wnum_crops_hcrop_size_wcrop_size_hicrop_positions_wcrop_positions_hpos_hpos_ws                   r"   pan_and_scan_batchedz)Gemma3ImageProcessor.pan_and_scan_batchedR   s   * RS) F?v~ BB	 djj#)=>?Kc$**U5O-O"PQS^_K a-K8+FKK
 ~ BB	 djj%#)=>?Kc$**V6P-P"QRT_`K a-K8+FKK$))EK$789$))F[$89: {K(+EEI5:;5GHK!OHH5:;5GHK!OHH !* 1 12BDT U
u 3 33UU[=P5PPQ
 	
 IH
s   G:G!Gr   c                 n    | j                  ||||      }|D cg c]  }t        |       }}||fS c c}w )N)r1   r   r   r   )rK   len)	r/   r1   r   r   r   r   
pas_images_r*   s	            r"    _process_images_for_pan_and_scanz5Gemma3ImageProcessor._process_images_for_pan_and_scan   sN     ..'A'A/Q	 / 

 /55S_5	59$$ 6s   2	do_resizesizeresamplez7PILImageResampling | tvF.InterpolationMode | int | None
do_rescalerescale_factordo_normalize
image_mean	image_stddisable_groupingreturn_tensorsc           	      @   i }i }t        ||
      \  }}|j                         D ]  \  }}|r| j                  |||||      \  }}|g|z   }i }t        ||
      \  }}|j                         D ]  \  }}| j                  |||      }|||<     t	        ||      }t        j                  |d      j                  dd      j                         }n%|D cg c]  }d }}|r| j                  |||      }|||<   |||<    t	        ||      } |r| D !"cg c]  }!|!D ]  }"|"  } }!}"t	        ||      }t        | |
      \  }}i }|j                         D ]   \  }}| j                  ||||||	      }|||<   " t	        ||      }#t        |#|d|      S c c}w c c}"}!w )	N)rY   )r1   r   r   r   r   )imagerR   rS   r   )dimr8   )r)   r*   )datatensor_type)r   itemsrP   resizer   torchstack	transpose
contiguousrescale_and_normalizer   )$r/   r1   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r   r   r   r   r+   processed_images_groupednum_crops_groupedgrouped_imagesgrouped_images_indexshape_imagesstacked_imagesrN   r*   processed_image_patches_groupedgrouped_image_patchesgrouped_image_patches_indexr9   stacked_image_patchesprocessed_image_patchesrO   resized_imagesimages_listr\   processed_imagess$                                       r"   _preprocessz Gemma3ImageProcessor._preprocess   sI   ( $& /DV^n/o,,,:,@,@,B $	D(L.(,(M(M)$3/I/I7Y )N )%
I #1!1J!>24/EZ"5EFB%'B 5J4O4O4Q S0E0,0KK3!!) -8 -)
 >S3E:S +99XZu*v'!&-D!!L!V!VWXZ[!\!g!g!i(671Q7	7%)[[,!!) &1 &N
 /8l+5C$\2I$	DJ ((@BVW7E_S^_%e_e_N_"#46JK	 0E^fv/w,,#% %3%9%9%; 	=!E>!77
NL*V_N /=$U+	= **BDXY"2KYg
 	
9 8 `s   	FF)NNNN)(r   r   r   r   BILINEARrS   r	   rW   r
   rX   rR   default_to_squaredo_convert_rgbrQ   rT   rV   r   r   r   r   r   valid_kwargsmodel_input_namesr   r.   r   r   r   r4   r   r   rK   listr   rP   r   strr   ru   __classcell__)r0   s   @r"   r%   r%   9   s   !**H'J%IC(DNIJLO!%!%)-&-L'5#(B!C # 4 4v>X7Y 4^j 4 4B
B
 %(B
 %(	B

 -2B
H%^$% % %(	%
 %(% -2%< (,1515;?!P
^$P
 P
 	P

 LP
 P
 P
 P
 DK'$.P
 4;&-P
 +P
 j(4/P
 P
 %($JP
 %($JP
  -2DL!P
$ 
%P
r!   r%   )r   r@   r:   rb   torchvision.transforms.v2r   tvFimage_processing_backendsr   image_processing_utilsr   image_transformsr   r   image_utilsr	   r
   r   r   r   processing_utilsr   r   utilsr   r   r   r%   __all__r    r!   r"   <module>r      sk    (    7 ; 2 E  5.U .$ }
- }
 }
@ "
"r!   