Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / tests / test_dataset.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 import copy
17 from pathlib import Path
18 import pytest
19 from .common import make_representation
20 from accuracy_checker.config import ConfigError
21
22 from accuracy_checker.dataset import Dataset
23
24
25 def copy_dataset_config(config):
26     new_config = copy.deepcopy(config)
27
28     return new_config
29
30 class MockPreprocessor:
31     @staticmethod
32     def process(images):
33         return images
34
35
36 class TestDataset:
37     dataset_config = {
38             'name': 'custom',
39             'annotation': 'custom',
40             'data_source': 'custom',
41             'metrics': [{'type': 'map'}]
42         }
43
44     def test_missed_name_raises_config_error_exception(self):
45         local_dataset = copy_dataset_config(self.dataset_config)
46         local_dataset.pop('name')
47
48         with pytest.raises(ConfigError):
49             Dataset(local_dataset, MockPreprocessor())
50
51     def test_setting_custom_dataset_with_missed_annotation_raises_config_error_exception(self):
52         local_dataset = copy_dataset_config(self.dataset_config)
53         local_dataset.pop('annotation')
54         with pytest.raises(ConfigError):
55             Dataset(local_dataset, MockPreprocessor())
56
57     @pytest.mark.usefixtures('mock_path_exists')
58     def test_setting_custom_dataset_with_missed_data_source_raises_config_error_exception(self):
59         local_dataset = copy_dataset_config(self.dataset_config)
60         local_dataset.pop('data_source')
61         with pytest.raises(ConfigError):
62             Dataset(local_dataset, MockPreprocessor())
63
64
65 @pytest.mark.usefixtures('mock_path_exists')
66 class TestAnnotationConversion:
67     dataset_config = {
68         'name': 'custom',
69         'data_source': 'custom',
70         'metrics': [{'type': 'map'}]
71     }
72
73     def test_annotation_conversion_unknown_converter_raise_config_error(self):
74         addition_options = {'annotation_conversion': {'converter': 'unknown'}}
75         config = copy_dataset_config(self.dataset_config)
76         config.update(addition_options)
77         with pytest.raises(ValueError):
78             Dataset(config, MockPreprocessor())
79
80     def test_annotation_conversion_converter_without_required_options_raise_config_error(self):
81         addition_options = {'annotation_conversion': {'converter': 'wider'}}
82         config = copy_dataset_config(self.dataset_config)
83         config.update(addition_options)
84         with pytest.raises(ConfigError):
85             Dataset(config, MockPreprocessor())
86
87     def test_annotation_conversion_raise_config_error_on_extra_args(self):
88         addition_options = {'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file', 'something_extra': 'extra'}}
89         config = copy_dataset_config(self.dataset_config)
90         config.update(addition_options)
91         with pytest.raises(ConfigError):
92             Dataset(config, MockPreprocessor())
93
94     def test_sucessful_annotation_conversion(self, mocker):
95         addition_options = {'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'}}
96         config = copy_dataset_config(self.dataset_config)
97         config.update(addition_options)
98         annotation_converter_mock = mocker.patch(
99             'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
100             return_value=(make_representation("0 0 0 5 5", True), None)
101         )
102         Dataset(config, MockPreprocessor())
103         annotation_converter_mock.assert_called_once_with()
104
105     def test_annotation_conversion_with_store_annotation(self, mocker):
106         addition_options = {
107             'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
108             'annotation': 'custom'
109         }
110         config = copy_dataset_config(self.dataset_config)
111         config.update(addition_options)
112         converted_annotation = make_representation('0 0 0 5 5', True)
113         mocker.patch(
114             'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
115             return_value=(converted_annotation, None)
116         )
117         annotation_saver_mock = mocker.patch(
118             'accuracy_checker.dataset.save_annotation'
119         )
120         Dataset(config, MockPreprocessor())
121
122         annotation_saver_mock.assert_called_once_with(converted_annotation, None, Path('custom'), None)
123
124     def test_annotation_conversion_subset_size(self, mocker):
125         addition_options = {
126             'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
127             'subsample_size': 1
128         }
129         config = copy_dataset_config(self.dataset_config)
130         config.update(addition_options)
131         converted_annotation = make_representation(['0 0 0 5 5', '0 1 1 10 10'], True)
132         mocker.patch(
133             'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
134             return_value=(converted_annotation, None)
135         )
136         dataset = Dataset(config, MockPreprocessor())
137         assert dataset.annotation == [converted_annotation[1]]
138
139     def test_annotation_conversion_subset_ratio(self, mocker):
140         addition_options = {
141             'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
142             'subsample_size': '50%'
143         }
144         config = copy_dataset_config(self.dataset_config)
145         config.update(addition_options)
146         converted_annotation = make_representation(['0 0 0 5 5', '0 1 1 10 10'], True)
147         mocker.patch(
148             'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
149             return_value=(converted_annotation, None)
150         )
151         subset_maker_mock = mocker.patch(
152             'accuracy_checker.dataset.make_subset'
153         )
154         Dataset(config, MockPreprocessor())
155         subset_maker_mock.assert_called_once_with(converted_annotation, 1, 666)
156
157     def test_annotation_conversion_subset_with_seed(self, mocker):
158         addition_options = {
159             'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
160             'subsample_size': 1,
161             'subsample_seed': 1
162         }
163         config = copy_dataset_config(self.dataset_config)
164         config.update(addition_options)
165         converted_annotation = make_representation(['0 0 0 5 5', '0 1 1 10 10'], True)
166         mocker.patch(
167             'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
168             return_value=(converted_annotation, None)
169         )
170         dataset = Dataset(config, MockPreprocessor())
171         annotation = dataset.annotation
172         assert annotation == [converted_annotation[0]]
173
174     def test_annotation_conversion_save_subset(self, mocker):
175         addition_options = {
176             'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
177             'annotation': 'custom',
178             'subsample_size': 1,
179         }
180         config = copy_dataset_config(self.dataset_config)
181         config.update(addition_options)
182         converted_annotation = make_representation(['0 0 0 5 5', '0 1 1 10 10'], True)
183         mocker.patch(
184             'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
185             return_value=(converted_annotation, None)
186         )
187         annotation_saver_mock = mocker.patch(
188             'accuracy_checker.dataset.save_annotation'
189         )
190         Dataset(config, MockPreprocessor())
191         annotation_saver_mock.assert_called_once_with([converted_annotation[1]], None, Path('custom'), None)