-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_KinD_color.py
364 lines (294 loc) · 13.2 KB
/
model_KinD_color.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
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
import os
import time
import random
from PIL import Image
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
from tqdm import tqdm
from utils import MSE, SSIM, PSNR, LPIPS
from torch.utils.tensorboard import SummaryWriter
import atexit
import logging
class Restoration_net(nn.Module):
def __init__(self, inchannel=4, kernel_size=(3, 3), padding=(1, 1)):
super(Restoration_net, self).__init__()
self.lrelu = nn.LeakyReLU(0.2)
self.restore_conv1 = nn.Sequential(
nn.Conv2d(inchannel, 32, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu,
nn.Conv2d(32, 32, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu
)
self.restore_conv2 = nn.Sequential(
nn.MaxPool2d((2, 2), (2, 2)),
nn.Conv2d(32, 64, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(64, 64, kernel_size, padding=padding), self.lrelu
)
self.restore_conv3 = nn.Sequential(
nn.MaxPool2d((2, 2), (2, 2)),
nn.Conv2d(64, 128, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(128, 128, kernel_size, padding=padding), self.lrelu
)
self.restore_conv4 = nn.Sequential(
nn.MaxPool2d((2, 2), (2, 2)),
nn.Conv2d(128, 256, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(256, 256, kernel_size, padding=padding), self.lrelu
)
self.restore_conv5 = nn.Sequential(
nn.MaxPool2d((2, 2), (2, 2)),
nn.Conv2d(256, 512, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(512, 512, kernel_size, padding=padding), self.lrelu
)
self.restore_upsample1 = nn.ConvTranspose2d(512, 256, (2, 2), (2, 2))
self.restore_upsample2 = nn.ConvTranspose2d(256, 128, (2, 2), (2, 2))
self.restore_upsample3 = nn.ConvTranspose2d(128, 64, (2, 2), (2, 2))
self.restore_upsample4 = nn.ConvTranspose2d(64, 32, (2, 2), (2, 2))
self.restore_deconv1 = nn.Sequential(
nn.Conv2d(512, 256, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(256, 256, kernel_size, padding=padding), self.lrelu
)
self.restore_deconv2 = nn.Sequential(
nn.Conv2d(256, 128, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(128, 128, kernel_size, padding=padding), self.lrelu
)
self.restore_deconv3 = nn.Sequential(
nn.Conv2d(128, 64, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(64, 64, kernel_size, padding=padding), self.lrelu
)
self.restore_deconv4 = nn.Sequential(
nn.Conv2d(64, 32, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(32, 32, kernel_size, padding=padding), self.lrelu,
nn.Conv2d(32, 3, kernel_size, padding=padding), nn.Sigmoid()
)
def forward(self, input_R, input_L):
input_all = torch.cat((input_R, input_L), dim=1)
conv1 = self.restore_conv1(input_all)
conv2 = self.restore_conv2(conv1) # downsample * 2
conv3 = self.restore_conv3(conv2)
conv4 = self.restore_conv4(conv3)
conv5 = self.restore_conv5(conv4)
up1 = torch.cat((self.restore_upsample1(conv5), conv4), dim=1)
deconv1 = self.restore_deconv1(up1)
up2 = torch.cat((self.restore_upsample2(deconv1), conv3), dim=1)
deconv2 = self.restore_deconv2(up2)
up3 = torch.cat((self.restore_upsample3(deconv2), conv2), dim=1)
deconv3 = self.restore_deconv3(up3)
up4 = torch.cat((self.restore_upsample4(deconv3), conv1), dim=1)
deconv4 = self.restore_deconv4(up4)
return deconv4
class DecomNet(nn.Module):
def __init__(self, channel=32, kernel_size=(3, 3), padding=(1, 1)):
super(DecomNet, self).__init__()
# Shallow feature extraction
self.lrelu = nn.LeakyReLU(0.2)
# Decom R
self.decom_conv1 = nn.Sequential(
nn.Conv2d(3, channel, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu
)
self.decom_conv2 = nn.Sequential(
nn.MaxPool2d((2, 2), (2, 2)),
nn.Conv2d(channel, channel*2, kernel_size, padding=padding), self.lrelu
)
self.decom_conv3 = nn.Sequential(
nn.MaxPool2d((2, 2), (2, 2)),
nn.Conv2d(channel*2, channel*4, kernel_size, padding=padding), self.lrelu
)
self.decom_upsample1 = nn.ConvTranspose2d(channel*4, channel*2, (2, 2), (2, 2))
self.decom_conv4 = nn.Sequential(
nn.Conv2d(channel*4, channel*2, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu
)
self.decom_upsample2 = nn.ConvTranspose2d(channel*2, channel, (2, 2), (2, 2))
self.decom_conv5 = nn.Sequential(
nn.Conv2d(channel*2, channel, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu
)
self.decom_out_R = nn.Sequential(
nn.Conv2d(channel, 3, (1, 1)), nn.Sigmoid()
)
# Decom I
self.decom_conv6 = nn.Sequential(
nn.Conv2d(channel, channel, kernel_size, padding=padding), self.lrelu
)
self.decom_out_I = nn.Sequential(
nn.Conv2d(channel * 2, 1, (1, 1)), nn.Sigmoid()
)
def forward(self, input_im):
conv1 = self.decom_conv1(input_im)
conv2 = self.decom_conv2(conv1)
conv3 = self.decom_conv3(conv2)
up1 = torch.cat((self.decom_upsample1(conv3), conv2), dim=1)
conv4 = self.decom_conv4(up1)
up2 = torch.cat((self.decom_upsample2(conv4), conv1), dim=1)
conv5 = self.decom_conv5(up2)
out_R = self.decom_out_R(conv5)
conv6 = self.decom_conv6(conv1)
out_I = self.decom_out_I(torch.cat((conv6, conv5), dim=1))
return out_R, out_I
# KinD RelightNet
class RelightNet(nn.Module):
def __init__(self, channel=32, kernel_size=(3, 3), padding=(1, 1)):
super(RelightNet, self).__init__()
self.lrelu = nn.LeakyReLU(0.2)
self.relight_conv6 = nn.Sequential(
nn.Conv2d(2, channel, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu,
nn.Conv2d(channel, channel, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu
)
self.relight_conv7 = nn.Sequential(
nn.Conv2d(channel, channel, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu,
nn.Conv2d(channel, channel, kernel_size, padding=padding, padding_mode='replicate'), self.lrelu,
nn.Conv2d(channel, 1, (1, 1))
)
self.out = nn.Sigmoid()
def forward(self, input_I, input_R, ratio):
input_L = torch.cat((input_I, ratio), dim=1)
conv6 = self.relight_conv6(input_L)
conv7 = self.relight_conv7(conv6)
out = self.out(conv7)
return out
class DenoiseNet_No_Seg(nn.Module):
def __init__(self, is_training=True, layer_num=15, channel=64):
super(DenoiseNet_No_Seg, self).__init__()
self.rdn = RDN_No_Seg(3, 64)
def forward(self, input_R, input_I):
out_R = self.rdn(input_R, input_I)
return out_R
class RDN_No_Seg(nn.Module):
def __init__(self, nChannel, nfeat, nResBlock=4, growthRate=0):
super(RDN_No_Seg, self).__init__()
self.conv1 = nn.Conv2d(4, nfeat, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(nfeat, nfeat, kernel_size=3, padding=1)
self.RDB1 = RDB(nfeat, nfeat, nResBlock)
self.RIRB1 = RIRB_No_Seg(nfeat, nfeat)
self.block1 = nn.Sequential(
RDB(nfeat, nfeat, nResBlock), RDB(nfeat, nfeat, nResBlock)
)
self.RDB2 = RDB(2 * nfeat, nfeat, nResBlock)
self.RIRB2 = RIRB_No_Seg(nfeat, nfeat)
self.block2 = nn.Sequential(
RDB(nfeat, nfeat, nResBlock), RDB(nfeat, nfeat, nResBlock)
)
self.conv3 = nn.Conv2d(3 * nfeat, nfeat, kernel_size=1, padding=0)
self.conv4 = nn.Conv2d(nfeat, nfeat, kernel_size=3, padding=1)
self.block3 = nn.Sequential(
nn.Conv2d(nfeat, nfeat, kernel_size=3, padding=1), nn.LeakyReLU(0.1),
nn.Conv2d(nfeat, nChannel, kernel_size=3, padding=1)
)
def forward(self, input_R, input_I):
input_all = torch.cat([input_R, input_I], dim=1)
F_ = self.conv1(input_all)
F_0 = self.RIRB1(self.RDB1(self.conv2(F_)))
# F_0 = self.RDB1(self.conv2(F_))
F_4 = self.block1(F_0)
F_F1 = torch.cat([F_4, F_], dim=1)
F_F1 = self.RIRB2(self.RDB2(F_F1))
# F_F1 = self.RDB2(F_F1)
F_8 = self.block2(F_F1)
FF = torch.cat([F_0, F_F1, F_8], dim=1)
FGF = self.conv4(self.conv3(FF))
out = self.block3(FGF)
return out
class RIRB_No_Seg(nn.Module):
def __init__(self, inchannel, nChannels):
super(RIRB_No_Seg, self).__init__()
self.conv1 = nn.Conv2d(inchannel, nChannels, kernel_size=1, padding=0)
self.resblocks1 = nn.ModuleList()
for i in range(2):
self.resblocks1.append(ResidualBlock(nChannels, nChannels))
self.conv2 = nn.Conv2d(nChannels, nChannels, kernel_size=1, padding=0, bias=False)
self.conv3 = nn.Conv2d(nChannels, nChannels, kernel_size=1, padding=0)
self.resblocks2 = nn.ModuleList()
for i in range(2):
self.resblocks2.append(ResidualBlock(nChannels, nChannels))
self.conv4 = nn.Conv2d(inchannel + nChannels, nChannels, kernel_size=1, padding=0)
def forward(self, input_R):
out = self.conv1(input_R)
for resblock1 in self.resblocks1:
out = resblock1(out)
out = self.conv2(out)
out = self.conv3(out)
for resblock2 in self.resblocks2:
out = resblock2(out)
out = torch.cat([out, input_R], dim=1)
out = self.conv4(out)
return out
class RDB(nn.Module):
def __init__(self, inchannel, nChannels, nResBlock):
super(RDB, self).__init__()
self.conv1 = nn.Conv2d(inchannel, nChannels, kernel_size=1, padding=0)
self.resblocks = nn.ModuleList()
for i in range(nResBlock):
self.resblocks.append(ResidualBlock(nChannels, nChannels))
self.conv2 = nn.Conv2d(inchannel + nChannels, nChannels, kernel_size=1, padding=0)
def forward(self, input_R):
out = self.conv1(input_R)
for resblock in self.resblocks:
out = resblock(out)
out = torch.cat([out, input_R], dim=1)
out = self.conv2(out)
return out
# Residual block
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, downsample=None, norm=None,
BN_momentum=0.1):
super(ResidualBlock, self).__init__()
bias = False if norm == 'BN' else True
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=bias)
self.norm = norm
if norm == 'BN':
self.bn1 = nn.BatchNorm2d(out_channels, momentum=BN_momentum)
self.bn2 = nn.BatchNorm2d(out_channels, momentum=BN_momentum)
elif norm == 'IN':
self.bn1 = nn.InstanceNorm2d(out_channels)
self.bn2 = nn.InstanceNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=bias)
self.downsample = downsample
def forward(self, x):
residual = x
out = self.conv1(x)
if self.norm in ['BN', 'IN']:
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
if self.norm in ['BN', 'IN']:
out = self.bn2(out)
if self.downsample:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class Mymodel(nn.Module):
def __init__(self, gpu):
super(Mymodel, self).__init__()
self.DecomNet = DecomNet()
self.RelightNet = RelightNet()
self.RestoreNet = Restoration_net()
# self.DenoiseNet = DenoiseNet_No_Seg()
if gpu is not None:
gpu = 0
self.gpu = torch.device('cuda:' + str(gpu))
def forward(self, input_low, input_high, ratio):
# Forward DecompNet
R_low, I_low = self.DecomNet(input_low)
R_high, I_high = self.DecomNet(input_high)
# Forward RestoreNet
R_restore = self.RestoreNet(R_low, I_low)
# Forward DenoiseNet
# R_denoise = self.DenoiseNet(R_low, I_low)
# Forward RelightNet
I_delta = self.RelightNet(I_low, R_low, ratio)
# Other variables
I_low_3 = torch.cat((I_low, I_low, I_low), dim=1)
I_high_3 = torch.cat((I_high, I_high, I_high), dim=1)
I_delta_3 = torch.cat((I_delta, I_delta, I_delta), dim=1)
R_denoise = R_restore
return input_low, input_high, R_low, I_low, R_high, I_high, I_delta, I_low_3, I_high_3, I_delta_3, R_denoise
def summary(self):
"""
Model summary
"""
model_parameters = filter(lambda p: p.requires_grad, self.parameters())
params = sum([np.prod(p.size()) for p in model_parameters])
print('Trainable parameters: {}'.format(params))
print(self)