forked from Janspiry/Palette-Image-to-Image-Diffusion-Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
executable file
·221 lines (194 loc) · 10.3 KB
/
model.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
import torch
import tqdm
from core.base_model import BaseModel
from core.logger import LogTracker
import copy
class EMA():
def __init__(self, beta=0.9999):
super().__init__()
self.beta = beta
def update_model_average(self, ma_model, current_model):
for current_params, ma_params in zip(current_model.parameters(), ma_model.parameters()):
old_weight, up_weight = ma_params.data, current_params.data
ma_params.data = self.update_average(old_weight, up_weight)
def update_average(self, old, new):
if old is None:
return new
return old * self.beta + (1 - self.beta) * new
class Palette(BaseModel):
def __init__(self, networks, losses, sample_num, task, optimizers, ema_scheduler=None, **kwargs):
''' must to init BaseModel with kwargs '''
super(Palette, self).__init__(**kwargs)
''' networks, dataloder, optimizers, losses, etc. '''
self.loss_fn = losses[0]
self.netG = networks[0]
if ema_scheduler is not None:
self.ema_scheduler = ema_scheduler
self.netG_EMA = copy.deepcopy(self.netG)
self.EMA = EMA(beta=self.ema_scheduler['ema_decay'])
else:
self.ema_scheduler = None
''' networks can be a list, and must convert by self.set_device function if using multiple GPU. '''
self.netG = self.set_device(self.netG, distributed=self.opt['distributed'])
if self.ema_scheduler is not None:
self.netG_EMA = self.set_device(self.netG_EMA, distributed=self.opt['distributed'])
self.load_networks()
self.optG = torch.optim.Adam(list(filter(lambda p: p.requires_grad, self.netG.parameters())), **optimizers[0])
self.optimizers.append(self.optG)
self.resume_training()
if self.opt['distributed']:
self.netG.module.set_loss(self.loss_fn)
self.netG.module.set_new_noise_schedule(phase=self.phase)
else:
self.netG.set_loss(self.loss_fn)
self.netG.set_new_noise_schedule(phase=self.phase)
''' can rewrite in inherited class for more informations logging '''
self.train_metrics = LogTracker(*[m.__name__ for m in losses], phase='train')
self.val_metrics = LogTracker(*[m.__name__ for m in self.metrics], phase='val')
self.test_metrics = LogTracker(*[m.__name__ for m in self.metrics], phase='test')
self.sample_num = sample_num
self.task = task
def set_input(self, data):
''' must use set_device in tensor '''
self.cond_image = self.set_device(data.get('cond_image'))
self.gt_image = self.set_device(data.get('gt_image'))
self.mask = self.set_device(data.get('mask'))
self.mask_image = data.get('mask_image')
self.path = data['path']
self.batch_size = len(data['path'])
def get_current_visuals(self, phase='train'):
dict = {
'gt_image': (self.gt_image.detach()[:].float().cpu()+1)/2,
'cond_image': (self.cond_image.detach()[:].float().cpu()+1)/2,
}
if self.task in ['inpainting','uncropping']:
dict.update({
'mask': self.mask.detach()[:].float().cpu(),
'mask_image': (self.mask_image+1)/2,
})
if phase != 'train':
dict.update({
'output': (self.output.detach()[:].float().cpu()+1)/2
})
return dict
def save_current_results(self):
ret_path = []
ret_result = []
for idx in range(self.batch_size):
ret_path.append('GT_{}'.format(self.path[idx]))
ret_result.append(self.gt_image[idx].detach().float().cpu())
ret_path.append('Process_{}'.format(self.path[idx]))
ret_result.append(self.visuals[idx::self.batch_size].detach().float().cpu())
ret_path.append('Out_{}'.format(self.path[idx]))
ret_result.append(self.visuals[idx-self.batch_size].detach().float().cpu())
if self.task in ['inpainting','uncropping']:
ret_path.extend(['Mask_{}'.format(name) for name in self.path])
ret_result.extend(self.mask_image)
self.results_dict = self.results_dict._replace(name=ret_path, result=ret_result)
return self.results_dict._asdict()
def train_step(self):
self.netG.train()
self.train_metrics.reset()
for train_data in tqdm.tqdm(self.phase_loader):
self.set_input(train_data)
self.optG.zero_grad()
loss = self.netG(self.gt_image, self.cond_image, mask=self.mask)
loss.backward()
self.optG.step()
self.iter += self.batch_size
self.writer.set_iter(self.epoch, self.iter, phase='train')
self.train_metrics.update(self.loss_fn.__name__, loss.item())
if self.iter % self.opt['train']['log_iter'] == 0:
for key, value in self.train_metrics.result().items():
self.logger.info('{:5s}: {}\t'.format(str(key), value))
self.writer.add_scalar(key, value)
for key, value in self.get_current_visuals().items():
self.writer.add_images(key, value)
if self.ema_scheduler is not None:
if self.iter > self.ema_scheduler['ema_start'] and self.iter % self.ema_scheduler['ema_iter'] == 0:
self.EMA.update_model_average(self.netG_EMA, self.netG)
for scheduler in self.schedulers:
scheduler.step()
return self.train_metrics.result()
def val_step(self):
self.netG.eval()
self.val_metrics.reset()
with torch.no_grad():
for val_data in tqdm.tqdm(self.val_loader):
self.set_input(val_data)
if self.opt['distributed']:
if self.task in ['inpainting','uncropping']:
self.output, self.visuals = self.netG.module.restoration(self.cond_image, y_t=self.cond_image,
y_0=self.gt_image, mask=self.mask, sample_num=self.sample_num)
else:
self.output, self.visuals = self.netG.module.restoration(self.cond_image, sample_num=self.sample_num)
else:
if self.task in ['inpainting','uncropping']:
self.output, self.visuals = self.netG.restoration(self.cond_image, y_t=self.cond_image,
y_0=self.gt_image, mask=self.mask, sample_num=self.sample_num)
else:
self.output, self.visuals = self.netG.restoration(self.cond_image, sample_num=self.sample_num)
self.iter += self.batch_size
self.writer.set_iter(self.epoch, self.iter, phase='val')
for met in self.metrics:
key = met.__name__
value = met(self.gt_image, self.output)
self.val_metrics.update(key, value)
self.writer.add_scalar(key, value)
for key, value in self.get_current_visuals(phase='val').items():
self.writer.add_images(key, value)
self.writer.save_images(self.save_current_results())
return self.val_metrics.result()
def test(self):
self.netG.eval()
self.test_metrics.reset()
with torch.no_grad():
for phase_data in tqdm.tqdm(self.phase_loader):
self.set_input(phase_data)
if self.opt['distributed']:
if self.task in ['inpainting','uncropping']:
self.output, self.visuals = self.netG.module.restoration(self.cond_image, y_t=self.cond_image,
y_0=self.gt_image, mask=self.mask, sample_num=self.sample_num)
else:
self.output, self.visuals = self.netG.module.restoration(self.cond_image, sample_num=self.sample_num)
else:
if self.task in ['inpainting','uncropping']:
self.output, self.visuals = self.netG.restoration(self.cond_image, y_t=self.cond_image,
y_0=self.gt_image, mask=self.mask, sample_num=self.sample_num)
else:
self.output, self.visuals = self.netG.restoration(self.cond_image, sample_num=self.sample_num)
self.iter += self.batch_size
self.writer.set_iter(self.epoch, self.iter, phase='test')
for met in self.metrics:
key = met.__name__
value = met(self.gt_image, self.output)
self.test_metrics.update(key, value)
self.writer.add_scalar(key, value)
for key, value in self.get_current_visuals(phase='test').items():
self.writer.add_images(key, value)
self.writer.save_images(self.save_current_results())
test_log = self.test_metrics.result()
''' save logged informations into log dict '''
test_log.update({'epoch': self.epoch, 'iters': self.iter})
''' print logged informations to the screen and tensorboard '''
for key, value in test_log.items():
self.logger.info('{:5s}: {}\t'.format(str(key), value))
def load_networks(self):
""" save pretrained model and training state, which only do on GPU 0. """
if self.opt['distributed']:
netG_label = self.netG.module.__class__.__name__
else:
netG_label = self.netG.__class__.__name__
self.load_network(network=self.netG, network_label=netG_label, strict=False)
if self.ema_scheduler is not None:
self.load_network(network=self.netG_EMA, network_label=netG_label+'_ema', strict=False)
def save_everything(self):
""" load pretrained model and training state. """
if self.opt['distributed']:
netG_label = self.netG.module.__class__.__name__
else:
netG_label = self.netG.__class__.__name__
self.save_network(network=self.netG, network_label=netG_label)
if self.ema_scheduler is not None:
self.save_network(network=self.netG_EMA, network_label=netG_label+'_ema')
self.save_training_state()