
    ^j5                         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mZmZ ddl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deded   fdZ ed       G d de             ZdgZy)    N)
functional   )TorchvisionBackend)BatchFeature)
ImageInputPILImageResamplingSizeDictvalidate_preprocess_arguments)ImagesKwargsUnpack)
TensorTypeauto_docstring)OPENAI_CLIP_MEANOPENAI_CLIP_STDc                   2    e Zd ZU dZedz  ed<   edz  ed<   y)InklingImageProcessorKwargsaw  
    rescale_image_frac (`float`, *optional*):
        Factor applied to the image's long edge before patch division, preserving aspect ratio. `None` disables
        the pre-scaling.
    rescale_image_max_upscaled_long_edge (`int`, *optional*):
        Cap, in pixels, on the upscaled long edge. Only limits growth: an image already above the cap is kept
        as is.
    Nrescale_image_frac$rescale_image_max_upscaled_long_edge)__name__
__module____qualname____doc__float__annotations__int     /var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/inkling/image_processing_inkling.pyr   r      s     $*-*4r   r   F)totalimagetorch.Tensor
patch_sizereturnc                    | j                   d   | j                   d   }}||z   dz
  |z  }||z  dz   }g }t        |      D ]?  }t        |      D ]/  }||z  }	||z  }
| d|	|	|z   |
|
|z   f   }|j                  |       1 A |S )N   .)shaperangeappend)r    r"   heightwidthnum_rowsnum_colspatchesijy_basex_basepatchs               r   divide_to_patchesr5   ,   s    KKOU[[_EF#a'J6H
"Q&HG8_ "x 	"A^F^F#v(;;VfzFY=YYZENN5!		"" Nr   z%Constructs a Inkling image processor.)custom_introc                       e Zd Zej                  ZeZeZ	dZ
dZdZdZdZdddZdZdZeZdee   f fdZ	 	 	 	 	 	 	 	 	 	 dd	edz  d
e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edz  dedz  dedz  ddfdZdedee   def fdZded   ded	ed
ededeee   z  dz  deee   z  dz  dddedz  de dz  de!e"z  dz  defdZ# xZ$S )InklingImageProcessorFT(   r+   r,   Ni   kwargsc                 $    t        |   di | y )Nr   )super__init__)selfr;   	__class__s     r   r>   zInklingImageProcessor.__init__J   s    "6"r   
do_rescalerescale_factordo_normalize
image_mean	image_std	do_resizesizedo_center_crop	crop_sizeresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                     |du rt        d      |j                  |j                  k7  rt        d|       t        |||||||	|||

       y)z@
        Validate the kwargs for the preprocess method.
        Fz4`do_resize` cannot be set to `False` for this model!z'Can resize only to square size but got )
rA   rB   rC   rD   rE   rH   rI   rF   rG   rJ   N)
ValueErrorr+   r,   r
   )r?   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   r;   s               r   _validate_preprocess_kwargsz1InklingImageProcessor._validate_preprocess_kwargsM   sa    " STT;;$**$FtfMNN%!)%!)	
r   imagesr#   c                 $    t        |   |fi |S )N)r=   
preprocess)r?   rN   r;   r@   s      r   rP   z InklingImageProcessor.preprocessq   s    
 w!&3F33r   r!   r   r   return_tensorsc           	         g }g }|D ]w  }|	|j                   dd  \  }}t        ||      }||	z  }|
t        |t        |
|            }||z  }|dk7  rSt        t	        j
                  ||z  dz         t	        j
                  ||z  dz               }| j                  |||      }t        ||j                        }|D cg c]  }|j                          }}| j                  |t        |j                  |j                        d      }t        j                  |d	      }| j                  ||||||      }|d
   j                  ddddd      }|j!                  |       |j!                  |j                   d          z t        j"                  |d	      j%                  ddddd      }|t        j&                  |      d}t)        ||      S c c}w )Nr%   g      ?g      ?r:   )rJ   g      )pad_size
fill_valuer   )dim).Nr'         r   )pixel_valuesnum_patches)datatensor_type)r(   maxminr	   mathfloorresizer5   r+   r   padr,   torchstackrescale_and_normalizerepeatr*   catpermutetensorr   )r?   rN   rG   rA   rB   rC   rD   rE   rJ   r   r   rQ   r;   per_image_patchesrY   r    r+   r,   	long_edgetarget_long_edgerationew_sizeimage_patchesimrX   rZ   s                             r   _preprocessz!InklingImageProcessor._preprocessx   s    13!# 	7E!- %BC 0.	#,/A#A 7C'*+;SAegp=q'r$(94C<'tzz&5.3:N/OW[WaWabgjoborubuWvwH KKx(KKE-eT[[AM2?@BRXXZ@M@ HH4::(Vcg % M "KK1=M 66z><U^M *)4;;Aq!QJM$$]3}22156/	72 yy!2:BB1aAqQ( <<4
 >BB# As   ?G)
NNNNNNNNNN)%r   r   r   r   LANCZOSrJ   r   rD   r   rE   default_to_squaredo_convert_rgbrF   rA   rC   rG   r   r   r   valid_kwargsr   r>   boolr   tupler	   rM   r   r   r   rP   listr   strr   rp   __classcell__)r@   s   @r   r8   r8   ;   s   !))H!JINIJL2&D+/(.L#(C!D #
 #''+$(2615!% $&*%)NR"
4K"
 "
 Tk	"

 E%L(4/"
 5<'$."
 $;"
 o"
 t"
 d?"
 L"
H44 &4 
	4/C^$/C /C 	/C
 /C /C DK'$./C 4;&-/C L/C "DL/C /2Dj/C j(4//C 
/Cr   r8   )r^   rb   torchvision.transforms.v2r   tvFimage_processing_backendsr   image_processing_utilsr   image_utilsr   r   r	   r
   processing_utilsr   r   utilsr   r   utils.constantsr   r   r   r   rw   r5   r8   __all__r   r   r   <module>r      s       7 ; 2 b b 4 / @5,e 5^  nAU  DEkC. kC FkC\ #
#r   