-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.py
189 lines (153 loc) · 6.55 KB
/
Node.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
from Search_space import *
from Search_space import Operations
class Node(nn.Module):
def __init__(self, prev_channel, channel, op_id, stride):
super(Node, self).__init__()
self.op = Operations[op_id](prev_channel, channel, stride, None)
self.out_channel = self.op.out_channel
def forward(self, x):
x = self.op(x)
return x
class Stage(nn.Module):
def __init__(self, op_list, prev_channel, channel, stride):
super(Stage, self).__init__()
self.num_node = len(op_list)
self.ops = nn.ModuleList()
for i in range(self.num_node):
if i == 0 and stride != 1:
node = Node(prev_channel, channel, op_list[i], stride)
else:
node = Node(prev_channel, channel, op_list[i], 1)
self.ops.append(node)
prev_channel = node.out_channel
self.out_channel = prev_channel
def forward(self, x):
for op in self.ops:
x = op(x)
return x
class NetworkCIFAR(nn.Module):
def __init__(self, args, classes, init_channel, stages, pools, use_aux_head, keep_prob):
super(NetworkCIFAR, self).__init__()
self.args = args
self.classes = classes
self.init_channel = init_channel
self.stages = stages
self.pools = pools
self.total_blocks = len(stages) + len(pools) # 5
self.pool_layer = [1, 3]
self.use_aux_head = use_aux_head
self.keep_prob = keep_prob
if self.use_aux_head:
self.aux_head_index = self.pool_layer[-1]
self.stem1 = ConvBNReLU(3, 32, 3, 1, 1)
prev_channel = 32
channel = self.init_channel
self.features = nn.ModuleList()
for i in range(self.total_blocks):
if i in self.pool_layer:
channel = 2 * prev_channel
cell = Node(prev_channel, channel, pools[(i - 1) // 2], 2)
else:
cell = Stage(self.stages[i // 2], prev_channel, channel, 1)
self.features.append(cell)
prev_channel = cell.out_channel
if self.use_aux_head and i == self.aux_head_index:
self.auxiliary_head = AuxHeadCIFAR(prev_channel, classes)
self.stem2 = ConvBNReLU(prev_channel, self.args.search_last_channel, 3, 1, 1)
self.global_pooling = nn.AdaptiveAvgPool2d(1)
self.dropout = nn.Dropout(1 - self.keep_prob)
self.classifier = nn.Linear(self.args.search_last_channel, classes)
self.init_parameters()
def init_parameters(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.zeros_(m.bias)
def forward(self, x):
aux_logits = None
out = self.stem1(x)
for i, feature in enumerate(self.features):
out = feature(out)
if self.use_aux_head and i == self.aux_head_index and self.training:
aux_logits = self.auxiliary_head(out)
out = self.stem2(out)
out = self.global_pooling(out)
out = self.dropout(out)
logits = self.classifier(out.view(out.size(0), -1))
if self.use_aux_head:
return logits, aux_logits
else:
return logits
class NetworkImageNet(nn.Module):
def __init__(self, args, classes, init_channel, stages, pools, use_aux_head, keep_prob):
super(NetworkImageNet, self).__init__()
self.args = args
self.classes = classes
self.init_channel = init_channel
self.stages = stages
self.pools = pools
self.total_blocks = len(stages) + len(pools) # 5
self.pool_layer = [1, 3]
self.use_aux_head = use_aux_head
self.keep_prob = keep_prob
if self.use_aux_head:
self.aux_head_index = self.pool_layer[-1]
self.stem0 = nn.Sequential(
ConvBNReLU(3, 16, 3, 2, 1),
ConvBNReLU(16, 32, 3, 2, 1)
)
self.stem1 = ConvBNReLU(32, 32, 3, 2, 1)
prev_channel = 32
channel = self.init_channel
self.features = nn.ModuleList()
for i in range(self.total_blocks):
if i in self.pool_layer:
channel = 2 * prev_channel
cell = Node(prev_channel, channel, pools[(i - 1) // 2], 2)
else:
cell = Stage(self.stages[i // 2], prev_channel, channel, 1)
self.features.append(cell)
prev_channel = cell.out_channel
if self.use_aux_head and i == self.aux_head_index:
self.auxiliary_head = AuxHeadImageNet(prev_channel, classes)
# search_last_channel = 1280
self.stem2 = ConvBNReLU(prev_channel, self.args.search_last_channel, 3, 1, 1)
self.global_pooling = nn.AdaptiveAvgPool2d(1)
self.dropout = nn.Dropout(1 - self.keep_prob)
self.classifier = nn.Linear(self.args.search_last_channel, classes)
self.init_parameters()
def init_parameters(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.zeros_(m.bias)
def forward(self, x):
aux_logits = None
out = self.stem0(x)
out = self.stem1(out)
for i, feature in enumerate(self.features):
out = feature(out)
if self.use_aux_head and i == self.aux_head_index and self.training:
aux_logits = self.auxiliary_head(out)
out = self.stem2(out)
out = self.global_pooling(out)
out = self.dropout(out)
logits = self.classifier(out.view(out.size(0), -1))
if self.use_aux_head:
return logits, aux_logits
else:
return logits