CoolFace
Apppublic

declare-lab/tango2

sourceHugging Faceupdated 2y agoView on Hugging Face
92likes
image_processor.cpython-39.pyc55 linesDownload Raw Back to __pycache__
1a

2��'d��@slddlZddlmZddlZddlZddlZddlmZddlm	Z	m3Z4ddlmZm
Z
Gdd�de	�ZdS)	�N)�Union)�Image�)�ConfigMixin�register_to_config)�CONFIG_NAME�PIL_INTERPOLATIONcs�eZdZdZeZedeee	ed��fdd�
�Z5edd	��Zed6d��Z
edd
��Zedd��Zejjejjd�dd�Zeejejjejfejd�dd�Zde	d�dd�Z�ZS)�VaeImageProcessora�7    Image Processor for VAE8 9    Args:10        do_resize (`bool`, *optional*, defaults to `True`):11            Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`.12        vae_scale_factor (`int`, *optional*, defaults to `8`):13            VAE scale factor. If `do_resize` is True, the image will be automatically resized to multiples of this14            factor.15        resample (`str`, *optional*, defaults to `lanczos`):16            Resampling filter to use when resizing the image.17        do_normalize (`bool`, *optional*, defaults to `True`):18            Whether to normalize the image to [-1,1]19    T��lanczos)�	do_resize�vae_scale_factor�resample�do_normalizecst���dS�N)�super�__init__)�selfrr
rr��	__class__��N/home/deep/Projects/audio_diffusion/diffusers/src/diffusers/image_processor.pyr-szVaeImageProcessor.__init__cCsT|jdkr|d}|d���d�}|jddkrBdd�|D�}nd	d�|D�}|S)20zL21        Convert a numpy image or a batch of images to a PIL image.22        �)N.���uint8�����rcSsg|]}tj|��dd��qS)�L)�mode)r�	fromarray�squeeze��.0�imagerrr�23<listcomp>A�z2VaeImageProcessor.numpy_to_pil.<locals>.<listcomp>cSsg|]}t�|��qSr)rrr rrrr#Cr$)�ndim�round�astype�shape)�images�24pil_imagesrrr�numpy_to_pil7s25zVaeImageProcessor.numpy_to_pilcCs,|jdkr|d}t�|�dddd��}|S)�;26        Convert a numpy image to a pytorch tensor27        r).Nrr�)r%�torch�28from_numpy�	transpose�r)rrr�numpy_to_ptGs29zVaeImageProcessor.numpy_to_ptcCs |���dddd�����}|S)r,rr-rr)�cpu�permute�float�numpyr1rrr�pt_to_numpyRszVaeImageProcessor.pt_to_numpycCsd|dS)z430        Normalize an image array to [-1,1]31        g@g�?rr1rrr�	normalizeZszVaeImageProcessor.normalize)r)�returncs@|j\}}�fdd�||fD�\}}|j||ft�jd�}|S)z�32        Resize a PIL image. Both height and width will be downscaled to the next integer multiple of `vae_scale_factor`33        c3s|]}||�jVqdSr)r
)r!�x�rrr�	<genexpr>fr$z+VaeImageProcessor.resize.<locals>.<genexpr>)r)�size�resizerr)rr)�w�hrr;rr>as34zVaeImageProcessor.resize)r"r9cs8tjjtjtjf�t|��r$|g}nDt|t�rDt�fdd�|D��sht	ddd�|D��dd�35������t|dtjj�r��jr��fd	d�|D�}d36d�|D�}tj|dd�}��
|�}�n.t|dtj��rX|djdkr�tj|dd�ntj|dd�}��
|�}|j\}}}}�j�r�|�jdk�s8|�jdk�r�t	d
�j�d|�d|�d���n�t|dtj��r�|djdk�r�tj|dd�ntj|dd�}|j\}}}}�j�r�|�jdk�s�|�jdk�r�t	d�j�d|�d|�d����j}|��dk�r$t�d|���d|���d�t�d}|�r4��|�}|S)zo37        Preprocess the image input, accepted formats are PIL images, numpy arrays or pytorch tensors"38        c3s|]}t|��VqdSr)�39isinstance�r!�i)�supported_formatsrrr<tr$z/VaeImageProcessor.preprocess.<locals>.<genexpr>zInput is in incorrect format: cSsg|]}t|��qSr)�typerBrrrr#vr$z0VaeImageProcessor.preprocess.<locals>.<listcomp>z. Currently, we only support z, rcsg|]}��|��qSr)r>rBr;rrr#{r$cSs"g|]}t�|��tj�d�qS)g�o@)�np�arrayr'�float32rBrrrr#|r$)�axis�zeCurrently we only support resizing for PIL image - please resize your numpy array to be divisible by zcurrently the sizes are z and zQ. You can also pass a PIL image instead to use resize option in VAEImageProcessorzhCurrently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by z�Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] when passing as pytorch tensor or numpy Array. You passed `image` with value range [�,�]F)�PILrrF�ndarrayr.�TensorrA�list�all�40ValueError�joinr�stackr2r%�concatenater(r
�catr�min�warnings�warn�max�
FutureWarningr8)rr"�_�height�widthrr)rrDr�41preprocessjsd42 �*43(44���,(45������46zVaeImageProcessor.preprocess�pil)�output_typecCsTt|tj�r|dkr|S|�|�}|dkr.|S|dkr@|�|�Std|�d���dS)N�ptrFr`zUnsupported output_type �.)rAr.rOr7r+rR)rr"rarrr�postprocess�s4748zVaeImageProcessor.postprocess)Tr49rT)r`)�__name__�50__module__�__qualname__�__doc__r�config_namer�bool�int�strr�staticmethodr+r2r7r8rMrr>rr.�FloatTensorrFrNrOr_rd�
__classcell__rrrrr	s:��	5152 535455�;��r	)rX�typingrr6rFrMr.r�configuration_utilsrr�utilsrrr	rrrr�<module>s