forked from facebookresearch/ClassyVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_resnext_test.py
300 lines (263 loc) · 9.72 KB
/
models_resnext_test.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
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import collections
import unittest
import torch
import torchvision.models
from classy_vision.generic.util import get_torch_version
from classy_vision.models import build_model, ResNeXt
from test.generic.utils import compare_model_state
MODELS = {
"small_resnext": {
"name": "resnext",
"num_blocks": [1, 1, 1, 1],
"init_planes": 4,
"reduction": 4,
"base_width_and_cardinality": [2, 32],
"small_input": True,
"zero_init_bn_residuals": True,
"basic_layer": True,
"final_bn_relu": True,
"heads": [
{
"name": "fully_connected",
"unique_id": "default_head",
"num_classes": 1000,
"fork_block": "block3-0",
"in_plane": 128,
}
],
},
"small_resnet": {
"name": "resnet",
"num_blocks": [1, 1, 1, 1],
"init_planes": 4,
"reduction": 4,
"small_input": True,
"zero_init_bn_residuals": True,
"basic_layer": True,
"final_bn_relu": True,
"heads": [
{
"name": "fully_connected",
"unique_id": "default_head",
"num_classes": 1000,
"fork_block": "block3-0",
"in_plane": 128,
}
],
},
"small_resnet_se": {
"name": "resnet",
"num_blocks": [1, 1, 1, 1],
"init_planes": 4,
"reduction": 4,
"small_input": True,
"zero_init_bn_residuals": True,
"basic_layer": True,
"final_bn_relu": True,
"use_se": True,
"heads": [
{
"name": "fully_connected",
"unique_id": "default_head",
"num_classes": 1000,
"fork_block": "block3-0",
"in_plane": 128,
}
],
},
}
def _find_block_full_path(model, block_name):
"""Find the full path for a given block name
e.g. block3-1 --> 3.block3-1
"""
for name, _ in model.named_modules():
if name.endswith(block_name):
return name
return None
def _post_training_quantize(model, input):
if get_torch_version() >= [1, 11]:
import torch.ao.quantization as tq
from torch.ao.quantization.quantize_fx import convert_fx, prepare_fx
else:
import torch.quantization as tq
from torch.quantization.quantize_fx import convert_fx, prepare_fx
model.eval()
heads = model.get_heads()
# since prepare changes the code of ClassyBlock we need to clear head first
# and reattach it later to avoid caching
model.clear_heads()
prepare_custom_config_dict = {}
head_path_from_blocks = [
_find_block_full_path(model.blocks, block_name) for block_name in heads.keys()
]
# we need to keep the modules used in head standalone since
# it will be accessed with path name directly in execution
# TODO[quant-example-inputs]: Fix the shape if it is needed in quantization
standalone_example_inputs = (torch.rand(1, 3, 3, 3),)
prepare_custom_config_dict["standalone_module_name"] = [
(
head,
{"": tq.default_qconfig},
standalone_example_inputs,
{"input_quantized_idxs": [0], "output_quantized_idxs": []},
None,
)
for head in head_path_from_blocks
]
# TODO[quant-example-inputs]: Fix the shape if it is needed in quantization
example_inputs = (torch.rand(1, 3, 3, 3),)
model.initial_block = prepare_fx(
model.initial_block, {"": tq.default_qconfig}, example_inputs
)
model.blocks = prepare_fx(
model.blocks,
{"": tq.default_qconfig},
example_inputs,
prepare_custom_config_dict,
)
model.set_heads(heads)
# calibration
model(input)
heads = model.get_heads()
model.clear_heads()
model.initial_block = convert_fx(model.initial_block)
model.blocks = convert_fx(model.blocks)
model.set_heads(heads)
return model
class TestResnext(unittest.TestCase):
def _test_model(self, model_config):
"""This test will build ResNeXt-* models, run a forward pass and
verify output shape, and then verify that get / set state
works.
I do this in one test so that we construct the model a minimum
number of times.
"""
model = build_model(model_config)
# Verify forward pass works
input = torch.ones([1, 3, 32, 32])
output = model.forward(input)
self.assertEqual(output.size(), (1, 1000))
# Verify get_set_state
new_model = build_model(model_config)
state = model.get_classy_state()
new_model.set_classy_state(state)
new_state = new_model.get_classy_state()
compare_model_state(self, state, new_state, check_heads=True)
def _test_quantize_model(self, model_config):
"""This test will build ResNeXt-* models, quantize the model
with fx graph mode quantization, run a forward pass and
verify output shape, and then verify that get / set state
works.
"""
model = build_model(model_config)
# Verify forward pass works
input = torch.ones([1, 3, 32, 32])
output = model.forward(input)
self.assertEqual(output.size(), (1, 1000))
model = _post_training_quantize(model, input)
# Verify forward pass works
input = torch.ones([1, 3, 32, 32])
output = model.forward(input)
self.assertEqual(output.size(), (1, 1000))
# Verify get_set_state
new_model = build_model(model_config)
new_model = _post_training_quantize(new_model, input)
state = model.get_classy_state()
new_model.set_classy_state(state)
# TODO: test get state for new_model and make sure
# it is the same as state,
# Currently allclose is not supported in quantized tensors
# so we can't check this right now
def test_build_preset_model(self):
configs = [
{"name": "resnet18", "use_se": True},
{
"name": "resnet50",
"heads": [
{
"name": "fully_connected",
"unique_id": "default_head",
"num_classes": 1000,
"fork_block": "block3-2",
"in_plane": 2048,
}
],
},
{
"name": "resnext50_32x4d",
"heads": [
{
"name": "fully_connected",
"unique_id": "default_head",
"num_classes": 1000,
"fork_block": "block3-2",
"in_plane": 2048,
}
],
},
]
for config in configs:
model = build_model(config)
self.assertIsInstance(model, ResNeXt)
def test_small_resnext(self):
self._test_model(MODELS["small_resnext"])
@unittest.skipIf(
get_torch_version() < [1, 13],
"This test is using a new api of FX Graph Mode Quantization which is only available after 1.13"
)
def test_quantized_small_resnext(self):
self._test_quantize_model(MODELS["small_resnext"])
def test_small_resnet(self):
self._test_model(MODELS["small_resnet"])
@unittest.skipIf(
get_torch_version() < [1, 13],
"This test is using a new api of FX Graph Mode Quantization which is only available after 1.13"
)
def test_quantized_small_resnet(self):
self._test_quantize_model(MODELS["small_resnet"])
def test_small_resnet_se(self):
self._test_model(MODELS["small_resnet_se"])
@unittest.skipIf(
get_torch_version() < [1, 13],
"This test is using a new api of FX Graph Mode Quantization which is only available after 1.13"
)
def test_quantized_small_resnet_se(self):
self._test_quantize_model(MODELS["small_resnet_se"])
class TestTorchvisionEquivalence(unittest.TestCase):
@staticmethod
def tensor_sizes(state):
size_count = collections.defaultdict(int)
for key, value in state.items():
if key.startswith("fc."):
continue # "head" for torchvision
size_count[value.size()] += 1
return dict(size_count)
def assert_tensor_sizes_match_torchvision(self, model_name):
classy_model = build_model({"name": model_name})
torchvision_model = getattr(torchvision.models, model_name)(pretrained=False)
classy_sizes = self.tensor_sizes(
classy_model.get_classy_state()["model"]["trunk"]
)
torchvision_sizes = self.tensor_sizes(torchvision_model.state_dict())
self.assertEqual(
classy_sizes,
torchvision_sizes,
f"{model_name} tensor shapes do not match torchvision",
)
def test_resnet18(self):
"""Resnet18 tensor shapes should match torchvision."""
self.assert_tensor_sizes_match_torchvision("resnet18")
def test_resnet34(self):
"""Resnet34 tensor shapes should match torchvision."""
self.assert_tensor_sizes_match_torchvision("resnet34")
def test_resnet50(self):
"""Resnet50 tensor shapes should match torchvision."""
self.assert_tensor_sizes_match_torchvision("resnet50")
def test_resnext50_32x4d(self):
"""Resnext50_32x4d tensor shapes should match torchvision."""
self.assert_tensor_sizes_match_torchvision("resnext50_32x4d")