-
Notifications
You must be signed in to change notification settings - Fork 149
/
predict.py
280 lines (244 loc) · 10.2 KB
/
predict.py
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
# Prediction interface for Cog ⚙️
# https://github.com/replicate/cog/blob/main/docs/python.md
import os
import PIL
import numpy as np
import copy
import torch
from omegaconf import OmegaConf
from PIL import Image
from tqdm import trange
from itertools import islice
from einops import rearrange, repeat
from torch import autocast
from pytorch_lightning import seed_everything
import torch.nn.functional as F
from ldm.util import instantiate_from_config
from scripts.wavelet_color_fix import (
wavelet_reconstruction,
adaptive_instance_normalization,
)
from cog import BasePredictor, Input, Path
class Predictor(BasePredictor):
def setup(self) -> None:
"""Load the model into memory to make running multiple predictions efficient"""
config = OmegaConf.load("configs/stableSRNew/v2-finetune_text_T_512.yaml")
self.model = load_model_from_config(config, "stablesr_000117.ckpt")
device = torch.device("cuda")
self.model.configs = config
self.model = self.model.to(device)
vqgan_config = OmegaConf.load(
"configs/autoencoder/autoencoder_kl_64x64x4_resi.yaml"
)
self.vq_model = load_model_from_config(vqgan_config, "vqgan_cfw_00011.ckpt")
self.vq_model = self.vq_model.to(device)
def predict(
self,
input_image: Path = Input(description="Input image"),
ddpm_steps: int = Input(
description="Number of DDPM steps for sampling", default=200
),
fidelity_weight: float = Input(
description="Balance the quality (lower number) and fidelity (higher number)",
default=0.5,
),
upscale: float = Input(
description="The upscale for super-resolution, 4x SR by default",
default=4.0,
),
tile_overlap: int = Input(
description="The overlap between tiles, betwwen 0 to 64",
ge=0,
le=64,
default=32,
),
colorfix_type: str = Input(
choices=["adain", "wavelet", "none"], default="adain"
),
seed: int = Input(
description="Random seed. Leave blank to randomize the seed", default=None
),
) -> Path:
"""Run a single prediction on the model"""
if seed is None:
seed = int.from_bytes(os.urandom(2), "big")
print(f"Using seed: {seed}")
self.vq_model.decoder.fusion_w = fidelity_weight
seed_everything(seed)
n_samples = 1
device = torch.device("cuda")
cur_image = load_img(str(input_image)).to(device)
cur_image = F.interpolate(
cur_image,
size=(int(cur_image.size(-2) * upscale), int(cur_image.size(-1) * upscale)),
mode="bicubic",
)
self.model.register_schedule(
given_betas=None,
beta_schedule="linear",
timesteps=1000,
linear_start=0.00085,
linear_end=0.0120,
cosine_s=8e-3,
)
self.model.num_timesteps = 1000
sqrt_alphas_cumprod = copy.deepcopy(self.model.sqrt_alphas_cumprod)
sqrt_one_minus_alphas_cumprod = copy.deepcopy(
self.model.sqrt_one_minus_alphas_cumprod
)
use_timesteps = set(space_timesteps(1000, [ddpm_steps]))
last_alpha_cumprod = 1.0
new_betas = []
timestep_map = []
for i, alpha_cumprod in enumerate(self.model.alphas_cumprod):
if i in use_timesteps:
new_betas.append(1 - alpha_cumprod / last_alpha_cumprod)
last_alpha_cumprod = alpha_cumprod
timestep_map.append(i)
new_betas = [beta.data.cpu().numpy() for beta in new_betas]
self.model.register_schedule(
given_betas=np.array(new_betas), timesteps=len(new_betas)
)
self.model.num_timesteps = 1000
self.model.ori_timesteps = list(use_timesteps)
self.model.ori_timesteps.sort()
self.model = self.model.to(device)
precision_scope = autocast
input_size = 512
output = "/tmp/out.png"
with torch.no_grad():
with precision_scope("cuda"):
with self.model.ema_scope():
init_image = cur_image
init_image = init_image.clamp(-1.0, 1.0)
ori_size = None
print(init_image.size())
if (
init_image.size(-1) < input_size
or init_image.size(-2) < input_size
):
ori_size = init_image.size()
new_h = max(ori_size[-2], input_size)
new_w = max(ori_size[-1], input_size)
init_template = torch.zeros(
1, init_image.size(1), new_h, new_w
).to(init_image.device)
init_template[:, :, : ori_size[-2], : ori_size[-1]] = init_image
else:
init_template = init_image
init_latent = self.model.get_first_stage_encoding(
self.model.encode_first_stage(init_template)
) # move to latent space
text_init = [""] * n_samples
semantic_c = self.model.cond_stage_model(text_init)
noise = torch.randn_like(init_latent)
# If you would like to start from the intermediate steps, you can add noise to LR to the specific steps.
t = repeat(torch.tensor([999]), "1 -> b", b=init_image.size(0))
t = t.to(device).long()
x_T = self.model.q_sample_respace(
x_start=init_latent,
t=t,
sqrt_alphas_cumprod=sqrt_alphas_cumprod,
sqrt_one_minus_alphas_cumprod=sqrt_one_minus_alphas_cumprod,
noise=noise,
)
samples, _ = self.model.sample_canvas(
cond=semantic_c,
struct_cond=init_latent,
batch_size=init_image.size(0),
timesteps=ddpm_steps,
time_replace=ddpm_steps,
x_T=x_T,
return_intermediates=True,
tile_size=int(input_size / 8),
tile_overlap=tile_overlap,
batch_size_sample=n_samples,
)
_, enc_fea_lq = self.vq_model.encode(init_template)
x_samples = self.vq_model.decode(
samples * 1.0 / self.model.scale_factor, enc_fea_lq
)
if ori_size is not None:
x_samples = x_samples[:, :, : ori_size[-2], : ori_size[-1]]
if colorfix_type == "adain":
x_samples = adaptive_instance_normalization(
x_samples, init_image
)
elif colorfix_type == "wavelet":
x_samples = wavelet_reconstruction(x_samples, init_image)
x_samples = torch.clamp((x_samples + 1.0) / 2.0, min=0.0, max=1.0)
for i in range(init_image.size(0)):
x_sample = 255.0 * rearrange(
x_samples[i].cpu().numpy(), "c h w -> h w c"
)
Image.fromarray(x_sample.astype(np.uint8)).save(output)
return Path(output)
def load_model_from_config(config, ckpt, verbose=False):
print(f"Loading model from {ckpt}")
pl_sd = torch.load(ckpt, map_location="cpu")
if "global_step" in pl_sd:
print(f"Global Step: {pl_sd['global_step']}")
sd = pl_sd["state_dict"]
model = instantiate_from_config(config.model)
m, u = model.load_state_dict(sd, strict=False)
if len(m) > 0 and verbose:
print("missing keys:")
print(m)
if len(u) > 0 and verbose:
print("unexpected keys:")
print(u)
model.cuda()
model.eval()
return model
def read_image(im_path):
im = np.array(Image.open(im_path).convert("RGB"))
im = im.astype(np.float32) / 255.0
im = im[None].transpose(0, 3, 1, 2)
im = (torch.from_numpy(im) - 0.5) / 0.5
return im.cuda()
def space_timesteps(num_timesteps, section_counts):
if isinstance(section_counts, str):
if section_counts.startswith("ddim"):
desired_count = int(section_counts[len("ddim") :])
for i in range(1, num_timesteps):
if len(range(0, num_timesteps, i)) == desired_count:
return set(range(0, num_timesteps, i))
raise ValueError(
f"cannot create exactly {num_timesteps} steps with an integer stride"
)
section_counts = [int(x) for x in section_counts.split(",")] # [250,]
size_per = num_timesteps // len(section_counts)
extra = num_timesteps % len(section_counts)
start_idx = 0
all_steps = []
for i, section_count in enumerate(section_counts):
size = size_per + (1 if i < extra else 0)
if size < section_count:
raise ValueError(
f"cannot divide section of {size} steps into {section_count}"
)
if section_count <= 1:
frac_stride = 1
else:
frac_stride = (size - 1) / (section_count - 1)
cur_idx = 0.0
taken_steps = []
for _ in range(section_count):
taken_steps.append(start_idx + round(cur_idx))
cur_idx += frac_stride
all_steps += taken_steps
start_idx += size
return set(all_steps)
def chunk(it, size):
it = iter(it)
return iter(lambda: tuple(islice(it, size)), ())
def load_img(path):
image = Image.open(path).convert("RGB")
w, h = image.size
print(f"loaded input image of size ({w}, {h}) from {path}")
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
image = image.resize((w, h), resample=PIL.Image.LANCZOS)
image = np.array(image).astype(np.float32) / 255.0
image = image[None].transpose(0, 3, 1, 2)
image = torch.from_numpy(image)
return 2.0 * image - 1.0