
    ^j_,                     X   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	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mZ  e       rdd
lmZ ddlmZ dedeeef   fdZ dedefdZ! G d ded      Z"de#e#d      deeef   fdZ$dddeeef   ddfdZ%e G d de             Z&dgZ'y)z#Image processor class for Idefics2.    N   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)IMAGENET_STANDARD_MEANIMAGENET_STANDARD_STD
ImageInputPILImageResamplingSizeDictmake_nested_list_of_images)ImagesKwargsUnpack)
TensorTypeauto_docstringis_vision_available)Image)
functionalsizereturnc                    | j                   dd \  }}|j                  }|j                  }||z  }||k\  r||kD  r|}t        ||z        }n||kD  r||kD  r|}t        ||z        }t	        ||      }t	        ||      }||fS )z
    Get the output size of the image after resizing given a dictionary specifying the max and min sizes.
    Images are always channels-first (CHW).
    N)shapeshortest_edgelongest_edgeintmax)imager   heightwidthmin_lenmax_lenaspect_ratios          /var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/idefics2/image_processing_idefics2.pyget_resize_output_image_sizer%   (   s    
 KK$MFE  GG6>L57?U\)*	%FW,F\)*!FwE5=    r   c                 &   t               rt        | t        j                        s| S | j                  dk(  r| S | j	                  d      }t        j
                  d|j                  d      }t        j                  ||      }|j	                  d      }|S )z
    Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image
    as is.
    RGBRGBA)   r*   r*   )r   
isinstancer   modeconvertnewr   alpha_composite)r   
image_rgba
backgroundr/   s       r$   convert_to_rgbr2   >   sy    
  
5%++(FzzUv&J6:??ODJ++J
CO%--e4Or&   c                       e Zd ZU dZeed<   y)Idefics2ImageProcessorKwargsz
    do_image_splitting (`bool`, *optional*, defaults to `self.do_image_splitting`):
        Whether to split the image into a sequence 4 equal sub-images concatenated with the original image.
    do_image_splittingN)__name__
__module____qualname____doc__bool__annotations__ r&   r$   r4   r4   P   s    
 r&   r4   F)totalimages_listztorch.Tensor|np.ndarrayc                     g }| D ]'  }|D ]   }|j                  |j                  dd        " ) t        d |D              }t        d |D              }||fS )zH
    Get the maximum height and width across all images in a batch.
    r   Nc              3   &   K   | ]	  }|d      yw)r   Nr<   .0r   s     r$   	<genexpr>z'get_max_height_width.<locals>.<genexpr>b   s     5T!W5   c              3   &   K   | ]	  }|d      yw)   Nr<   rA   s     r$   rC   z'get_max_height_width.<locals>.<genexpr>c   s     4DG4rD   )appendr   r   )r>   image_sizesimagesr   
max_height	max_widths         r$   get_max_height_widthrL   Y   sm     K 1 	1Eu{{23/0	11 555J444I	""r&   torch.Tensoroutput_sizec                     | j                   dd \  }}t        j                  |t        j                  | j                        }d|d|d|f<   |S )zc
    Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding.
    r   NdtypedevicerF   )r   torchzerosint64rR   )r   rN   input_heightinput_widthmasks        r$   make_pixel_maskrY   g   sO     !&BC 0L+;;{%++ellKD()D,	$%Kr&   c                       e Zd ZeZej                  ZeZ	e
ZdZdZdZdZdZdZdZddd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edefdZ	 d'dddeddddf fdZd(d
ededefdZd
dde e d      fdZ!	 d)ddde"eef   dede"d   fdZ#d
e 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$dz  d$e$dz  d%e&e'z  dz  defd&Z( xZ)S )*Idefics2ImageProcessorTFiz  i  )r   r   pixel_valuespixel_attention_maskkwargsc                 $    t        |   di | y )Nr<   )super__init__)selfr^   	__class__s     r$   ra   zIdefics2ImageProcessor.__init__   s    "6"r&   rI   r   c                 $    t        |   |fi |S N)r`   
preprocess)rb   rI   r^   rc   s      r$   rf   z!Idefics2ImageProcessor.preprocess   s    w!&3F33r&   r   c                     t        |      S )zConvert an image to RGB format.)r2   )rb   r   s     r$   r2   z%Idefics2ImageProcessor.convert_to_rgb   s    e$$r&   NrM   r   resamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                    |j                   r|j                  rt        ||      }n<|j                  r%|j                  r|j                  |j                  f}nt        d      t        |   |t        |d   |d         fd|i|S )z7Resize using Idefics2 shortest_edge/longest_edge logic.zWSize must contain 'height' and 'width' keys or 'shortest_edge' and 'longest_edge' keys.r   rF   )r   r    rh   )	r   r   r%   r   r    
ValueErrorr`   resizer   )rb   r   r   rh   r^   new_sizerc   s         r$   rk   zIdefics2ImageProcessor.resize   sz     $"3"33E4@H[[TZZTZZ0Hvwww~eXXa[QR%Tr_grkqrrr&   expected_ndimsc                 >    | j                  |      }t        ||      S )z1Prepare a nested images structure for processing.)rm   )fetch_imagesr   )rb   rI   rm   s      r$   _prepare_images_structurez0Idefics2ImageProcessor._prepare_images_structure   s    ""6*)&PPr&   c           	          |j                   dd \  }}|dz  }|dz  }|dd|d|f   |dd||df   |d|dd|f   |d|d|df   |g}t        t        |d               D cg c]  }|D cg c]  }||   	 c} }}}|S c c}w c c}}w )zy
        Split a batch of images into 4 equal sub-images, and concatenate that sequence with the original image.
        r   N   .r   )r   rangelen)	rb   rI   r   r    	mid_width
mid_heightbatch_split_imagesir   s	            r$   split_imagesz#Idefics2ImageProcessor.split_images   s     RS)QJ	q[
 3ZiZ/03YZ/03
ZiZ/03
YZ/0
 SXX[\nop\qXrRstQ5GHEuQxHtt!! Its   &	B
/B;B
B
padded_sizefill)rM   rM   c                 V   |j                   dd }|d   |d   z
  }|d   |d   z
  }|dk  s|dk  rt        d| d| d      ||k7  rdd||f}t        j                  |||d	      }t	        j
                  |t        j                  |j                  
      }d|d|d   d|d   f<   ||fS )z]
        Pad an image to the specified size and create the corresponding pixel mask.
        r   Nr   rF   zzPadding dimensions are negative. Please make sure that the padded size is larger than the original size. Got padded size: z, original size: .constant)r{   padding_moderP   )r   rj   tvFpadrS   rT   rU   rR   )	rb   r   rz   r{   original_sizepadding_bottompadding_rightpadding
pixel_masks	            r$   r   zIdefics2ImageProcessor.pad   s     BC($Q-*::#Aq)99A!233>-?PQ^P__`b 
 K'!]N;GGGE7JOE[[EKKU
