6sys.path.insert(0, osp.dirname(osp.abspath(__file__)) +
'/../../..')
12Test the speed of cython-based evaluation code. The speed improvements
13can be much bigger when using the real reid data, which contains a larger
14amount of query and gallery images.
15Note: you might encounter the following error:
16 'AssertionError: Error: all query identities do not appear in gallery'.
17This is normal because the inputs are random numbers. Just try again.
20print(
'*** Compare running time ***')
26sys.path.insert(0, osp.dirname(osp.abspath(__file__)) + '/../../..')
27from fastreid.evaluation import evaluate_rank
28from fastreid.evaluation import evaluate_roc
33q_feats = np.random.rand(num_q, dim).astype(np.float32) * 20
34q_feats = q_feats / np.linalg.norm(q_feats, ord=2, axis=1, keepdims=True)
35g_feats = np.random.rand(num_g, dim).astype(np.float32) * 20
36g_feats = g_feats / np.linalg.norm(g_feats, ord=2, axis=1, keepdims=True)
37distmat = 1 - np.dot(q_feats, g_feats.transpose())
38q_pids = np.random.randint(0, num_q, size=num_q)
39g_pids = np.random.randint(0, num_g, size=num_g)
40q_camids = np.random.randint(0, 5, size=num_q)
41g_camids = np.random.randint(0, 5, size=num_g)
44print(
'=> Using CMC metric')
45pytime = timeit.timeit(
46 'evaluate_rank(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, max_rank, use_distmat=True, use_cython=False)',
50cytime = timeit.timeit(
51 'evaluate_rank(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, max_rank, use_distmat=True, use_cython=True)',
55print(
'Python time: {} s'.format(pytime))
56print(
'Cython time: {} s'.format(cytime))
57print(
'CMC Cython is {} times faster than python\n'.format(pytime / cytime))
59print(
'=> Using ROC metric')
60pytime = timeit.timeit(
61 'evaluate_roc(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, use_cython=False)',
65cytime = timeit.timeit(
66 'evaluate_roc(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, use_cython=True)',
70print(
'Python time: {} s'.format(pytime))
71print(
'Cython time: {} s'.format(cytime))
72print(
'ROC Cython is {} times faster than python\n'.format(pytime / cytime))
74print(
"=> Check precision")
79q_feats = np.random.rand(num_q, dim).astype(np.float32) * 20
80q_feats = q_feats / np.linalg.norm(q_feats, ord=2, axis=1, keepdims=
True)
81g_feats = np.random.rand(num_g, dim).astype(np.float32) * 20
82g_feats = g_feats / np.linalg.norm(g_feats, ord=2, axis=1, keepdims=
True)
83distmat = 1 - np.dot(q_feats, g_feats.transpose())
84q_pids = np.random.randint(0, num_q, size=num_q)
85g_pids = np.random.randint(0, num_g, size=num_g)
86q_camids = np.random.randint(0, 5, size=num_q)
87g_camids = np.random.randint(0, 5, size=num_g)
88cmc_py_d, mAP_py_d, mINP_py_d = evaluate_rank(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, max_rank,
89 use_distmat=
True, use_cython=
False)
90cmc_py, mAP_py, mINP_py = evaluate_rank(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, max_rank,
91 use_distmat=
False, use_cython=
False)
92np.testing.assert_allclose(cmc_py_d, cmc_py, rtol=1e-3, atol=1e-6)
93np.testing.assert_allclose(mAP_py_d, mAP_py, rtol=1e-3, atol=1e-6)
94np.testing.assert_allclose(mINP_py_d, mINP_py, rtol=1e-3, atol=1e-6)
95print(
'Results between distmat and features are the same in python!')
97cmc_cy_d, mAP_cy_d, mINP_cy_d = evaluate_rank(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, max_rank,
98 use_distmat=
True, use_cython=
True)
99cmc_cy, mAP_cy, mINP_cy = evaluate_rank(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, max_rank,
100 use_distmat=
False, use_cython=
True)
101np.testing.assert_allclose(cmc_cy_d, cmc_cy, rtol=1e-3, atol=1e-6)
102np.testing.assert_allclose(mAP_cy_d, mAP_cy, rtol=1e-3, atol=1e-6)
103np.testing.assert_allclose(mINP_cy_d, mINP_cy, rtol=1e-3, atol=1e-6)
104print(
'Results between distmat and features are the same in cython!')
106np.testing.assert_allclose(cmc_py, cmc_cy, rtol=1e-3, atol=1e-6)
107np.testing.assert_allclose(mAP_py, mAP_cy, rtol=1e-3, atol=1e-6)
108np.testing.assert_allclose(mINP_py, mINP_cy, rtol=1e-3, atol=1e-6)
109print(
'Rank results between python and cython are the same!')
111scores_cy, labels_cy = evaluate_roc(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids)
112scores_py, labels_py = evaluate_roc(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids,
115np.testing.assert_allclose(scores_cy, scores_py, rtol=1e-3, atol=1e-6)
116np.testing.assert_allclose(labels_cy, labels_py, rtol=1e-3, atol=1e-6)
117print(
'ROC results between python and cython are the same!\n')
119print(
"=> Check exact values")
120print(
"mAP = {} \ncmc = {}\nmINP = {}\nScores = {}".format(np.array(mAP_cy), cmc_cy, np.array(mINP_cy), scores_cy))