Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / tests / test_adapters.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 import pytest
19
20 from accuracy_checker.adapters import SSDAdapter, Adapter
21 from accuracy_checker.config import ConfigError
22 from .common import make_representation
23
24
25 def test_detection_adapter():
26     raw = {
27         'detection_out': np.array([[[[0, 3, 0.2, 0, 0, 1, 1], [0, 2, 0.5, 4, 4, 7, 7], [0, 5, 0.7, 3, 3, 9, 8]]]])
28     }
29     expected = make_representation('0.2,3,0,0,1,1;0.5,2,4,4,7,7;0.7,5,3,3,9,8')
30
31     actual = SSDAdapter({}, output_blob='detection_out')([raw], ['0'])
32
33     assert np.array_equal(actual, expected)
34
35
36 def test_detection_adapter_partially_filling_output_blob():
37     raw = {
38         'detection_out': np.array(
39             [[[[0, 3, 0.2, 0, 0, 1, 1], [0, 2, 0.5, 4, 4, 7, 7], [0, 5, 0.7, 3, 3, 9, 8], [-1, 0, 0, 0, 0, 0, 0]]]]
40         )
41     }
42     expected = make_representation('0.2,3,0,0,1,1;0.5,2,4,4,7,7;0.7,5,3,3,9,8')
43
44     actual = SSDAdapter({}, output_blob='detection_out')([raw], ['0'])
45
46     assert np.array_equal(actual, expected)
47
48
49 def test_detection_adapter_partially_filling_output_blob_with_zeros_at_the_end():
50     raw = {
51         'detection_out': np.array([[[
52             [0,  3, 0.2, 0, 0, 1, 1],
53             [0,  2, 0.5, 4, 4, 7, 7],
54             [0,  5, 0.7, 3, 3, 9, 8],
55             [-1, 0, 0,   0, 0, 0, 0],
56             [0,  0, 0,   0, 0, 0, 0]
57         ]]])
58     }
59     expected = make_representation('0.2,3,0,0,1,1;0.5,2,4,4,7,7;0.7,5,3,3,9,8')
60
61     actual = SSDAdapter({}, output_blob='detection_out')([raw], ['0'])
62
63     assert np.array_equal(actual, expected)
64
65
66 def test_detection_adapter_batch_2():
67     raw = {
68         'detection_out': np.array([[[[0, 3, 0.2, 0, 0, 1, 1], [0, 2, 0.5, 4, 4, 7, 7], [1, 5, 0.7, 3, 3, 9, 8]]]])
69     }
70     expected = make_representation(['0.2,3,0,0,1,1;0.5,2,4,4,7,7', '0.7,5,3,3,9,8'])
71
72     actual = SSDAdapter({}, output_blob='detection_out')([raw], ['0', '1'])
73
74     assert np.array_equal(actual, expected)
75
76
77 def test_dictionary_adapter_no_raise_warning_on_specific_args():
78     adapter_config = {'type': 'age_gender', 'gender_out': 'gender', 'age_out': 'age'}
79     with pytest.warns(None) as record:
80         Adapter.provide('age_gender', adapter_config)
81         assert len(record) == 0
82
83
84 def test_age_gender_adapter_raise_config_error_on_extra_args():
85     adapter_config = {'type': 'age_gender', 'gender_out': 'gender', 'age_out': 'age', 'something_extra': 'extra'}
86     with pytest.raises(ConfigError):
87         Adapter.provide('age_gender', adapter_config)
88
89
90 def test_face_person_detection_adapter_raise_config_error_on_extra_args():
91     adapter_config = {
92         'type': 'face_person_detection',
93         'face_detection_out': 'face',
94         'person_detection_out': 'person',
95         'something_extra': 'extra'
96     }
97     with pytest.raises(ConfigError):
98         Adapter.provide('face_person_detection', adapter_config)
99
100
101 def test_head_pose_adapter_raise_config_error_on_extra_args():
102     adapter_config = {
103         'type': 'head_pose',
104         'angle_yaw': 'yaw',
105         'angle_pitch': 'pitch',
106         'angle_roll': 'roll',
107         'something_extra': 'extra'
108     }
109     with pytest.raises(ConfigError):
110         Adapter.provide('head_pose', adapter_config)
111
112
113 def test_vehicle_attributes_adapter_raise_config_error_on_extra_args():
114     adapter_config = {
115         'type': 'vehicle_attributes',
116         'color_out': 'color',
117         'type_out': 'type',
118         'something_extra': 'extra'
119     }
120     with pytest.raises(ConfigError):
121         Adapter.provide('vehicle_attributes', adapter_config)