Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / tests / test_reid_metrics.py
1 """
2 Copyright (c) 2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 """
16
17 import numpy as np
18 from accuracy_checker.metrics.reid import eval_cmc
19
20
21 class TestCMC:
22     def test_only_distance_matrix(self):
23         distance_matrix = np.array([
24             [0, 1, 2, 3, 4],
25             [1, 0, 2, 3, 4],
26             [0, 1, 2, 3, 4],
27             [0, 1, 2, 3, 4],
28             [1, 2, 3, 4, 0]
29         ])
30         m, n = distance_matrix.shape
31
32         result = eval_cmc(
33             distance_matrix,
34             query_ids=np.arange(m),
35             gallery_ids=np.arange(n),
36             query_cams=np.zeros(m).astype(np.int32),
37             gallery_cams=np.ones(n).astype(np.int32)
38         )
39
40         assert np.all(result[:5] == [0.6, 0.6, 0.8, 1.0, 1.0])
41
42     def test_duplicate_ids(self):
43         distance_matrix = np.array([
44             [0, 1, 2, 3],
45             [0, 1, 2, 3],
46             [0, 1, 2, 3],
47             [0, 1, 2, 3]
48         ])
49
50         result = eval_cmc(
51             distance_matrix,
52             query_ids=np.array([0, 0, 1, 1]),
53             gallery_ids=np.array([0, 0, 1, 1]),
54             top_k=4,
55             gallery_cams=np.ones(distance_matrix.shape[1]).astype(np.int32),
56             query_cams=np.zeros(distance_matrix.shape[0]).astype(np.int32),
57             separate_camera_set=False,
58             single_gallery_shot=False
59         )
60
61         assert np.all(result == [0.5, 0.5, 1, 1])
62
63     def test_duplicate_cams(self):
64         distance_matrix = np.tile(np.arange(5), (5, 1))
65
66         result = eval_cmc(
67             distance_matrix,
68             query_ids=np.array([0, 0, 0, 1, 1]),
69             gallery_ids=np.array([0, 0, 0, 1, 1]),
70             query_cams=np.array([0, 0, 0, 0, 0]),
71             gallery_cams=np.array([0, 1, 1, 1, 1]),
72             top_k=5,
73             separate_camera_set=False,
74             single_gallery_shot=False
75         )
76
77         assert np.all(result == [0.6, 0.6, 0.6, 1, 1])