-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathIRN_color_model.py
208 lines (162 loc) · 7.87 KB
/
IRN_color_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
import logging
from collections import OrderedDict
import torch
import torch.nn as nn
from torch.nn.parallel import DataParallel, DistributedDataParallel
import models.networks as networks
import models.lr_scheduler as lr_scheduler
from .base_model import BaseModel
from models.modules.loss import ReconstructionLoss
from models.modules.Quantization import Quantization
logger = logging.getLogger('base')
class IRNColorModel(BaseModel):
def __init__(self, opt):
super(IRNColorModel, self).__init__(opt)
if opt['dist']:
self.rank = torch.distributed.get_rank()
else:
self.rank = -1 # non dist training
train_opt = opt['train']
test_opt = opt['test']
self.train_opt = train_opt
self.test_opt = test_opt
self.netG = networks.define_grey(opt).to(self.device)
if opt['dist']:
self.netG = DistributedDataParallel(self.netG, device_ids=[torch.cuda.current_device()])
else:
self.netG = DataParallel(self.netG)
# print network
self.print_network()
self.load()
self.Quantization = Quantization()
if self.is_train:
self.netG.train()
# loss
self.Reconstruction_forw = ReconstructionLoss(losstype=self.train_opt['pixel_criterion_forw'])
self.Reconstruction_back = ReconstructionLoss(losstype=self.train_opt['pixel_criterion_back'])
# optimizers
wd_G = train_opt['weight_decay_G'] if train_opt['weight_decay_G'] else 0
optim_params = []
for k, v in self.netG.named_parameters():
if v.requires_grad:
optim_params.append(v)
else:
if self.rank <= 0:
logger.warning('Params [{:s}] will not optimize.'.format(k))
self.optimizer_G = torch.optim.Adam(optim_params, lr=train_opt['lr_G'],
weight_decay=wd_G,
betas=(train_opt['beta1'], train_opt['beta2']))
self.optimizers.append(self.optimizer_G)
# schedulers
if train_opt['lr_scheme'] == 'MultiStepLR':
for optimizer in self.optimizers:
self.schedulers.append(
lr_scheduler.MultiStepLR_Restart(optimizer, train_opt['lr_steps'],
restarts=train_opt['restarts'],
weights=train_opt['restart_weights'],
gamma=train_opt['lr_gamma'],
clear_state=train_opt['clear_state']))
elif train_opt['lr_scheme'] == 'CosineAnnealingLR_Restart':
for optimizer in self.optimizers:
self.schedulers.append(
lr_scheduler.CosineAnnealingLR_Restart(
optimizer, train_opt['T_period'], eta_min=train_opt['eta_min'],
restarts=train_opt['restarts'], weights=train_opt['restart_weights']))
else:
raise NotImplementedError('MultiStepLR learning rate scheme is enough.')
self.log_dict = OrderedDict()
def feed_data(self, data):
self.ref_Grey = data['Grey'].to(self.device)
self.real_H = data['GT'].to(self.device) # GT
def gaussian_batch(self, dims):
return torch.randn(tuple(dims)).to(self.device)
def loss_forward(self, out, y, z):
l_forw_fit = self.train_opt['lambda_fit_forw'] * self.Reconstruction_forw(out, y)
z = z.reshape([out.shape[0], -1])
l_forw_ce = self.train_opt['lambda_ce_forw'] * torch.sum(z**2) / z.shape[0]
return l_forw_fit, l_forw_ce
def loss_backward(self, x, y):
x_samples = self.netG(x=y, rev=True)
x_samples_image = x_samples[:, :3, :, :]
l_back_rec = self.train_opt['lambda_rec_back'] * self.Reconstruction_back(x, x_samples_image)
return l_back_rec
def optimize_parameters(self, step):
self.optimizer_G.zero_grad()
# forward decolorization
self.input = self.real_H
self.output = self.netG(x=self.input)
zshape = self.output[:, 1:, :, :].shape
Grey_ref = self.ref_Grey.detach()
l_forw_fit, l_forw_ce = self.loss_forward(self.output[:, :1, :, :], Grey_ref, self.output[:, 1:, :, :])
# backward upscaling
Grey = self.Quantization(self.output[:, :1, :, :])
gaussian_scale = self.train_opt['gaussian_scale'] if self.train_opt['gaussian_scale'] != None else 0
y_ = torch.cat((Grey, gaussian_scale * self.gaussian_batch(zshape)), dim=1)
l_back_rec = self.loss_backward(self.real_H, y_)
# total loss
loss = l_forw_fit + l_back_rec + l_forw_ce
loss.backward()
# gradient clipping
if self.train_opt['gradient_clipping']:
nn.utils.clip_grad_norm_(self.netG.parameters(), self.train_opt['gradient_clipping'])
self.optimizer_G.step()
# set log
self.log_dict['l_forw_fit'] = l_forw_fit.item()
self.log_dict['l_forw_ce'] = l_forw_ce.item()
self.log_dict['l_back_rec'] = l_back_rec.item()
def test(self):
Lshape = self.ref_Grey.shape
self.input = self.real_H
zshape = [Lshape[0], 2, Lshape[2], Lshape[3]]
gaussian_scale = 0
if self.test_opt and self.test_opt['gaussian_scale'] != None:
gaussian_scale = self.test_opt['gaussian_scale']
self.netG.eval()
with torch.no_grad():
self.forw_L = self.netG(x=self.input)[:, :1, :, :]
self.forw_L = self.Quantization(self.forw_L)
y_forw = torch.cat((self.forw_L, gaussian_scale * self.gaussian_batch(zshape)), dim=1)
self.fake_H = self.netG(x=y_forw, rev=True)[:, :3, :, :]
self.netG.train()
def decolorize(self, img):
self.netG.eval()
with torch.no_grad():
Grey_img = self.netG(x=img)[:, :1, :, :]
Grey_img = self.Quantization(Grey_img)
self.netG.train()
return Grey_img
def colorize(self, Grey_img, gaussian_scale=0):
Lshape = Grey_img.shape
zshape = [Lshape[0], 2, Lshape[2], Lshape[3]]
y_ = torch.cat((Grey_img, gaussian_scale * self.gaussian_batch(zshape)), dim=1)
self.netG.eval()
with torch.no_grad():
img = self.netG(x=y_, rev=True)[:, :3, :, :]
self.netG.train()
return img
def get_current_log(self):
return self.log_dict
def get_current_visuals(self):
out_dict = OrderedDict()
out_dict['Grey_ref'] = self.ref_Grey.detach()[0].float().cpu()
out_dict['Color'] = self.fake_H.detach()[0].float().cpu()
out_dict['Grey'] = self.forw_L.detach()[0].float().cpu()
out_dict['GT'] = self.real_H.detach()[0].float().cpu()
return out_dict
def print_network(self):
s, n = self.get_network_description(self.netG)
if isinstance(self.netG, nn.DataParallel) or isinstance(self.netG, DistributedDataParallel):
net_struc_str = '{} - {}'.format(self.netG.__class__.__name__,
self.netG.module.__class__.__name__)
else:
net_struc_str = '{}'.format(self.netG.__class__.__name__)
if self.rank <= 0:
logger.info('Network G structure: {}, with parameters: {:,d}'.format(net_struc_str, n))
logger.info(s)
def load(self):
load_path_G = self.opt['path']['pretrain_model_G']
if load_path_G is not None:
logger.info('Loading model for G [{:s}] ...'.format(load_path_G))
self.load_network(load_path_G, self.netG, self.opt['path']['strict_load'])
def save(self, iter_label):
self.save_network(self.netG, 'G', iter_label)