forked from PaddlePaddle/PaddleSlim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ofa_v2.py
330 lines (275 loc) · 11 KB
/
test_ofa_v2.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
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
sys.path.append("../")
import numpy as np
import unittest
import paddle
import paddle.nn as nn
from paddle.nn import ReLU
from paddleslim.nas import ofa
from paddleslim.nas.ofa import OFA, RunConfig, DistillConfig
from paddleslim.nas.ofa.convert_super import supernet
from paddleslim.nas.ofa.convert_super import Convert, supernet
class ModelV1(nn.Layer):
def __init__(self, name=''):
super(ModelV1, self).__init__()
self.model = nn.Sequential(nn.Conv2D(3, 12, 16), nn.ReLU())
self.cls = self.create_parameter(
attr=paddle.ParamAttr(
name=name + 'cls',
initializer=nn.initializer.Assign(
paddle.zeros(shape=(2, 12, 17, 17)))),
shape=(2, 12, 17, 17))
def forward(self, inputs):
return self.cls + self.model(inputs)
class ModelShortcut(nn.Layer):
def __init__(self):
super(ModelShortcut, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2D(3, 12, 1), nn.BatchNorm2D(12), nn.ReLU())
self.branch1 = nn.Sequential(
nn.Conv2D(12, 12, 1),
nn.BatchNorm2D(12),
nn.ReLU(),
nn.Conv2D(
12, 12, 1, groups=12),
nn.BatchNorm2D(12),
nn.ReLU(),
nn.Conv2D(
12, 12, 1, groups=12),
nn.BatchNorm2D(12),
nn.ReLU())
self.branch2 = nn.Sequential(
nn.Conv2D(12, 12, 1),
nn.BatchNorm2D(12),
nn.ReLU(),
nn.Conv2D(
12, 12, 1, groups=12),
nn.BatchNorm2D(12),
nn.ReLU(),
nn.Conv2D(12, 12, 1),
nn.BatchNorm2D(12),
nn.ReLU())
self.out = nn.Sequential(
nn.Conv2D(12, 12, 1), nn.BatchNorm2D(12), nn.ReLU())
def forward(self, x):
x = self.conv1(x)
y = self.branch1(x)
y = x + y
z = self.branch2(y)
z = z + y
z = self.out(z)
return z
class ModelElementwise(nn.Layer):
def __init__(self):
super(ModelElementwise, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2D(3, 12, 1), nn.BatchNorm2D(12), nn.ReLU())
self.conv2 = nn.Sequential(
nn.Conv2D(12, 24, 3), nn.BatchNorm2D(24), nn.ReLU())
self.conv3 = nn.Sequential(
nn.Conv2D(24, 12, 1), nn.BatchNorm2D(12), nn.ReLU())
self.out = nn.Sequential(
nn.Conv2D(12, 6, 1), nn.BatchNorm2D(6), nn.ReLU())
def forward(self, x):
d = paddle.randn(shape=[2, 12, x.shape[2], x.shape[3]], dtype='float32')
d = nn.functional.softmax(d)
x = self.conv1(x)
x = x + d
x = self.conv2(x)
x = self.conv3(x)
x = self.out(x)
return x
class ModelMultiExit(nn.Layer):
def __init__(self):
super(ModelMultiExit, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2D(3, 12, 3), nn.BatchNorm2D(12), nn.ReLU())
self.block1 = nn.Sequential(
nn.Conv2D(12, 24, 7),
nn.BatchNorm2D(24),
nn.ReLU(),
nn.MaxPool2D(
kernel_size=3, stride=2, padding=0),
nn.Conv2D(24, 24, 7),
nn.BatchNorm2D(24),
nn.ReLU(),
nn.MaxPool2D(
kernel_size=3, stride=2, padding=0))
self.block2 = nn.Sequential(
nn.Conv2D(24, 24, 1),
nn.BatchNorm2D(24),
nn.ReLU(),
nn.MaxPool2D(
kernel_size=3, stride=2, padding=1))
self.out1 = nn.Sequential(
nn.Conv2D(24, 24, 1), nn.BatchNorm2D(24), nn.ReLU())
self.out2 = nn.Sequential(
nn.Conv2D(48, 24, 7),
nn.BatchNorm2D(24),
nn.ReLU(), nn.Conv2D(24, 24, 3), nn.BatchNorm2D(24), nn.ReLU())
def forward(self, x):
x = self.conv1(x)
b1 = self.block1(x)
adapt = nn.UpsamplingBilinear2D(size=[b1.shape[2], b1.shape[2]])
b2 = self.block2(b1)
up = adapt(b2)
y1 = self.out1(b1)
y2 = paddle.concat([b1, up], axis=1)
y2 = self.out2(y2)
return [y1, y2]
class ModelInputDict(nn.Layer):
def __init__(self):
super(ModelInputDict, self).__init__()
self.conv0 = nn.Sequential(
nn.Conv2D(3, 12, 1), nn.BatchNorm2D(12), nn.ReLU())
self.conv1 = nn.Sequential(
nn.Conv2D(12, 12, 1), nn.BatchNorm2D(12), nn.ReLU())
self.conv2 = nn.Sequential(
nn.Conv2D(12, 12, 1), nn.BatchNorm2D(12), nn.ReLU())
self.conv3 = nn.Sequential(
nn.Conv2D(12, 12, 1), nn.BatchNorm2D(12), nn.ReLU())
def forward(self, x, data):
x = self.conv1(self.conv0(x))
y = self.conv2(x)
y = y + data['data']
return self.conv3(y)
class TestOFAV2(unittest.TestCase):
def setUp(self):
model = ModelV1()
sp_net_config = supernet(expand_ratio=[0.25, 0.5, 1.0])
self.model = Convert(sp_net_config).convert(model)
self.images = paddle.randn(shape=[2, 3, 32, 32], dtype='float32')
def test_ofa(self):
self.ofa_model = OFA(self.model)
self.ofa_model.set_epoch(0)
self.ofa_model.set_task('expand_ratio')
out, _ = self.ofa_model(self.images)
class TestOFAV2Export(unittest.TestCase):
def setUp(self):
model = ModelV1(name='export')
sp_net_config = supernet(expand_ratio=[0.25, 0.5, 1.0])
self.model = Convert(sp_net_config).convert(model)
self.images = paddle.randn(shape=[2, 3, 32, 32], dtype='float32')
self.ofa_model = OFA(self.model)
def test_export(self):
origin_model = ModelV1(name='origin')
net_config = {'model.0': {}}
self.ofa_model.export(
net_config,
input_shapes=[1, 3, 32, 32],
input_dtypes=['float32'],
origin_model=origin_model)
class Testelementwise(unittest.TestCase):
def setUp(self):
model = ModelElementwise()
sp_net_config = supernet(expand_ratio=[0.25, 0.5, 1.0])
self.model = Convert(sp_net_config).convert(model)
self.images = paddle.randn(shape=[2, 3, 32, 32], dtype='float32')
def test_elementwise(self):
self.ofa_model = OFA(self.model)
self.ofa_model.set_epoch(0)
self.ofa_model.set_task('expand_ratio')
out, _ = self.ofa_model(self.images)
assert list(self.ofa_model._ofa_layers.keys()) == ['conv2.0', 'conv3.0']
class TestMultiExit(unittest.TestCase):
def setUp(self):
self.images = paddle.randn(shape=[1, 3, 224, 224], dtype='float32')
model = ModelMultiExit()
sp_net_config = supernet(expand_ratio=[0.25, 0.5, 1.0])
self.model = Convert(sp_net_config).convert(model)
def test_multiexit(self):
self.ofa_model = OFA(self.model)
self.ofa_model.set_epoch(0)
self.ofa_model.set_task('expand_ratio')
out, _ = self.ofa_model(self.images)
assert list(self.ofa_model._ofa_layers.keys(
)) == ['conv1.0', 'block1.0', 'block1.4', 'block2.0', 'out2.0']
class TestShortcutSkiplayers(unittest.TestCase):
def setUp(self):
model = ModelShortcut()
sp_net_config = supernet(expand_ratio=[0.5, 1.0])
self.model = Convert(sp_net_config).convert(model)
self.images = paddle.randn(shape=[2, 3, 32, 32], dtype='float32')
self.init_config()
self.ofa_model = OFA(self.model, run_config=self.run_config)
self.ofa_model._clear_search_space(self.images)
def init_config(self):
default_run_config = {'skip_layers': ['branch1.6']}
self.run_config = RunConfig(**default_run_config)
def test_shortcut(self):
self.ofa_model.set_epoch(0)
self.ofa_model.set_task('expand_ratio')
for i in range(5):
self.ofa_model(self.images)
assert list(self.ofa_model._ofa_layers.keys()) == ['branch2.0']
class TestShortcutSkiplayersCase1(TestShortcutSkiplayers):
def init_config(self):
default_run_config = {'skip_layers': ['conv1.0']}
self.run_config = RunConfig(**default_run_config)
class TestShortcutSkiplayersCase2(TestShortcutSkiplayers):
def init_config(self):
default_run_config = {'skip_layers': ['branch2.0']}
self.run_config = RunConfig(**default_run_config)
def test_shortcut(self):
assert list(self.ofa_model._ofa_layers.keys()) == ['conv1.0']
class TestInputDict(unittest.TestCase):
def setUp(self):
model = ModelInputDict()
sp_net_config = supernet(expand_ratio=[0.5, 1.0])
self.model = Convert(sp_net_config).convert(model)
self.images = paddle.randn(shape=[2, 3, 32, 32], dtype='float32')
self.images2 = {
'data': paddle.randn(
shape=[2, 12, 32, 32], dtype='float32')
}
default_run_config = {'skip_layers': ['conv1.0', 'conv2.0']}
self.run_config = RunConfig(**default_run_config)
self.ofa_model = OFA(self.model, run_config=self.run_config)
self.ofa_model._clear_search_space(self.images, data=self.images2)
def test_export(self):
config = self.ofa_model._sample_config(
task="expand_ratio", sample_type="smallest")
self.ofa_model.export(
config,
input_shapes=[[1, 3, 32, 32], {
'data': [1, 12, 32, 32]
}],
input_dtypes=['float32', 'float32'])
class TestInputDict(unittest.TestCase):
def setUp(self):
model = ModelInputDict()
sp_net_config = supernet(expand_ratio=[0.5, 1.0])
self.model = Convert(sp_net_config).convert(model)
self.images = paddle.randn(shape=[2, 3, 32, 32], dtype='float32')
self.images2 = {
'data': paddle.randn(
shape=[2, 12, 32, 32], dtype='float32')
}
default_run_config = {'skip_layers': ['conv1.0', 'conv2.0']}
self.run_config = RunConfig(**default_run_config)
self.ofa_model = OFA(self.model, run_config=self.run_config)
self.ofa_model._clear_search_space(self.images, data=self.images2)
def test_export(self):
config = self.ofa_model._sample_config(
task="expand_ratio", sample_type="smallest")
self.ofa_model.export(
config,
input_shapes=[[1, 3, 32, 32], {
'data': [1, 12, 32, 32]
}],
input_dtypes=['float32', 'float32'])
if __name__ == '__main__':
unittest.main()