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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
from diffusers import (
|
| 6 |
+
ControlNetModel,
|
| 7 |
+
StableDiffusionControlNetPipeline,
|
| 8 |
+
UniPCMultistepScheduler,
|
| 9 |
+
)
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
stable_model_list = [
|
| 13 |
+
"runwayml/stable-diffusion-v1-5",
|
| 14 |
+
"stabilityai/stable-diffusion-2-1",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
controlnet_canny_model_list = [
|
| 18 |
+
"lllyasviel/sd-controlnet-canny",
|
| 19 |
+
"thibaud/controlnet-sd21-canny-diffusers",
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
stable_prompt_list = ["a photo of a man.", "a photo of a girl."]
|
| 24 |
+
|
| 25 |
+
stable_negative_prompt_list = ["bad, ugly", "deformed"]
|
| 26 |
+
|
| 27 |
+
data_list = [
|
| 28 |
+
"data/test.png",
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def controlnet_canny(
|
| 33 |
+
dict_image: str,
|
| 34 |
+
controlnet_model_path: str,
|
| 35 |
+
):
|
| 36 |
+
image = dict_image["image"].convert("RGB").resize((512, 512))
|
| 37 |
+
image = np.array(image)
|
| 38 |
+
|
| 39 |
+
image = cv2.Canny(image, 100, 200)
|
| 40 |
+
image = image[:, :, None]
|
| 41 |
+
image = np.concatenate([image, image, image], axis=2)
|
| 42 |
+
image = Image.fromarray(image)
|
| 43 |
+
|
| 44 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 45 |
+
controlnet_model_path, torch_dtype=torch.float16
|
| 46 |
+
)
|
| 47 |
+
return controlnet, image
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def stable_diffusion_controlnet_canny(
|
| 51 |
+
image_path: str,
|
| 52 |
+
stable_model_path: str,
|
| 53 |
+
controlnet_model_path: str,
|
| 54 |
+
prompt: str,
|
| 55 |
+
negative_prompt: str,
|
| 56 |
+
guidance_scale: int,
|
| 57 |
+
num_inference_step: int,
|
| 58 |
+
):
|
| 59 |
+
|
| 60 |
+
controlnet, image = controlnet_canny(
|
| 61 |
+
image_path=image_path, controlnet_model_path=controlnet_model_path
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 65 |
+
pretrained_model_name_or_path=stable_model_path,
|
| 66 |
+
controlnet=controlnet,
|
| 67 |
+
safety_checker=None,
|
| 68 |
+
torch_dtype=torch.float16,
|
| 69 |
+
)
|
| 70 |
+
pipe.to("cuda")
|
| 71 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 72 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 73 |
+
|
| 74 |
+
output = pipe(
|
| 75 |
+
prompt=prompt,
|
| 76 |
+
image=image,
|
| 77 |
+
negative_prompt=negative_prompt,
|
| 78 |
+
num_inference_steps=num_inference_step,
|
| 79 |
+
guidance_scale=guidance_scale,
|
| 80 |
+
).images
|
| 81 |
+
|
| 82 |
+
return output[0]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def stable_diffusion_controlnet_canny_app():
|
| 86 |
+
with gr.Blocks():
|
| 87 |
+
with gr.Row():
|
| 88 |
+
with gr.Column():
|
| 89 |
+
controlnet_canny_image_file = gr.Image(
|
| 90 |
+
type="filepath", label="Image"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
controlnet_canny_stable_model_id = gr.Dropdown(
|
| 94 |
+
choices=stable_model_list,
|
| 95 |
+
value=stable_model_list[0],
|
| 96 |
+
label="Stable Model Id",
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
controlnet_canny_model_id = gr.Dropdown(
|
| 100 |
+
choices=controlnet_canny_model_list,
|
| 101 |
+
value=controlnet_canny_model_list[0],
|
| 102 |
+
label="Controlnet Model Id",
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
controlnet_canny_prompt = gr.Textbox(
|
| 106 |
+
lines=1, value=stable_prompt_list[0], label="Prompt"
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
controlnet_canny_negative_prompt = gr.Textbox(
|
| 110 |
+
lines=1,
|
| 111 |
+
value=stable_negative_prompt_list[0],
|
| 112 |
+
label="Negative Prompt",
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 116 |
+
controlnet_canny_guidance_scale = gr.Slider(
|
| 117 |
+
minimum=0.1,
|
| 118 |
+
maximum=15,
|
| 119 |
+
step=0.1,
|
| 120 |
+
value=7.5,
|
| 121 |
+
label="Guidance Scale",
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
controlnet_canny_num_inference_step = gr.Slider(
|
| 125 |
+
minimum=1,
|
| 126 |
+
maximum=100,
|
| 127 |
+
step=1,
|
| 128 |
+
value=50,
|
| 129 |
+
label="Num Inference Step",
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
controlnet_canny_predict = gr.Button(value="Generator")
|
| 133 |
+
|
| 134 |
+
with gr.Column():
|
| 135 |
+
output_image = gr.Image(label="Output")
|
| 136 |
+
|
| 137 |
+
gr.Examples(
|
| 138 |
+
fn=stable_diffusion_controlnet_canny,
|
| 139 |
+
examples=[
|
| 140 |
+
[
|
| 141 |
+
data_list[0],
|
| 142 |
+
stable_model_list[0],
|
| 143 |
+
controlnet_canny_model_list[0],
|
| 144 |
+
stable_prompt_list[0],
|
| 145 |
+
stable_negative_prompt_list[0],
|
| 146 |
+
7.5,
|
| 147 |
+
50,
|
| 148 |
+
]
|
| 149 |
+
],
|
| 150 |
+
inputs=[
|
| 151 |
+
controlnet_canny_image_file,
|
| 152 |
+
controlnet_canny_stable_model_id,
|
| 153 |
+
controlnet_canny_model_id,
|
| 154 |
+
controlnet_canny_prompt,
|
| 155 |
+
controlnet_canny_negative_prompt,
|
| 156 |
+
controlnet_canny_guidance_scale,
|
| 157 |
+
controlnet_canny_num_inference_step,
|
| 158 |
+
],
|
| 159 |
+
outputs=[output_image],
|
| 160 |
+
cache_examples=False,
|
| 161 |
+
label="Controlnet Canny Example",
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
controlnet_canny_predict.click(
|
| 165 |
+
fn=stable_diffusion_controlnet_canny,
|
| 166 |
+
inputs=[
|
| 167 |
+
controlnet_canny_image_file,
|
| 168 |
+
controlnet_canny_stable_model_id,
|
| 169 |
+
controlnet_canny_model_id,
|
| 170 |
+
controlnet_canny_prompt,
|
| 171 |
+
controlnet_canny_negative_prompt,
|
| 172 |
+
controlnet_canny_guidance_scale,
|
| 173 |
+
controlnet_canny_num_inference_step,
|
| 174 |
+
],
|
| 175 |
+
outputs=[output_image],
|
| 176 |
+
)
|
diffusion_webui/controlnet_inpaint/controlnet_inpaint_app.py
CHANGED
|
@@ -4,14 +4,14 @@ import torch
|
|
| 4 |
from diffusers import UniPCMultistepScheduler
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
from diffusion_webui.
|
| 8 |
from diffusion_webui.controlnet_inpaint.pipeline_stable_diffusion_controlnet_inpaint import (
|
| 9 |
StableDiffusionControlNetInpaintPipeline,
|
| 10 |
)
|
| 11 |
|
| 12 |
stable_inpaint_model_list = [
|
| 13 |
-
"stabilityai/stable-diffusion-2-inpainting",
|
| 14 |
"runwayml/stable-diffusion-inpainting",
|
|
|
|
| 15 |
]
|
| 16 |
|
| 17 |
controlnet_model_list = [
|
|
@@ -36,7 +36,7 @@ def load_img(image_path: str):
|
|
| 36 |
|
| 37 |
|
| 38 |
def stable_diffusion_inpiant_controlnet_canny(
|
| 39 |
-
|
| 40 |
stable_model_path: str,
|
| 41 |
controlnet_model_path: str,
|
| 42 |
prompt: str,
|
|
@@ -45,15 +45,11 @@ def stable_diffusion_inpiant_controlnet_canny(
|
|
| 45 |
guidance_scale: int,
|
| 46 |
num_inference_steps: int,
|
| 47 |
):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
mask_image = pil_image["mask"].convert("RGB").resize((512, 512))
|
| 51 |
-
|
| 52 |
-
# normal_image = load_img(normal_image_path)
|
| 53 |
-
# mask_image = load_img(mask_image_path)
|
| 54 |
|
| 55 |
controlnet, control_image = controlnet_canny(
|
| 56 |
-
|
| 57 |
controlnet_model_path=controlnet_model_path,
|
| 58 |
)
|
| 59 |
|
|
@@ -91,7 +87,7 @@ def stable_diffusion_inpiant_controlnet_canny_app():
|
|
| 91 |
source="upload",
|
| 92 |
tool="sketch",
|
| 93 |
elem_id="image_upload",
|
| 94 |
-
type="
|
| 95 |
label="Upload",
|
| 96 |
)
|
| 97 |
|
|
|
|
| 4 |
from diffusers import UniPCMultistepScheduler
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
from diffusion_webui.controlnet_inpaint.canny_inpaint import controlnet_canny
|
| 8 |
from diffusion_webui.controlnet_inpaint.pipeline_stable_diffusion_controlnet_inpaint import (
|
| 9 |
StableDiffusionControlNetInpaintPipeline,
|
| 10 |
)
|
| 11 |
|
| 12 |
stable_inpaint_model_list = [
|
|
|
|
| 13 |
"runwayml/stable-diffusion-inpainting",
|
| 14 |
+
"stabilityai/stable-diffusion-2-inpainting",
|
| 15 |
]
|
| 16 |
|
| 17 |
controlnet_model_list = [
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def stable_diffusion_inpiant_controlnet_canny(
|
| 39 |
+
dict_image: str,
|
| 40 |
stable_model_path: str,
|
| 41 |
controlnet_model_path: str,
|
| 42 |
prompt: str,
|
|
|
|
| 45 |
guidance_scale: int,
|
| 46 |
num_inference_steps: int,
|
| 47 |
):
|
| 48 |
+
normal_image = dict_image["image"].convert("RGB").resize((512, 512))
|
| 49 |
+
mask_image = dict_image["mask"].convert("RGB").resize((512, 512))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
controlnet, control_image = controlnet_canny(
|
| 52 |
+
dict_image=dict_image,
|
| 53 |
controlnet_model_path=controlnet_model_path,
|
| 54 |
)
|
| 55 |
|
|
|
|
| 87 |
source="upload",
|
| 88 |
tool="sketch",
|
| 89 |
elem_id="image_upload",
|
| 90 |
+
type="pil",
|
| 91 |
label="Upload",
|
| 92 |
)
|
| 93 |
|