ckyrkou commited on
Commit
6bcd76a
·
verified ·
1 Parent(s): 86472ec

Upload model.py

Browse files
Files changed (1) hide show
  1. model.py +175 -14
model.py CHANGED
@@ -90,11 +90,76 @@ import math
90
  # init.orthogonal_(self.conv5.weight, init.calculate_gain('relu'))
91
  # init.orthogonal_(self.conv6.weight, init.calculate_gain('relu'))
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  class block(nn.Module):
94
  def __init__(self,channels_in,channels_out,kernel,stride,pad):
95
  super(block, self).__init__()
96
  self.conv = nn.Conv2d(channels_in, channels_out, kernel, stride, pad)
97
- self.act = nn.LeakyReLU(0.1)
 
98
  self.norm = nn.InstanceNorm2d(channels_out)
99
 
100
  def forward(self, x):
@@ -122,7 +187,7 @@ class SubPixelConvolutionalBlock(nn.Module):
122
  kernel_size=kernel_size, padding=kernel_size // 2)
123
  # These additional channels are shuffled to form additional pixels, upscaling each dimension by the scaling factor
124
  self.pixel_shuffle = nn.PixelShuffle(upscale_factor=scaling_factor)
125
- self.lrelu = nn.LeakyReLU(0.1)
126
 
127
  def forward(self, input):
128
  """
@@ -152,13 +217,12 @@ class Net(nn.Module):
152
  self.conv7_1 = block(32, 16, (3, 3), (1, 1), (1, 1))
153
  self.conv8 = nn.Conv2d(16, 3, (1, 1), (1, 1), (0, 0))
154
  #self.pixel_shuffle = nn.PixelShuffle(upscale_factor)
155
- self.relu = nn.ReLU()
156
  #self.upsample = nn.Upsample(scale_factor=2, mode='bicubic')
157
  self.spc1 = SubPixelConvolutionalBlock( kernel_size=3, n_channels=128, scaling_factor=2)
158
  #self.spc1 = nn.Upsample(scale_factor=2, mode='bicubic')
159
  self.spc2 = SubPixelConvolutionalBlock( kernel_size=3, n_channels=128, scaling_factor=2)
160
  #self.spc2 = nn.Upsample(scale_factor=2, mode='bicubic')
161
-
162
  self.spc3 = SubPixelConvolutionalBlock(kernel_size=3, n_channels=128, scaling_factor=4)
163
 
164
  #self._initialize_weights()
@@ -178,21 +242,118 @@ class Net(nn.Module):
178
  x = self.relu(self.conv8(x))
179
  return x
180
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  def _initialize_weights(self):
182
- init.orthogonal_(self.conv1.weight, init.calculate_gain('relu'))
183
- init.orthogonal_(self.conv2.weight, init.calculate_gain('relu'))
184
- init.orthogonal_(self.conv3.weight, init.calculate_gain('relu'))
185
- init.orthogonal_(self.conv4.weight, init.calculate_gain('relu'))
186
- init.orthogonal_(self.conv4_1.weight, init.calculate_gain('relu'))
187
- init.orthogonal_(self.conv5.weight, init.calculate_gain('relu'))
188
- init.orthogonal_(self.conv6.weight, init.calculate_gain('relu'))
189
- init.orthogonal_(self.conv7.weight, init.calculate_gain('relu'))
190
- init.orthogonal_(self.conv7_1.weight, init.calculate_gain('relu'))
191
- init.orthogonal_(self.conv8.weight, init.calculate_gain('relu'))
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
 
 
 
 
 
 
193
 
194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
  class ConvolutionalBlock(nn.Module):
198
  """
 
90
  # init.orthogonal_(self.conv5.weight, init.calculate_gain('relu'))
91
  # init.orthogonal_(self.conv6.weight, init.calculate_gain('relu'))
92
 
