forked from NoamRosenberg/autodeeplab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_model.py
193 lines (162 loc) · 8.26 KB
/
new_model.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
import torch
import torch.nn as nn
import numpy as np
import cell_level_search
from genotypes import PRIMITIVES
from genotypes import Genotype
import torch.nn.functional as F
from operations import *
class Cell(nn.Module):
def __init__(self, steps, block_multiplier, prev_prev_fmultiplier,
prev_filter_multiplier,
cell_arch, network_arch,
filter_multiplier,
prev_downup, downup_sample):
super(Cell, self).__init__()
self.cell_arch = cell_arch
self.C_in = block_multiplier * filter_multiplier * block_multiplier
self.C_out = filter_multiplier * block_multiplier
self.C_prev = int(block_multiplier * prev_filter_multiplier)
self.C_prev_prev = int(block_multiplier * prev_prev_fmultiplier)
if downup_sample == -1:
self.preprocess = FactorizedReduce(self.C_prev, self.C_out, affine=False)
elif downup_sample == 0:
self.preprocess = ReLUConvBN(self.C_prev, self.C_out, 1, 1, 0, affine=False)
elif downup_sample == 1:
self.preprocess = FactorizedIncrease(self.C_prev, self.C_out)
if prev_downup is not None:
if prev_downup == -2:
self.pre_preprocess = DoubleFactorizedReduce(self.C_prev_prev, self.C_out, affine=False)
elif prev_downup == -1:
self.pre_preprocess = FactorizedReduce(self.C_prev_prev, self.C_out, affine=False)
elif prev_downup == 0:
self.pre_preprocess = ReLUConvBN(self.C_prev_prev, self.C_out, 1, 1, 0, affine=False)
elif prev_downup == 1:
self.pre_preprocess = FactorizedIncrease(self.C_prev_prev, self.C_out)
elif prev_downup == 2:
self.pre_preprocess = DoubleFactorizedIncrease(self.C_prev_prev, self.C_out)
self._steps = steps
self.block_multiplier = block_multiplier
self._ops = nn.ModuleList()
for x in self.cell_arch:
primitive = PRIMITIVES[x[1]]
op = OPS[primitive](self.C_out,stride=1,affine=False)
self._ops.append(op)
self.ReLUConvBN = ReLUConvBN (self.C_in, self.C_out, 1, 1, 0)
def forward(self, prev_prev_input, prev_input):
if prev_prev_input is not None:
s0 = self.pre_preprocess(prev_prev_input)
else:
s0 = 0
s1 = self.preprocess(prev_input)
states = [s0, s1]
offset = 0
ops_index = 0
for i in range(self._steps):
new_states = []
for j, h in enumerate(states):
branch_index = offset + j
if branch_index in self.cell_arch[:,0]:
if prev_prev_input is None and j==0:
ops_index += 1
continue
new_state = self._ops[ops_index](h)
new_states.append(new_state)
ops_index += 1
s = sum(new_states)
offset += len(states)
states.append(s)
concat_feature = torch.cat(states[-self.block_multiplier:], dim=1)
return prev_input, self.ReLUConvBN (concat_feature)
class newModel (nn.Module) :
def __init__(self, network_arch, cell_arch, num_classes, num_layers, criterion = None, filter_multiplier = 8, block_multiplier = 5, step = 5, cell=Cell):
super(newModel, self).__init__()
self.cells = nn.ModuleList()
self.network_arch = torch.from_numpy(network_arch)
self.cell_arch = torch.from_numpy(cell_arch)
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
C_initial = 128
self.stem0 = nn.Sequential(
nn.Conv2d(3, 64, 3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU ()
)
self.stem1 = nn.Sequential(
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU ()
)
self.stem2 = nn.Sequential(
nn.Conv2d(64, C_initial, 3, stride=2, padding=1),
nn.BatchNorm2d(C_initial),
nn.ReLU ()
)
#C_prev_prev = 64
initial_fm = C_initial / self._block_multiplier
filter_param_dict = {0: 1, 1: 2, 2: 4, 3: 8}
for i in range (self._num_layers) :
if i==0:
level_option = torch.sum(self.network_arch[0], dim=1)
level = torch.argmax(level_option).item()
three_branch_options = torch.sum(self.network_arch[i], dim=0)
downup_sample = torch.argmax(three_branch_options).item() - 1
_cell = cell (self._step, self._block_multiplier, -1,
initial_fm,
self.cell_arch, self.network_arch[i],
self._filter_multiplier * filter_param_dict[level],
None, downup_sample)
elif i==1:
level_option = torch.sum(self.network_arch[i], dim=1)
prev_level_option = torch.sum(self.network_arch[i-1], dim=1)
level = torch.argmax(level_option).item()
prev_level = torch.argmax(prev_level_option).item()
three_branch_options = torch.sum(self.network_arch[i], dim=0)
downup_sample = torch.argmax(three_branch_options).item() - 1
prev_three_branch_options = torch.sum(self.network_arch[i-1], dim=0)
prev_downup_sample = torch.argmax(prev_three_branch_options).item() - 1
total_downup = prev_downup_sample + downup_sample
_cell = cell(self._step, self._block_multiplier, initial_fm,
self._filter_multiplier * filter_param_dict[prev_level],
self.cell_arch, self.network_arch[i],
self._filter_multiplier * filter_param_dict[level],
total_downup, downup_sample)
else:
level_option = torch.sum(self.network_arch[i], dim=1)
prev_level_option = torch.sum(self.network_arch[i-1], dim=1)
prev_prev_level_option = torch.sum(self.network_arch[i-2], dim=1)
level = torch.argmax(level_option).item()
prev_level = torch.argmax(prev_level_option).item()
prev_prev_level = torch.argmax(prev_prev_level_option).item()
three_branch_options = torch.sum(self.network_arch[i], dim=0)
downup_sample = torch.argmax(three_branch_options).item() - 1
prev_three_branch_options = torch.sum(self.network_arch[i-1], dim=0)
prev_downup_sample = torch.argmax(prev_three_branch_options).item() - 1
total_downup = prev_downup_sample + downup_sample
_cell = cell(self._step, self._block_multiplier, self._filter_multiplier * filter_param_dict[prev_prev_level],
self._filter_multiplier * filter_param_dict[prev_level],
self.cell_arch, self.network_arch[i],
self._filter_multiplier * filter_param_dict[level],
total_downup, downup_sample)
self.cells += [_cell]
last_level_option = torch.sum(self.network_arch[-1], dim=1)
last_level = torch.argmax(last_level_option).item()
aspp_num_input_channels = self._block_multiplier * self._filter_multiplier * filter_param_dict[last_level]
atrous_rate = int(96 / (filter_param_dict[last_level] * 4))
self.aspp = ASPP (aspp_num_input_channels, self._num_classes, atrous_rate, atrous_rate) #96 / 4 as in the paper
def forward(self, x):
stem = self.stem0 (x)
stem = self.stem1 (stem)
stem = self.stem2 (stem)
two_last_inputs = (None, stem)
for i in range(self._num_layers):
two_last_inputs = self.cells[i](two_last_inputs[0], two_last_inputs[1])
last_input = two_last_inputs[-1]
aspp_result = self.aspp(last_input)
upsample = nn.Upsample(size=x.size()[2:], mode='bilinear', align_corners=True)
aspp_result = upsample(aspp_result)
return aspp_result