-
Notifications
You must be signed in to change notification settings - Fork 2
/
RNNResNet20.py
353 lines (309 loc) · 13.6 KB
/
RNNResNet20.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
import logging
import networkx as nx
import matplotlib.pyplot as plt
from naslib.defaults.trainer import Trainer
from naslib.optimizers import DARTSOptimizer
from naslib.utils import utils, setup_logger
from naslib.search_spaces.core.graph import Graph, EdgeData
from naslib.search_spaces.core import primitives as ops
from torch import nn
from IPython.display import clear_output
import torch
from naslib.search_spaces.core.primitives import AbstractPrimitive
from activation_sub_func.binary_func import Maximum, Minimum, Sub, Add, Mul, Div, SigMul, ExpBetaSub2, ExpBetaSubAbs, \
BetaMix, Stack
from activation_sub_func.unary_func import Power, Sin, Cos, Abs_op, Sign, Beta, Beta_mul, Beta_add, Log, Exp, \
Sinh, Cosh, \
Tanh, Asinh, Atan, Maximum0, Minimum0, Sigmoid, LogExp, Exp2, Erf, Sinc, Sqrt
"""Naslib search space for ResNet8 with complex activation cell"""
class ActivationFuncResNet20SearchSpace(Graph):
"""
https://www.researchgate.net/figure/ResNet-20-architecture_fig3_351046093
"""
OPTIMIZER_SCOPE = [
f"activation_{i}" for i in range(1, 20)
]
QUERYABLE = False
def __init__(self):
super().__init__()
# cell definition
activation_cell = Graph()
activation_cell.name = 'activation_cell'
activation_cell.add_node(1) # input node
activation_cell.add_node(2) # unary node / intermediate node
activation_cell.add_node(3) # unary node / intermediate node
activation_cell.add_node(4) # binary node / output node
activation_cell.add_edges_from([(1, 2, EdgeData())]) # mutable intermediate edge
activation_cell.add_edges_from([(1, 3, EdgeData())]) # mutable intermediate edge
activation_cell.add_edges_from([(2, 4, EdgeData().finalize())]) # mutable intermediate edge
activation_cell.add_edges_from([(3, 4, EdgeData().finalize())]) # mutable intermediate edge
activation_cell.nodes[4]['comb_op'] = Stack()
activation_cell.add_node(5) # binary node / output node
activation_cell.add_edges_from([(4, 5, EdgeData())]) # mutable intermediate edge
activation_cell.add_node(6)
activation_cell.add_edges_from([(5, 6, EdgeData().finalize())]) # unary node / intermediate node
activation_cell.add_node(7)
activation_cell.add_edges_from([(6, 7, EdgeData())]) # mutable intermediate edge
activation_cell.add_node(8)
activation_cell.add_edges_from([(1, 8, EdgeData())]) # mutable intermediate edge
activation_cell.add_node(9)
activation_cell.add_edges_from([(8, 9, EdgeData().finalize())]) # mutable intermediate edge
activation_cell.add_edges_from([(7, 9, EdgeData().finalize())]) # mutable intermediate edge
activation_cell.nodes[9]['comb_op'] = Stack()
activation_cell.add_node(10)
activation_cell.add_edges_from([(9, 10, EdgeData())]) # mutable intermediate edge
activation_cell.add_node(11)
activation_cell.add_edges_from([(10, 11, EdgeData().finalize())]) # mutable intermediate edge
# macroarchitecture definition
self.name = 'makrograph'
self.add_node(1) # input
self.add_node(2) # intermediate
self.add_node(3,
subgraph=activation_cell.copy().set_scope("activation_1").set_input([2])) # activation cell 3
self.nodes[3]['subgraph'].name = "activation_1"
self.update_edges(
update_func=lambda edge: self._set_ops(edge, 16),
scope=f"activation_{1}",
private_edge_data=True, )
self.add_node(4)
self.add_node(5,
subgraph=activation_cell.copy().set_scope("activation_2").set_input([4])) # activation cell 3
self.nodes[5]['subgraph'].name = "activation_2"
self.update_edges(
update_func=lambda edge: self._set_ops(edge, 16),
scope=f"activation_{2}",
private_edge_data=True, )
self.add_node(6)
# Todo add option here with a func which has a arg channels
self.add_node(7,
subgraph=activation_cell.copy().set_scope("activation_3").set_input([6])) # activation cell 3
self.nodes[7]['subgraph'].name = "activation_3"
self.update_edges(
update_func=lambda edge: self._set_ops(edge, 16),
scope=f"activation_{3}",
private_edge_data=True, )
self.add_edges_from([
(1, 2, EdgeData()),
(2, 3, EdgeData()),
(3, 4, EdgeData()),
(4, 5, EdgeData()),
(5, 6, EdgeData()),
(3, 6, EdgeData()),
(6, 7, EdgeData())
])
self.edges[1, 2].set('op',
ops.Sequential(
nn.BatchNorm2d(3),
nn.Conv2d(3, 16, 3, padding=1), )) # convolutional edge
self.edges[3, 4].set('op',
ops.Sequential(
nn.BatchNorm2d(16),
nn.Conv2d(16, 16, 3, padding=1), )) # convolutional edge
self.edges[5, 6].set('op',
ops.Sequential(
nn.BatchNorm2d(16),
nn.Conv2d(16, 16, 3, padding=1), )) # convolutional edge
conv_option = {
"in_channels": 16,
"out_channels": 16,
"kernel_size": 3,
"padding": 1
}
self._create_base_block(7, 4, activation_cell, conv_option)
self._create_base_block(11, 6, activation_cell, conv_option)
conv_option_a = {
"in_channels": 16,
"out_channels": 32,
"kernel_size": 3,
"padding": 1,
"stride": 2
}
conv_option_b = {
"in_channels": 16,
"out_channels": 32,
"kernel_size": 1,
"padding": 0,
"stride": 2
}
self._create_reduction_block(15, 8, activation_cell, conv_option_a, conv_option_b)
conv_option = {
"in_channels": 32,
"out_channels": 32,
"kernel_size": 3,
"padding": 1
}
self._create_base_block(19, 10, activation_cell, conv_option)
self._create_base_block(23, 12, activation_cell, conv_option)
conv_option_a = {
"in_channels": 32,
"out_channels": 64,
"kernel_size": 3,
"padding": 1,
"stride": 2
}
conv_option_b = {
"in_channels": 32,
"out_channels": 64,
"kernel_size": 1,
"padding": 0,
"stride": 2
}
self._create_reduction_block(27, 14, activation_cell, conv_option_a, conv_option_b)
conv_option = {
"in_channels": 64,
"out_channels": 64,
"kernel_size": 3,
"padding": 1
}
self._create_base_block(31, 16, activation_cell, conv_option)
self._create_base_block(34, 18, activation_cell, conv_option)
# add head
self.add_node(39)
self.add_edges_from([
(38, 39, EdgeData())
])
self.edges[38, 39].set('op',
ops.Sequential(
nn.AvgPool2d(8),
nn.Flatten(),
nn.Linear(64, 10),
nn.Softmax()
)) # convolutional edge
self.add_node(40)
self.add_edges_from([
(39, 40, EdgeData().finalize())
])
def _create_base_block(self, start: int, stage: int, cell, conv_option: dict):
self.add_node(start + 1)
self.add_node(start + 2, subgraph=cell.copy().set_scope(f"activation_{stage}").set_input(
[start + 1])) # activation cell 3
self.nodes[start + 2]['subgraph'].name = f"activation_{stage}"
self.update_edges(
update_func=lambda edge: self._set_ops(edge, conv_option["out_channels"]),
scope=f"activation_{stage}",
private_edge_data=True, )
self.add_node(start + 3)
self.add_node(start + 4, subgraph=cell.copy().set_scope(f"activation_{stage + 1}").set_input(
[start + 3])) # activation cell 3
self.nodes[start + 4]['subgraph'].name = f"activation_{stage + 1}"
self.update_edges(
update_func=lambda edge: self._set_ops(edge, conv_option["out_channels"]),
scope=f"activation_{stage + 1}",
private_edge_data=True, )
self.add_edges_from([
(start, start + 1, EdgeData()),
(start, start + 3, EdgeData()),
(start + 1, start + 2, EdgeData()),
(start + 2, start + 3, EdgeData()),
(start + 3, start + 4, EdgeData()),
])
self.edges[start, start + 1].set('op',
ops.Sequential(
nn.BatchNorm2d(conv_option["in_channels"]),
nn.Conv2d(**conv_option), )) # convolutional edge
self.edges[start + 2, start + 3].set('op',
ops.Sequential(
nn.BatchNorm2d(conv_option["in_channels"]),
nn.Conv2d(**conv_option), )) # convolutional edge
def _create_reduction_block(self, start: int, stage: int, cell, conv_option_a: dict, conv_option_b: dict):
self.add_node(start + 1)
self.add_node(start + 2, subgraph=cell.copy().set_scope(f"activation_{stage}").set_input(
[start + 1])) # activation cell 3
self.nodes[start + 2]['subgraph'].name = f"activation_{stage}"
self.update_edges(
update_func=lambda edge: self._set_ops(edge, conv_option_a["out_channels"]),
scope=f"activation_{stage}",
private_edge_data=True, )
self.add_node(start + 3)
self.add_node(start + 4, subgraph=cell.copy().set_scope(f"activation_{stage + 1}").set_input(
[start + 3])) # activation cell 3
self.nodes[start + 4]['subgraph'].name = f"activation_{stage + 1}"
self.update_edges(
update_func=lambda edge: self._set_ops(edge, conv_option_b["out_channels"]),
scope=f"activation_{stage + 1}",
private_edge_data=True, )
self.add_edges_from([
(start, start + 1, EdgeData()),
(start, start + 3, EdgeData()), # add conv
(start + 1, start + 2, EdgeData()),
(start + 2, start + 3, EdgeData()),
(start + 3, start + 4, EdgeData()),
])
self.edges[start, start + 1].set('op',
ops.Sequential(
nn.BatchNorm2d(conv_option_a["in_channels"]),
nn.Conv2d(**conv_option_a), )) # convolutional edge
conv_option_a["in_channels"] = conv_option_a["out_channels"]
conv_option_a["stride"] = 1
self.edges[start, start + 3].set('op',
ops.Sequential(
nn.BatchNorm2d(conv_option_b["in_channels"]),
nn.Conv2d(**conv_option_b), )) # convolutional edge
self.edges[start + 2, start + 3].set('op',
ops.Sequential(
nn.BatchNorm2d(conv_option_a["in_channels"]),
nn.Conv2d(**conv_option_a), )) # convolutional edge
def _set_ops(self, edge, channels=32):
# unary (1, 2), (1, 3), (1, 8), (6, 7)
if (edge.head, edge.tail) in {(1, 2), (1, 3), (1, 8), (6, 7)}:
edge.data.set("op", [
ops.Identity(),
ops.Zero(stride=1),
Power(2),
Power(3),
Sqrt(),
Sin(),
Cos(),
Abs_op(),
Sign(),
Beta_mul(channels=channels),
Beta_add(channels=channels),
Log(),
Exp(),
Sinh(),
# Cosh(),
Tanh(),
Asinh(),
Atan(),
Sinc(),
Maximum0(),
Minimum0(),
Sigmoid(),
LogExp(),
Exp2(),
Erf(),
Beta(channels=channels),
])
# binary (4, 5), (9, 10)
elif (edge.head, edge.tail) in {(4, 5), (9, 10)}:
edge.data.set("op", [
Add(),
Sub(),
Mul(),
# Div(),
Maximum(),
Minimum(),
SigMul(),
ExpBetaSub2(channels=channels),
ExpBetaSubAbs(channels=channels),
BetaMix(channels=channels),
])
config = utils.get_config_from_args(config_type='nas')
config.optimizer = 'darts'
config.search.batch_size = 32
config.search.learning_rate = 0.05
config.search.epochs = 200
utils.set_seed(config.seed)
clear_output(wait=True)
utils.log_args(config)
logger = setup_logger(config.save + '/log.log')
logger.setLevel(logging.INFO)
search_space = ActivationFuncResNet20SearchSpace()
# nx.draw_kamada_kawai(search_space)
# plt.show()
optimizer = DARTSOptimizer(config)
optimizer.adapt_search_space(search_space)
# with torch.autograd.set_detect_anomaly(True):
trainer = Trainer(optimizer, config)
trainer.search()
trainer.evaluate_oneshot()