-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel.py
More file actions
385 lines (311 loc) · 13.3 KB
/
model.py
File metadata and controls
385 lines (311 loc) · 13.3 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import torch
import torch.nn as nn
class ResidualBlock(nn.Module):
def __init__(self, in_features):
super(ResidualBlock, self).__init__()
self.block = nn.Sequential(
nn.ReflectionPad2d(1),
nn.Conv2d(in_features, in_features, 3),
nn.InstanceNorm2d(in_features),
nn.ReLU(inplace=True),
nn.ReflectionPad2d(1),
nn.Conv2d(in_features, in_features, 3),
nn.InstanceNorm2d(in_features),
)
def forward(self, x):
return x + self.block(x)
class ResNet_old(nn.Module):
def __init__(self, input_shape, num_residual_blocks):
super(ResNet_old, self).__init__()
channels = input_shape[0]
# Initial convolution block
out_features = 64
model = [
nn.ReflectionPad2d(channels),
nn.Conv2d(channels, out_features, 7),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
# Residual blocks
for _ in range(num_residual_blocks):
model += [ResidualBlock(out_features)]
# Output layer
# model += [nn.ReflectionPad2d(channels), nn.Conv2d(out_features, channels, 9), nn.Tanh()]
model += [nn.ReflectionPad2d(channels), nn.Conv2d(out_features, channels, 7), nn.ReLU()]
self.model = nn.Sequential(*model)
def forward(self, x):
return self.model(x)
class ResNet(nn.Module):
def __init__(self, input_shape, num_residual_blocks):
super(ResNet, self).__init__()
channels = input_shape[0]
# Initial convolution block
out_features = 64
model = [
nn.ReflectionPad2d(channels),
nn.Conv2d(channels, out_features, channels * 2 + 1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
in_features = out_features
# Downsampling
for _ in range(2):
out_features *= 2
model += [
nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
in_features = out_features
# Residual blocks
for _ in range(num_residual_blocks):
model += [ResidualBlock(out_features)]
# Upsampling
for _ in range(2):
out_features //= 2
model += [
nn.Upsample(scale_factor=2),
nn.Conv2d(in_features, out_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
in_features = out_features
# Output layer
# model += [nn.ReflectionPad2d(channels), nn.Conv2d(out_features, channels, 9), nn.Tanh()]
model += [nn.ReflectionPad2d(channels), nn.Conv2d(out_features, channels, channels * 2 + 1), nn.ReLU()]
self.model = nn.Sequential(*model)
def forward(self, x):
return self.model(x)
class UNetDown(nn.Module):
def __init__(self, in_size, out_size, normalize=True, dropout=0.0):
super(UNetDown, self).__init__()
layers = [nn.Conv2d(in_size, out_size, 4, 2, 1, bias=False)]
if normalize:
layers.append(nn.InstanceNorm2d(out_size))
layers.append(nn.LeakyReLU(0.2))
if dropout:
layers.append(nn.Dropout(dropout))
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
class UNetUp(nn.Module):
def __init__(self, in_size, out_size, dropout=0.0):
super(UNetUp, self).__init__()
layers = [
nn.ConvTranspose2d(in_size, out_size, 4, 2, 1, bias=False),
nn.InstanceNorm2d(out_size),
nn.ReLU(inplace=True),
]
if dropout:
layers.append(nn.Dropout(dropout))
self.model = nn.Sequential(*layers)
def forward(self, x, skip_input):
x = self.model(x)
x = torch.cat((x, skip_input), 1)
return x
class UNet(nn.Module):
def __init__(self, in_channels=3, out_channels=3):
super(UNet, self).__init__()
self.down1 = UNetDown(in_channels, 64, normalize=False)
self.down2 = UNetDown(64, 128)
self.down3 = UNetDown(128, 256)
self.down4 = UNetDown(256, 512, dropout=0.5)
self.down5 = UNetDown(512, 512, dropout=0.5)
self.down6 = UNetDown(512, 512, dropout=0.5)
self.down7 = UNetDown(512, 512, dropout=0.5)
self.down8 = UNetDown(512, 512, normalize=False, dropout=0.5)
self.up1 = UNetUp(512, 512, dropout=0.5)
self.up2 = UNetUp(1024, 512, dropout=0.5)
self.up3 = UNetUp(1024, 512, dropout=0.5)
self.up4 = UNetUp(1024, 512, dropout=0.5)
self.up5 = UNetUp(1024, 256)
self.up6 = UNetUp(512, 128)
self.up7 = UNetUp(256, 64)
self.final = nn.Sequential(
nn.Upsample(scale_factor=2),
nn.ZeroPad2d((1, 0, 1, 0)),
nn.Conv2d(128, out_channels, 4, padding=1),
# nn.Tanh(),
nn.ReLU(),
)
def forward(self, x):
# U-Net generator with skip connections from encoder to decoder
d1 = self.down1(x)
d2 = self.down2(d1)
d3 = self.down3(d2)
d4 = self.down4(d3)
d5 = self.down5(d4)
d6 = self.down6(d5)
d7 = self.down7(d6)
d8 = self.down8(d7)
u1 = self.up1(d8, d7)
u2 = self.up2(u1, d6)
u3 = self.up3(u2, d5)
u4 = self.up4(u3, d4)
u5 = self.up5(u4, d3)
u6 = self.up6(u5, d2)
u7 = self.up7(u6, d1)
return self.final(u7)
class GeneratorResNet(nn.Module):
def __init__(self, input_shape, num_residual_blocks):
super(GeneratorResNet, self).__init__()
channels = input_shape[0]
# Initial convolution block
out_features = 64
model = [
nn.ReflectionPad2d(channels),
nn.Conv2d(channels, out_features, 9),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
in_features = out_features
# Downsampling
for _ in range(2):
out_features *= 2
model += [
nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
in_features = out_features
# Residual blocks
for _ in range(num_residual_blocks):
model += [ResidualBlock(out_features)]
# Upsampling
for _ in range(2):
out_features //= 2
model += [
nn.Upsample(scale_factor=2),
nn.Conv2d(in_features, out_features, 3, stride=1, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True),
]
in_features = out_features
# Output layer
model += [nn.ReflectionPad2d(channels), nn.Conv2d(out_features, channels, 9), nn.Tanh()]
self.model = nn.Sequential(*model)
def forward(self, x):
torch.cuda.empty_cache()
return self.model(x)
##############################
# Discriminator
##############################
class Discriminator(nn.Module):
def __init__(self, input_shape):
super(Discriminator, self).__init__()
channels, height, width = input_shape
# Calculate output shape of image discriminator (PatchGAN)
self.output_shape = (1, height // 2 ** 4, width // 2 ** 4)
def discriminator_block(in_filters, out_filters, normalize=True):
"""Returns downsampling layers of each discriminator block"""
layers = [nn.Conv2d(in_filters, out_filters, 4, stride=2, padding=1)]
if normalize:
layers.append(nn.InstanceNorm2d(out_filters))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
self.model = nn.Sequential(
*discriminator_block(channels, 64, normalize=False),
*discriminator_block(64, 128),
*discriminator_block(128, 256),
*discriminator_block(256, 512),
nn.ZeroPad2d((1, 0, 1, 0)),
nn.Conv2d(512, 1, 4, padding=1)
)
def forward(self, img):
return self.model(img)
class UwUNet(torch.nn.Module):
def __init__(self, input_channel=4, output_channel=4, intermediate_channel=100, multi_channel=32, depth=4):
super().__init__()
mult_chan = multi_channel
depth = depth
starting_chan = input_channel
intermediate_chan = intermediate_channel
final_chan = output_channel
self.spec_conv = SubNet2Conv(starting_chan,intermediate_chan) #First Spectral Channel Convoultions
self.spec_down_conv = SubNet2Conv(intermediate_chan, final_chan) #Second spectral Channel Convolutions
self.spec_down_down_conv = SubNet2Conv(final_chan,1) #Third Spectral Channel Convolution to reduce to 1 channel
self.net_recurse = _Net_recurse(n_in_channels=1, mult_chan=mult_chan, depth=depth) #Spatial U-net defined below (4 layers deep)
self.conv_out = torch.nn.Conv2d(mult_chan, 1, kernel_size=3, padding=1) #Conv of 32 -> 1 -> 1 channels post spatial U-Net
self.spec_convt = torch.nn.ConvTranspose2d(1, final_chan, kernel_size=3, padding=1) #Transpose convolution to return to desired channel size
self.spec_bn = torch.nn.BatchNorm2d(final_chan) #batchnorm for transpose conv
self.spec_relu = torch.nn.ReLU() #ReLu for transpose conv
self.spec_final = SubNet2Conv(2*final_chan, final_chan) #Final convolution after concatenation of pre and post unet stacks
self.spec_final_pool = SubNet2Conv(final_chan, 1) #pooling convolution to take you to single channel output
def forward(self, x):
x_spec_conv = self.spec_conv(x)
x_spec_down = self.spec_down_conv(x_spec_conv)
x_list = list(torch.split(x_spec_down, 1, 1))
#x_spec_down_down = self.spec_down_down_conv(x_spec_down)
for chan in x_list:
sing_chan_spat = self.net_recurse(chan)
sing_chan_spat = self.conv_out(sing_chan_spat)
x_spec_down = torch.cat((x_spec_down,sing_chan_spat),1)
x_spec_prepool = self.spec_final(x_spec_down)
#if final_chan == 1 use this return statement
#return torch.squeeze(self.spec_final_pool(x_spec_prepool),1) #This corrects an error in bufferedpatchdataset.py that doesn't like the mismatch of tensor dimensions
#otherwise (i.e. final_chan > 1) use this return statement
return x_spec_prepool
class _Net_recurse(torch.nn.Module):
def __init__(self, n_in_channels, mult_chan=2, depth=0):
"""Class for recursive definition of U-network.p
Parameters:
in_channels - (int) number of channels for input.
mult_chan - (int) factor to determine number of output channels
depth - (int) if 0, this subnet will only be convolutions that double the channel count.
"""
super().__init__()
self.depth = depth
n_out_channels = n_in_channels*mult_chan
self.sub_2conv_more = SubNet2Conv(n_in_channels, n_out_channels)
if depth > 0:
self.sub_2conv_less = SubNet2Conv(2*n_out_channels, n_out_channels)
self.conv_down = torch.nn.Conv2d(n_out_channels, n_out_channels, 2, stride=2)
self.bn0 = torch.nn.BatchNorm2d(n_out_channels)
self.relu0 = torch.nn.ReLU()
self.convt = torch.nn.ConvTranspose2d(2*n_out_channels, n_out_channels, kernel_size=2, stride=2)
self.bn1 = torch.nn.BatchNorm2d(n_out_channels)
self.relu1 = torch.nn.ReLU()
self.sub_u = _Net_recurse(n_out_channels, mult_chan=2, depth=(depth - 1))
def forward(self, x):
if self.depth == 0:
return self.sub_2conv_more(x)
else: # depth > 0
x_2conv_more = self.sub_2conv_more(x)
x_conv_down = self.conv_down(x_2conv_more)
x_bn0 = self.bn0(x_conv_down)
x_relu0 = self.relu0(x_bn0)
x_sub_u = self.sub_u(x_relu0)
x_convt = self.convt(x_sub_u)
x_bn1 = self.bn1(x_convt)
x_relu1 = self.relu1(x_bn1)
x_cat = torch.cat((x_2conv_more, x_relu1), 1) # concatenate
x_2conv_less = self.sub_2conv_less(x_cat)
return x_2conv_less
class SubNet2Conv(torch.nn.Module):
def __init__(self, n_in, n_out):
super().__init__()
self.conv1 = torch.nn.Conv2d(n_in, n_out, kernel_size=3, padding=1)
self.bn1 = torch.nn.BatchNorm2d(n_out)
self.relu1 = torch.nn.ReLU()
self.conv2 = torch.nn.Conv2d(n_out, n_out, kernel_size=3, padding=1)
self.bn2 = torch.nn.BatchNorm2d(n_out)
self.relu2 = torch.nn.ReLU()
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
return x
class SubNet2Convt(torch.nn.Module):
def __init__(self, n_in, n_out):
super().__init__()
self.convt1 = torch.nn.ConvTranspose2d(n_in, n_out, kernel_size=3, padding=1)
self.bn1 = torch.nn.BatchNorm2d(n_out)
self.relu1 = torch.nn.ReLU()
def forward(self, x):
x = self.convt1(x)
x = self.bn1(x)
x = self.relu1(x)
return x