93
+ class ResidualBlockG(nn.Module):
94
+ def __init__(self, channels):
95
+ super(ResidualBlockG, self).__init__()
96
+ self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, padding=1)
97
+ self.bn1 = nn.BatchNorm2d(channels)
98
+ self.prelu = nn.ReLU()
99
+ self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1)
100
+ self.bn2 = nn.BatchNorm2d(channels)
101
+ def forward(self, x):
102
+ residual = self.conv1(x)
103
+ residual = self.bn1(residual)
104
+ residual = self.prelu(residual)
105
+ residual = self.conv2(residual)
106
+ residual = self.bn2(residual)
107
+ return x + residual
108
+
109
+ class UpsampleBlock(nn.Module):
110
+ def __init__(self, in_channels, up_scale):
111
+ super(UpsampleBlock, self).__init__()
112
+ self.conv = nn.Conv2d(in_channels, in_channels * up_scale ** 2,
113
+ kernel_size=3, padding=1)
114
+ self.pixel_shuffle = nn.PixelShuffle(up_scale)
115
+ self.prelu = nn.ReLU()
116
+ def forward(self, x):
117
+ x = self.conv(x)
118
+ x = self.pixel_shuffle(x)
119
+ x = self.prelu(x)
120
+ return x
121
+
122
+
123
+ class Generator(nn.Module):
124
+ def __init__(self, scale_factor):
125
+ super(Generator, self).__init__()
126
+ upsample_block_num = int(math.log(scale_factor, 2))
127
+
128
+ self.block1 = nn.Sequential(
129
+ nn.Conv2d(3, 64, kernel_size=9, padding=4),
130
+ nn.ReLU()
131
+ )
132
+
133
+ self.block2 = ResidualBlockG(64)
134
+ self.block3 = ResidualBlockG(64)
135
+ self.block4 = ResidualBlockG(64)
136
+ self.block5 = ResidualBlockG(64)
137
+ self.block6 = ResidualBlockG(64)
138
+ self.block7 = nn.Sequential(
139
+ nn.Conv2d(64, 64, kernel_size=3, padding=1),
140
+ nn.BatchNorm2d(64)
141
+ )
142
+ block8 = [UpsampleBlock(64, 2) for _ in range(upsample_block_num)]
143
+ block8.append(nn.Conv2d(64, 3, kernel_size=9, padding=4))
144
+ self.block8 = nn.Sequential(*block8)
145
+
146
+ def forward(self, x):
147
+ block1 = self.block1(x)
148
+ block2 = self.block2(block1)
149
+ block3 = self.block3(block2)
150
+ block4 = self.block4(block3)
151
+ block5 = self.block5(block4)
152
+ block6 = self.block6(block5)
153
+ block7 = self.block7(block6)
154
+ block8 = self.block8(block1 + block7)
155
+ return block8
156
+
157
  class block(nn.Module):
158
  def __init__(self,channels_in,channels_out,kernel,stride,pad):
159
  super(block, self).__init__()
160
  self.conv = nn.Conv2d(channels_in, channels_out, kernel, stride, pad)
161
+ #self.act = nn.LeakyReLU(0.02)
162
+ self.act = nn.ReLU()
163
  self.norm = nn.InstanceNorm2d(channels_out)
164
 
165
  def forward(self, x):
 
187
  kernel_size=kernel_size, padding=kernel_size // 2)
188
  # These additional channels are shuffled to form additional pixels, upscaling each dimension by the scaling factor
189
  self.pixel_shuffle = nn.PixelShuffle(upscale_factor=scaling_factor)
190
+ self.lrelu = nn.ReLU()
191
 
192
  def forward(self, input):
