-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_lbmc.py
287 lines (248 loc) · 11.7 KB
/
train_lbmc.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
# Python
import os
import sys
import time
import visdom
import random
import itertools
from tqdm import tqdm
import matplotlib.pyplot as plt
from collections import OrderedDict
# NumPy and PyTorch
import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
# Cho et al. dependency
import configs
from train_kpcn import validate_kpcn, train, train_epoch_kpcn
from support.networks import PathNet
from support.interfaces import LBMCInterface
from support.datasets import MSDenoiseDataset
from support.utils import BasicArgumentParser
from support.losses import TonemappedRelativeMSE, RelativeMSE, FeatureMSE, GlobalRelativeSimilarityLoss
# Gharbi et al. dependency
sys.path.insert(1, configs.PATH_LBMC)
try:
from train import tonemap
from utils import SMAPE, PSNR
from train import LEARNING_RATE
from layer_network import LayerNet
except ImportError as error:
print('Put appropriate paths in the configs.py file.')
raise
from ttools.modules.image_operators import crop_like
def init_data(args):
# Initialize datasets
datasets = {}
datasets['train'] = MSDenoiseDataset(args.data_dir, 8, 'lbmc', 'train', args.batch_size, 'random',
use_g_buf=True, use_sbmc_buf=False, use_llpm_buf=args.use_llpm_buf, pnet_out_size=0)
datasets['val'] = MSDenoiseDataset(args.data_dir, 8, 'lbmc', 'val', BS_VAL, 'grid',
use_g_buf=True, use_sbmc_buf=False, use_llpm_buf=args.use_llpm_buf, pnet_out_size=0)
# Initialize dataloaders
dataloaders = {}
dataloaders['train'] = DataLoader(
datasets['train'],
batch_size=args.batch_size,
num_workers=1,
pin_memory=False,
)
dataloaders['val'] = DataLoader(
datasets['val'],
batch_size=BS_VAL,
num_workers=1,
pin_memory=False
)
return datasets, dataloaders
def init_model(dataset, args):
interfaces = []
lr_pnets = args.lr_pnet
pnet_out_sizes = args.pnet_out_size
w_manifs = args.w_manif
tmp = [lr_pnets, pnet_out_sizes, w_manifs]
for lr_pnet, pnet_out_size, w_manif in list(itertools.product(*tmp)):
# Initialize models (NOTE: modified for each model)
models = {}
print('Train a LBMC network.')
if args.use_llpm_buf:
if args.disentangle in ['m10r01', 'm11r01']:
n_in = dataset['train'].dncnn_in_size + pnet_out_size // 2
else:
n_in = dataset['train'].dncnn_in_size + pnet_out_size
models['dncnn'] = LayerNet(n_in, tonemap, True)
print('Initialize the LBMC for path descriptors (# of input channels: %d).'%(n_in))
n_in = dataset['train'].pnet_in_size
n_out = pnet_out_size
print('Train a LLPM feature extractor. (# of input channels: %d, # of output channels: %d).'%(n_in, n_out))
models['backbone'] = PathNet(ic=n_in, outc=n_out)
else:
n_in = dataset['train'].dncnn_in_size
models['dncnn'] = LayerNet(n_in, tonemap, True)
print('Initialize the LBMC for vanilla buffers (# of input channels: %d).'%(n_in))
# Load pretrained weights
if len(list(itertools.product(*tmp))) == 1:
model_fn = os.path.join(args.save, args.model_name + '.pth')
else:
model_fn = os.path.join(args.save, '%s_lp%f_pos%d_wgt%f.pth'%(args.model_name, lr_pnet, pnet_out_size, w_manif))
assert args.start_epoch != 0 or not os.path.isfile(model_fn), 'Model %s already exists.'%(model_fn)
is_pretrained = (args.start_epoch != 0) and os.path.isfile(model_fn)
if is_pretrained:
ck = torch.load(model_fn)
for model_name in models:
try:
models[model_name].load_state_dict(ck['state_dict_' + model_name])
except RuntimeError:
new_state_dict = OrderedDict()
for k, v in ck['state_dict_' + model_name].items():
name = k[7:]
new_state_dict[name] = v
models[model_name].load_state_dict(new_state_dict)
print('Pretraining weights are loaded.')
else:
print('Train models from scratch.')
# Use GPU parallelism if needed
if args.single_gpu:
print('Data Sequential')
for model_name in models:
models[model_name] = models[model_name].cuda(args.device_id)
else:
print('Data Parallel')
if torch.cuda.device_count() == 1:
print('Single CUDA machine detected')
for model_name in models:
models[model_name] = models[model_name].cuda()
elif torch.cuda.device_count() > 1:
print('%d CUDA machines detected' % (torch.cuda.device_count()))
for model_name in models:
models[model_name] = nn.DataParallel(models[model_name], output_device=1).cuda()
else:
assert False, 'No detected GPU device.'
# Initialize optimizers
optims = {}
params = {}
for model_name in models:
lr = args.lr_dncnn if 'dncnn' == model_name else lr_pnet
optims['optim_' + model_name] = optim.Adam(models[model_name].parameters(), lr=lr)
if not is_pretrained:
continue
if 'optims' in ck:
state = ck['optims']['optim_' + model_name].state_dict()
elif 'optim_' + model_name in ck['params']:
state = ck['params']['optim_' + model_name].state_dict()
else:
print('No state for the optimizer for %s, use the initial optimizer and learning rate.'%(model_name))
continue
if not args.lr_ckpt:
print('Set the new learning rate %.3e for %s.'%(lr, model_name))
state['param_groups'][0]['lr'] = lr
else:
print('Use the checkpoint (%s) learning rate for %s.'%(model_fn, model_name))
optims['optim_' + model_name].load_state_dict(state)
# Initialize losses (NOTE: modified for each model)
def recon_loss(im, ref):
return SMAPE(torch.clamp(im, min=0, max=1e2), torch.clamp(ref, min=0, max=1e2))
loss_funcs = {
'l_recon': recon_loss,
'l_test': RelativeMSE()
}
if args.manif_learn:
if args.manif_loss == 'FMSE':
loss_funcs['l_manif'] = FeatureMSE()
print('Manifold loss: FeatureMSE')
elif args.manif_loss == 'GRS':
loss_funcs['l_manif'] = GlobalRelativeSimilarityLoss()
print('Manifold loss: Global Relative Similarity')
else:
print('Manifold loss: None (i.e., ablation study)')
# Initialize a training interface (NOTE: modified for each model)
itf = LBMCInterface(models, optims, loss_funcs, args, args.use_llpm_buf, args.manif_learn, w_manif, args.disentangle)
if is_pretrained:
print('Use the checkpoint best error %.3e'%(args.best_err))
itf.best_err = args.best_err
interfaces.append(itf)
# Initialize a visdom visualizer object
params['plots'] = {}
params['data_device'] = 1 if torch.cuda.device_count() > 1 and not args.single_gpu else args.device_id
if args.visual:
params['vis'] = visdom.Visdom(server='http://localhost')
else:
print('No visual.')
# Required for LBMC
params['sched_dncnn'] = optim.lr_scheduler.StepLR(optims['optim_dncnn'], step_size=3, gamma=0.5, last_epoch=args.start_epoch-1)
if is_pretrained:
params['sched_dncnn'].load_state_dict(ck['params']['sched_dncnn'].state_dict())
# Make the save directory if needed
if not os.path.isdir(args.save):
os.mkdir(args.save)
return interfaces, params
def main(args):
# Set random seeds
random.seed("Inyoung Cho, Yuchi Huo, Sungeui Yoon @ KAIST")
np.random.seed(0)
torch.manual_seed(0)
torch.backends.cudnn.benchmark = True
#torch.backends.cudnn.deterministic = True
# Get ready
dataset, dataloaders = init_data(args)
interfaces, params = init_model(dataset, args)
train(interfaces, dataloaders, params, args)
if __name__ == '__main__':
""" NOTE: Example Training Scripts """
""" LBMC Vanilla
Train a LBMC model from scratch:
python train_lbmc.py --single_gpu --batch_size 8 --val_epoch 1 --data_dir /mnt/ssd3/iycho/KPCN --model_name LBMC_vanilla --desc "LBMC_vanilla" --num_epoch 6
"""
""" LBMC Manifold
Train a LBMC model from scratch:
python train_lbmc.py --single_gpu --batch_size 8 --val_epoch 1 --data_dir /mnt/ssd3/iycho/KPCN --model_name LBMC_Manifold_P3 --desc "LBMC_Manifold_P3" --num_epoch 6 --use_llpm_buf --manif_learn --manif_loss FMSE
"""
BS_VAL = 4 # validation set batch size
parser = BasicArgumentParser()
# Basic
parser.add_argument('--desc', type=str, required=True,
help='short description of the current experiment.')
parser.add_argument('--single_gpu', action='store_true',
help='use only one GPU.')
parser.add_argument('--device_id', type=int, default=0,
help='device id')
parser.add_argument('--lr_ckpt', action='store_true',
help='')
parser.add_argument('--best_err', type=float, required=False)
parser.add_argument('--use_g_buf', action='store_false')
# Baseline
parser.add_argument('--lr_dncnn', type=float, default=0.0001,
help='learning rate of PathNet.')
# Manifold module
parser.add_argument('--use_llpm_buf', action='store_true',
help='use the llpm-specific buffer.')
parser.add_argument('--manif_learn', action='store_true',
help='use the manifold learning loss.')
parser.add_argument('--pnet_out_size', type=int, nargs='+', default=[3],
help='# of channels of outputs of PathNet.')
parser.add_argument('--lr_pnet', type=float, nargs='+', default=[0.0001],
help='learning rate of PathNet.')
parser.add_argument('--manif_loss', type=str,
help='`FMSE` or `GRS`')
parser.add_argument('--w_manif', type=float, nargs='+', default=[0.1],
help='ratio of the manifold learning loss to \
the reconstruction loss.')
parser.add_argument('--disentangle', type=str, default='m11r11',
help='`m11r11`, `m10r01`, `m10r11`, or `m11r01`')
parser.add_argument('--not_save', action='store_true',
help='do not save checkpoint (debugging purpose).')
args = parser.parse_args()
if args.manif_learn and not args.use_llpm_buf:
raise RuntimeError('The manifold learning module requires a llpm-specific buffer.')
if args.manif_learn and not args.manif_loss:
raise RuntimeError('The manifold learning module requires a manifold loss.')
if not args.manif_learn and args.manif_loss:
raise RuntimeError('A manifold loss is not necessary when the manifold learning module is opted out.')
if args.manif_learn and args.manif_loss not in ['GRS', 'FMSE']:
raise RuntimeError('Argument `manif_loss` should be either `FMSE` or `GRS`')
if args.disentangle not in ['m11r11', 'm10r01', 'm10r11', 'm11r01']:
raise RuntimeError('Argument `disentangle` should be either `m11r11`, `m10r01`, `m10r11`, or `m11r01`')
for s in args.pnet_out_size:
if args.disentangle != 'm11r11' and s % 2 != 0:
raise RuntimeError('Argument `pnet_out_size` should be a list of even numbers')
main(args)