
    ^j                        d 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mZ ddlmZmZ dd	lmZmZ  G d
 ded      Z	 ddedededededeeef   fdZdddeddfdZ	 d dddededed   fdZe G d de             ZdgZy)!z"Image processor class for SigLIP2.    N)
functional   )TorchvisionBackend)BatchFeature)
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstringc                   &    e Zd ZU dZeed<   eed<   y)Siglip2ImageProcessorKwargsar  
    patch_size (`int`, *optional*, defaults to `self.patch_size`):
        The size (resolution) of each patch the image will be split to.
    max_num_patches (`int`, *optional*, defaults to `self.max_num_patches`):
        The image will be resized to have at most this number of patches,
        and then padded in "patch" dimension to match this number exactly.
    
patch_sizemax_num_patchesN)__name__
__module____qualname____doc__int__annotations__     /var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/siglip2/image_processing_siglip2.pyr   r      s     Or   r   F)totalimage_heightimage_widthr   r   epsreturnc                    ddl dt        dt        dt        dt        ffd}|dz  d	}}||z
  |k\  r:||z   d
z  } ||| |      }	 ||||      }
|	|z  |
|z  z  }||k  r|}n|}||z
  |k\  r:|} ||| |      }	 ||||      }
|	|
fS )a"  
    Determine image size based on max number of patches, ensure dimensions are divisible by patch size and image is at least 1 patch.

    Args:
        image_height (`int`):
            Original image height.
        image_width (`int`):
            Original image width.
        patch_size (`int`):
            Patch size for processing.
        max_num_patches (`int`):
            Maximum number of patches.
        eps (`float`):
            Small threshold for binary search.

    Returns:
        Tuple: (target_height, target_width)
    r   Nscalesizer   r   c                 j    || z  }j                  ||z        |z  }t        ||      }t        |      S N)ceilmaxr   )r!   r"   r   scaled_sizemaths       r   get_scaled_image_sizezAget_image_size_for_max_num_patches.<locals>.get_scaled_image_sizeA   s;    Uliij 89JF*k2;r   
   g      Y@   )r(   floatr   )r   r   r   r   r   r)   	scale_min	scale_maxr!   target_heighttarget_widthnum_patchesr(   s               @r   "get_image_size_for_max_num_patchesr2   *   s    *  U  #  3  3   8UyIy S
(Y&!+-e\:N,UKL$z1lZ6OP/)II y S
( E)%zJM(ZHL,&&r   imagetorch.Tensorc                     | j                   \  }}}||z  }||z  }| j                  |||||      }|j                  ddddd      }|j                  ||z  d      }|S )z
    Convert 3D tensor image of shape (num_channels, image_height, image_width) into 2D tensor of patches of shape
    (num_patches_height * num_patches_width, patch_size * patch_size * num_channels).
       r   r+      r   )shapereshapepermute)r3   r   num_channelsr   r   num_patches_heightnum_patches_widthpatched_images           r   convert_image_to_patchesr@   Z   s{    
 /4kk+L,%3#z1MM,0BJPacmnM!))!Q1a8M!))*<?P*PRTUMr   tensortarget_length	pad_value)r4   r4   c                     | j                   d   }||z
  }t        j                  |ft        j                        }|dkD  rLddg| j                  dz
  z  d|gz   }t        j
                  j                  j                  | |d|      } d|| d | |fS )z3
    Pad the tensor along the first dimension.
    r   )dtyper6   constant)modevalueN)r9   torchonesint32ndimnnr   pad)rA   rB   rC   current_lengthpadding_lengthmaskpaddings          r   pad_along_first_dimrS   h   s     \\!_N"^3N::}&ekk:Da&FKK!O,>/BB$$((zQZ([!"n_4<r   c                       e Zd ZeZej                  Zg dZg dZ	dZ
dZdZdZdZg dZdee   f fdZededee   d	ef fd
       Zd	ef fdZded   de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ez  dz  d	efdZ xZS )Siglip2ImageProcessor)      ?rV   rV   T      pixel_valuespixel_attention_maskspatial_shapeskwargsc                 $    t        |   di | y )Nr   )super__init__selfr]   	__class__s     r   r`   zSiglip2ImageProcessor.__init__   s    "6"r   imagesr   c                 $    t        |   |fi |S r$   )r_   
preprocess)rb   rd   r]   rc   s      r   rf   z Siglip2ImageProcessor.preprocess   s    w!&3F33r   c                 F    |j                  dd        t        |   di |S )N	do_resizer   )popr_   _validate_preprocess_kwargsra   s     r   rj   z1Siglip2ImageProcessor._validate_preprocess_kwargs   s$    

;%w2<V<<r   r4   rh   r   r   resamplez7PILImageResampling | tvF.InterpolationMode | int | None
do_rescalerescale_factordo_normalize
image_meanN	image_stdreturn_tensorsc           	         g }g }g }|D ]  }|rMt        |j                  d   |j                  d   ||      \  }}t        ||      }| j                  |||      }| j	                  |||||	|
      }t        ||      }t        ||      \  }}|j                  d   |z  }|j                  d   |z  }|j                  ||f       |j                  |       |j                  |        t        |||d|      }|S )Nr8   )r   r   r   r   )heightwidth)r3   r"   rk   rY   )datatensor_type)	r2   r9   r	   resizerescale_and_normalizer@   rS   appendr   )rb   rd   rh   r   r   rk   rl   rm   rn   ro   rp   rq   r]   pixel_masksrZ   r\   r3   rt   ru   	size_dictpatchesrQ   r=   r>   batch_features                            r   _preprocessz!Siglip2ImageProcessor._preprocess   s*     	%E B!&R %B)$3	! %F%@	%i(S..uj.R^`jluvE /ujAG/IMGT!&RJ!> %B: =!!#57H"IJ(t$-	%0 % ,(3"0
 '
 r   ) r   r   r   r   valid_kwargsr   BILINEARrk   ro   rp   rh   rl   rn   r   r   model_input_namesr   r`   r   r   r   rf   tuplerj   listboolr   r,   strr   r   __classcell__)rc   s   @r   rU   rU   x   s1   .L!**H JIIJLJOR#(C!D # 4 4v>Y7Z 4_k 4 4=u =
3^$3 3 	3
 3 L3 3 3 3 DK'$.3 4;&-3 j(4/3 
3r   rU   )gh㈵>)r   )r   rI   torchvision.transforms.v2r   tvFimage_processing_backendsr   image_processing_utilsr   image_utilsr   r   r	   processing_utilsr
   r   utilsr   r   r   r   r,   r   r2   r@   rS   rU   __all__r   r   r   <module>r      s    )  7 ; 2 C C 4
,e 
 ^b-'-'$'-'58-'KN-'UZ-'
38_-'`N    BC+.;>
)*  K. K K\ #
#r   