193
  """
 
217
  self.conv7_1 = block(32, 16, (3, 3), (1, 1), (1, 1))
218
  self.conv8 = nn.Conv2d(16, 3, (1, 1), (1, 1), (0, 0))
219
  #self.pixel_shuffle = nn.PixelShuffle(upscale_factor)
220
+ self.relu = nn.ReLU(inplace=True)
221
  #self.upsample = nn.Upsample(scale_factor=2, mode='bicubic')
222
  self.spc1 = SubPixelConvolutionalBlock( kernel_size=3, n_channels=128, scaling_factor=2)
223
  #self.spc1 = nn.Upsample(scale_factor=2, mode='bicubic')
224
  self.spc2 = SubPixelConvolutionalBlock( kernel_size=3, n_channels=128, scaling_factor=2)
225
  #self.spc2 = nn.Upsample(scale_factor=2, mode='bicubic')
 
226
  self.spc3 = SubPixelConvolutionalBlock(kernel_size=3, n_channels=128, scaling_factor=4)
227
 
228
  #self._initialize_weights()
 
242
  x = self.relu(self.conv8(x))
243
  return x
244
 
245
+ # def _initialize_weights(self):
246
+ # init.orthogonal_(self.conv1.weight, init.calculate_gain('relu'))
247
+ # init.orthogonal_(self.conv2.weight, init.calculate_gain('relu'))
248
+ # init.orthogonal_(self.conv3.weight, init.calculate_gain('relu'))
249
+ # init.orthogonal_(self.conv4.weight, init.calculate_gain('relu'))
250
+ # init.orthogonal_(self.conv4_1.weight, init.calculate_gain('relu'))
251
+ # init.orthogonal_(self.conv5.weight, init.calculate_gain('relu'))
252
+ # init.orthogonal_(self.conv6.weight, init.calculate_gain('relu'))
253
+ # init.orthogonal_(self.conv7.weight, init.calculate_gain('relu'))
254
+ # init.orthogonal_(self.conv7_1.weight, init.calculate_gain('relu'))
255
+ # init.orthogonal_(self.conv8.weight, init.calculate_gain('relu'))
256
+
257
  def _initialize_weights(self):
258
+ for m in self.modules():
259
+ if isinstance(m, nn.Conv2d):
260
+ # n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
261
+ # m.weight.data.normal_(0, math.sqrt(2. / n))
262
+ # g = nn.init.calculate_gain('leaky_relu', 0.1)
263
+ m.weight.data.normal_(0, 0.01)
264
+ # nn.init.xavier_normal_(m.weight,gai=g)
265
+ if m.bias is not None:
266
+ m.bias.data.zero_()
267
+ elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.InstanceNorm2d):
268
+ m.weight.data.fill_(1)
269
+ m.bias.data.zero_()
270
+ elif isinstance(m, nn.Linear):
271
+ m.weight.data.normal_(0, 0.01)
272
+ m.bias.data.zero_()
273
+ # self.sp.weight.data.normal_(0,0.01)
274
+
275
+
276
+ class dws_block(nn.Module):
277
+ def __init__(self, in_channels, out_channels, kernel_size=3, padding=1):
278
+ super(dws_block, self).__init__()
279
+ self.dc = nn.Conv2d(in_channels, in_channels, kernel_size=kernel_size, padding=padding, groups=in_channels)
280
+ self.pc = nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0)
281
 
282
+ def forward(self, x):
283
+ # Encoder
284
+ x = self.dc(x)
285
+ x = self.pc(x)
286
+
287
+ return x
288
 
289
 
290
+ class UNet(nn.Module):
291
+ def __init__(self, in_channels, out_channels):
292
+ super(UNet, self).__init__()
293
+
294
+ # Encoder (contracting path)
295
+ self.encoder1 = self.contracting_block(in_channels, 64)
296
+ self.encoder2 = self.contracting_block(64, 128)
297
+ self.encoder3 = self.contracting_block(128, 256)
298
+ self.encoder4 = self.contracting_block(256, 512)
299
+
300
+ # Bottleneck
301
+ self.bottleneck = nn.Sequential(
302
+ dws_block(512, 1024, kernel_size=3, padding=1),
303
+ nn.ReLU(inplace=True),
304
+ dws_block(1024, 1024, kernel_size=3, padding=1),
305
+ nn.ReLU(inplace=True)
306
+ )
307
+
308
+ # Decoder (expansive path)
309
+ self.decoder1 = self.expansive_block(1024, 512)
310
+ self.decoder2 = self.expansive_block(512, 256)
311
+ self.decoder3 = self.expansive_block(256, 128)
312
+ self.decoder4 = self.expansive_block(128, 64)
313
+ self.decoder5 = self.expansive_block(32, 32)
314
+ self.decoder6 = self.expansive_block(16, 32)
315
+
316
+ # Output layer
317
+ self.final_conv = nn.Conv2d(16, out_channels, kernel_size=1)
318
+
319
+ def contracting_block(self, in_channels, out_channels):
320
+ return nn.Sequential(
321
+ dws_block(in_channels, out_channels, kernel_size=3, padding=1),
322
+ nn.ReLU(inplace=True),
323
+ dws_block(out_channels, out_channels, kernel_size=3, padding=1),
324
+ nn.ReLU(inplace=True),
325
+ nn.MaxPool2d(kernel_size=2, stride=2)
326
+ )
327
+
328
+ def expansive_block(self, in_channels, out_channels):
329
+ return nn.Sequential(
330
+ dws_block(in_channels, out_channels, kernel_size=3, padding=1),
331
+ nn.ReLU(inplace=True),
332
+ dws_block(out_channels, out_channels, kernel_size=3, padding=1),
333
+ nn.ReLU(inplace=True),
334
+ nn.ConvTranspose2d(out_channels, out_channels // 2, kernel_size=2, stride=2)
335
+ )
336
 
337
+ def forward(self, x):
338
+ # Encoder
339
+ enc1 = self.encoder1(x)
340
+ enc2 = self.encoder2(enc1)
341
+ enc3 = self.encoder3(enc2)
342
+ enc4 = self.encoder4(enc3)
343
+
344
+ # Bottleneck
345
+ bottleneck = self.bottleneck(enc4)
346
+
347
+ # Decoder
348
+ dec1 = self.decoder1(bottleneck)
349
+ dec2 = self.decoder2(torch.cat([dec1, enc3], dim=1))
350
+ dec3 = self.decoder3(torch.cat([dec2, enc2], dim=1))
351
+ dec4 = self.decoder4(torch.cat([dec3, enc1], dim=1))
352
+ dec5 = self.decoder5(dec4)
353
+ dec6 = self.decoder6(dec5)
354
+ # Output layer
355
+ output = self.final_conv(dec6)
356
+ return output
357
 
358
  class ConvolutionalBlock(nn.Module):
359
  """