Repository navigation
Expand file tree
/
Copy pathsimple_script.py
More file actions
307 lines (242 loc) · 8.49 KB
/
Copy pathsimple_script.py
File metadata and controls
307 lines (242 loc) · 8.49 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Starting with a clean slate
from modal import Secret, Stub, Image, gpu, method, web_endpoint
ARTFUSION_GITHUB_PATH = "https://github.com/cadolphs/ArtFusion.git"
ARTFUSION_PATH = "/git/artfusion/"
MODEL_DIR = "/models"
EXAMPLE_STYLE_DIR = "/example_styles"
MODEL_NAME = "artfusion_r12_step=317673.ckpt"
BASE_MODEL = "lagerbaer/artfusion"
CKPT_PATH = f"{MODEL_DIR}/{MODEL_NAME}"
CFG_PATH = f"{ARTFUSION_PATH}/configs/kl16_content12.yaml"
DEVICE = "cuda"
stub = Stub("art-fusion")
from fastapi import Form
from pydantic import BaseModel
def download_model_to_folder():
from huggingface_hub import snapshot_download
import os
os.makedirs(MODEL_DIR, exist_ok=True)
snapshot_download(
BASE_MODEL,
local_dir=MODEL_DIR,
token=os.environ["HUGGINGFACE_TOKEN"],
)
def instantiate_model():
"""Useful to pre-load the required weights"""
import sys
sys.path.append(ARTFUSION_PATH)
from omegaconf import OmegaConf
from main import instantiate_from_config
config = OmegaConf.load(CFG_PATH)
config.model.params.ckpt_path = CKPT_PATH
config.model.params.first_stage_config.params.ckpt_path = None
model = instantiate_from_config(config.model)
image = (
Image.debian_slim()
.apt_install("python3-opencv")
.pip_install(
"torch",
"torchvision",
"pytorch-lightning",
"omegaconf",
"einops",
"transformers",
"imageio",
"imageio-ffmpeg",
"hf-transfer~=0.1",
)
.apt_install("git")
.run_commands(
f"cd / && mkdir -p {ARTFUSION_PATH} && cd {ARTFUSION_PATH} && git clone --depth 1 {ARTFUSION_GITHUB_PATH} .",
force_build=False,
)
.run_commands(f"cd / && mkdir -p {MODEL_DIR}")
.run_function(
download_model_to_folder, secret=Secret.from_name("my-huggingface-secret")
)
.run_function(instantiate_model)
.run_commands(f"cd / && mkdir -p {EXAMPLE_STYLE_DIR}")
.copy_local_file("scream.jpg", f"{EXAMPLE_STYLE_DIR}/scream.jpg")
)
def preprocess_image(image, size=(256, 256), max_size=256):
import numpy as np
import torch
from einops import rearrange
if not image.mode == "RGB":
image = image.convert("RGB")
im_max_size = max(image.size)
max_size = min(max_size, im_max_size)
image.thumbnail((max_size, max_size))
image = np.array(image).astype(np.uint8)
image = (image / 127.5 - 1.0).astype(np.float32)
image = rearrange(image, "h w c -> c h w")
return torch.from_numpy(image)[None, :].to(DEVICE)
def tensor_to_rgb(x):
import torch
return torch.clamp((x + 1.0) / 2.0, min=0.0, max=1.0)
def convert_samples(samples):
from einops import rearrange
from PIL import Image
import numpy as np
if isinstance(samples, (list, tuple)):
samples = torch.cat(samples, dim=0)
samples = rearrange(samples[0, :], "c h w -> h w c").cpu().numpy() * 255.0
samples = Image.fromarray(samples.astype(np.uint8))
return samples
@stub.cls(image=image, gpu=gpu.T4(), container_idle_timeout=180)
class StyleTransfer:
def __enter__(self):
from omegaconf import OmegaConf
from pytorch_lightning import seed_everything
import sys
sys.path.append(ARTFUSION_PATH)
from main import instantiate_from_config
seed_everything(42)
config = OmegaConf.load(CFG_PATH)
config.model.params.ckpt_path = CKPT_PATH
config.model.params.first_stage_config.params.ckpt_path = None
model = instantiate_from_config(config.model)
self.model = model.eval().to(DEVICE)
def get_content_style_features(
self, content_image, style_image, max_size=256, style_size=256
):
import torch
model = self.model
style_image = preprocess_image(style_image, max_size=style_size)
content_image = preprocess_image(content_image, max_size=max_size)
with torch.no_grad(), model.ema_scope("Plotting"):
vgg_features = model.vgg(model.vgg_scaling_layer(style_image))
c_style = model.get_style_features(vgg_features)
null_style = c_style.clone()
null_style[:] = model.null_style_vector.weight[0]
content_encoder_posterior = model.encode_first_stage(content_image)
content_encoder_posterior = model.get_first_stage_encoding(
content_encoder_posterior
)
c_content = model.get_content_features(content_encoder_posterior)
null_content = torch.zeros_like(c_content)
c = {"c1": c_content, "c2": c_style}
c_null_style = {"c1": c_content, "c2": null_style}
c_null_content = {"c1": null_content, "c2": c_style}
return c, c_null_style, c_null_content
def style_transfer(
self,
content_image,
style_image,
content_s,
style_s,
ddim_steps,
eta,
max_size=None,
style_size=None,
):
import torch
c, c_null_style, c_null_content = self.get_content_style_features(
content_image, style_image, max_size=max_size, style_size=style_size
)
with torch.no_grad(), self.model.ema_scope("Plotting"):
samples = self.model.sample_log(
cond=c,
batch_size=1,
x_T=torch.rand_like(c["c1"]),
ddim=True,
ddim_steps=ddim_steps,
eta=eta,
unconditional_guidance_scale=content_s,
unconditional_conditioning=c_null_content,
unconditional_guidance_scale_2=style_s,
unconditional_conditioning_2=c_null_style,
)[0]
x_samples = self.model.decode_first_stage(samples)
x_samples = tensor_to_rgb(x_samples)
return x_samples
@method()
def generate(
self,
content_bytes=None,
style_bytes=None,
content_strength=0.5,
style_strength=1.0,
max_size=1024,
style_size=256,
ddim_steps=10,
eta=0,
):
from PIL import Image
import io
content_image = Image.open(io.BytesIO(content_bytes))
style_image = Image.open(io.BytesIO(style_bytes))
x_samples = self.style_transfer(
content_image,
style_image,
content_s=content_strength,
style_s=style_strength,
max_size=max_size,
style_size=style_size,
ddim_steps=ddim_steps,
eta=eta,
)
x_samples = convert_samples(x_samples)
img_bytes = io.BytesIO()
x_samples.save(img_bytes, format="PNG")
return img_bytes.getvalue()
@stub.local_entrypoint()
def main(
content_file_name: str,
style_file_name: str,
content_strength: float = 0.5,
style_strength: float = 1.0,
max_size: int = 512,
style_size: int = 256,
eta: float = 0,
ddim_steps: int = 10,
):
import io
with open(content_file_name, "rb") as f:
content_bytes = io.BytesIO(f.read()).getvalue()
with open(style_file_name, "rb") as f:
style_bytes = io.BytesIO(f.read()).getvalue()
image_bytes = StyleTransfer().generate.remote(
content_bytes,
style_bytes,
content_strength=content_strength,
style_strength=style_strength,
max_size=max_size,
style_size=style_size,
eta=eta,
ddim_steps=ddim_steps,
)
output_path = "output.png"
with open(output_path, "wb") as f:
f.write(image_bytes)
class Base64Image(BaseModel):
image_base64: str
@stub.function(image=image, container_idle_timeout=180)
@web_endpoint(method="POST")
def screamify(image: Base64Image):
from io import BytesIO
import base64
import re
image_content = image.image_base64
if "," in image_content:
image_content = image_content.split(",")[1]
print("Image content [truncated]: ")
print(image_content[:200])
content_bytes = BytesIO(base64.b64decode(image_content)).getvalue()
with open(f"{EXAMPLE_STYLE_DIR}/scream.jpg", "rb") as f:
style_bytes = BytesIO(f.read()).getvalue()
print("Running style transfer")
image_bytes = StyleTransfer().generate.remote(
content_bytes,
style_bytes,
content_strength=1,
style_strength=3,
max_size=1024,
style_size=256,
eta=0,
ddim_steps=10,
)
print("Done")
img_str = base64.b64encode(image_bytes).decode("utf-8")
img_str = "data:image/png;base64," + img_str
return {"image_base64": img_str}