-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffusion.py
More file actions
607 lines (517 loc) · 23.5 KB
/
Copy pathdiffusion.py
File metadata and controls
607 lines (517 loc) · 23.5 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import os
import math
import torch
import logging
from torch import nn
from inspect import isfunction
import functools
from functools import partial
import numpy as np
from tqdm import tqdm
import copy
from collections import OrderedDict
import torch.nn.functional as F
from torch.nn import init
from unet import UNet
logger = logging.getLogger('base')
def weights_init_normal(m, std=0.02):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
init.normal_(m.weight.data, 0.0, std)
if m.bias is not None:
m.bias.data.zero_()
elif classname.find('Linear') != -1:
init.normal_(m.weight.data, 0.0, std)
if m.bias is not None:
m.bias.data.zero_()
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, std) # BN also uses norm
init.constant_(m.bias.data, 0.0)
def weights_init_kaiming(m, scale=1):
classname = m.__class__.__name__
if classname.find('Conv2d') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
m.weight.data *= scale
if m.bias is not None:
m.bias.data.zero_()
elif classname.find('Linear') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
m.weight.data *= scale
if m.bias is not None:
m.bias.data.zero_()
elif classname.find('BatchNorm2d') != -1:
init.constant_(m.weight.data, 1.0)
init.constant_(m.bias.data, 0.0)
def weights_init_orthogonal(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
init.orthogonal_(m.weight.data, gain=1)
if m.bias is not None:
m.bias.data.zero_()
elif classname.find('Linear') != -1:
init.orthogonal_(m.weight.data, gain=1)
if m.bias is not None:
m.bias.data.zero_()
elif classname.find('BatchNorm2d') != -1:
init.constant_(m.weight.data, 1.0)
init.constant_(m.bias.data, 0.0)
def init_weights(net, init_type='kaiming', scale=1, std=0.02):
# scale for 'kaiming', std for 'normal'.
logger.info('Initialization method [{:s}]'.format(init_type))
if init_type == 'normal':
weights_init_normal_ = functools.partial(weights_init_normal, std=std)
net.apply(weights_init_normal_)
elif init_type == 'kaiming':
weights_init_kaiming_ = functools.partial(
weights_init_kaiming, scale=scale)
net.apply(weights_init_kaiming_)
elif init_type == 'orthogonal':
net.apply(weights_init_orthogonal)
else:
raise NotImplementedError(
'initialization method [{:s}] not implemented'.format(init_type))
def load_pretrained_model(net, pretrained_path, strict=True):
logger.info('Loading pretrained model [{:s}] ...'.format(pretrained_path))
pretrained_dict = torch.load(pretrained_path)
if isinstance(net, nn.DataParallel):
net = net.module
if hasattr(pretrained_dict, '_metadata'):
del pretrained_dict._metadata
net.load_state_dict(pretrained_dict, strict=strict)
return net
def define_G(opt, loss_type='l1'):
model_opt = opt['model']
if model_opt['diffusion']['loss_type'] != None and model_opt['diffusion']['loss_type'] != '':
loss_type = model_opt['diffusion']['loss_type']
if ('norm_groups' not in model_opt['unet']) or model_opt['unet']['norm_groups'] is None:
model_opt['unet']['norm_groups']=32
model = UNet(
in_channel=model_opt['unet']['in_channel'],
out_channel=model_opt['unet']['out_channel'],
norm_groups=model_opt['unet']['norm_groups'],
inner_channel=model_opt['unet']['inner_channel'],
channel_mults=model_opt['unet']['channel_multiplier'],
attn_res=model_opt['unet']['attn_res'],
res_blocks=model_opt['unet']['res_blocks'],
dropout=model_opt['unet']['dropout'],
image_size=model_opt['diffusion']['image_size']
)
netG = GaussianDiffusion(
model,
image_size=model_opt['diffusion']['image_size'],
channels=model_opt['diffusion']['channels'],
loss_type= loss_type, # L1 or L2
conditional=model_opt['diffusion']['conditional'],
schedule_opt=model_opt['beta_schedule']['train']
)
if opt['gpu_ids'] and opt['distributed']:
assert torch.cuda.is_available()
netG = nn.DataParallel(netG)
return netG
def _warmup_beta(linear_start, linear_end, n_timestep, warmup_frac):
betas = linear_end * np.ones(n_timestep, dtype=np.float64)
warmup_time = int(n_timestep * warmup_frac)
betas[:warmup_time] = np.linspace(
linear_start, linear_end, warmup_time, dtype=np.float64)
return betas
def make_beta_schedule(schedule, n_timestep, linear_start=1e-4, linear_end=2e-2, cosine_s=8e-3):
if schedule == 'quad':
betas = np.linspace(linear_start ** 0.5, linear_end ** 0.5,
n_timestep, dtype=np.float64) ** 2
elif schedule == 'linear':
betas = np.linspace(linear_start, linear_end,
n_timestep, dtype=np.float64)
elif schedule == 'warmup10':
betas = _warmup_beta(linear_start, linear_end,
n_timestep, 0.1)
elif schedule == 'warmup50':
betas = _warmup_beta(linear_start, linear_end,
n_timestep, 0.5)
elif schedule == 'const':
betas = linear_end * np.ones(n_timestep, dtype=np.float64)
elif schedule == 'jsd': # 1/T, 1/(T-1), 1/(T-2), ..., 1
betas = 1. / np.linspace(n_timestep,
1, n_timestep, dtype=np.float64)
elif schedule == "cosine":
timesteps = (
torch.arange(n_timestep + 1, dtype=torch.float64) /
n_timestep + cosine_s
)
alphas = timesteps / (1 + cosine_s) * math.pi / 2
alphas = torch.cos(alphas).pow(2)
alphas = alphas / alphas[0]
betas = 1 - alphas[1:] / alphas[:-1]
betas = betas.clamp(max=0.999)
else:
raise NotImplementedError(schedule)
return betas
# gaussian diffusion trainer class
def exists(x):
return x is not None
def default(val, d):
if exists(val):
return val
return d() if isfunction(d) else d
class GaussianDiffusion(nn.Module):
def __init__(
self,
denoise_fn,
image_size,
channels=3,
loss_type='l1',
conditional=True,
schedule_opt=None
):
super().__init__()
self.channels = channels
self.image_size = image_size
self.denoise_fn = denoise_fn
self.loss_type = loss_type
self.conditional = conditional
self.device = torch.device('cuda')
if schedule_opt is not None:
pass
# self.set_new_noise_schedule(schedule_opt)
def set_loss(self, device):
if self.loss_type == 'l1':
self.loss_func = nn.L1Loss(reduction='sum').to(device)
elif self.loss_type == 'l2':
self.loss_func = nn.MSELoss(reduction='sum').to(device)
else:
raise NotImplementedError()
def set_new_noise_schedule(self, schedule_opt, device):
to_torch = partial(torch.tensor, dtype=torch.float32, device=device)
betas = make_beta_schedule(
schedule=schedule_opt['schedule'],
n_timestep=schedule_opt['n_timestep'],
linear_start=schedule_opt['linear_start'],
linear_end=schedule_opt['linear_end'])
betas = betas.detach().cpu().numpy() if isinstance(
betas, torch.Tensor) else betas
alphas = 1. - betas
alphas_cumprod = np.cumprod(alphas, axis=0)
alphas_cumprod_prev = np.append(1., alphas_cumprod[:-1])
self.sqrt_alphas_cumprod_prev = np.sqrt(
np.append(1., alphas_cumprod))
timesteps, = betas.shape
self.num_timesteps = int(timesteps)
self.register_buffer('betas', to_torch(betas))
self.register_buffer('alphas_cumprod', to_torch(alphas_cumprod))
self.register_buffer('alphas_cumprod_prev',
to_torch(alphas_cumprod_prev))
# calculations for diffusion q(x_t | x_{t-1}) and others
self.register_buffer('sqrt_alphas_cumprod',
to_torch(np.sqrt(alphas_cumprod)))
self.register_buffer('sqrt_one_minus_alphas_cumprod',
to_torch(np.sqrt(1. - alphas_cumprod)))
self.register_buffer('log_one_minus_alphas_cumprod',
to_torch(np.log(1. - alphas_cumprod)))
self.register_buffer('sqrt_recip_alphas_cumprod',
to_torch(np.sqrt(1. / alphas_cumprod)))
self.register_buffer('sqrt_recipm1_alphas_cumprod',
to_torch(np.sqrt(1. / alphas_cumprod - 1)))
# calculations for posterior q(x_{t-1} | x_t, x_0)
posterior_variance = betas * \
(1. - alphas_cumprod_prev) / (1. - alphas_cumprod)
# above: equal to 1. / (1. / (1. - alpha_cumprod_tm1) + alpha_t / beta_t)
self.register_buffer('posterior_variance',
to_torch(posterior_variance))
# below: log calculation clipped because the posterior variance is 0 at the beginning of the diffusion chain
self.register_buffer('posterior_log_variance_clipped', to_torch(
np.log(np.maximum(posterior_variance, 1e-20))))
self.register_buffer('posterior_mean_coef1', to_torch(
betas * np.sqrt(alphas_cumprod_prev) / (1. - alphas_cumprod)))
self.register_buffer('posterior_mean_coef2', to_torch(
(1. - alphas_cumprod_prev) * np.sqrt(alphas) / (1. - alphas_cumprod)))
# calc ddim alpha
def compute_alpha(self, beta, t):
beta = torch.cat([torch.zeros(1).to(beta.device), beta], dim=0)
a = (1 - beta).cumprod(dim=0).index_select(0, t + 1).view(-1, 1, 1, 1)
return a
def slerp(self, z1, z2, alpha):
theta = torch.acos(torch.sum(z1 * z2) / (torch.norm(z1) * torch.norm(z2)))
return (
torch.sin((1 - alpha) * theta) / torch.sin(theta) * z1
+ torch.sin(alpha * theta) / torch.sin(theta) * z2
)
def predict_start_from_noise(self, x_t, t, noise):
return self.sqrt_recip_alphas_cumprod[t] * x_t - \
self.sqrt_recipm1_alphas_cumprod[t] * noise
def q_posterior(self, x_start, x_t, t):
posterior_mean = self.posterior_mean_coef1[t] * \
x_start + self.posterior_mean_coef2[t] * x_t
posterior_log_variance_clipped = self.posterior_log_variance_clipped[t]
return posterior_mean, posterior_log_variance_clipped
def p_mean_variance(self, x, t, clip_denoised: bool, condition_x=None):
batch_size = x.shape[0]
noise_level = torch.FloatTensor(
[self.sqrt_alphas_cumprod_prev[t+1]]).repeat(batch_size, 1).to(x.device)
if condition_x is not None:
x_recon = self.predict_start_from_noise(
x, t=t, noise=self.denoise_fn(torch.cat([condition_x, x], dim=1), noise_level))
else:
x_recon = self.predict_start_from_noise(
x, t=t, noise=self.denoise_fn(x, noise_level))
if clip_denoised:
x_recon.clamp_(-1., 1.)
model_mean, posterior_log_variance = self.q_posterior(
x_start=x_recon, x_t=x, t=t)
return model_mean, posterior_log_variance
@torch.no_grad()
def p_sample(self, x, t, clip_denoised=True, condition_x=None):
model_mean, model_log_variance = self.p_mean_variance(
x=x, t=t, clip_denoised=clip_denoised, condition_x=condition_x)
noise = torch.randn_like(x) if t > 0 else torch.zeros_like(x)
return model_mean + noise * (0.5 * model_log_variance).exp()
@torch.no_grad()
def p_sample_loop(self, mask, ori_img = None,continous=False,condition_ddim = False,steps = 2000,eta = 1.0):
device = self.betas.device
mask_img = mask
if condition_ddim: # ddim
timesteps = steps
ddim_eta = eta
alpha = 0.5 # from 0 to 1.0
sample_inter = (1 | (timesteps // 10))
ret_img = ori_img
ret_img = torch.cat([ret_img, mask_img], dim=0)
skip = self.num_timesteps // timesteps
seq = range(0, self.num_timesteps, skip)
seq_next = [-1] + list(seq[:-1])
batch_size = ori_img.shape[0]
# init noise (Impact of initial noise)
shape = ori_img.shape
z1 = torch.randn([shape[0], 3, shape[2], shape[3]], device=device)
z2 = torch.randn([shape[0], 3, shape[2], shape[3]], device=device)
# for alpha in alpha_scale:
x = self.slerp(z1, z2, alpha)
for i, j in tqdm(zip(reversed(seq), reversed(seq_next)), desc='sampling loop time step', total=len(seq)):
t = (torch.ones(batch_size) * i).to(x.device)
next_t = (torch.ones(batch_size) * j).to(x.device)
at = self.compute_alpha(self.betas, t.long())
at_next = self.compute_alpha(self.betas, next_t.long())
noise_level = torch.FloatTensor([self.sqrt_alphas_cumprod_prev[i + 1]]).repeat(batch_size, 1).to(
x.device)
et = self.denoise_fn(torch.cat([mask_img, x], dim=1), noise_level) # condition and uncondition(noise)
x0_t = (x - et * (1 - at).sqrt()) / at.sqrt()
# x0_t.clamp_(-1., 1.)
sigma = (
ddim_eta * ((1 - at / at_next) * (1 - at_next) / (1 - at)).sqrt()
)
c2 = ((1 - at_next) - sigma ** 2).sqrt()
xt_next = at_next.sqrt() * x0_t + sigma * torch.randn_like(x) + c2 * et
x = xt_next
if i % sample_inter == 0 or (i == len(seq) - 1):
ret_img = torch.cat([ret_img, xt_next], dim=0)
else:
sample_inter = (1 | (self.num_timesteps//10))
if not self.conditional:
shape = ori_img.shape
img = torch.randn(shape, device=device)
ret_img = img
for i in tqdm(reversed(range(0, self.num_timesteps)), desc='sampling loop time step', total=self.num_timesteps):
img = self.p_sample(img, i)
if i % sample_inter == 0:
ret_img = torch.cat([ret_img, img], dim=0)
else:
x = mask_img
shape = x.shape
img = torch.randn(shape, device=device)
ret_img = x
for i in tqdm(reversed(range(0, self.num_timesteps)), desc='sampling loop time step', total=self.num_timesteps):
img = self.p_sample(img, i, condition_x=x)
if i % sample_inter == 0:
ret_img = torch.cat([ret_img, img], dim=0)
if continous:
return ret_img, mask_img
else:
return ret_img[-1]
@torch.no_grad()
def sample(self, batch_size=1, continous=False): # drom random noise
image_size = self.image_size
channels = self.channels
return self.p_sample_loop((batch_size, channels, image_size, image_size), continous)
@torch.no_grad()
def inpainting(self, mask, ori_img=None, continous=False,condition_ddim = False,steps = 2000,eta = 1):
# from original imagea
return self.p_sample_loop(mask, ori_img, continous,condition_ddim,steps,eta)
def q_sample(self, x_start, continuous_sqrt_alpha_cumprod, noise=None): # x_t
noise = default(noise, lambda: torch.randn_like(x_start))
# random gamma
return (
continuous_sqrt_alpha_cumprod * x_start +
(1 - continuous_sqrt_alpha_cumprod**2).sqrt() * noise
)
def p_losses(self, x_in, noise=None):
mask_in = x_in['Mask']
ori_in = x_in['Image']
[b, c, h, w] = ori_in.shape
t = np.random.randint(1, self.num_timesteps + 1)
continuous_sqrt_alpha_cumprod = torch.FloatTensor(
np.random.uniform(
self.sqrt_alphas_cumprod_prev[t-1],
self.sqrt_alphas_cumprod_prev[t],
size=b
)
).to(mask_in.device)
continuous_sqrt_alpha_cumprod = continuous_sqrt_alpha_cumprod.view(b, -1)
noise = default(noise, lambda: torch.randn_like(ori_in))
x_noisy = self.q_sample( # adding noise, get x_t
x_start=ori_in, continuous_sqrt_alpha_cumprod=continuous_sqrt_alpha_cumprod.view(-1, 1, 1, 1), noise=noise)
# denoising. denoise_fn: unet. x_noisy:x_t
if not self.conditional:
noise_pred = self.denoise_fn(x_noisy, continuous_sqrt_alpha_cumprod)
else:
noise_pred = self.denoise_fn(
torch.cat([mask_in, x_noisy], dim=1), continuous_sqrt_alpha_cumprod)
loss = self.loss_func(noise_pred, noise)
return loss
def forward(self, x, *args, **kwargs):
return self.p_losses(x, *args, **kwargs)
class DDPM:
def __init__(self, opt):
self.opt = opt
self.device = torch.device(
'cuda' if opt['gpu_ids'] is not None else 'cpu')
self.begin_step = 0
self.begin_epoch = 0
# define network and load pretrained models
self.netG = self.set_device(define_G(opt))
self.schedule_phase = None
# set loss and load resume state
self.set_loss()
self.set_new_noise_schedule(
opt['model']['beta_schedule']['train'], schedule_phase='train')
if self.opt['phase'] == 'train':
self.netG.train()
optim_params = list(self.netG.parameters())
self.optG = torch.optim.Adam(
optim_params, lr=opt['train']["optimizer"]["lr"])
self.log_dict = OrderedDict()
self.load_network() # load pretrained models in define_G
self.print_network()
def feed_data(self, data):
self.data = self.set_device(data)
def optimize_parameters(self):
self.optG.zero_grad()
l_pix = self.netG(self.data) # return loss
# need to average in multi-gpu
b, c, h, w = self.data['Image'].shape
l_pix = l_pix.sum()/int(b*c*h*w)
l_pix.backward()
self.optG.step()
# set log
self.log_dict['l_pix'] = l_pix.item()
def test(self, continous=False,condition_ddim = False,steps = 2000,eta = 1):
self.netG.eval()
with torch.no_grad():
inpainting_parallel = nn.DataParallel(InpaintingModule(self.netG))
self.fake, self.mask = inpainting_parallel(
self.data['Mask'], self.data['Image'], continous,condition_ddim,steps,eta)
self.netG.train()
def sample(self, batch_size=1, continous=False):
self.netG.eval()
with torch.no_grad():
if isinstance(self.netG, nn.DataParallel):
self.fake = self.netG.module.sample(batch_size, continous)
else:
self.fake = self.netG.sample(batch_size, continous)
self.netG.train()
def set_loss(self):
if isinstance(self.netG, nn.DataParallel):
self.netG.module.set_loss(self.device)
else:
self.netG.set_loss(self.device)
def set_device(self, x):
if isinstance(x, dict):
for key, item in x.items():
if item is not None:
x[key] = item.to(self.device)
elif isinstance(x, list):
for item in x:
if item is not None:
item = item.to(self.device)
else:
x = x.to(self.device)
return x
def set_new_noise_schedule(self, schedule_opt, schedule_phase='train'):
if self.schedule_phase is None or self.schedule_phase != schedule_phase:
self.schedule_phase = schedule_phase
if isinstance(self.netG, nn.DataParallel):
self.netG.module.set_new_noise_schedule(
schedule_opt, self.device)
else:
self.netG.set_new_noise_schedule(schedule_opt, self.device)
def get_current_log(self):
return self.log_dict
def get_current_visuals(self, need_LR=True, sample=False):
out_dict = OrderedDict()
out_dict['REAL'] = self.data['Image'].detach().float().cpu()
out_dict['FAKE'] = self.fake.detach().float().cpu()
out_dict['MASK'] = self.data['Mask'].detach().float().cpu()
return out_dict
def get_network_description(self, network):
'''Get the string and total parameters of the network'''
if isinstance(network, nn.DataParallel):
network = network.module
s = str(network)
n = sum(map(lambda x: x.numel(), network.parameters()))
return s, n
def print_network(self):
s, n = self.get_network_description(self.netG)
if isinstance(self.netG, nn.DataParallel):
net_struc_str = '{} - {}'.format(self.netG.__class__.__name__,
self.netG.module.__class__.__name__)
else:
net_struc_str = '{}'.format(self.netG.__class__.__name__)
logger.info(
'Network G structure: {}, with parameters: {:,d}'.format(net_struc_str, n))
logger.info(s)
def save_network(self, epoch, iter_step):
gen_path = os.path.join(
self.opt['path']['checkpoint'], 'I{}_E{}_gen.pth'.format(iter_step, epoch))
opt_path = os.path.join(
self.opt['path']['checkpoint'], 'I{}_E{}_opt.pth'.format(iter_step, epoch))
# gen
network = self.netG
if isinstance(self.netG, nn.DataParallel):
network = network.module
state_dict = network.state_dict()
for key, param in state_dict.items():
state_dict[key] = param.cpu()
torch.save(state_dict, gen_path)
# opt
opt_state = {'epoch': epoch, 'iter': iter_step,
'scheduler': None, 'optimizer': None}
opt_state['optimizer'] = self.optG.state_dict()
torch.save(opt_state, opt_path)
logger.info(
'Saved model in [{:s}] ...'.format(gen_path))
def load_network(self):
load_path = self.opt['path']['resume_state']
gen_path = load_path
if isinstance(self.netG, nn.DataParallel):
network = self.netG.module
else:
network = self.netG
if load_path is not None:
if self.opt['phase'] == 'train':
if self.opt['path']['resume_state'] is not None and self.opt['path']['resume_state'] != '':
logger.info(
'Loading pretrained model for G [{:s}] ...'.format(load_path))
network.load_state_dict(torch.load(
gen_path), strict=(not self.opt['model']['finetune_norm']))
else:
init_weights(network, init_type='orthogonal')
else:
logger.info(
'Loading pretrained model for G [{:s}] ...'.format(load_path))
network.load_state_dict(torch.load(
gen_path), strict=(not self.opt['model']['finetune_norm']))
class InpaintingModule(nn.Module):
def __init__(self, original_model):
super(InpaintingModule, self).__init__()
self.original_model = original_model
def forward(self, mask, image, continous, condition_ddim, steps, eta):
if isinstance(self.original_model, nn.DataParallel):
return self.original_model.module.inpainting(mask, image, continous, condition_ddim, steps, eta)
return self.original_model.inpainting(mask, image, continous, condition_ddim, steps, eta)