-
Notifications
You must be signed in to change notification settings - Fork 30
/
noise2noise_fzh.py
329 lines (263 loc) · 12.6 KB
/
noise2noise_fzh.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
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
#coding:utf-8
import torch
import torch.nn as nn
from torch.optim import Adam, lr_scheduler
from unet_fzh import UNet
from utils import *
import os
import json
class Noise2Noise(object):
"""Implementation of Noise2Noise from Lehtinen et al. (2018)."""
def __init__(self, params, trainable, pretrain_model_path=None):
"""Initializes model."""
self.p = params
self.trainable = trainable
self.pretrain_model_path = pretrain_model_path
self._compile()
def _compile(self):
"""Compiles model (architecture, loss function, optimizers, etc.)."""
print('Noise2Noise: Learning Image Restoration without Clean Data (Lethinen et al., 2018)')
# Model (3x3=9 channels for Monte Carlo since it uses 3 HDR buffers)
if self.p.noise_type == 'mc':
self.is_mc = True
self.model = UNet(in_channels=9)
else:
self.is_mc = False
self.model = UNet(in_channels=3)
# Set optimizer and loss, if in training mode
if self.trainable:
self.optim = Adam(self.model.parameters(),
lr=self.p.learning_rate,
betas=self.p.adam[:2],
eps=self.p.adam[2])
# Learning rate adjustment
self.scheduler = lr_scheduler.ReduceLROnPlateau(self.optim,
patience=self.p.nb_epochs/4, factor=0.5, verbose=True)
# Loss function
if self.p.loss == 'hdr':
assert self.is_mc, 'Using HDR loss on non Monte Carlo images'
self.loss = HDRLoss()
elif self.p.loss == 'l2':
self.loss = nn.MSELoss()
elif self.p.loss == 'l1':
self.loss = nn.L1Loss()
elif self.p.loss == 'l0':
self.loss = L0Loss(nb_epochs=self.p.nb_epochs)
else:
self.loss = nn.SmoothL1Loss()
# CUDA support
self.use_cuda = torch.cuda.is_available() and self.p.cuda
if self.use_cuda:
self.model = self.model.cuda()
self.load_model(self.pretrain_model_path)
if self.trainable:
self.loss = self.loss.cuda()
def _print_params(self):
"""Formats parameters to print when training."""
print('Training parameters: ')
self.p.cuda = self.use_cuda
param_dict = vars(self.p)
pretty = lambda x: x.replace('_', ' ').capitalize()
print('\n'.join(' {} = {}'.format(pretty(k), str(v)) for k, v in param_dict.items()))
print()
def save_model(self, epoch, stats, first=False):
"""Saves model to files; can be overwritten at every epoch to save disk space."""
# Create directory for model checkpoints, if nonexistent
if first:
if self.p.clean_targets:
ckpt_dir_name = f'{datetime.now():{self.p.noise_type}-clean-%H%M}'
else:
ckpt_dir_name = f'{datetime.now():{self.p.noise_type}-%H%M}'
if self.p.ckpt_overwrite:
if self.p.clean_targets:
ckpt_dir_name = f'{self.p.noise_type}-clean'
else:
ckpt_dir_name = self.p.noise_type
self.ckpt_dir = os.path.join(self.p.ckpt_save_path, ckpt_dir_name)
if not os.path.isdir(self.p.ckpt_save_path):
os.mkdir(self.p.ckpt_save_path)
if not os.path.isdir(self.ckpt_dir):
os.mkdir(self.ckpt_dir)
# Save checkpoint dictionary
if self.p.ckpt_overwrite:
fname_unet = '{}/n2n-{}.pth'.format(self.ckpt_dir, self.p.noise_type)
else:
valid_loss = stats['valid_loss'][epoch]
fname_unet = '{}/n2n-epoch{}-{:>1.5f}.pth'.format(self.ckpt_dir, epoch + 1, valid_loss)
print('Saving checkpoint to: {}\n'.format(fname_unet))
torch.save(self.model.state_dict(), fname_unet)
# Save stats to JSON
fname_dict = '{}/n2n-stats.json'.format(self.ckpt_dir)
with open(fname_dict, 'w') as fp:
json.dump(stats, fp, indent=2)
def load_model(self, ckpt_fname):
"""Loads model from checkpoint file."""
print('Loading checkpoint from: {}'.format(ckpt_fname))
if self.use_cuda:
weights = torch.load(ckpt_fname)
# weights["_block4_1.0.weight"] = nn.init.kaiming_normal_(torch.empty(96, 96, 3, 3),
# mode='fan_in', nonlinearity='relu')
# weights["_block4_1.0.bias"] = torch.rand(96)
# weights["_block4_1.2.weight"] = nn.init.kaiming_normal_(torch.empty(96, 96, 3, 3),
# mode='fan_in', nonlinearity='relu')
# weights["_block4_1.2.bias"] = torch.rand(96)
# weights["_block4_1.4.weight"] = nn.init.kaiming_normal_(torch.empty(96, 96, 3, 3),
# mode='fan_in', nonlinearity='relu')
# weights["_block4_1.4.bias"] = torch.rand(96)
# weights["_block4.0.weight"] = nn.init.kaiming_normal_(torch.empty(96, 144, 3, 3),
# mode='fan_in', nonlinearity='relu')
# weights["_block4.0.bias"] = torch.rand(96)
self.model.load_state_dict(weights)
else:
self.model.load_state_dict(torch.load(ckpt_fname, map_location='cpu'))
def _on_epoch_end(self, stats, train_loss, epoch, epoch_start, valid_loader):
"""Tracks and saves starts after each epoch."""
# Evaluate model on validation set
print('\rTesting model on validation set... ', end='')
epoch_time = time_elapsed_since(epoch_start)[0]
valid_loss, valid_time, valid_psnr = self.eval(valid_loader)
show_on_epoch_end(epoch_time, valid_time, valid_loss, valid_psnr)
# Decrease learning rate if plateau
self.scheduler.step(valid_loss)
# Save checkpoint
stats['train_loss'].append(train_loss)
stats['valid_loss'].append(valid_loss)
stats['valid_psnr'].append(valid_psnr)
self.save_model(epoch, stats, epoch == 0)
# Plot stats
if self.p.plot_stats:
loss_str = f'{self.p.loss.upper()} loss'
plot_per_epoch(self.ckpt_dir, 'Valid loss', stats['valid_loss'], loss_str)
plot_per_epoch(self.ckpt_dir, 'Valid PSNR', stats['valid_psnr'], 'PSNR (dB)')
def test(self, test_loader, show):
"""Evaluates denoiser on test set."""
self.model.train(False)
source_imgs = []
denoised_imgs = []
clean_imgs = []
# Create directory for denoised images
denoised_dir = os.path.dirname(self.p.data)
save_path = os.path.join(denoised_dir, 'denoised')
if not os.path.isdir(save_path):
os.mkdir(save_path)
for batch_idx, (source, target) in enumerate(test_loader):
# Only do first <show> images
# if show == 0 or batch_idx >= show:
# break
source_imgs.append(source)
clean_imgs.append(target)
if self.use_cuda:
source = source.cuda()
# Denoise
denoised_img = self.model(source).detach()
denoised_imgs.append(denoised_img)
# Squeeze tensors
source_imgs = [t.squeeze(0) for t in source_imgs]
denoised_imgs = [t.squeeze(0) for t in denoised_imgs]
clean_imgs = [t.squeeze(0) for t in clean_imgs]
# Create montage and save images
print('Saving images and montages to: {}'.format(save_path))
for i in range(len(source_imgs)):
img_name = test_loader.dataset.imgs[i]
print('====img_name:', img_name)
create_montage(img_name, self.p.noise_type, save_path, source_imgs[i], denoised_imgs[i], clean_imgs[i], show)
def eval(self, valid_loader):
"""Evaluates denoiser on validation set."""
self.model.train(False)
valid_start = datetime.now()
loss_meter = AvgMeter()
psnr_meter = AvgMeter()
for batch_idx, (source, target) in enumerate(valid_loader):
if self.use_cuda:
source = source.cuda()
target = target.cuda()
# Denoise
source_denoised = self.model(source)
# Update loss
# loss = self.loss(source_denoised, target)
loss = self.loss(source_denoised, target, (self.p.nb_epochs/2-1)) # l0 loss才用
loss_meter.update(loss.item())
# Compute PSRN
if self.is_mc:
source_denoised = reinhard_tonemap(source_denoised)
# TODO: Find a way to offload to GPU, and deal with uneven batch sizes
for i in range(self.p.batch_size):
source_denoised = source_denoised.cpu()
target = target.cpu()
psnr_meter.update(psnr(source_denoised[i], target[i]).item())
valid_loss = loss_meter.avg
valid_time = time_elapsed_since(valid_start)[0]
psnr_avg = psnr_meter.avg
return valid_loss, valid_time, psnr_avg
def train(self, train_loader, valid_loader):
"""Trains denoiser on training set."""
self.model.train(True)
self._print_params()
num_batches = len(train_loader)
# assert num_batches % self.p.report_interval == 0, 'Report interval must divide total number of batches'
# Dictionaries of tracked stats
stats = {'noise_type': self.p.noise_type,
'noise_param': self.p.noise_param,
'train_loss': [],
'valid_loss': [],
'valid_psnr': []}
# Main training loop
train_start = datetime.now()
for epoch in range(self.p.nb_epochs):
print('EPOCH {:d} / {:d}'.format(epoch + 1, self.p.nb_epochs))
# Some stats trackers
epoch_start = datetime.now()
train_loss_meter = AvgMeter()
loss_meter = AvgMeter()
time_meter = AvgMeter()
# Minibatch SGD
for batch_idx, (source, target) in enumerate(train_loader):
batch_start = datetime.now()
progress_bar(batch_idx, num_batches, self.p.report_interval, loss_meter.val)
if self.use_cuda:
source = source.cuda()
target = target.cuda()
# Denoise image
source_denoised = self.model(source)
# loss = self.loss(source_denoised, target)
loss = self.loss(source_denoised, target, epoch)#l0 loss才用
loss_meter.update(loss.item())
# Zero gradients, perform a backward pass, and update the weights
self.optim.zero_grad()
loss.backward()
self.optim.step()
# Report/update statistics
time_meter.update(time_elapsed_since(batch_start)[1])
if (batch_idx + 1) % self.p.report_interval == 0 and batch_idx:
show_on_report(batch_idx, num_batches, loss_meter.avg, time_meter.avg)
train_loss_meter.update(loss_meter.avg)
loss_meter.reset()
time_meter.reset()
# Epoch end, save and reset tracker
self._on_epoch_end(stats, train_loss_meter.avg, epoch, epoch_start, valid_loader)
train_loss_meter.reset()
train_elapsed = time_elapsed_since(train_start)[0]
print('Training done! Total elapsed time: {}\n'.format(train_elapsed))
class HDRLoss(nn.Module):
"""High dynamic range loss."""
def __init__(self, eps=0.01):
"""Initializes loss with numerical stability epsilon."""
super(HDRLoss, self).__init__()
self._eps = eps
def forward(self, denoised, target):
"""Computes loss by unpacking render buffer."""
loss = ((denoised - target) ** 2) / (denoised + self._eps) ** 2
return torch.mean(loss.view(-1))
class L0Loss(nn.Module):
"""High dynamic range loss."""
def __init__(self, eps=1e-8, nb_epochs=10):
"""Initializes loss with numerical stability epsilon."""
super(L0Loss, self).__init__()
self._eps = eps
self.nb_epochs = nb_epochs
def forward(self, denoised, target, epoch):
"""Computes loss by unpacking render buffer."""
# gamma = 2.0 * (self.nb_epochs - epoch) / self.nb_epochs
gamma = 2.0 * (epoch+1) / self.nb_epochs
loss = ((torch.abs(denoised - target) + self._eps) ** gamma)
return torch.mean(loss.view(-1))