3
3
4
4
# FCN32s
5
5
class fcn32s (nn .Module ):
6
-
7
6
def __init__ (self , n_classes = 21 , learned_billinear = False ):
8
7
super (fcn32s , self ).__init__ ()
9
8
self .learned_billinear = learned_billinear
@@ -14,14 +13,16 @@ def __init__(self, n_classes=21, learned_billinear=False):
14
13
nn .ReLU (inplace = True ),
15
14
nn .Conv2d (64 , 64 , 3 , padding = 1 ),
16
15
nn .ReLU (inplace = True ),
17
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
16
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
17
+ )
18
18
19
19
self .conv_block2 = nn .Sequential (
20
20
nn .Conv2d (64 , 128 , 3 , padding = 1 ),
21
21
nn .ReLU (inplace = True ),
22
22
nn .Conv2d (128 , 128 , 3 , padding = 1 ),
23
23
nn .ReLU (inplace = True ),
24
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
24
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
25
+ )
25
26
26
27
self .conv_block3 = nn .Sequential (
27
28
nn .Conv2d (128 , 256 , 3 , padding = 1 ),
@@ -30,7 +31,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
30
31
nn .ReLU (inplace = True ),
31
32
nn .Conv2d (256 , 256 , 3 , padding = 1 ),
32
33
nn .ReLU (inplace = True ),
33
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
34
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
35
+ )
34
36
35
37
self .conv_block4 = nn .Sequential (
36
38
nn .Conv2d (256 , 512 , 3 , padding = 1 ),
@@ -39,7 +41,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
39
41
nn .ReLU (inplace = True ),
40
42
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
41
43
nn .ReLU (inplace = True ),
42
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
44
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
45
+ )
43
46
44
47
self .conv_block5 = nn .Sequential (
45
48
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
@@ -48,7 +51,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
48
51
nn .ReLU (inplace = True ),
49
52
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
50
53
nn .ReLU (inplace = True ),
51
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
54
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
55
+ )
52
56
53
57
self .classifier = nn .Sequential (
54
58
nn .Conv2d (512 , 4096 , 7 ),
@@ -57,15 +61,15 @@ def __init__(self, n_classes=21, learned_billinear=False):
57
61
nn .Conv2d (4096 , 4096 , 1 ),
58
62
nn .ReLU (inplace = True ),
59
63
nn .Dropout2d (),
60
- nn .Conv2d (4096 , self .n_classes , 1 ),)
64
+ nn .Conv2d (4096 , self .n_classes , 1 ),
65
+ )
61
66
62
67
# TODO: Add support for learned upsampling
63
68
if self .learned_billinear :
64
69
raise NotImplementedError
65
70
# upscore = nn.ConvTranspose2d(self.n_classes, self.n_classes, 64, stride=32, bias=False)
66
71
# upscore.scale_factor = None
67
72
68
-
69
73
def forward (self , x ):
70
74
conv1 = self .conv_block1 (x )
71
75
conv2 = self .conv_block2 (conv1 )
@@ -79,19 +83,20 @@ def forward(self, x):
79
83
80
84
return out
81
85
82
-
83
86
def init_vgg16_params (self , vgg16 , copy_fc8 = True ):
84
- blocks = [self .conv_block1 ,
85
- self .conv_block2 ,
86
- self .conv_block3 ,
87
- self .conv_block4 ,
88
- self .conv_block5 ]
87
+ blocks = [
88
+ self .conv_block1 ,
89
+ self .conv_block2 ,
90
+ self .conv_block3 ,
91
+ self .conv_block4 ,
92
+ self .conv_block5 ,
93
+ ]
89
94
90
95
ranges = [[0 , 4 ], [5 , 9 ], [10 , 16 ], [17 , 23 ], [24 , 29 ]]
91
96
features = list (vgg16 .features .children ())
92
97
93
98
for idx , conv_block in enumerate (blocks ):
94
- for l1 , l2 in zip (features [ranges [idx ][0 ]: ranges [idx ][1 ]], conv_block ):
99
+ for l1 , l2 in zip (features [ranges [idx ][0 ] : ranges [idx ][1 ]], conv_block ):
95
100
if isinstance (l1 , nn .Conv2d ) and isinstance (l2 , nn .Conv2d ):
96
101
# print(idx, l1, l2)
97
102
assert l1 .weight .size () == l2 .weight .size ()
@@ -111,8 +116,8 @@ def init_vgg16_params(self, vgg16, copy_fc8=True):
111
116
l2 .weight .data = l1 .weight .data [:n_class , :].view (l2 .weight .size ())
112
117
l2 .bias .data = l1 .bias .data [:n_class ]
113
118
114
- class fcn16s (nn .Module ):
115
119
120
+ class fcn16s (nn .Module ):
116
121
def __init__ (self , n_classes = 21 , learned_billinear = False ):
117
122
super (fcn16s , self ).__init__ ()
118
123
self .learned_billinear = learned_billinear
@@ -123,14 +128,16 @@ def __init__(self, n_classes=21, learned_billinear=False):
123
128
nn .ReLU (inplace = True ),
124
129
nn .Conv2d (64 , 64 , 3 , padding = 1 ),
125
130
nn .ReLU (inplace = True ),
126
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
131
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
132
+ )
127
133
128
134
self .conv_block2 = nn .Sequential (
129
135
nn .Conv2d (64 , 128 , 3 , padding = 1 ),
130
136
nn .ReLU (inplace = True ),
131
137
nn .Conv2d (128 , 128 , 3 , padding = 1 ),
132
138
nn .ReLU (inplace = True ),
133
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
139
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
140
+ )
134
141
135
142
self .conv_block3 = nn .Sequential (
136
143
nn .Conv2d (128 , 256 , 3 , padding = 1 ),
@@ -139,7 +146,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
139
146
nn .ReLU (inplace = True ),
140
147
nn .Conv2d (256 , 256 , 3 , padding = 1 ),
141
148
nn .ReLU (inplace = True ),
142
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
149
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
150
+ )
143
151
144
152
self .conv_block4 = nn .Sequential (
145
153
nn .Conv2d (256 , 512 , 3 , padding = 1 ),
@@ -148,7 +156,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
148
156
nn .ReLU (inplace = True ),
149
157
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
150
158
nn .ReLU (inplace = True ),
151
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
159
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
160
+ )
152
161
153
162
self .conv_block5 = nn .Sequential (
154
163
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
@@ -157,7 +166,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
157
166
nn .ReLU (inplace = True ),
158
167
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
159
168
nn .ReLU (inplace = True ),
160
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
169
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
170
+ )
161
171
162
172
self .classifier = nn .Sequential (
163
173
nn .Conv2d (512 , 4096 , 7 ),
@@ -166,7 +176,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
166
176
nn .Conv2d (4096 , 4096 , 1 ),
167
177
nn .ReLU (inplace = True ),
168
178
nn .Dropout2d (),
169
- nn .Conv2d (4096 , self .n_classes , 1 ),)
179
+ nn .Conv2d (4096 , self .n_classes , 1 ),
180
+ )
170
181
171
182
self .score_pool4 = nn .Conv2d (512 , self .n_classes , 1 )
172
183
@@ -176,7 +187,6 @@ def __init__(self, n_classes=21, learned_billinear=False):
176
187
# upscore = nn.ConvTranspose2d(self.n_classes, self.n_classes, 64, stride=32, bias=False)
177
188
# upscore.scale_factor = None
178
189
179
-
180
190
def forward (self , x ):
181
191
conv1 = self .conv_block1 (x )
182
192
conv2 = self .conv_block2 (conv1 )
@@ -193,19 +203,20 @@ def forward(self, x):
193
203
194
204
return out
195
205
196
-
197
206
def init_vgg16_params (self , vgg16 , copy_fc8 = True ):
198
- blocks = [self .conv_block1 ,
199
- self .conv_block2 ,
200
- self .conv_block3 ,
201
- self .conv_block4 ,
202
- self .conv_block5 ]
207
+ blocks = [
208
+ self .conv_block1 ,
209
+ self .conv_block2 ,
210
+ self .conv_block3 ,
211
+ self .conv_block4 ,
212
+ self .conv_block5 ,
213
+ ]
203
214
204
215
ranges = [[0 , 4 ], [5 , 9 ], [10 , 16 ], [17 , 23 ], [24 , 29 ]]
205
216
features = list (vgg16 .features .children ())
206
217
207
218
for idx , conv_block in enumerate (blocks ):
208
- for l1 , l2 in zip (features [ranges [idx ][0 ]: ranges [idx ][1 ]], conv_block ):
219
+ for l1 , l2 in zip (features [ranges [idx ][0 ] : ranges [idx ][1 ]], conv_block ):
209
220
if isinstance (l1 , nn .Conv2d ) and isinstance (l2 , nn .Conv2d ):
210
221
# print(idx, l1, l2)
211
222
assert l1 .weight .size () == l2 .weight .size ()
@@ -224,9 +235,9 @@ def init_vgg16_params(self, vgg16, copy_fc8=True):
224
235
l2 .weight .data = l1 .weight .data [:n_class , :].view (l2 .weight .size ())
225
236
l2 .bias .data = l1 .bias .data [:n_class ]
226
237
238
+
227
239
# FCN 8s
228
240
class fcn8s (nn .Module ):
229
-
230
241
def __init__ (self , n_classes = 21 , learned_billinear = False ):
231
242
super (fcn8s , self ).__init__ ()
232
243
self .learned_billinear = learned_billinear
@@ -237,14 +248,16 @@ def __init__(self, n_classes=21, learned_billinear=False):
237
248
nn .ReLU (inplace = True ),
238
249
nn .Conv2d (64 , 64 , 3 , padding = 1 ),
239
250
nn .ReLU (inplace = True ),
240
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
251
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
252
+ )
241
253
242
254
self .conv_block2 = nn .Sequential (
243
255
nn .Conv2d (64 , 128 , 3 , padding = 1 ),
244
256
nn .ReLU (inplace = True ),
245
257
nn .Conv2d (128 , 128 , 3 , padding = 1 ),
246
258
nn .ReLU (inplace = True ),
247
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
259
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
260
+ )
248
261
249
262
self .conv_block3 = nn .Sequential (
250
263
nn .Conv2d (128 , 256 , 3 , padding = 1 ),
@@ -253,7 +266,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
253
266
nn .ReLU (inplace = True ),
254
267
nn .Conv2d (256 , 256 , 3 , padding = 1 ),
255
268
nn .ReLU (inplace = True ),
256
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
269
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
270
+ )
257
271
258
272
self .conv_block4 = nn .Sequential (
259
273
nn .Conv2d (256 , 512 , 3 , padding = 1 ),
@@ -262,7 +276,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
262
276
nn .ReLU (inplace = True ),
263
277
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
264
278
nn .ReLU (inplace = True ),
265
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
279
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
280
+ )
266
281
267
282
self .conv_block5 = nn .Sequential (
268
283
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
@@ -271,7 +286,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
271
286
nn .ReLU (inplace = True ),
272
287
nn .Conv2d (512 , 512 , 3 , padding = 1 ),
273
288
nn .ReLU (inplace = True ),
274
- nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),)
289
+ nn .MaxPool2d (2 , stride = 2 , ceil_mode = True ),
290
+ )
275
291
276
292
self .classifier = nn .Sequential (
277
293
nn .Conv2d (512 , 4096 , 7 ),
@@ -280,7 +296,8 @@ def __init__(self, n_classes=21, learned_billinear=False):
280
296
nn .Conv2d (4096 , 4096 , 1 ),
281
297
nn .ReLU (inplace = True ),
282
298
nn .Dropout2d (),
283
- nn .Conv2d (4096 , self .n_classes , 1 ),)
299
+ nn .Conv2d (4096 , self .n_classes , 1 ),
300
+ )
284
301
285
302
self .score_pool4 = nn .Conv2d (512 , self .n_classes , 1 )
286
303
self .score_pool3 = nn .Conv2d (256 , self .n_classes , 1 )
@@ -310,19 +327,20 @@ def forward(self, x):
310
327
311
328
return out
312
329
313
-
314
330
def init_vgg16_params (self , vgg16 , copy_fc8 = True ):
315
- blocks = [self .conv_block1 ,
316
- self .conv_block2 ,
317
- self .conv_block3 ,
318
- self .conv_block4 ,
319
- self .conv_block5 ]
331
+ blocks = [
332
+ self .conv_block1 ,
333
+ self .conv_block2 ,
334
+ self .conv_block3 ,
335
+ self .conv_block4 ,
336
+ self .conv_block5 ,
337
+ ]
320
338
321
339
ranges = [[0 , 4 ], [5 , 9 ], [10 , 16 ], [17 , 23 ], [24 , 29 ]]
322
340
features = list (vgg16 .features .children ())
323
341
324
342
for idx , conv_block in enumerate (blocks ):
325
- for l1 , l2 in zip (features [ranges [idx ][0 ]: ranges [idx ][1 ]], conv_block ):
343
+ for l1 , l2 in zip (features [ranges [idx ][0 ] : ranges [idx ][1 ]], conv_block ):
326
344
if isinstance (l1 , nn .Conv2d ) and isinstance (l2 , nn .Conv2d ):
327
345
assert l1 .weight .size () == l2 .weight .size ()
328
346
assert l1 .bias .size () == l2 .bias .size ()
0 commit comments