| from share import * |
| import config |
|
|
| import einops |
| import gradio as gr |
| import numpy as np |
| import torch |
| import random |
| import cv2 |
|
|
| from pytorch_lightning import seed_everything |
| from annotator.util import resize_image, HWC3 |
| from cldm.model import create_model, load_state_dict |
| from cldm.ddim_hacked import DDIMSampler |
|
|
|
|
| model = create_model('models/cldm_v15-mask.yaml').cpu() |
| model.load_state_dict(load_state_dict('/tmp/paint-by-example-controlnet-full/controlnet-full-step=20999.ckpt', location='cuda')) |
| model = model.cuda() |
| ddim_sampler = DDIMSampler(model) |
|
|
| def process(ref_image, control_img, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta): |
| with torch.no_grad(): |
| ref = cv2.resize(ref_image, (224,224)) |
| ref = torch.from_numpy(np.array(ref).astype(np.float32)).cuda() / 255.0 |
| ref = torch.stack([ref for _ in range(num_samples)], dim=0) |
| ref = einops.rearrange(ref, 'b h w c -> b c h w').clone() |
| |
| control = cv2.resize(control_img, (image_resolution ,image_resolution)) |
| control = resize_image(HWC3(control), image_resolution) |
| control = torch.from_numpy(np.array(control).astype(np.float32)).cuda() / 255.0 |
| control = torch.stack([control for _ in range(num_samples)], dim=0) |
| control = einops.rearrange(control, 'b h w c -> b c h w').clone() |
| B, C, H, W = control.shape |
| |
| |
| if seed == -1: |
| seed = random.randint(0, 65535) |
| seed_everything(seed) |
|
|
| if config.save_memory: |
| model.low_vram_shift(is_diffusing=False) |
| cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning(ref)]} |
| un_cond = {"c_concat": [control], "c_crossattn": [model.learnable_vector]} |
| shape = (4, H // 8, W // 8) |
|
|
| if config.save_memory: |
| model.low_vram_shift(is_diffusing=True) |
| |
| model.control_scales = [strength * (0.825 ** float(12 - i)) for i in range(13)] if guess_mode else ([strength] * 13) |
| samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples, |
| shape, cond, verbose=False, eta=eta, |
| unconditional_guidance_scale=scale, |
| unconditional_conditioning=un_cond) |
|
|
| if config.save_memory: |
| model.low_vram_shift(is_diffusing=False) |
|
|
| x_samples = model.decode_first_stage(samples) |
| x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8) |
|
|
| results = [x_samples[i] for i in range(num_samples)] |
| return results |
|
|
| block = gr.Blocks().queue() |
| with block: |
| with gr.Row(): |
| gr.Markdown("## Paint-by-Example + ControlNet") |
| with gr.Row(): |
| with gr.Column(): |
| control_image = gr.Image(label="img mask", source='upload', type="numpy") |
| ref_image = gr.Image(label="ref image", source='upload', type="numpy") |
| run_button = gr.Button(label="Run") |
| with gr.Accordion("Advanced options", open=False): |
| num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1) |
| image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64) |
| strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01) |
| guess_mode = gr.Checkbox(label='Guess Mode', value=False) |
| ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1) |
| scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1) |
| seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True) |
| eta = gr.Number(label="eta (DDIM)", value=0.0) |
| |
| with gr.Column(): |
| result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto') |
| ips = [ref_image, control_image, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta] |
| run_button.click(fn=process, inputs=ips, outputs=[result_gallery]) |
|
|
|
|
| block.launch(debug=True, share=True) |