=>
%]1%%'9q)9'99:j  r&   	do_resize
do_rescalerescale_factordo_normalize
image_mean	image_stddo_padr5   disable_groupingreturn_tensorsc           	         t        ||d      \  }}i }|j                         D ]  \  }}|r| j                  |      }|||<    t        ||d      }|r/t	        |      D ]!  \  }}|D cg c]  }|D ]  }|  c}}||<   # t        ||d      \  }}i }|j                         D ]   \  }}|r| j                  |||      }|||<   " t        ||d      }t        ||d      \  }}i }|j                         D ]   \  }}| j                  ||||||	      }|||<   " t        ||d      }|
rt        d |D              }t        |      \  }}t        j                  t        |      |g|d   d   j                  d   ||fd|d   d   j                  i} t        j                  t        |      |g||fd|d   d   j                  i}!t	        |      D ]9  \  }}t	        |      D ]&  \  }"}| j                  |||f      \  | ||"f<   |!||"f<   ( ; | }|
r|!d}#nB|d	k(  r9d
t        j                  |D cg c]  }t        j                  |       c}      i}#nd
|i}#t!        |#|      S c c}}w c c}w )NT)r   	is_nested)r   )rh   c              3   2   K   | ]  }t        |        y wre   )rt   )rB   images_s     r$   rC   z5Idefics2ImageProcessor._preprocess.<locals>.<genexpr>  s      N'W Ns   r   rR   )r\   r]   ptr\   )datatensor_type)r   itemsry   r   	enumeraterk   rescale_and_normalizer   rL   rS   rT   rt   r   rR   r   stackr   )$rb   rI   r   r   rh   r   r   r   r   r   r   r5   r   r   r^   grouped_imagesgrouped_images_indexsplit_images_groupedr   stacked_imagesry   rx   group_imagessublistr   resized_images_groupedresized_imagesprocessed_images_groupedprocessed_imagesmax_num_imagesrJ   rK   processed_images_paddedpixel_attention_masksjr   s$                                       r$   _preprocessz"Idefics2ImageProcessor._preprocess   s:   " 0E%50
,,  "%3%9%9%; 	9!E>!!%!2!2>!B*8 '	9 &&:<P\`a#,\#: [<8D"ZWRY"Z5"Z5"ZQ[ 0E+;t0
,, "$%3%9%9%; 	;!E>!%^TH!U,:"5)	; ((>@T`de/D-=0
,, $& %3%9%9%; 	=!E>!77
NL*V_N /=$U+		=
 **BDXdhi  N=M NNN$89I$J!J	&+kk$%' #1%a(..q1:yI' (*1-44	'# %*KK$%% i(% (*1-44	%! ''78 	6 )& 1 HAuQUQYQY
I6RN+AqD13HA3N
  7$4NcdDt#"EKKSc0dV1D0d$efD"$45D>BBg #[` 1es   *I+5I1
re   )r   )r   )*r6   r7   r8   r4   valid_kwargsr   BILINEARrh   r   r   r	   r   r   r   r   r   do_convert_rgbr5   default_to_squarer   model_input_namesr   ra   r   r
   r   rf   r2   r   rk   r   rp   listry   tupler   r:   floatstrr   r   __classcell__)rc   s   @r$   r[   r[   q   sE   /L!**H'J%IIJLFN #6D')?@#(D!E # 4 4v>Z7[ 4`l 4 4%J %: % OS	ss s L	s 
s"Q
 QC QXb Q
"> "d4;O6P "* PQ!#!27S/!IL!	-	.!2OCT.)*OC OC 	OC
 LOC OC OC OC DK'$.OC 4;&-OC tOC !4KOC +OC j(4/OC  
!OCr&   r[   )(r9   numpynprS   image_processing_backendsr   image_processing_utilsr   image_transformsr   r   image_utilsr   r	   r
   r   r   r   processing_utilsr   r   utilsr   r   r   PILr   torchvision.transforms.v2r   r   r   r   r%   r2   r4   r   rL   rY   r[   __all__r<   r&   r$   <module>r      s    *   ; 2 E  5 D D  7h 5c? ,*  $<u #d40I+J&K #PUVY[^V^P_ #> c3h N  lC/ lC lC^ $
$r&   