Spaces:
Runtime error
Runtime error
File size: 8,328 Bytes
a202a34 7db9719 a202a34 7db9719 a202a34 7db9719 a202a34 7bc24ed a202a34 7bc24ed f036335 a202a34 7db9719 a202a34 7db9719 a202a34 7db9719 a202a34 1f7767c a202a34 1f7767c a202a34 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
import cv2
import gradio as gr
import numpy as np
import torch
from diffusers import StableDiffusionControlNetPipeline, StableDiffusionLatentUpscalePipeline, ControlNetModel, AutoencoderKL
from diffusers import UniPCMultistepScheduler
from PIL import Image
from lpw import _encode_prompt
controlnet_ColorCanny = ControlNetModel.from_pretrained("ghoskno/Color-Canny-Controlnet-model", torch_dtype=torch.float16)
vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16)
pipe = StableDiffusionControlNetPipeline.from_pretrained("Lykon/DreamShaper", vae=vae, controlnet=controlnet_ColorCanny, torch_dtype=torch.float16)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
pipe.enable_xformers_memory_efficient_attention()
pipe.enable_attention_slicing()
# Generator seed
generator = torch.manual_seed(0)
def HWC3(x):
assert x.dtype == np.uint8
if x.ndim == 2:
x = x[:, :, None]
assert x.ndim == 3
H, W, C = x.shape
assert C == 1 or C == 3 or C == 4
if C == 3:
return x
if C == 1:
return np.concatenate([x, x, x], axis=2)
if C == 4:
color = x[:, :, 0:3].astype(np.float32)
alpha = x[:, :, 3:4].astype(np.float32) / 255.0
y = color * alpha + 255.0 * (1.0 - alpha)
y = y.clip(0, 255).astype(np.uint8)
return y
def resize_image(input_image, resolution, max_edge=False, edge_limit=False):
H, W, C = input_image.shape
H = float(H)
W = float(W)
if max_edge:
k = float(resolution) / max(H, W)
else:
k = float(resolution) / min(H, W)
H *= k
W *= k
H, W = int(H), int(W)
img = cv2.resize(input_image, (W, H), interpolation=cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA)
if not edge_limit:
return img
pH = int(np.round(H / 64.0)) * 64
pW = int(np.round(W / 64.0)) * 64
pimg = np.zeros((pH, pW, 3), dtype=img.dtype)
oH, oW = (pH-H)//2, (pW-W)//2
pimg[oH:oH+H, oW:oW+W] = img
return pimg
def get_canny_filter(image, low_threshold=100, high_threshold=200):
image = cv2.Canny(image, low_threshold, high_threshold)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
return image
def get_color_filter(cond_image, mask_size=64):
H, W = cond_image.shape[:2]
cond_image = cv2.resize(cond_image, (W // mask_size, H // mask_size), interpolation=cv2.INTER_CUBIC)
color = cv2.resize(cond_image, (W, H), interpolation=cv2.INTER_NEAREST)
return color
def get_colorcanny(image, mask_size):
canny_img = get_canny_filter(image)
color_img = get_color_filter(image, int(mask_size))
color_img[np.where(canny_img > 128)] = 255
return color_img
def process(input_image, prompt, n_prompt, strength=1.0, color_mask_size=96, size=512, scale=6.0, ddim_steps=20):
prompt_embeds, negative_prompt_embeds = _encode_prompt(pipe, prompt, pipe.device, 1, True, n_prompt, 3)
input_image = resize_image(input_image, size, max_edge=True, edge_limit=True)
cond_img = get_colorcanny(input_image, color_mask_size)
cond_img = Image.fromarray(cond_img)
output = pipe(
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_prompt_embeds,
image=cond_img,
generator=generator,
num_images_per_prompt=1,
num_inference_steps=ddim_steps,
guidance_scale=scale,
controlnet_conditioning_scale=float(strength)
)
return [output.images[0], cond_img]
def inpaint_process(inpaint_image, input_image, prompt, n_prompt, strength=1.0, color_mask_size=96, size=512, scale=6.0, ddim_steps=20):
if inpaint_image is None:
return process(input_image, prompt, n_prompt, strength, color_mask_size, size, scale, ddim_steps)
prompt_embeds, negative_prompt_embeds = _encode_prompt(pipe, prompt, pipe.device, 1, True, n_prompt, 3)
input_image = resize_image(input_image, size, max_edge=True, edge_limit=True)
inpaint_image = resize_image(inpaint_image, size, max_edge=True, edge_limit=True)
canny_img = get_canny_filter(input_image)
color_img = get_color_filter(inpaint_image, int(color_mask_size))
color_img[np.where(canny_img > 128)] = 255
cond_img = Image.fromarray(color_img)
output = pipe(
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_prompt_embeds,
image=cond_img,
generator=generator,
num_images_per_prompt=1,
num_inference_steps=ddim_steps,
guidance_scale=scale,
controlnet_conditioning_scale=float(strength)
)
return [output.images[0], cond_img]
block = gr.Blocks().queue()
with block:
gr.Markdown("""
# 🧨 Color-Canny-ControlNet
This is an extended model of ControlNet that not only utilizes the Canny edge of images but also incorporates the color features.
We trained this model on the cleaned laion-art dataset that contains 2.6 million images with 2 epochs, using the Canny edge and color mosaic of the images as input. The processed dataset and pretrained model can be found in [ghoskno/laion-art-en-colorcanny](https://huggingface.co/datasets/ghoskno/laion-art-en-colorcanny) and [ghoskno/Color-Canny-Controlnet-model](https://huggingface.co/ghoskno/Color-Canny-Controlnet-model).
This allows generated images to maintain the same color composition as the original images. If you are looking to control both the contours and colors of the original image while using ControlNet to generate images, then this is the best option for you! You can try out this model or test the examples provided below 🤗.
## Update
Hi, everyone, We have added a Color-Canny-ControlNet accelerated version of our implementation based on Nvidia Triton and operator optimization. This faster ControlNet is deployed on a Nvidia A10 machine, and for a 512-pixel image, the inference takes about 1.2s, which is more faster than general implementation.
Welcome to try this [faster Color-Canny-ControlNet](http://121.40.118.209:7860/).
""")
with gr.Row():
with gr.Column():
input_image = gr.Image(source='upload', type="numpy")
color_image = gr.ImagePaint(type="numpy")
prompt = gr.Textbox(label="Prompt", value='')
n_prompt = gr.Textbox(label="Negative Prompt", value='')
with gr.Row():
run_button = gr.Button(label="Run")
run_edit_button = gr.Button(value='Run with inpaint color', label="Run with inpaint color")
with gr.Accordion('Advanced', open=False):
strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
color_mask_size = gr.Slider(label="Color Mask Size", minimum=32, maximum=256, value=96, step=16)
size = gr.Slider(label="Size", minimum=256, maximum=768, value=512, step=128)
scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=6.0, step=0.1)
ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=20, step=1)
with gr.Column():
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
ips = [input_image, prompt, n_prompt, strength, color_mask_size, size, scale, ddim_steps]
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
run_edit_button.click(fn=inpaint_process, inputs=[color_image] + ips, outputs=[result_gallery])
gr.Examples(
examples=[
["./asserts/1.png", "a concept art of by Makoto Shinkai, a girl is standing in the middle of the sea", "text, bad anatomy, blurry, (low quality, blurry)"],
["./asserts/2.png", "a concept art with vivid ocean by Makoto Shinkai", "text, bad anatomy, blurry, (low quality, blurry)"],
["./asserts/3.png", "sky city on the sea, with waves churning and wind power plants on the island", "text, bad anatomy, blurry, (low quality, blurry)"],
],
inputs=[
input_image, prompt, n_prompt
],
outputs=[result_gallery],
fn=process,
cache_examples=True,
)
block.launch(debug = True, server_name='0.0.0.0') |