This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
/
test_models.py
executable file
·167 lines (129 loc) · 5.61 KB
/
test_models.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
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""Units test for all models in model zoo."""
import datetime
import json
import os
import shutil
import tempfile
import unittest
import pycls.core.builders as builders
import pycls.core.distributed as dist
import pycls.core.logging as logging
import pycls.core.net as net
import pycls.core.trainer as trainer
import pycls.models.model_zoo as model_zoo
from parameterized import parameterized
from pycls.core.config import cfg, load_cfg, reset_cfg
# Location of pycls directory
_PYCLS_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
# If True run selected tests
_RUN_COMPLEXITY_TESTS = True
_RUN_ERROR_TESTS = False
_RUN_TIMING_TESTS = False
def test_complexity(key):
"""Measure the complexity of a single model."""
reset_cfg()
cfg_file = os.path.join(_PYCLS_DIR, key)
load_cfg(cfg_file)
return net.complexity(builders.get_model())
def test_timing(key):
"""Measure the timing of a single model."""
reset_cfg()
load_cfg(model_zoo.get_config_file(key))
cfg.PREC_TIME.WARMUP_ITER, cfg.PREC_TIME.NUM_ITER = 5, 50
cfg.OUT_DIR, cfg.LOG_DEST = tempfile.mkdtemp(), "file"
dist.multi_proc_run(num_proc=cfg.NUM_GPUS, fun=trainer.time_model)
log_file = os.path.join(cfg.OUT_DIR, "stdout.log")
data = logging.sort_log_data(logging.load_log_data(log_file))["iter_times"]
shutil.rmtree(cfg.OUT_DIR)
return data
def test_error(key):
"""Measure the error of a single model."""
reset_cfg()
load_cfg(model_zoo.get_config_file(key))
cfg.TEST.WEIGHTS = model_zoo.get_weights_file(key)
cfg.OUT_DIR, cfg.LOG_DEST = tempfile.mkdtemp(), "file"
dist.multi_proc_run(num_proc=cfg.NUM_GPUS, fun=trainer.test_model)
log_file = os.path.join(cfg.OUT_DIR, "stdout.log")
data = logging.sort_log_data(logging.load_log_data(log_file))["test_epoch"]
data = {"top1_err": data["top1_err"][-1], "top5_err": data["top5_err"][-1]}
shutil.rmtree(cfg.OUT_DIR)
return data
def generate_complexity_tests():
"""Generate complexity tests for every model in the configs directory."""
configs_dir = os.path.join(_PYCLS_DIR, "configs")
keys = [os.path.join(r, f) for r, _, fs in os.walk(configs_dir) for f in fs]
keys = [os.path.relpath(k, _PYCLS_DIR) for k in keys if ".yaml" in k]
generate_tests("complexity", test_complexity, keys)
def generate_timing_tests():
"""Generate timing tests for every model in the model zoo."""
keys = model_zoo.get_model_list()
generate_tests("timing", test_timing, keys)
def generate_error_tests():
"""Generate error tests for every model in the model zoo."""
keys = model_zoo.get_model_list()
generate_tests("error", test_error, keys)
def generate_tests(test_name, test_fun, keys):
"""Generate and store all the unit tests."""
data = load_test_data(test_name)
keys = sorted(k for k in keys if k not in data)
for key in keys:
data[key] = test_fun(key)
print("data['{}'] = {}".format(key, data[key]))
save_test_data(data, test_name)
def save_test_data(data, test_name):
"""Save the data file for a given set of tests."""
filename = os.path.join(_PYCLS_DIR, "dev/model_{}.json".format(test_name))
with open(filename, "w") as file:
data["date-created"] = str(datetime.datetime.now())
json.dump(data, file, sort_keys=True, indent=4)
def load_test_data(test_name):
"""Load the data file for a given set of tests."""
filename = os.path.join(_PYCLS_DIR, "dev/model_{}.json".format(test_name))
if not os.path.exists(filename):
return {}
with open(filename, "r") as f:
return json.load(f)
def parse_tests(data):
"""Parse the data file in a format useful for the unit tests."""
return [[f, data[f]] for f in data if f != "date-created"]
class TestComplexity(unittest.TestCase):
"""Generates unit tests for complexity."""
@parameterized.expand(parse_tests(load_test_data("complexity")), skip_on_empty=True)
@unittest.skipIf(not _RUN_COMPLEXITY_TESTS, "Skipping complexity tests")
def test(self, key, out_expected):
print("Testing complexity of: {}".format(key))
out = test_complexity(key)
self.assertEqual(out, out_expected)
class TestError(unittest.TestCase):
"""Generates unit tests for error."""
@parameterized.expand(parse_tests(load_test_data("error")), skip_on_empty=True)
@unittest.skipIf(not _RUN_ERROR_TESTS, "Skipping error tests")
def test(self, key, out_expected):
print("\nTesting error of: {}".format(key))
out = test_error(key)
print("expected = {}".format(out_expected))
print("measured = {}".format(out))
for k in out.keys():
self.assertAlmostEqual(out[k], out_expected[k], 2)
class TestTiming(unittest.TestCase):
"""Generates unit tests for timing."""
@parameterized.expand(parse_tests(load_test_data("timing")), skip_on_empty=True)
@unittest.skipIf(not _RUN_TIMING_TESTS, "Skipping timing tests")
def test(self, key, out_expected):
print("\nTesting timing of: {}".format(key))
out = test_timing(key)
print("expected = {}".format(out_expected))
print("measured = {}".format(out))
for k in out.keys():
self.assertLessEqual(out[k] / out_expected[k], 1.10)
self.assertLessEqual(out_expected[k] / out[k], 1.10)
if __name__ == "__main__":
# generate_complexity_tests()
# generate_timing_tests()
# generate_error_tests()
unittest.main()