CoolFace
Apppublic

xdecoder/Instruct-X-Decoder

sourceHugging Faceafl-3.0updated 3y agoView on Hugging Face
163likes
inpainting.cpython-38.pyc55 linesDownload Raw Back to __pycache__
1U

2A8�cN�@s�ddlZddlZddlZddlZddlZddlmZddl	m3Z4ddlmZddl
mZddlmZddlmZdd	lmZe�d5�ddd�Zd
d�Zddd�Ze��ddd��Zdd�Zdd�Ze��dd��ZdS)�N)�Image)�	OmegaConf)�repeat)�WatermarkEncoder)�Path�)�DDIMSampler)�instantiate_from_configFcCsN|dk	rJt�t�|�tj�}|�|d�}t�|dd�dd�ddd�f�}|S)NZdwtDct�����)�cv2�cvtColor�np�array�
COLOR_RGB2BGR�encoder�	fromarray)�img�6wm_encoder�r�)/data/arXiv/demo/Demo/utils/inpainting.py�
put_watermarks7"rcCs`t�|�}t|j�}|jt�|�ddd�tj��r@t�d�nt�d�}|�	|�}t8|�}|S)N�9state_dictF)�strict�cuda�cpu)r�loadr	�model�load_state_dict�torchr�is_available�device�tor)�config�ckptrr �samplerrrr�initialize_models1011���12r%cCs�t�|�d��}|d�dddd�}t�|�jtjd�dd}t�|�d	��}|�tj�d13}|d}d||dk<d||dk<t�|�}||dk}t	|j|d
�d|d�||gt	|j|d
�d|d�t	|j|d
�d|d�d�}|S)N�RGBr�r�)�dtypeg�_@��?�Lg�o@)NNg�?)r z1 ... -> n ...)�n)�image�txt�mask�masked_image)14r
r�convert�	transposer�15from_numpyr!�float32�astyper)r-r/r.r �num_samplesr0�batchrrr�
make_batch_sd)s 16�r8�c17
stj��rt�d�nt�d�}18|j}td�d}t����d|�d��t	j19�|�}
|
�|d|	d|d�}t�
|�j|20tjd	�}t����\t�d���Dt||||21|d22�}|j�|d�}t�}|jD]f}||��}||jk�r|d|	d|dg}tjjj||dd�d
�}n|�|�|��}|�|�q�tj|dd�}|g|gd�}|�|d�}|g|gd�}|j |	d|dg}|j!||||dd|||d�	\}}|�"|�}tj#|ddddd�}|�$��%��&dddd�d}W5QRXW5QRX�fdd�|D�S)Nrrz^Creating invisible watermark encoder (see https://github.com/ShieldMnt/invisible-watermark)...ZSDV2�byteszutf-8��)r r))r.r r6r.�����)�sizer)�dim)Zc_concatZc_crossattn�Fr*)�verbose�etaZunconditional_guidance_scaleZunconditional_conditioningZx_Tg@g)�min�maxrr(r'��cs$g|]}tt�|�tj�����qSr)rrrr5r
�uint8)�.0r�rrr�23<listcomp>�szinpaint.<locals>.<listcomp>)'rrrr r�printrZ
set_watermarkrr
�random�RandomState�randnr3r!r4�no_grad�autocastr8Zcond_stage_model�listZconcat_keys�floatZmasked_image_key�nn�24functional�interpolateZget_first_stage_encodingZencode_first_stage�append�catZget_unconditional_conditioning�channels�sampleZdecode_first_stage�clampr�numpyr2)r$r-r/�prompt�seed�scale�25ddim_stepsr6�w�hr r�wmZprngZ26start_coder7�cZc_catZck�ccZbchw�condZuc_crossZuc_full�shapeZsamples_cfg�
intermediatesZx_samples_ddim�resultrrHr�inpaintDst���27���28��2930�0rhcCsftjdt�t�|j�d��t�fdd�d|j\}}t�tj	t�|�d|fd|fdfdd��}|S)N)r(r(�@r)�axis)rr�edge)�mode)31r
rD�ceilrr>r5�intrr�pad)�input_image�pad_w�pad_hZ	im_paddedrrr�	pad_image�s32�����"�rscCsHt�t�|j�d��t�d\}}t�t�|�d|�d|�f�}|S)Nri)r
�floorrr>r5rnrr)rp�crop_w�crop_h�33im_croppedrrr�34crop_image�s$ rxcCsb|d�d�}|d�d�}t|�}	t|�}35|	j\}}td||�t||	|36|||||||d�37}
|
S)a_summary_38 39    Args:40        input_image (_type_): dict41            - image: PIL.Image. Input image.42            - mask: PIL.Image. Mask image.43        prompt (_type_): string to be used as prompt. 44        ddim_steps (_type_): typical 4545        num_samples (_type_): typical 446        scale (_type_): typical 10.0 Guidance Scale.47        seed (_type_): typical 152916051948    49    r-r&r/z
Inpainting...)50r$r-r/r[r\r]r^r6r`r_)r1rsr>rJrh)rrpr[r^r6r]r\Z51init_imageZ	init_maskr-r/�width�heightrgrrr�predict�s&52�r{)N)r)rr9r9)�sysrrrZr
�gradio�gr�PILr�	omegaconfr�einopsrZimwatermarkr�pathlibrZddimr�utilr	�set_grad_enabledrr%r8rNrhrsrxr{rrrr�<module>s,5354�55=