-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_deeplab.py
executable file
·545 lines (461 loc) · 26 KB
/
auto_deeplab.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
from typing import Optional
import torch.nn as nn
import cell_level_search
from genotypes import PRIMITIVES
import torch.nn.functional as F
from operations import *
from decoding_formulas import Decoder
from torchvision import models
from torchvision.models._utils import IntermediateLayerGetter
class AutoDeeplab(nn.Module):
def __init__(self, num_classes, num_layers, criterion=None, filter_multiplier=8, block_multiplier=5, step=5, cell=cell_level_search.Cell, num_bands: int = 3, backbone_module: Optional[dict] = None):
super(AutoDeeplab, self).__init__()
self.cells = nn.ModuleList()
self._num_layers = num_layers
self._num_classes = num_classes
self._step = step
self._block_multiplier = block_multiplier
self._filter_multiplier = filter_multiplier
self._criterion = criterion
self._initialize_alphas_betas()
self.num_bands = num_bands
f_initial = int(self._filter_multiplier)
half_f_initial = int(f_initial / 2)
if backbone_module:
# Replace stride with dilation to make the model fit for use as backbone (important!)
backbone = models.resnet50(
replace_stride_with_dilation=(False, True, True))
# Modify first layer of backbone to accept specified number of bands
bias = False if backbone.conv1.bias is None else True
backbone.conv1 = nn.Conv2d(
in_channels=num_bands,
out_channels=backbone.conv1.out_channels,
kernel_size=backbone.conv1.kernel_size,
stride=backbone.conv1.stride,
padding=backbone.conv1.padding,
bias=bias)
# Modify last layer of backbone to output specified number of classes
bias = False if backbone.fc.bias is None else True
backbone.fc = nn.Linear(
in_features=backbone.fc.in_features,
out_features=num_classes,
bias=bias)
backbone.load_state_dict(backbone_module['model_state_dict'])
backbone = IntermediateLayerGetter(
backbone, return_layers={"layer4": "out"})
self.backbone = backbone
self.stem2 = nn.Sequential(
nn.Conv2d(2048,
f_initial * self._block_multiplier, 3, stride=2, padding=1),
nn.BatchNorm2d(f_initial * self._block_multiplier),
nn.ReLU()
)
else:
self.backbone = None
self.stem0 = nn.Sequential(
nn.Conv2d(num_bands, half_f_initial * self._block_multiplier,
3, stride=2, padding=1),
nn.BatchNorm2d(half_f_initial * self._block_multiplier),
nn.ReLU()
)
self.stem1 = nn.Sequential(
nn.Conv2d(half_f_initial * self._block_multiplier,
half_f_initial * self._block_multiplier, 3, stride=1, padding=1),
nn.BatchNorm2d(half_f_initial * self._block_multiplier),
nn.ReLU()
)
# handle the case where solis backbone is used
if not backbone_module:
self.stem2 = nn.Sequential(
nn.Conv2d(half_f_initial * self._block_multiplier,
f_initial * self._block_multiplier, 3, stride=2, padding=1),
nn.BatchNorm2d(f_initial * self._block_multiplier),
nn.ReLU()
)
# intitial_fm = C_initial
for i in range(self._num_layers):
if i == 0:
cell1 = cell(self._step, self._block_multiplier, -1,
None, f_initial, None,
self._filter_multiplier)
cell2 = cell(self._step, self._block_multiplier, -1,
f_initial, None, None,
self._filter_multiplier * 2)
self.cells += [cell1]
self.cells += [cell2]
elif i == 1:
cell1 = cell(self._step, self._block_multiplier, f_initial,
None, self._filter_multiplier, self._filter_multiplier * 2,
self._filter_multiplier)
cell2 = cell(self._step, self._block_multiplier, -1,
self._filter_multiplier, self._filter_multiplier * 2, None,
self._filter_multiplier * 2)
cell3 = cell(self._step, self._block_multiplier, -1,
self._filter_multiplier * 2, None, None,
self._filter_multiplier * 4)
self.cells += [cell1]
self.cells += [cell2]
self.cells += [cell3]
elif i == 2:
cell1 = cell(self._step, self._block_multiplier, self._filter_multiplier,
None, self._filter_multiplier, self._filter_multiplier * 2,
self._filter_multiplier)
cell2 = cell(self._step, self._block_multiplier, self._filter_multiplier * 2,
self._filter_multiplier, self._filter_multiplier * 2, self._filter_multiplier * 4,
self._filter_multiplier * 2)
cell3 = cell(self._step, self._block_multiplier, -1,
self._filter_multiplier * 2, self._filter_multiplier * 4, None,
self._filter_multiplier * 4)
cell4 = cell(self._step, self._block_multiplier, -1,
self._filter_multiplier * 4, None, None,
self._filter_multiplier * 8)
self.cells += [cell1]
self.cells += [cell2]
self.cells += [cell3]
self.cells += [cell4]
elif i == 3:
cell1 = cell(self._step, self._block_multiplier, self._filter_multiplier,
None, self._filter_multiplier, self._filter_multiplier * 2,
self._filter_multiplier)
cell2 = cell(self._step, self._block_multiplier, self._filter_multiplier * 2,
self._filter_multiplier, self._filter_multiplier * 2, self._filter_multiplier * 4,
self._filter_multiplier * 2)
cell3 = cell(self._step, self._block_multiplier, self._filter_multiplier * 4,
self._filter_multiplier * 2, self._filter_multiplier *
4, self._filter_multiplier * 8,
self._filter_multiplier * 4)
cell4 = cell(self._step, self._block_multiplier, -1,
self._filter_multiplier * 4, self._filter_multiplier * 8, None,
self._filter_multiplier * 8)
self.cells += [cell1]
self.cells += [cell2]
self.cells += [cell3]
self.cells += [cell4]
else:
cell1 = cell(self._step, self._block_multiplier, self._filter_multiplier,
None, self._filter_multiplier, self._filter_multiplier * 2,
self._filter_multiplier)
cell2 = cell(self._step, self._block_multiplier, self._filter_multiplier * 2,
self._filter_multiplier, self._filter_multiplier * 2, self._filter_multiplier * 4,
self._filter_multiplier * 2)
cell3 = cell(self._step, self._block_multiplier, self._filter_multiplier * 4,
self._filter_multiplier * 2, self._filter_multiplier *
4, self._filter_multiplier * 8,
self._filter_multiplier * 4)
cell4 = cell(self._step, self._block_multiplier, self._filter_multiplier * 8,
self._filter_multiplier * 4, self._filter_multiplier * 8, None,
self._filter_multiplier * 8)
self.cells += [cell1]
self.cells += [cell2]
self.cells += [cell3]
self.cells += [cell4]
self.aspp_4 = nn.Sequential(
ASPP(self._filter_multiplier * self._block_multiplier,
self._num_classes, 24, 24) # 96 / 4 as in the paper
)
self.aspp_8 = nn.Sequential(
ASPP(self._filter_multiplier * 2 * self._block_multiplier,
self._num_classes, 12, 12) # 96 / 8
)
self.aspp_16 = nn.Sequential(
ASPP(self._filter_multiplier * 4 * self._block_multiplier,
self._num_classes, 6, 6) # 96 / 16
)
self.aspp_32 = nn.Sequential(
ASPP(self._filter_multiplier * 8 * self._block_multiplier,
self._num_classes, 3, 3) # 96 / 32
)
def forward(self, x):
# TODO: GET RID OF THESE LISTS, we dont need to keep everything.
# TODO: Is this the reason for the memory issue ?
# Check the chatgpt code above "benefits of avocado" for a better way to do this
self.level_4 = []
self.level_8 = []
self.level_16 = []
self.level_32 = []
if self.backbone is not None:
with torch.no_grad():
# ResNet backbone returns a dict with keys 'out' and 'aux'
# 'out' is the output of the last layer of the backbone
# 'aux' is the output of the layer before the last layer
# We only need 'out' for the decoder
temp = self.backbone(x)['out']
print(temp.shape)
self.level_4.append(self.stem2(temp))
else:
temp = self.stem0(x)
temp = self.stem1(temp)
self.level_4.append(self.stem2(temp))
# Solis
count = 0
normalized_betas = torch.randn(self._num_layers, 4, 3).cuda()
# Softmax on alphas and betas
if torch.cuda.device_count() > 1:
print('1')
img_device = torch.device('cuda', x.get_device())
normalized_alphas = F.softmax(
self.alphas.to(device=img_device), dim=-1)
# normalized_betas[layer][ith node][0 : ➚, 1: ➙, 2 : ➘]
for layer in range(len(self.betas)):
if layer == 0:
normalized_betas[layer][0][1:] = F.softmax(
self.betas[layer][0][1:].to(device=img_device), dim=-1) * (2 / 3)
elif layer == 1:
normalized_betas[layer][0][1:] = F.softmax(
self.betas[layer][0][1:].to(device=img_device), dim=-1) * (2 / 3)
normalized_betas[layer][1] = F.softmax(
self.betas[layer][1].to(device=img_device), dim=-1)
elif layer == 2:
normalized_betas[layer][0][1:] = F.softmax(
self.betas[layer][0][1:].to(device=img_device), dim=-1) * (2 / 3)
normalized_betas[layer][1] = F.softmax(
self.betas[layer][1].to(device=img_device), dim=-1)
normalized_betas[layer][2] = F.softmax(
self.betas[layer][2].to(device=img_device), dim=-1)
else:
normalized_betas[layer][0][1:] = F.softmax(
self.betas[layer][0][1:].to(device=img_device), dim=-1) * (2 / 3)
normalized_betas[layer][1] = F.softmax(
self.betas[layer][1].to(device=img_device), dim=-1)
normalized_betas[layer][2] = F.softmax(
self.betas[layer][2].to(device=img_device), dim=-1)
normalized_betas[layer][3][:2] = F.softmax(
self.betas[layer][3][:1].to(device=img_device), dim=-1) * (2 / 3)
else:
normalized_alphas = F.softmax(self.alphas, dim=-1)
for layer in range(len(self.betas)):
if layer == 0:
normalized_betas[layer][0][1:] = F.softmax(
self.betas[layer][0][1:], dim=-1) * (2 / 3)
elif layer == 1:
normalized_betas[layer][0][1:] = F.softmax(
self.betas[layer][0][1:], dim=-1) * (2 / 3)
normalized_betas[layer][1] = F.softmax(
self.betas[layer][1], dim=-1)
elif layer == 2:
normalized_betas[layer][0][1:] = F.softmax(
self.betas[layer][0][1:], dim=-1) * (2 / 3)
normalized_betas[layer][1] = F.softmax(
self.betas[layer][1], dim=-1)
normalized_betas[layer][2] = F.softmax(
self.betas[layer][2], dim=-1)
else:
normalized_betas[layer][0][1:] = F.softmax(
self.betas[layer][0][1:], dim=-1) * (2 / 3)
normalized_betas[layer][1] = F.softmax(
self.betas[layer][1], dim=-1)
normalized_betas[layer][2] = F.softmax(
self.betas[layer][2], dim=-1)
normalized_betas[layer][3][:2] = F.softmax(
self.betas[layer][3][:2], dim=-1) * (2 / 3)
for layer in range(self._num_layers):
if layer == 0:
level4_new, = self.cells[count](
None, None, self.level_4[-1], None, normalized_alphas)
count += 1
level8_new, = self.cells[count](
None, self.level_4[-1], None, None, normalized_alphas)
count += 1
level4_new = normalized_betas[layer][0][1] * level4_new
level8_new = normalized_betas[layer][0][2] * level8_new
self.level_4.append(level4_new)
self.level_8.append(level8_new)
elif layer == 1:
level4_new_1, level4_new_2 = self.cells[count](self.level_4[-2],
None,
self.level_4[-1],
self.level_8[-1],
normalized_alphas)
count += 1
level4_new = normalized_betas[layer][0][1] * \
level4_new_1 + normalized_betas[layer][1][0] * level4_new_2
level8_new_1, level8_new_2 = self.cells[count](None,
self.level_4[-1],
self.level_8[-1],
None,
normalized_alphas)
count += 1
level8_new = normalized_betas[layer][0][2] * \
level8_new_1 + normalized_betas[layer][1][2] * level8_new_2
level16_new, = self.cells[count](None,
self.level_8[-1],
None,
None,
normalized_alphas)
level16_new = normalized_betas[layer][1][2] * level16_new
count += 1
self.level_4.append(level4_new)
self.level_8.append(level8_new)
self.level_16.append(level16_new)
elif layer == 2:
level4_new_1, level4_new_2 = self.cells[count](self.level_4[-2],
None,
self.level_4[-1],
self.level_8[-1],
normalized_alphas)
count += 1
level4_new = normalized_betas[layer][0][1] * \
level4_new_1 + normalized_betas[layer][1][0] * level4_new_2
level8_new_1, level8_new_2, level8_new_3 = self.cells[count](self.level_8[-2],
self.level_4[-1],
self.level_8[-1],
self.level_16[-1],
normalized_alphas)
count += 1
level8_new = normalized_betas[layer][0][2] * level8_new_1 + normalized_betas[layer][1][1] * level8_new_2 + normalized_betas[layer][2][
0] * level8_new_3
level16_new_1, level16_new_2 = self.cells[count](None,
self.level_8[-1],
self.level_16[-1],
None,
normalized_alphas)
count += 1
level16_new = normalized_betas[layer][1][2] * \
level16_new_1 + \
normalized_betas[layer][2][1] * level16_new_2
level32_new, = self.cells[count](None,
self.level_16[-1],
None,
None,
normalized_alphas)
level32_new = normalized_betas[layer][2][2] * level32_new
count += 1
self.level_4.append(level4_new)
self.level_8.append(level8_new)
self.level_16.append(level16_new)
self.level_32.append(level32_new)
elif layer == 3:
level4_new_1, level4_new_2 = self.cells[count](self.level_4[-2],
None,
self.level_4[-1],
self.level_8[-1],
normalized_alphas)
count += 1
level4_new = normalized_betas[layer][0][1] * \
level4_new_1 + normalized_betas[layer][1][0] * level4_new_2
level8_new_1, level8_new_2, level8_new_3 = self.cells[count](self.level_8[-2],
self.level_4[-1],
self.level_8[-1],
self.level_16[-1],
normalized_alphas)
count += 1
level8_new = normalized_betas[layer][0][2] * level8_new_1 + normalized_betas[layer][1][1] * level8_new_2 + normalized_betas[layer][2][
0] * level8_new_3
level16_new_1, level16_new_2, level16_new_3 = self.cells[count](self.level_16[-2],
self.level_8[-1],
self.level_16[-1],
self.level_32[-1],
normalized_alphas)
count += 1
level16_new = normalized_betas[layer][1][2] * level16_new_1 + normalized_betas[layer][2][1] * level16_new_2 + normalized_betas[layer][3][
0] * level16_new_3
level32_new_1, level32_new_2 = self.cells[count](None,
self.level_16[-1],
self.level_32[-1],
None,
normalized_alphas)
count += 1
level32_new = normalized_betas[layer][2][2] * \
level32_new_1 + \
normalized_betas[layer][3][1] * level32_new_2
self.level_4.append(level4_new)
self.level_8.append(level8_new)
self.level_16.append(level16_new)
self.level_32.append(level32_new)
else:
level4_new_1, level4_new_2 = self.cells[count](self.level_4[-2],
None,
self.level_4[-1],
self.level_8[-1],
normalized_alphas)
count += 1
level4_new = normalized_betas[layer][0][1] * \
level4_new_1 + normalized_betas[layer][1][0] * level4_new_2
level8_new_1, level8_new_2, level8_new_3 = self.cells[count](self.level_8[-2],
self.level_4[-1],
self.level_8[-1],
self.level_16[-1],
normalized_alphas)
count += 1
level8_new = normalized_betas[layer][0][2] * level8_new_1 + normalized_betas[layer][1][1] * level8_new_2 + normalized_betas[layer][2][
0] * level8_new_3
level16_new_1, level16_new_2, level16_new_3 = self.cells[count](self.level_16[-2],
self.level_8[-1],
self.level_16[-1],
self.level_32[-1],
normalized_alphas)
count += 1
level16_new = normalized_betas[layer][1][2] * level16_new_1 + normalized_betas[layer][2][1] * level16_new_2 + normalized_betas[layer][3][
0] * level16_new_3
level32_new_1, level32_new_2 = self.cells[count](self.level_32[-2],
self.level_16[-1],
self.level_32[-1],
None,
normalized_alphas)
count += 1
level32_new = normalized_betas[layer][2][2] * \
level32_new_1 + \
normalized_betas[layer][3][1] * level32_new_2
self.level_4.append(level4_new)
self.level_8.append(level8_new)
self.level_16.append(level16_new)
self.level_32.append(level32_new)
self.level_4 = self.level_4[-2:]
self.level_8 = self.level_8[-2:]
self.level_16 = self.level_16[-2:]
self.level_32 = self.level_32[-2:]
aspp_result_4 = self.aspp_4(self.level_4[-1])
aspp_result_8 = self.aspp_8(self.level_8[-1])
aspp_result_16 = self.aspp_16(self.level_16[-1])
aspp_result_32 = self.aspp_32(self.level_32[-1])
upsample = nn.Upsample(
size=x.size()[2:], mode='bilinear', align_corners=True)
aspp_result_4 = upsample(aspp_result_4)
aspp_result_8 = upsample(aspp_result_8)
aspp_result_16 = upsample(aspp_result_16)
aspp_result_32 = upsample(aspp_result_32)
sum_feature_map = aspp_result_4 + aspp_result_8 + aspp_result_16 + aspp_result_32
return sum_feature_map
def _initialize_alphas_betas(self):
k = sum(1 for i in range(self._step) for n in range(2 + i))
num_ops = len(PRIMITIVES)
# alphas = torch.tensor(1e-3 * torch.randn(k, num_ops).cuda(), requires_grad=True)
alphas = (1e-3 * torch.randn(k, num_ops)
).clone().detach().requires_grad_(True)
betas = (1e-3 * torch.randn(self._num_layers, 4, 3)
).clone().detach().requires_grad_(True)
self._arch_parameters = [
alphas,
betas,
]
self._arch_param_names = [
'alphas',
'betas',
]
[self.register_parameter(name, torch.nn.Parameter(param)) for name, param in zip(
self._arch_param_names, self._arch_parameters)]
# def decode_viterbi(self):
# decoder = Decoder(self.bottom_betas, self.betas8, self.betas16, self.top_betas)
# return decoder.viterbi_decode()
# def decode_dfs(self):
# decoder = Decoder(self.bottom_betas, self.betas8, self.betas16, self.top_betas)
# return decoder.dfs_decode()
def arch_parameters(self):
return [param for name, param in self.named_parameters() if name in self._arch_param_names]
def weight_parameters(self):
return [param for name, param in self.named_parameters() if name not in self._arch_param_names]
def genotype(self):
decoder = Decoder(self.alphas_cell, self._block_multiplier, self._step)
return decoder.genotype_decode()
def _loss(self, input, target):
logits = self(input)
return self._criterion(logits, target)
def main():
model = AutoDeeplab(7, 12, None)
x = torch.tensor(torch.ones(4, 3, 224, 224))
resultdfs = model.decode_dfs()
resultviterbi = model.decode_viterbi()[0]
print(resultviterbi)
print(model.genotype())
if __name__ == '__main__':
main()