
    ^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 ddlmZmZmZmZmZ dd	lmZ dd
lmZmZ dZdZ G d ded      Ze G d de             ZdgZy)zImage processor class for Vilt.    N)
functional   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)IMAGENET_STANDARD_MEANIMAGENET_STANDARD_STDPILImageResamplingSizeDictget_max_height_width)ImagesKwargs)
TensorTypeauto_docstringi5  i   c                       e Zd ZU dZeed<   y)ViltImageProcessorKwargsz
    size_divisor (`int`, *optional*, defaults to `self.size_divisor`):
        The size by which to make sure both the height and width can be divided. Only has an effect if `do_resize`
        is set to `True`.
    size_divisorN)__name__
__module____qualname____doc__int__annotations__     y/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/vilt/image_processing_vilt.pyr   r   )   s     r   r   F)totalc                   H    e Zd ZeZej                  ZeZ	e
ZddiZdZdZdZdZdZdZddgZ	 	 dd	d
dedddedz  dd
f
 fdZd	ed
   deez  dz  dedz  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dz  deez  dz  dedz  defdZ xZ S )ViltImageProcessorshortest_edgei  T    Fpixel_values
pixel_maskNimagesztorch.Tensorsizeresamplez7PILImageResampling | tvF.InterpolationMode | int | Noner   returnc                    |j                   }t        t        t        z  |z        }|j                  d   }|j                  d   }||k  r|}	|||z  z  }
n
|||z  z  }	|}
t        |	|
      |kD  r|t        |	|
      z  }|	|z  }	|
|z  }
t        |	dz         }	t        |
dz         }
||	|z  |z  }	|
|z  |z  }
t        |   |t        |	|
      |      S )a  
        Resize an image or batch of images to specified size.

        Args:
            images (`torch.Tensor`): Image or batch of images to resize.
            size (`SizeDict`): Size dictionary with shortest_edge key.
            resample (`PILImageResampling | tvF.InterpolationMode | int`, *optional*): Interpolation method to use.
            size_divisor (`int`, *optional*): Value to ensure height/width are divisible by.

        Returns:
            `torch.Tensor`: Resized image or batch of images.
        g      ?)heightwidth)r&   )	r    r   MAX_LONGER_EDGEMAX_SHORTER_EDGEshapemaxsuperresizer   )selfr$   r%   r&   r   shorterlongerheightswidthsnew_heights
new_widthsscale	__class__s               r   r2   zViltImageProcessor.resizeB   s   * $$_'77'AB,,r"b! V!K7W#45J!Wv%56K J {J'&0Sj99E%-K#e+J++,c)*
 #%5DK#|3lBJ w~fhk&T_g~hhr   return_tensorsdisable_groupingc                 T   t        |      }t        ||      \  }}i }i }|j                         D ]Z  \  }	}
|dk(  r@t        |
      dkD  r2|
j                  }t        j                  |t
        j                  |      }|
j                  dd }|d   |d   k7  xs |d   |d   k7  }|r|d   |d   z
  }|d   |d   z
  }dd||g}t        j                  |
|d      }j                         }|d|d   d|d   f   j                  d       |j                  d      j                  |
j                  d   dd      }nH|
}t        j                  |
j                  d   |d   |d   ft
        j                  |
j                        }|||	<   |||	<   ] t!        ||      }t!        ||      }||fS )	a`  
        Pad a batch of images to the same size based on the maximum dimensions.

        Args:
            images (`list[torch.Tensor]`): List of images to pad.
            return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return.

        Returns:
            `tuple`: Tuple containing padded images and pixel masks.
        r=   ptr   )dtypedevicer)   N   )fill)r   r   itemslenrB   torchzerosint64r/   tvFpadclonefill_	unsqueezerepeatonesr   )r3   r$   r<   r=   max_sizegrouped_imagesgrouped_images_indexprocessed_imagesprocessed_masksr/   stacked_imagesrB   mask_templateoriginal_sizeneeds_paddingpadding_bottompadding_rightpaddingpadded_imagesr#   pixel_maskss                        r   
_pad_batchzViltImageProcessor._pad_batchv   s   " (/ 0EV^n/o,,%3%9%9%; 	1!E>%#n*=*A'.. %HEKKPV W*005M)!,;^}Q?OS[\]S^?^M!)!}Q/?!? (mA.> >a? #a H*002
-]1--/Aq1A/AABHHK(2215<<^=Q=QRS=TVWYZ[ .#jj#))!,hqk8A;G++)00 '4U#%0OE"9	1> ''79MN$_6JKk))r   	do_resize
do_rescalerescale_factordo_normalize
image_mean	image_stddo_padc           	         t        ||      \  }}i }|j                         D ]   \  }}|r| j                  ||||      }|||<   " t        ||      }t        ||      \  }}i }|j                         D ]   \  }}| j	                  ||||||	      }|||<   " t        ||      }i }|
r| j                  |||      \  }}||d}n|dk(  rt        j                  |      }d|i}t        ||      S )Nr?   )r"   r#   r@   r"   )datatensor_type)	r   rE   r2   r   rescale_and_normalizer_   rG   stackr   )r3   r$   r`   r%   r&   ra   rb   rc   rd   re   rf   r=   r<   r   kwargsrR   rS   resized_images_groupedr/   rV   resized_imagesprocessed_images_groupedrT   rh   r"   r#   s                             r   _preprocesszViltImageProcessor._preprocess   sJ   $ 0EV^n/o,,!#%3%9%9%; 	;!E>!%^T8\!Z,:"5)	; ((>@TU 0E^fv/w,,#% %3%9%9%; 	=!E>!77
NL*V_N /=$U+	= **BDXY '+ .CS (7 ($L* %1
KD %#(;;/?#@ "$45D>BBr   )NN)N)!r   r   r   r   valid_kwargsr   BICUBICr&   r	   rd   r
   re   r%   r`   ra   rc   r   rf   default_to_squaremodel_input_namesr   r   r2   liststrr   booltupler_   floatr   rp   __classcell__)r;   s   @r   r   r   3   s   +L!))H'J%IS!DIJLLF'6 OS#'2i2i 2i L	2i
 Dj2i 
2ih:*^$:* j(4/:* +	:*
 
:*T $(5C^$5C 5C 	5C
 L5C 5C 5C 5C DK'$.5C 4;&-5C t5C +5C j(4/5C Dj5C  
!5Cr   r   )r   rG   torchvision.transforms.v2r   rJ   image_processing_backendsr   image_processing_utilsr   image_transformsr   r   image_utilsr	   r
   r   r   r   processing_utilsr   utilsr   r   r-   r.   r   r   __all__r   r   r   <module>r      su    &  7 ; 2 E  -  |5  sC+ sC sCl  
 r   