forked from mac-mvak/StyleDiffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyletransfer.py
272 lines (227 loc) · 12.6 KB
/
styletransfer.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
import time
from glob import glob
from tqdm import tqdm
import os
import numpy as np
import cv2
from PIL import Image
import torch
from torch import nn
import torch.nn.functional as F
import torchvision.utils as tvu
from models.ddpm.diffusion import DDPM
from models.improved_ddpm.script_util import i_DDPM
from utils.text_dic import SRC_TRG_TXT_DIC
from utils.diffusion_utils import get_beta_schedule, denoising_step
from losses import id_loss
from collections import defaultdict
from losses.clip_loss import CLIPLoss
from datasets.data_utils import get_dataset, get_dataloader
from configs.paths_config import DATASET_PATHS, MODEL_PATHS, HYBRID_MODEL_PATHS, HYBRID_CONFIG
from datasets.GENERIC_dataset import GENERIC_dataset
from datasets.imagenet_dic import IMAGENET_DIC
from utils.align_utils import run_alignment
class StyleTransfer(object):
def __init__(self, args, config, device=None):
self.args = args
self.config = config
if device is None:
device = torch.device(
"cuda") if torch.cuda.is_available() else torch.device("cpu")
self.device = device
self.model_var_type = config.model.var_type
betas = get_beta_schedule(
beta_start=config.diffusion.beta_start,
beta_end=config.diffusion.beta_end,
num_diffusion_timesteps=config.diffusion.num_diffusion_timesteps
)
self.betas = torch.from_numpy(betas).float().to(self.device)
self.num_timesteps = betas.shape[0]
alphas = 1.0 - betas
alphas_cumprod = np.cumprod(alphas, axis=0)
alphas_cumprod_prev = np.append(1.0, alphas_cumprod[:-1])
posterior_variance = betas * \
(1.0 - alphas_cumprod_prev) / (1.0 - alphas_cumprod)
if self.model_var_type == "fixedlarge":
self.logvar = np.log(np.append(posterior_variance[1], betas[1:]))
elif self.model_var_type == 'fixedsmall':
self.logvar = np.log(np.maximum(posterior_variance, 1e-20))
def transfer_style(self):
#print(f' {self.src_txts}')
#print(f'-> {self.trg_txts}')
model = i_DDPM(self.config.data.dataset, self.args.image_size)
if self.args.image_size == 256:
model_path = 'pretrained/256x256_diffusion_uncond.pt'
elif self.args.image_size == 512:
model_path = 'pretrained/512x512_diffusion.pt'
init_ckpt = torch.load(model_path)
u = model.load_state_dict(init_ckpt)
model.to(self.device)
model = model
# ----------- Precompute Latents -----------#
print("Prepare identity latent")
seq_inv = np.linspace(0, 1, self.args.n_inv_step) * self.args.t_0_transfer
seq_inv = [int(s) for s in list(seq_inv)]
seq_inv_next = [-1] + list(seq_inv[:-1])
n = self.args.bs_train
img_lat_pairs_dic = {}
lats_dict = defaultdict(list)
for mode in ['train', 'test', 'style']:
img_lat_pairs = []
if mode != 'style':
lats = torch.load('precomputed/' #
f'{self.config.data.category}_{mode}_t{self.args.t_0_remove}_size{self.args.image_size}_nim{self.args.n_precomp_img}_ninv{self.args.n_inv_step}_{self.args.removal_mode}_pairs.pth')
else:
image_name = self.args.style_image.split('.')[0]
lats = torch.load('precomputed/'f'{self.config.data.category}_style_{image_name}_t{self.args.t_0_remove}_size{self.args.image_size}_nim{self.args.n_precomp_img}_ninv{self.args.n_inv_step}_{self.args.removal_mode}_pairs.pth')
for step, xs in enumerate(lats):
if mode == 'style':
img = xs[2]
style_color = xs[0].clone().to(self.config.device)
style_gray = xs[2].clone().to(self.config.device)
else:
img = xs[1]
x0 = img.to(self.config.device)
x = x0.clone()
model.eval()
time_s = time.time()
with torch.no_grad():
with tqdm(total=len(seq_inv), desc=f"Inversion transfer process {mode} {step}") as progress_bar:
for it, (i, j) in enumerate(zip((seq_inv_next[1:]), (seq_inv[1:]))):
t = (torch.ones(n) * i).to(self.device)
t_prev = (torch.ones(n) * j).to(self.device)
x = denoising_step(x, t=t, t_next=t_prev, models=model,
logvars=self.logvar,
sampling_type='ddim',
b=self.betas,
eta=0,
learn_sigma=True)
progress_bar.update(1)
time_e = time.time()
print(f'{time_e - time_s} seconds')
x_lat = x.clone()
lats_dict[mode].append((x0, x_lat))
if step == self.args.n_precomp_img - 1:
break
if mode == 'train' and step == self.args.n_train_img - 1:
break
if mode == 'test' and step == self.args.n_test_img - 1:
break
# ----------- Optimizer and Scheduler -----------#
print(f"Setting optimizer with lr={self.args.lr_clip_finetune}")
optim_ft = torch.optim.Adam(model.parameters(), weight_decay=0, lr=self.args.lr_clip_finetune)
# optim_ft = torch.optim.SGD(model.parameters(), weight_decay=0, lr=self.args.lr_clip_finetune)#, momentum=0.9)
init_opt_ckpt = optim_ft.state_dict()
scheduler_ft = torch.optim.lr_scheduler.StepLR(optim_ft, step_size=1, gamma=self.args.sch_gamma)
init_sch_ckpt = scheduler_ft.state_dict()
# ----------- Loss -----------#
print("Loading losses")
clip_loss_func = CLIPLoss(
self.device,
lambda_direction=self.args.dir_loss,
lambda_l1=self.args.l1_loss_w,
clip_model=self.args.clip_model_name)
# ----------- Finetune Diffusion Models -----------#
print("Start finetuning")
print(f"Sampling type: {self.args.sample_type.upper()} with eta {self.args.eta}")
if self.args.n_train_step != 0:
seq_train = np.linspace(0, 1, self.args.n_train_step) * self.args.t_0_transfer
seq_train = [int(s) for s in list(seq_train)]
print('Uniform skip type')
else:
seq_train = list(range(self.args.t_0_transfer))
print('No skip')
seq_train_next = [-1] + list(seq_train[:-1])
seq_test = np.linspace(0, 1, self.args.n_test_step) * self.args.t_0_transfer
seq_test = [int(s) for s in list(seq_test)]
seq_test_next = [-1] + list(seq_test[:-1])
model.load_state_dict(init_ckpt)
optim_ft.load_state_dict(init_opt_ckpt)
scheduler_ft.load_state_dict(init_sch_ckpt)
# ----------- Train -----------#
for it_out in range(self.args.n_iter):
exp_id = os.path.split(self.args.exp)[-1]
save_name = f'checkpoint/{exp_id}_-{it_out}.pth'
if self.args.do_train:
for k in range(self.args.k_r):
with tqdm(total=len(seq_train), desc=f"Style reconstruction iteration_{k}") as progress_bar:
x = lats_dict['style'][0][1].clone()
for t_it, (i, j) in enumerate(zip(reversed(seq_train), reversed(seq_train_next))):
t = (torch.ones(n) * i).to(self.device)
t_next = (torch.ones(n) * j).to(self.device)
x, x0_t = denoising_step(x, t=t, t_next=t_next, models=model,
logvars=self.logvar,
sampling_type=self.args.sample_type,
b=self.betas,
eta=self.args.eta,
learn_sigma=True,
out_x0_t=True)
progress_bar.update(1)
x = x.detach().clone()
loss_style = self.args.style_loss_w * F.mse_loss(style_color, x0_t)
loss_style.backward()
optim_ft.step()
optim_ft.zero_grad()
for step, xs in enumerate(lats_dict['train']):
model.train()
time_in_start = time.time()
optim_ft.zero_grad()
x0, x_lat = xs
x = x_lat.clone().to(self.device)
x0 = x0.to(self.device)
with tqdm(total=len(seq_train), desc=f"CLIP iteration") as progress_bar:
for t_it, (i, j) in enumerate(zip(reversed(seq_train), reversed(seq_train_next))):
t = (torch.ones(n) * i).to(self.device)
t_next = (torch.ones(n) * j).to(self.device)
x, x0_t = denoising_step(x, t=t, t_next=t_next, models=model,
logvars=self.logvar,
sampling_type=self.args.sample_type,
b=self.betas,
eta=self.args.eta,
learn_sigma=True,
out_x0_t=True)
progress_bar.update(1)
x = x.detach().clone()
loss_clip = clip_loss_func(x0_t, x0, style_gray, style_color)
loss_clip.backward()
optim_ft.step()
optim_ft.zero_grad()
#print(f"CLIP {step}-{it_out}: loss_clip: {loss_clip:.3f}")
# break
if self.args.save_train_image:
tvu.save_image((x0_t + 1) * 0.5, os.path.join(self.args.image_folder,
f'train_{step}_2_clip_{it_out}_ngen{self.args.n_train_step}.png'))
time_in_end = time.time()
print(f"Training for 1 image takes {time_in_end - time_in_start:.4f}s")
if step == self.args.n_train_img - 1:
break
torch.save(model.state_dict(), save_name)
print(f'Model {save_name} is saved.')
scheduler_ft.step()
# ----------- Eval -----------#
if self.args.do_test:
if not self.args.do_train:
print(save_name)
model.module.load_state_dict(torch.load(save_name))
model.eval()
for step, xs in enumerate(lats_dict['test']):
with torch.no_grad():
x0, x_lat = xs
x = x_lat.clone().to(self.device)
x0 = x0.to(self.device)
with tqdm(total=len(seq_test), desc=f"Eval iteration") as progress_bar:
for i, j in zip(reversed(seq_test), reversed(seq_test_next)):
t = (torch.ones(n) * i).to(self.device)
t_next = (torch.ones(n) * j).to(self.device)
x = denoising_step(x, t=t, t_next=t_next, models=model,
logvars=self.logvar,
sampling_type=self.args.sample_type,
b=self.betas,
eta=self.args.eta,
learn_sigma=True)
progress_bar.update(1)
print(f"Eval {step}-{it_out}")
tvu.save_image((x + 1) * 0.5, os.path.join(self.args.image_folder,
f'test_{step}_2_clip_{it_out}_ngen{self.args.n_test_step}.png'))
if step == self.args.n_test_img - 1:
break