Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,192 +1,192 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from PIL import Image, ImageFilter, ImageOps,ImageEnhance
|
4 |
-
from scipy.ndimage import rank_filter, maximum_filter
|
5 |
-
import numpy as np
|
6 |
-
import pathlib
|
7 |
-
import glob
|
8 |
-
import os
|
9 |
-
from diffusers import StableDiffusionControlNetPipeline, DDIMScheduler, AutoencoderKL, ControlNetModel
|
10 |
-
from ip_adapter import IPAdapter
|
11 |
-
|
12 |
-
|
13 |
-
DESCRIPTION = """# [FilterPrompt](https://arxiv.org/abs/2404.13263): Guiding Imgae Transfer in Diffusion Models
|
14 |
-
<img id="teaser" alt="teaser" src="https://raw.githubusercontent.com/Meaoxixi/FilterPrompt/gh-pages/resources/teaser.png" />
|
15 |
-
"""
|
16 |
-
##################################################################################################################
|
17 |
-
# 0. Get Pre-Models' Path Ready
|
18 |
-
##################################################################################################################
|
19 |
-
|
20 |
-
|
21 |
-
image_encoder_path = "
|
22 |
-
ip_ckpt = "
|
23 |
-
controlnet_softEdge_model_path = "
|
24 |
-
controlnet_depth_model_path = "
|
25 |
-
device = "cuda:0"
|
26 |
-
##################################################################################################################
|
27 |
-
# 1. load pipeline
|
28 |
-
##################################################################################################################
|
29 |
-
torch.cuda.empty_cache()
|
30 |
-
## 1.1 noise_scheduler
|
31 |
-
noise_scheduler = DDIMScheduler(
|
32 |
-
num_train_timesteps=1000,
|
33 |
-
beta_start=0.00085,
|
34 |
-
beta_end=0.012,
|
35 |
-
beta_schedule="scaled_linear",
|
36 |
-
clip_sample=False,
|
37 |
-
set_alpha_to_one=False,
|
38 |
-
steps_offset=1,
|
39 |
-
)
|
40 |
-
# 1.2 vae
|
41 |
-
vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
|
42 |
-
# 1.3 ControlNet
|
43 |
-
## 1.3.1 load controlnet_softEdge
|
44 |
-
controlnet_softEdge = ControlNetModel.from_pretrained(controlnet_softEdge_model_path, torch_dtype=torch.float16)
|
45 |
-
## 1.3.2 load controlnet_depth
|
46 |
-
controlnet_depth = ControlNetModel.from_pretrained(controlnet_depth_model_path, torch_dtype=torch.float16)
|
47 |
-
# 1.4 load SD pipeline
|
48 |
-
pipe_softEdge = StableDiffusionControlNetPipeline.from_pretrained(
|
49 |
-
base_model_path,
|
50 |
-
controlnet=controlnet_softEdge,
|
51 |
-
torch_dtype=torch.float16,
|
52 |
-
scheduler=noise_scheduler,
|
53 |
-
vae=vae,
|
54 |
-
feature_extractor=None,
|
55 |
-
safety_checker=None
|
56 |
-
)
|
57 |
-
pipe_depth = StableDiffusionControlNetPipeline.from_pretrained(
|
58 |
-
base_model_path,
|
59 |
-
controlnet=controlnet_depth,
|
60 |
-
torch_dtype=torch.float16,
|
61 |
-
scheduler=noise_scheduler,
|
62 |
-
vae=vae,
|
63 |
-
feature_extractor=None,
|
64 |
-
safety_checker=None
|
65 |
-
)
|
66 |
-
print("1 Model loading completed !")
|
67 |
-
print("##################################################################")
|
68 |
-
def image_grid(imgs, rows, cols):
|
69 |
-
assert len(imgs) == rows * cols
|
70 |
-
w, h = imgs[0].size
|
71 |
-
grid = Image.new('RGB', size=(cols * w, rows * h))
|
72 |
-
for i, img in enumerate(imgs):
|
73 |
-
grid.paste(img, box=(i % cols * w, i // cols * h))
|
74 |
-
return grid
|
75 |
-
#########################################################################
|
76 |
-
## funcitions for task 1 : style transfer
|
77 |
-
#########################################################################
|
78 |
-
def gaussian_blur(image, blur_radius):
|
79 |
-
image = Image.open(image)
|
80 |
-
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
81 |
-
return blurred_image
|
82 |
-
|
83 |
-
def task1_StyleTransfer(photo, blur_radius, sketch):
|
84 |
-
photoImage = Image.open(photo)
|
85 |
-
blurPhoto = gaussian_blur(photo, blur_radius)
|
86 |
-
|
87 |
-
Control_factor = 1.2
|
88 |
-
IP_factor = 0.6
|
89 |
-
ip_model = IPAdapter(pipe_depth, image_encoder_path, ip_ckpt, device, Control_factor=Control_factor, IP_factor=IP_factor)
|
90 |
-
|
91 |
-
depth_image= Image.open(sketch)
|
92 |
-
img_array = np.array(depth_image)
|
93 |
-
gray_img_array = np.dot(img_array[..., :3], [0.2989, 0.5870, 0.1140])
|
94 |
-
# 反相
|
95 |
-
inverted_array = 255 - gray_img_array
|
96 |
-
gray_img_array = inverted_array.astype(np.uint8)
|
97 |
-
processed_image = Image.fromarray(gray_img_array)
|
98 |
-
contrast_factor = 2
|
99 |
-
enhancer = ImageEnhance.Contrast(processed_image)
|
100 |
-
processed_image = enhancer.enhance(contrast_factor)
|
101 |
-
|
102 |
-
images = ip_model.generate(pil_image=photoImage, image=processed_image, num_samples=1, num_inference_steps=30, seed=52)
|
103 |
-
original = image_grid(images, 1, 1)
|
104 |
-
images = ip_model.generate(pil_image=blurPhoto, image=processed_image, num_samples=1, num_inference_steps=30, seed=52)
|
105 |
-
result= image_grid(images, 1, 1)
|
106 |
-
|
107 |
-
return original,result
|
108 |
-
|
109 |
-
def task1_test(photo, blur_radius, sketch):
|
110 |
-
original = photo
|
111 |
-
print(type(original))
|
112 |
-
# <class 'str'>
|
113 |
-
result = sketch
|
114 |
-
return original, result
|
115 |
-
#########################################################################
|
116 |
-
## funcitions for task 2 : color transfer
|
117 |
-
#########################################################################
|
118 |
-
# todo
|
119 |
-
|
120 |
-
#############################################
|
121 |
-
# Demo
|
122 |
-
#############################################
|
123 |
-
theme = gr.themes.Monochrome(primary_hue="blue").set(
|
124 |
-
loader_color="#FF0000",
|
125 |
-
slider_color="#FF0000",
|
126 |
-
)
|
127 |
-
|
128 |
-
with gr.Blocks(theme=theme) as demo:
|
129 |
-
gr.Markdown(DESCRIPTION)
|
130 |
-
|
131 |
-
# 1. 第一个任务Style Transfer的界面代码(青铜器拓本转照片)
|
132 |
-
with gr.Group():
|
133 |
-
## 1.1 任务描述
|
134 |
-
gr.Markdown(
|
135 |
-
"""
|
136 |
-
## Case 1: Style transfer
|
137 |
-
- In this task, our main goal is to achieve the style transfer from sketch to photo.
|
138 |
-
- In the original generation result, the surface of the object has redundant pattern representation from the style image.
|
139 |
-
- Next, you can control the Gaussian kernel size of GaussianBlur to weaken the expression of redundant pattern features in the generated results.
|
140 |
-
""")
|
141 |
-
## 1.2 输入输出控件布局
|
142 |
-
#### 用Column()控制空间在列上的排列关系
|
143 |
-
with gr.Row():
|
144 |
-
# 第一列
|
145 |
-
with gr.Column():
|
146 |
-
with gr.Row():
|
147 |
-
### 1.2.1.1 输入真实照片
|
148 |
-
photo = gr.Image(label="Input photo", type="filepath")
|
149 |
-
print(photo)
|
150 |
-
print(type(photo))
|
151 |
-
with gr.Row():
|
152 |
-
### 1.2.1.2 高斯核控件
|
153 |
-
gaussianKernel = gr.Slider(minimum=0, maximum=8, step=1, value=2, label="Gaussian Blur Radius")
|
154 |
-
# 第二列
|
155 |
-
with gr.Column():
|
156 |
-
with gr.Row():
|
157 |
-
# 1.2.2.1 输入素描图
|
158 |
-
sketch = gr.Image(label="Input sketch", type="filepath")
|
159 |
-
#print(sketch)
|
160 |
-
with gr.Row():
|
161 |
-
# 1.2.2.2 按钮:开始生成图片
|
162 |
-
task1Button = gr.Button("Preprocess")
|
163 |
-
# 第三列:显示初始的生成图
|
164 |
-
with gr.Column():
|
165 |
-
with gr.Row():
|
166 |
-
original_result_task1 = gr.Image(label="Original generation result", interactive=False, type="pil")
|
167 |
-
# 第四列:显示使用高斯滤波之后的生成图
|
168 |
-
with gr.Column():
|
169 |
-
result_image_1 = gr.Image(label="Generate results after using GaussianBlur",type="pil")
|
170 |
-
|
171 |
-
## 1.3 示例图展示
|
172 |
-
with gr.Row():
|
173 |
-
paths = sorted(pathlib.Path("images/inputExample").glob("*.jpg"))
|
174 |
-
gr.Examples(examples=[[path.as_posix()] for path in paths], inputs = sketch)
|
175 |
-
with gr.Row():
|
176 |
-
gr.Image(value="images/1_gaussian_filter.png", label=" Task example Image", type="filepath")
|
177 |
-
|
178 |
-
# 1. task 1 - style transfer 的界面代码写完了,现在写控件之间交互的逻辑
|
179 |
-
task1Button.click(
|
180 |
-
fn=task1_StyleTransfer,
|
181 |
-
#fn=task1_test,
|
182 |
-
inputs=[photo, gaussianKernel, sketch],
|
183 |
-
outputs=[original_result_task1, result_image_1],
|
184 |
-
)
|
185 |
-
|
186 |
-
##################################################################################################################
|
187 |
-
# 2. run Demo on gradio
|
188 |
-
##################################################################################################################
|
189 |
-
|
190 |
-
if __name__ == "__main__":
|
191 |
-
demo.queue(max_size=5).launch()
|
192 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image, ImageFilter, ImageOps,ImageEnhance
|
4 |
+
from scipy.ndimage import rank_filter, maximum_filter
|
5 |
+
import numpy as np
|
6 |
+
import pathlib
|
7 |
+
import glob
|
8 |
+
import os
|
9 |
+
from diffusers import StableDiffusionControlNetPipeline, DDIMScheduler, AutoencoderKL, ControlNetModel
|
10 |
+
from ip_adapter import IPAdapter
|
11 |
+
|
12 |
+
|
13 |
+
DESCRIPTION = """# [FilterPrompt](https://arxiv.org/abs/2404.13263): Guiding Imgae Transfer in Diffusion Models
|
14 |
+
<img id="teaser" alt="teaser" src="https://raw.githubusercontent.com/Meaoxixi/FilterPrompt/gh-pages/resources/teaser.png" />
|
15 |
+
"""
|
16 |
+
##################################################################################################################
|
17 |
+
# 0. Get Pre-Models' Path Ready
|
18 |
+
##################################################################################################################
|
19 |
+
vae_model_path = "https://huggingface.co/stabilityai/sd-vae-ft-mse/tree/main"
|
20 |
+
base_model_path = "https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main"
|
21 |
+
image_encoder_path = "https://huggingface.co/h94/IP-Adapter/tree/main/models/image_encoder"
|
22 |
+
ip_ckpt = "https://huggingface.co/h94/IP-Adapter/tree/main/models/ip-adapter_sd15.bin"
|
23 |
+
controlnet_softEdge_model_path = "https://huggingface.co/lllyasviel/control_v11p_sd15_softedge/tree/main"
|
24 |
+
controlnet_depth_model_path = "https://huggingface.co/lllyasviel/control_v11f1p_sd15_depth/tree/main"
|
25 |
+
# device = "cuda:0"
|
26 |
+
##################################################################################################################
|
27 |
+
# 1. load pipeline
|
28 |
+
##################################################################################################################
|
29 |
+
torch.cuda.empty_cache()
|
30 |
+
## 1.1 noise_scheduler
|
31 |
+
noise_scheduler = DDIMScheduler(
|
32 |
+
num_train_timesteps=1000,
|
33 |
+
beta_start=0.00085,
|
34 |
+
beta_end=0.012,
|
35 |
+
beta_schedule="scaled_linear",
|
36 |
+
clip_sample=False,
|
37 |
+
set_alpha_to_one=False,
|
38 |
+
steps_offset=1,
|
39 |
+
)
|
40 |
+
# 1.2 vae
|
41 |
+
vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
|
42 |
+
# 1.3 ControlNet
|
43 |
+
## 1.3.1 load controlnet_softEdge
|
44 |
+
controlnet_softEdge = ControlNetModel.from_pretrained(controlnet_softEdge_model_path, torch_dtype=torch.float16)
|
45 |
+
## 1.3.2 load controlnet_depth
|
46 |
+
controlnet_depth = ControlNetModel.from_pretrained(controlnet_depth_model_path, torch_dtype=torch.float16)
|
47 |
+
# 1.4 load SD pipeline
|
48 |
+
pipe_softEdge = StableDiffusionControlNetPipeline.from_pretrained(
|
49 |
+
base_model_path,
|
50 |
+
controlnet=controlnet_softEdge,
|
51 |
+
torch_dtype=torch.float16,
|
52 |
+
scheduler=noise_scheduler,
|
53 |
+
vae=vae,
|
54 |
+
feature_extractor=None,
|
55 |
+
safety_checker=None
|
56 |
+
)
|
57 |
+
pipe_depth = StableDiffusionControlNetPipeline.from_pretrained(
|
58 |
+
base_model_path,
|
59 |
+
controlnet=controlnet_depth,
|
60 |
+
torch_dtype=torch.float16,
|
61 |
+
scheduler=noise_scheduler,
|
62 |
+
vae=vae,
|
63 |
+
feature_extractor=None,
|
64 |
+
safety_checker=None
|
65 |
+
)
|
66 |
+
print("1 Model loading completed !")
|
67 |
+
print("##################################################################")
|
68 |
+
def image_grid(imgs, rows, cols):
|
69 |
+
assert len(imgs) == rows * cols
|
70 |
+
w, h = imgs[0].size
|
71 |
+
grid = Image.new('RGB', size=(cols * w, rows * h))
|
72 |
+
for i, img in enumerate(imgs):
|
73 |
+
grid.paste(img, box=(i % cols * w, i // cols * h))
|
74 |
+
return grid
|
75 |
+
#########################################################################
|
76 |
+
## funcitions for task 1 : style transfer
|
77 |
+
#########################################################################
|
78 |
+
def gaussian_blur(image, blur_radius):
|
79 |
+
image = Image.open(image)
|
80 |
+
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
81 |
+
return blurred_image
|
82 |
+
|
83 |
+
def task1_StyleTransfer(photo, blur_radius, sketch):
|
84 |
+
photoImage = Image.open(photo)
|
85 |
+
blurPhoto = gaussian_blur(photo, blur_radius)
|
86 |
+
|
87 |
+
Control_factor = 1.2
|
88 |
+
IP_factor = 0.6
|
89 |
+
ip_model = IPAdapter(pipe_depth, image_encoder_path, ip_ckpt, device, Control_factor=Control_factor, IP_factor=IP_factor)
|
90 |
+
|
91 |
+
depth_image= Image.open(sketch)
|
92 |
+
img_array = np.array(depth_image)
|
93 |
+
gray_img_array = np.dot(img_array[..., :3], [0.2989, 0.5870, 0.1140])
|
94 |
+
# 反相
|
95 |
+
inverted_array = 255 - gray_img_array
|
96 |
+
gray_img_array = inverted_array.astype(np.uint8)
|
97 |
+
processed_image = Image.fromarray(gray_img_array)
|
98 |
+
contrast_factor = 2
|
99 |
+
enhancer = ImageEnhance.Contrast(processed_image)
|
100 |
+
processed_image = enhancer.enhance(contrast_factor)
|
101 |
+
|
102 |
+
images = ip_model.generate(pil_image=photoImage, image=processed_image, num_samples=1, num_inference_steps=30, seed=52)
|
103 |
+
original = image_grid(images, 1, 1)
|
104 |
+
images = ip_model.generate(pil_image=blurPhoto, image=processed_image, num_samples=1, num_inference_steps=30, seed=52)
|
105 |
+
result= image_grid(images, 1, 1)
|
106 |
+
|
107 |
+
return original,result
|
108 |
+
|
109 |
+
def task1_test(photo, blur_radius, sketch):
|
110 |
+
original = photo
|
111 |
+
print(type(original))
|
112 |
+
# <class 'str'>
|
113 |
+
result = sketch
|
114 |
+
return original, result
|
115 |
+
#########################################################################
|
116 |
+
## funcitions for task 2 : color transfer
|
117 |
+
#########################################################################
|
118 |
+
# todo
|
119 |
+
|
120 |
+
#############################################
|
121 |
+
# Demo
|
122 |
+
#############################################
|
123 |
+
theme = gr.themes.Monochrome(primary_hue="blue").set(
|
124 |
+
loader_color="#FF0000",
|
125 |
+
slider_color="#FF0000",
|
126 |
+
)
|
127 |
+
|
128 |
+
with gr.Blocks(theme=theme) as demo:
|
129 |
+
gr.Markdown(DESCRIPTION)
|
130 |
+
|
131 |
+
# 1. 第一个任务Style Transfer的界面代码(青铜器拓本转照片)
|
132 |
+
with gr.Group():
|
133 |
+
## 1.1 任务描述
|
134 |
+
gr.Markdown(
|
135 |
+
"""
|
136 |
+
## Case 1: Style transfer
|
137 |
+
- In this task, our main goal is to achieve the style transfer from sketch to photo.
|
138 |
+
- In the original generation result, the surface of the object has redundant pattern representation from the style image.
|
139 |
+
- Next, you can control the Gaussian kernel size of GaussianBlur to weaken the expression of redundant pattern features in the generated results.
|
140 |
+
""")
|
141 |
+
## 1.2 输入输出控件布局
|
142 |
+
#### 用Column()控制空间在列上的排列关系
|
143 |
+
with gr.Row():
|
144 |
+
# 第一列
|
145 |
+
with gr.Column():
|
146 |
+
with gr.Row():
|
147 |
+
### 1.2.1.1 输入真实照片
|
148 |
+
photo = gr.Image(label="Input photo", type="filepath")
|
149 |
+
print(photo)
|
150 |
+
print(type(photo))
|
151 |
+
with gr.Row():
|
152 |
+
### 1.2.1.2 高斯核控件
|
153 |
+
gaussianKernel = gr.Slider(minimum=0, maximum=8, step=1, value=2, label="Gaussian Blur Radius")
|
154 |
+
# 第二列
|
155 |
+
with gr.Column():
|
156 |
+
with gr.Row():
|
157 |
+
# 1.2.2.1 输入素描图
|
158 |
+
sketch = gr.Image(label="Input sketch", type="filepath")
|
159 |
+
#print(sketch)
|
160 |
+
with gr.Row():
|
161 |
+
# 1.2.2.2 按钮:开始生成图片
|
162 |
+
task1Button = gr.Button("Preprocess")
|
163 |
+
# 第三列:显示初始的生成图
|
164 |
+
with gr.Column():
|
165 |
+
with gr.Row():
|
166 |
+
original_result_task1 = gr.Image(label="Original generation result", interactive=False, type="pil")
|
167 |
+
# 第四列:显示使用高斯滤波之后的生成图
|
168 |
+
with gr.Column():
|
169 |
+
result_image_1 = gr.Image(label="Generate results after using GaussianBlur",type="pil")
|
170 |
+
|
171 |
+
## 1.3 示例图展示
|
172 |
+
with gr.Row():
|
173 |
+
paths = sorted(pathlib.Path("images/inputExample").glob("*.jpg"))
|
174 |
+
gr.Examples(examples=[[path.as_posix()] for path in paths], inputs = sketch)
|
175 |
+
with gr.Row():
|
176 |
+
gr.Image(value="images/1_gaussian_filter.png", label=" Task example Image", type="filepath")
|
177 |
+
|
178 |
+
# 1. task 1 - style transfer 的界面代码写完了,现在写控件之间交互的逻辑
|
179 |
+
task1Button.click(
|
180 |
+
fn=task1_StyleTransfer,
|
181 |
+
#fn=task1_test,
|
182 |
+
inputs=[photo, gaussianKernel, sketch],
|
183 |
+
outputs=[original_result_task1, result_image_1],
|
184 |
+
)
|
185 |
+
|
186 |
+
##################################################################################################################
|
187 |
+
# 2. run Demo on gradio
|
188 |
+
##################################################################################################################
|
189 |
+
|
190 |
+
if __name__ == "__main__":
|
191 |
+
demo.queue(max_size=5).launch()
|
192 |
+
|