Safemotion Lib
Loading...
Searching...
No Matches
test_cython.py
Go to the documentation of this file.
1import sys
2import timeit
3import numpy as np
4import os.path as osp
5
6sys.path.insert(0, osp.dirname(osp.abspath(__file__)) + '/../../..')
7
8from fastreid.evaluation import evaluate_rank
9from fastreid.evaluation import evaluate_roc
10
11"""
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.
18"""
19
20print('*** Compare running time ***')
21
22setup = '''
23import sys
24import os.path as osp
25import numpy as np
26sys.path.insert(0, osp.dirname(osp.abspath(__file__)) + '/../../..')
27from fastreid.evaluation import evaluate_rank
28from fastreid.evaluation import evaluate_roc
29num_q = 30
30num_g = 300
31dim = 512
32max_rank = 5
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)
42'''
43
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)',
47 setup=setup,
48 number=20
49)
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)',
52 setup=setup,
53 number=20
54)
55print('Python time: {} s'.format(pytime))
56print('Cython time: {} s'.format(cytime))
57print('CMC Cython is {} times faster than python\n'.format(pytime / cytime))
58
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)',
62 setup=setup,
63 number=20
64)
65cytime = timeit.timeit(
66 'evaluate_roc(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, use_cython=True)',
67 setup=setup,
68 number=20
69)
70print('Python time: {} s'.format(pytime))
71print('Cython time: {} s'.format(cytime))
72print('ROC Cython is {} times faster than python\n'.format(pytime / cytime))
73
74print("=> Check precision")
75num_q = 30
76num_g = 300
77dim = 512
78max_rank = 5
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!')
96
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!')
105
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!')
110
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,
113 use_cython=False)
114
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')
118
119print("=> Check exact values")
120print("mAP = {} \ncmc = {}\nmINP = {}\nScores = {}".format(np.array(mAP_cy), cmc_cy, np.array(mINP_cy), scores_cy))