2 Copyright (c) 2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 from pathlib import Path
19 from .common import make_representation
20 from accuracy_checker.config import ConfigError
22 from accuracy_checker.dataset import Dataset
25 def copy_dataset_config(config):
26 new_config = copy.deepcopy(config)
30 class MockPreprocessor:
39 'annotation': 'custom',
40 'data_source': 'custom',
41 'metrics': [{'type': 'map'}]
44 def test_missed_name_raises_config_error_exception(self):
45 local_dataset = copy_dataset_config(self.dataset_config)
46 local_dataset.pop('name')
48 with pytest.raises(ConfigError):
49 Dataset(local_dataset, MockPreprocessor())
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())
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())
65 @pytest.mark.usefixtures('mock_path_exists')
66 class TestAnnotationConversion:
69 'data_source': 'custom',
70 'metrics': [{'type': 'map'}]
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())
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())
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())
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)
102 Dataset(config, MockPreprocessor())
103 annotation_converter_mock.assert_called_once_with()
105 def test_annotation_conversion_with_store_annotation(self, mocker):
107 'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
108 'annotation': 'custom'
110 config = copy_dataset_config(self.dataset_config)
111 config.update(addition_options)
112 converted_annotation = make_representation('0 0 0 5 5', True)
114 'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
115 return_value=(converted_annotation, None)
117 annotation_saver_mock = mocker.patch(
118 'accuracy_checker.dataset.save_annotation'
120 Dataset(config, MockPreprocessor())
122 annotation_saver_mock.assert_called_once_with(converted_annotation, None, Path('custom'), None)
124 def test_annotation_conversion_subset_size(self, mocker):
126 'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
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)
133 'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
134 return_value=(converted_annotation, None)
136 dataset = Dataset(config, MockPreprocessor())
137 assert dataset.annotation == [converted_annotation[1]]
139 def test_annotation_conversion_subset_ratio(self, mocker):
141 'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
142 'subsample_size': '50%'
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)
148 'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
149 return_value=(converted_annotation, None)
151 subset_maker_mock = mocker.patch(
152 'accuracy_checker.dataset.make_subset'
154 Dataset(config, MockPreprocessor())
155 subset_maker_mock.assert_called_once_with(converted_annotation, 1, 666)
157 def test_annotation_conversion_subset_with_seed(self, mocker):
159 'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
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)
167 'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
168 return_value=(converted_annotation, None)
170 dataset = Dataset(config, MockPreprocessor())
171 annotation = dataset.annotation
172 assert annotation == [converted_annotation[0]]
174 def test_annotation_conversion_save_subset(self, mocker):
176 'annotation_conversion': {'converter': 'wider', 'annotation_file': 'file'},
177 'annotation': 'custom',
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)
184 'accuracy_checker.annotation_converters.WiderFormatConverter.convert',
185 return_value=(converted_annotation, None)
187 annotation_saver_mock = mocker.patch(
188 'accuracy_checker.dataset.save_annotation'
190 Dataset(config, MockPreprocessor())
191 annotation_saver_mock.assert_called_once_with([converted_annotation[1]], None, Path('custom'), None)