Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / tests / test_dlsdk_launcher.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 subprocess
18
19 import pytest
20
21 pytest.importorskip('accuracy_checker.launcher.dlsdk_launcher')
22 import os
23 import cv2
24 import numpy as np
25
26 from pathlib import Path
27 from unittest.mock import PropertyMock
28 from accuracy_checker.config import ConfigError
29 from accuracy_checker.launcher import DLSDKLauncher
30 from accuracy_checker.launcher.dlsdk_launcher import DLSDKLauncherConfig
31 from accuracy_checker.launcher.launcher import create_launcher
32 from tests.common import update_dict
33 from accuracy_checker.dataset import DataRepresentation
34 from accuracy_checker.utils import contains_all
35
36
37 @pytest.fixture()
38 def mock_inference_engine(mocker):
39     try:
40         mocker.patch('openvino.inference_engine.IEPlugin')
41         mocker.patch('openvino.inference_engine.IENetwork')
42     except ImportError:
43         mocker.patch('inference_engine.IEPlugin')
44         mocker.patch('inference_engine.IENetwork')
45
46
47 @pytest.fixture()
48 def mock_inputs(mocker):
49     mocker.patch(
50         'accuracy_checker.launcher.input_feeder.InputFeeder._parse_inputs_config', return_value=({}, ['data'], None)
51     )
52
53
54 def get_dlsdk_test_model(models_dir, config_update=None):
55     config = {
56         'framework': 'dlsdk',
57         'weights': str(models_dir / 'SampLeNet.bin'),
58         'model': str(models_dir / 'SampLeNet.xml'),
59         'device': 'CPU',
60         'adapter': 'classification',
61         '_models_prefix': str(models_dir)
62     }
63     if config_update:
64         config.update(config_update)
65
66     return create_launcher(config)
67
68
69 def get_image(image_path, input_shape):
70     _, h, w = input_shape
71     img_raw = cv2.imread(str(image_path))
72
73     return DataRepresentation(cv2.resize(img_raw, (w, h)))
74
75
76 class TestDLSDKLauncherInfer:
77     def test_infer(self, data_dir, models_dir):
78         dlsdk_test_model = get_dlsdk_test_model(models_dir)
79         result = dlsdk_test_model.predict(['1.jpg'], [get_image(data_dir / '1.jpg', dlsdk_test_model.inputs['data'])])
80
81         assert dlsdk_test_model.adapter.output_blob == 'fc3'
82         assert result[0].label == 6
83
84     def test_launcher_creates(self, models_dir):
85         assert get_dlsdk_test_model(models_dir).inputs['data'] == [3, 32, 32]
86
87     def test_infer_with_additional_outputs(self, data_dir, models_dir):
88         dlsdk_test_model = get_dlsdk_test_model(models_dir, {'outputs': ['fc1', 'fc2']})
89         result = dlsdk_test_model.predict(['1.jpg'], [get_image(data_dir / '1.jpg', dlsdk_test_model.inputs['data'])])
90         outputs = list(dlsdk_test_model.network.outputs.keys())
91         adapter_output_blob = dlsdk_test_model.adapter.output_blob
92
93         assert contains_all(outputs, ['fc1', 'fc2', 'fc3'])
94         assert adapter_output_blob == 'fc3'
95         assert result[0].label == 6
96
97     def test_dlsdk_launcher_provide_input_shape_to_adapter(self, mocker, models_dir):
98         raw_results = {}
99
100         def raw_results_callback(outputs):
101             raw_results.update(outputs)
102
103         launcher = get_dlsdk_test_model(models_dir)
104
105         adapter_mock = mocker.patch('accuracy_checker.adapters.ClassificationAdapter.process')
106         launcher.predict(['1.png'], [DataRepresentation(np.zeros((32, 32, 3)))], output_callback=raw_results_callback)
107         adapter_mock.assert_called_once_with([raw_results], ['1.png'], [{'input_shape': {'data': [3, 32, 32]}, 'image_size': (32, 32, 3)}])
108
109     def test_dlsd_launcher_set_batch_size(self, models_dir):
110         dlsdk_test_model = get_dlsdk_test_model(models_dir, {'batch': 2})
111         assert dlsdk_test_model.batch == 2
112
113
114 @pytest.mark.usefixtures('mock_path_exists')
115 class TestDLSDKLauncherAffinity:
116     def test_dlsdk_launcher_valid_affinity_map(self, mocker, models_dir):
117         affinity_map = {'conv1' : 'GPU'}
118
119         mocker.patch(
120             'accuracy_checker.launcher.dlsdk_launcher.read_yaml', return_value=affinity_map
121         )
122
123         dlsdk_test_model = get_dlsdk_test_model(models_dir, {'device' : 'HETERO:CPU,GPU', 'affinity_map' : './affinity_map.yml'})
124         layers = dlsdk_test_model.network.layers
125         for key, value in affinity_map.items():
126             assert layers[key].affinity == value
127
128     def test_dlsdk_launcher_affinity_map_invalid_device(self, mocker, models_dir):
129         affinity_map = {'conv1' : 'GPU'}
130
131         mocker.patch(
132             'accuracy_checker.launcher.dlsdk_launcher.read_yaml', return_value=affinity_map
133         )
134
135         with pytest.raises(ConfigError):
136             get_dlsdk_test_model(models_dir, {'device' : 'HETERO:CPU,CPU', 'affinity_map' : './affinity_map.yml'})
137
138     def test_dlsdk_launcher_affinity_map_invalid_layer(self, mocker, models_dir):
139         affinity_map = {'none-existing-layer' : 'CPU'}
140
141         mocker.patch(
142             'accuracy_checker.launcher.dlsdk_launcher.read_yaml', return_value=affinity_map
143         )
144
145         with pytest.raises(ConfigError):
146             get_dlsdk_test_model(models_dir, {'device' : 'HETERO:CPU,CPU', 'affinity_map' : './affinity_map.yml'})
147
148
149 @pytest.mark.usefixtures('mock_path_exists', 'mock_inference_engine', 'mock_inputs')
150 class TestDLSDKLauncher:
151     def test_program_bitsream_when_device_is_fpga(self, mocker):
152         subprocess_mock = mocker.patch('subprocess.run')
153         config = {
154             'framework': 'dlsdk',
155             'weights': 'custom_weights',
156             'model': 'custom_model',
157             'device': 'fpga',
158             'bitstream': Path('custom_bitstream'),
159             'adapter': 'classification',
160             '_models_prefix': 'prefix',
161             '_aocl': Path('aocl')
162         }
163         launcher = create_launcher(config, {'label_map': {}})
164         subprocess_mock.assert_called_once_with(['aocl', 'program', 'acl0', 'custom_bitstream'])
165         launcher.release()
166
167     def test_program_bitsream_when_fpga_in_hetero_device(self, mocker):
168         subprocess_mock = mocker.patch('subprocess.run')
169         config = {
170             'framework': 'dlsdk',
171             'weights': 'custom_weights',
172             'model': 'custom_model',
173             'device': 'hetero:fpga,cpu',
174             'bitstream': Path('custom_bitstream'),
175             'adapter': 'classification',
176             '_models_prefix': 'prefix',
177             '_aocl': Path('aocl')
178         }
179         launcher = create_launcher(config, {'label_map': {}})
180         subprocess_mock.assert_called_once_with(['aocl', 'program', 'acl0', 'custom_bitstream'])
181         launcher.release()
182
183     def test_does_not_program_bitsream_when_device_is_not_fpga(self, mocker):
184         subprocess_mock = mocker.patch('subprocess.run')
185         config = {
186             'framework': 'dlsdk',
187             'weights': 'custom_weights',
188             'model': 'custom_model',
189             'device': 'cpu',
190             'bitstream': Path('custom_bitstream'),
191             'adapter': 'classification',
192             '_models_prefix': 'prefix',
193             '_aocl': Path('aocl')
194         }
195         create_launcher(config)
196         subprocess_mock.assert_not_called()
197
198     def test_does_not_program_bitsream_when_hetero_without_fpga(self, mocker):
199         subprocess_mock = mocker.patch('subprocess.run')
200
201         config = {
202             'framework': 'dlsdk',
203             'weights': 'custom_weights',
204             'model': 'custom_model',
205             'device': 'hetero:cpu,cpu',
206             'bitstream': Path('custom_bitstream'),
207             'adapter': 'classification',
208             '_models_prefix': 'prefix',
209             '_aocl': Path('aocl')
210         }
211         create_launcher(config)
212         subprocess_mock.assert_not_called()
213
214     def test_does_not_program_bitstream_if_compiler_mode_3_in_env_when_fpga_in_hetero_device(self, mocker):
215         subprocess_mock = mocker.patch('subprocess.run')
216         mocker.patch('os.environ.get', return_value='3')
217
218         config = {
219             'framework': 'dlsdk',
220             'weights': 'custom_weights',
221             'model': 'custom_model',
222             'device': 'hetero:fpga,cpu',
223             'bitstream': Path('custom_bitstream'),
224             'adapter': 'classification',
225             '_models_prefix': 'prefix',
226             '_aocl': Path('aocl')
227         }
228         create_launcher(config)
229
230         subprocess_mock.assert_not_called()
231
232     def test_does_not_program_bitstream_if_compiler_mode_3_in_env_when_fpga_in_device(self, mocker):
233         subprocess_mock = mocker.patch('subprocess.run')
234         mocker.patch('os.environ.get', return_value='3')
235
236         config = {
237             'framework': 'dlsdk',
238             'weights': 'custom_weights',
239             'model': 'custom_model',
240             'device': 'fpga',
241             'bitstream': Path('custom_bitstream'),
242             'adapter': 'classification',
243             '_models_prefix': 'prefix',
244             '_aocl': Path('aocl')
245         }
246         create_launcher(config)
247
248         subprocess_mock.assert_not_called()
249
250     def test_sets_dla_aocx_when_device_is_fpga(self, mocker):
251         mocker.patch('os.environ')
252
253         config = {
254             'framework': 'dlsdk',
255             'weights': 'custom_weights',
256             'model': 'custom_model',
257             'device': 'fpga',
258             'bitstream': Path('custom_bitstream'),
259             'adapter': 'classification',
260             '_models_prefix': 'prefix'
261         }
262         create_launcher(config, {'label_map': {}})
263
264         os.environ.__setitem__.assert_called_once_with('DLA_AOCX', 'custom_bitstream')
265
266     def test_sets_dla_aocx_when_fpga_in_hetero_device(self, mocker):
267         mocker.patch('os.environ')
268
269         config = {
270             'framework': 'dlsdk',
271             'weights': 'custom_weights',
272             'model': 'custom_model',
273             'device': 'hetero:fpga,cpu',
274             'bitstream': Path('custom_bitstream'),
275             'adapter': 'classification',
276             '_models_prefix': 'prefix'
277         }
278         create_launcher(config, {'label_map': {}})
279         os.environ.__setitem__.assert_called_once_with('DLA_AOCX', 'custom_bitstream')
280
281     def test_does_not_set_dla_aocx_when_device_is_not_fpga(self, mocker):
282         mocker.patch('os.environ')
283
284         config = {
285             'framework': 'dlsdk',
286             'weights': 'custom_weights',
287             'model': 'custom_model',
288             'device': 'cpu',
289             'bitstream': 'custom_bitstream',
290             'adapter': 'classification',
291             '_models_prefix': 'prefix'
292         }
293         create_launcher(config)
294
295         os.environ.__setitem__.assert_not_called()
296
297     def test_does_not_set_dla_aocx_when_hetero_without_fpga(self, mocker):
298         mocker.patch('os.environ')
299
300         config = {
301             'framework': 'dlsdk',
302             'weights': 'custom_weights',
303             'model': 'custom_model',
304             'device': 'hetero:cpu,cpu',
305             'bitstream': 'custom_bitstream',
306             'adapter': 'classification',
307             '_models_prefix': 'prefix'
308         }
309         create_launcher(config)
310
311         os.environ.__setitem__.assert_not_called()
312
313     def test_does_not_set_dla_aocx_if_compiler_mode_3_in_env_when_fpga_in_hetero_device(self, mocker):
314         mocker.patch('os.environ')
315         mocker.patch('os.environ.get', return_value='3')
316
317         config = {
318             'framework': 'dlsdk',
319             'weights': 'custom_weights',
320             'model': 'custom_model',
321             'device': 'hetero:fpga,cpu',
322             'bitstream': 'custom_bitstream',
323             'adapter': 'classification',
324             '_models_prefix': 'prefix'
325         }
326         create_launcher(config)
327
328         os.environ.__setitem__.assert_not_called()
329
330     def test_does_not_set_dla_aocx_if_compiler_mode_3_in_env_when_fpga_in_device(self, mocker):
331         mocker.patch('os.environ')
332         mocker.patch('os.environ.get', return_value='3')
333
334         config = {
335             'framework': 'dlsdk',
336             'weights': 'custom_weights',
337             'model': 'custom_model',
338             'device': 'fpga',
339             'bitstream': 'custom_bitstream',
340             'adapter': 'classification',
341             '_models_prefix': 'prefix'
342         }
343         create_launcher(config)
344
345         os.environ.__setitem__.assert_not_called()
346
347     def test_model_converted_from_caffe(self, mocker):
348         mock = mocker.patch(
349             'accuracy_checker.launcher.dlsdk_launcher.convert_model',
350             return_value=('converted_model', 'converted_weights')
351         )
352
353         config = {
354             'framework': 'dlsdk',
355             'caffe_model': '/path/to/source_models/custom_model',
356             'caffe_weights': '/path/to/source_models/custom_weights',
357             "device": 'cpu',
358             'bitstream': Path('custom_bitstream'),
359             '_models_prefix': '/path/to/source_models',
360             'adapter': 'classification'
361         }
362         DLSDKLauncher(config, dummy_adapter)
363
364         mock.assert_called_once_with(
365             'custom_model', '/path/to/source_models/custom_model', '/path/to/source_models/custom_weights', 'caffe',
366             [], None, None, None, None
367         )
368
369     def test_model_converted_with_mo_params(self, mocker):
370         mock = mocker.patch(
371             'accuracy_checker.launcher.dlsdk_launcher.convert_model',
372             return_value=('converted_model', 'converted_weights')
373         )
374
375         config = {
376             'framework': "dlsdk",
377             'caffe_model': '/path/to/source_models/custom_model',
378             'caffe_weights': '/path/to/source_models/custom_weights',
379             'device': 'cpu',
380             'bitstream': Path('custom_bitstream'),
381             '_models_prefix': '/path/to/source_models',
382             'mo_params': {'data_type': 'FP16'},
383             'adapter': 'classification'
384         }
385         DLSDKLauncher(config, dummy_adapter)
386
387         mock.assert_called_once_with(
388             'custom_model', '/path/to/source_models/custom_model', '/path/to/source_models/custom_weights', 'caffe',
389             [], {'data_type': 'FP16'}, None, None, None
390         )
391
392     def test_model_converted_with_mo_flags(self, mocker):
393         mock = mocker.patch(
394             'accuracy_checker.launcher.dlsdk_launcher.convert_model',
395             return_value=('converted_model', 'converted_weights')
396         )
397
398         config = {
399             'framework': 'dlsdk',
400             'caffe_model': '/path/to/source_models/custom_model',
401             'caffe_weights': '/path/to/source_models/custom_weights',
402             'device': 'cpu',
403             'bitstream': Path('custom_bitstream'),
404             '_models_prefix': '/path/to/source_models',
405             'mo_flags': ['reverse_input_channels'],
406             'adapter': 'classification'
407         }
408
409         DLSDKLauncher(config, dummy_adapter)
410
411         mock.assert_called_once_with(
412             'custom_model', '/path/to/source_models/custom_model', '/path/to/source_models/custom_weights', 'caffe',
413             [], None, ['reverse_input_channels'], None, None
414         )
415
416     def test_model_converted_to_output_dir_in_mo_params(self, mocker):
417         config = {
418             'framework': 'dlsdk',
419             'tf_model': '/path/to/source_models/custom_model',
420             'device': 'cpu',
421             '_models_prefix': '/path/to',
422             'adapter': 'classification',
423             'mo_params': {'output_dir': '/path/to/output/models'}
424         }
425         mocker.patch('accuracy_checker.launcher.model_conversion.find_mo', return_value='ModelOptimizer')
426         prepare_args_patch = mocker.patch('accuracy_checker.launcher.model_conversion.prepare_args')
427         args = {
428             'input_model': '/path/to/source_models/custom_model',
429             'model_name': 'custom_model',
430             'output_dir': '/path/to/output/models',
431             'framework': 'tf'
432         }
433
434         mocker.patch(
435             'accuracy_checker.launcher.model_conversion.exec_mo_binary',
436             return_value=subprocess.CompletedProcess(args, returncode=0)
437         )
438         DLSDKLauncher(config, dummy_adapter)
439         prepare_args_patch.assert_called_once_with('ModelOptimizer', flag_options=[], value_options=args)
440
441     def test_model_converted_from_tf(self, mocker):
442         mock = mocker.patch(
443             'accuracy_checker.launcher.dlsdk_launcher.convert_model',
444             return_value=('converted_model', 'converted_weights')
445         )
446
447         config = {
448             'framework': 'dlsdk',
449             'tf_model': '/path/to/source_models/custom_model',
450             'device': 'cpu',
451             '_models_prefix': '/path/to/source_models',
452             'adapter': 'classification'
453         }
454         DLSDKLauncher(config, dummy_adapter)
455
456         mock.assert_called_once_with(
457             'custom_model', '/path/to/source_models/custom_model', '', 'tf', [], None, None, None, None
458         )
459
460     def test_model_converted_from_tf_with_arg_path_to_custom_tf_config(self, mocker):
461         config = {
462             'framework': 'dlsdk',
463             'tf_model': '/path/to/source_models/custom_model',
464             'device': 'cpu',
465             '_models_prefix': '/path/to',
466             'adapter': 'classification',
467             'mo_params': {'tensorflow_use_custom_operations_config': 'ssd_v2_support.json'},
468             '_tf_custom_op_config_dir': 'config/dir'
469         }
470         mocker.patch('accuracy_checker.launcher.model_conversion.find_mo', return_value=Path('/path/ModelOptimizer'))
471         prepare_args_patch = mocker.patch('accuracy_checker.launcher.model_conversion.prepare_args')
472
473         args = {
474             'input_model': '/path/to/source_models/custom_model',
475             'model_name': 'custom_model',
476             'framework': 'tf',
477             'tensorflow_use_custom_operations_config': 'config/dir/ssd_v2_support.json'
478         }
479
480         mocker.patch(
481             'accuracy_checker.launcher.model_conversion.exec_mo_binary',
482             return_value=subprocess.CompletedProcess(args, returncode=0)
483         )
484         DLSDKLauncher(config, dummy_adapter)
485         prepare_args_patch.assert_called_once_with('/path/ModelOptimizer', flag_options=[], value_options=args)
486
487     def test_model_converted_from_tf_with_default_path_to_custom_tf_config(self, mocker):
488         config = {
489             'framework': 'dlsdk',
490             'tf_model': '/path/to/source_models/custom_model',
491             'device': 'cpu',
492             '_models_prefix': '/path/to',
493             'adapter': 'classification',
494             'mo_params': {'tensorflow_use_custom_operations_config': 'config.json'}
495         }
496         mocker.patch('accuracy_checker.launcher.model_conversion.find_mo', return_value=Path('/path/ModelOptimizer'))
497         prepare_args_patch = mocker.patch('accuracy_checker.launcher.model_conversion.prepare_args')
498
499         args = {
500             'input_model': '/path/to/source_models/custom_model',
501             'model_name': 'custom_model',
502             'framework': 'tf',
503             'tensorflow_use_custom_operations_config': '/path/extensions/front/tf/config.json'
504         }
505
506         mocker.patch(
507             'accuracy_checker.launcher.model_conversion.exec_mo_binary',
508             return_value=subprocess.CompletedProcess(args, returncode=0)
509         )
510         DLSDKLauncher(config, dummy_adapter)
511         prepare_args_patch.assert_called_once_with('/path/ModelOptimizer', flag_options=[], value_options=args)
512
513     def test_model_converted_from_tf_with_default_path_to_obj_detection_api_config(self, mocker):
514         config = {
515             'framework': 'dlsdk',
516             'tf_model': '/path/to/source_models/custom_model',
517             'device': 'cpu',
518             '_models_prefix': '/path/to',
519             'adapter': 'classification',
520             'mo_params': {'tensorflow_object_detection_api_pipeline_config': 'operations.config'},
521             '_tf_obj_detection_api_pipeline_config_path': None
522         }
523         mocker.patch('accuracy_checker.launcher.model_conversion.find_mo', return_value=Path('/path/ModelOptimizer'))
524         prepare_args_patch = mocker.patch('accuracy_checker.launcher.model_conversion.prepare_args')
525
526         args = {
527             'input_model': '/path/to/source_models/custom_model',
528             'model_name': 'custom_model',
529             'framework': 'tf',
530             'tensorflow_object_detection_api_pipeline_config': '/path/to/source_models/operations.config'
531         }
532
533         mocker.patch(
534             'accuracy_checker.launcher.model_conversion.exec_mo_binary',
535             return_value=subprocess.CompletedProcess(args, returncode=0)
536         )
537         DLSDKLauncher(config, dummy_adapter)
538         prepare_args_patch.assert_called_once_with('/path/ModelOptimizer', flag_options=[], value_options=args)
539
540     def test_model_converted_from_tf_with_arg_path_to_obj_detection_api_config(self, mocker):
541         config = {
542             'framework': 'dlsdk',
543             'tf_model': '/path/to/source_models/custom_model',
544             'device': 'cpu',
545             '_models_prefix': '/path/to',
546             'adapter': 'classification',
547             'mo_params': {'tensorflow_object_detection_api_pipeline_config': 'operations.config'},
548             '_tf_custom_op_config_dir': 'config/dir',
549             '_tf_obj_detection_api_pipeline_config_path': 'od_api'
550         }
551         mocker.patch('accuracy_checker.launcher.model_conversion.find_mo', return_value=Path('/path/ModelOptimizer'))
552         prepare_args_patch = mocker.patch('accuracy_checker.launcher.model_conversion.prepare_args')
553
554         args = {
555             'input_model': '/path/to/source_models/custom_model',
556             'model_name': 'custom_model',
557             'framework': 'tf',
558             'tensorflow_object_detection_api_pipeline_config': 'od_api/operations.config'
559         }
560
561         mocker.patch(
562             'accuracy_checker.launcher.model_conversion.exec_mo_binary',
563             return_value=subprocess.CompletedProcess(args, returncode=0)
564         )
565         DLSDKLauncher(config, dummy_adapter)
566         prepare_args_patch.assert_called_once_with('/path/ModelOptimizer', flag_options=[], value_options=args)
567
568     def test_model_converted_from_mxnet(self, mocker):
569         mock = mocker.patch(
570             'accuracy_checker.launcher.dlsdk_launcher.convert_model',
571             return_value=('converted_model', 'converted_weights')
572         )
573
574         config = {
575             'framework': 'dlsdk',
576             'mxnet_weights': '/path/to/source_models/custom_weights',
577             'device': 'cpu',
578             '_models_prefix': '/path/to/source_models',
579             'adapter': 'classification'
580         }
581         DLSDKLauncher(config, dummy_adapter)
582
583         mock.assert_called_once_with(
584             'custom_weights', '', '/path/to/source_models/custom_weights', 'mxnet', [], None, None, None, None
585         )
586
587     def test_model_converted_from_onnx(self, mocker):
588         mock = mocker.patch(
589             'accuracy_checker.launcher.dlsdk_launcher.convert_model',
590             return_value=('converted_model', 'converted_weights')
591         )
592
593         config = {
594             'framework': 'dlsdk',
595             'onnx_model': '/path/to/source_models/custom_model',
596             'device': 'cpu',
597             '_models_prefix': '/path/to/source_models',
598             'adapter': 'classification'
599         }
600         DLSDKLauncher(config, dummy_adapter)
601
602         mock.assert_called_once_with(
603             'custom_model', '/path/to/source_models/custom_model', '', 'onnx', [], None, None, None, None
604         )
605
606     def test_model_converted_from_kaldi(self, mocker):
607         mock = mocker.patch(
608             'accuracy_checker.launcher.dlsdk_launcher.convert_model',
609             return_value=('converted_model', 'converted_weights')
610         )
611
612         config = {
613             'framework': 'dlsdk',
614             'kaldi_model': '/path/to/source_models/custom_model',
615             'device': 'cpu',
616             '_models_prefix': '/path/to/source_models',
617             'adapter': 'classification'
618         }
619         DLSDKLauncher(config, dummy_adapter)
620
621         mock.assert_called_once_with(
622             'custom_model', '/path/to/source_models/custom_model', '', 'kaldi', [], None, None, None, None
623         )
624
625     def test_raises_with_multiple_models_caffe_dlsdk(self):
626         config = {
627             'framework': 'dlsdk',
628             'caffe_model': 'caffe_model',
629             'caffe_weights': 'caffe_weights',
630             'model': 'custom_model',
631             'weights': 'custom_weights',
632             'device': 'cpu',
633             '_models_prefix': 'prefix'
634         }
635
636         with pytest.raises(ConfigError):
637             DLSDKLauncher(config, dummy_adapter)
638
639     def test_raises_with_multiple_models_tf_dlsdk(self):
640         config = {
641             'framework': 'dlsdk',
642             'tf_model': 'tf_model',
643             'model': 'custom_model',
644             'weights': 'custom_weights',
645             'device': 'cpu',
646             '_models_prefix': 'prefix'
647         }
648
649         with pytest.raises(ConfigError):
650             DLSDKLauncher(config, dummy_adapter)
651
652     def test_raises_with_multiple_models_mxnet_dlsdk(self):
653         config = {
654             'framework': 'dlsdk',
655             'mxnet_weights': 'mxnet_weights',
656             'model': 'custom_model',
657             'weights': 'custom_weights',
658             'device': 'cpu',
659             '_models_prefix': 'prefix'
660         }
661
662         with pytest.raises(ConfigError):
663             DLSDKLauncher(config, dummy_adapter)
664
665     def test_raises_with_multiple_models_onnx_dlsdk(self):
666         config = {
667             'framework': 'dlsdk',
668             'onnx_model': 'onnx_model',
669             'model': 'custom_model',
670             'weights': 'custom_weights',
671             'device': 'cpu',
672             '_models_prefix': 'prefix'
673         }
674
675         with pytest.raises(ConfigError):
676             DLSDKLauncher(config, dummy_adapter)
677
678     def test_raises_with_multiple_models_kaldi_dlsdk(self):
679         config = {
680             'framework': 'dlsdk',
681             'onnx_model': 'kaldi_model',
682             'model': 'custom_model',
683             'weights': 'custom_weights',
684             'device': 'cpu',
685             '_models_prefix': 'prefix'
686         }
687
688         with pytest.raises(ConfigError):
689             DLSDKLauncher(config, dummy_adapter)
690
691     def test_raises_with_multiple_models_mxnet_caffe(self):
692         config = {
693             'framework': 'dlsdk',
694             'mxnet_weights': 'mxnet_weights',
695             'caffe_model': 'caffe_model',
696             'caffe_weights': 'caffe_weights',
697             'device': 'cpu',
698             '_models_prefix': 'prefix'
699         }
700
701         with pytest.raises(ConfigError):
702             DLSDKLauncher(config, dummy_adapter)
703
704     def test_raises_with_multiple_models_tf_caffe(self):
705
706         config = {
707             'framework': 'dlsdk',
708             'tf_model': 'tf_model',
709             'caffe_model': 'caffe_model',
710             'caffe_weights': 'caffe_weights',
711             'device': 'cpu',
712             '_models_prefix': 'prefix'
713         }
714
715         with pytest.raises(ConfigError):
716             DLSDKLauncher(config, dummy_adapter)
717
718     def test_raises_with_multiple_models_onnx_caffe(self):
719
720         config = {
721             'framework': 'dlsdk',
722             'onnx_model': 'onnx_model',
723             'caffe_model': 'caffe_model',
724             'caffe_weights': 'caffe_weights',
725             'device': 'cpu',
726             '_models_prefix': 'prefix'
727         }
728
729         with pytest.raises(ConfigError):
730             DLSDKLauncher(config, dummy_adapter)
731
732     def test_raises_with_multiple_models_mxnet_tf(self):
733         config = {
734             'framework': 'dlsdk',
735             'mxnet_weights': 'mxnet_weights',
736             'tf_model': 'tf_model',
737             'device': 'cpu',
738             '_models_prefix': 'prefix'
739         }
740
741         with pytest.raises(ConfigError):
742             DLSDKLauncher(config, dummy_adapter)
743
744     def test_raises_with_multiple_models_onnx_tf(self):
745         config = {
746             'framework': 'dlsdk',
747             'onnx_model': 'onnx_model',
748             'tf_model': 'tf_model',
749             'device': 'cpu',
750             '_models_prefix': 'prefix'
751         }
752
753         with pytest.raises(ConfigError):
754             DLSDKLauncher(config, dummy_adapter)
755
756     def test_raises_with_multiple_models_mxnet_caffe_tf(self):
757         config = {
758             'framework': 'dlsdk',
759             'mxnet_weights': 'mxnet_weights',
760             'caffe_model': 'caffe_model',
761             'caffe_weights': 'caffe_weights',
762             'tf_model': 'tf_model',
763             'device': 'cpu',
764             '_models_prefix': 'prefix'
765         }
766
767         with pytest.raises(ConfigError):
768             DLSDKLauncher(config, dummy_adapter)
769
770     def test_raises_with_multiple_models_dlsdk_caffe_tf(self):
771         config = {
772             'framework': 'dlsdk',
773             'model': 'custom_model',
774             'weights': 'custom_weights',
775             'caffe_model': 'caffe_model',
776             'caffe_weights': 'caffe_weights',
777             'tf_model': 'tf_model',
778             'device': 'cpu',
779             '_models_prefix': 'prefix'
780         }
781
782         with pytest.raises(ConfigError):
783             DLSDKLauncher(config, dummy_adapter)
784
785     def test_raises_with_multiple_models_dlsdk_caffe_onnx(self):
786         config = {
787             'framework': 'dlsdk',
788             'model': 'custom_model',
789             'weights': 'custom_weights',
790             'caffe_model': 'caffe_model',
791             'caffe_weights': 'caffe_weights',
792             'onnx_model': 'onnx_model',
793             'device': 'cpu',
794             '_models_prefix': 'prefix'
795         }
796
797         with pytest.raises(ConfigError):
798             DLSDKLauncher(config, dummy_adapter)
799
800     def test_raises_with_multiple_models_dlsdk_caffe_mxnet(self):
801         config = {
802             'framework': 'dlsdk',
803             'model': 'custom_model',
804             'weights': 'custom_weights',
805             'caffe_model': 'caffe_model',
806             'caffe_weights': 'caffe_weights',
807             'mxnet_weights': 'mxnet_weights',
808             'device': 'cpu',
809             '_models_prefix': 'prefix'
810         }
811
812         with pytest.raises(ConfigError):
813             DLSDKLauncher(config, dummy_adapter)
814
815     def test_raises_with_multiple_models_dlsdk_tf_mxnet(self):
816         config = {
817             'framework': "dlsdk",
818             'model': 'custom_model',
819             'weights': 'custom_weights',
820             'mxnet_weights': 'mxnet_weights',
821             'tf_model': 'tf_model',
822             'device': 'cpu',
823             '_models_prefix': 'prefix'
824         }
825
826         with pytest.raises(ConfigError):
827             DLSDKLauncher(config, dummy_adapter)
828
829     def test_raises_with_multiple_models_dlsdk_tf_onnx(self):
830         config = {
831             'framework': 'dlsdk',
832             'model': 'custom_model',
833             'weights': 'custom_weights',
834             'onnx_model': 'onnx_model',
835             'tf_model': 'tf_model',
836             'device': 'cpu',
837             '_models_prefix': 'prefix'
838         }
839
840         with pytest.raises(ConfigError):
841             DLSDKLauncher(config, dummy_adapter)
842
843     def test_raises_with_multiple_models_dlsdk_tf_mxnet_caffe(self):
844         config = {
845             'framework': 'dlsdk',
846             'model': 'custom_model',
847             'weights': 'custom_weights',
848             'caffe_model': 'caffe_model',
849             'caffe_weights': 'caffe_weights',
850             'mxnet_weights': 'mxnet_weights',
851             'onnx_model': 'onnx_model',
852             'tf_model': 'tf_model',
853             'device': 'cpu',
854             '_models_prefix': 'prefix'
855         }
856         with pytest.raises(ConfigError):
857             DLSDKLauncher(config, dummy_adapter)
858
859     def test_raises_with_multiple_models_dlsdk_tf_mxnet_caffe_onnx(self):
860         config = {
861             'framework': 'dlsdk',
862             'model': 'custom_model',
863             'weights': 'custom_weights',
864             'caffe_model': 'caffe_model',
865             'caffe_weights': 'caffe_weights',
866             'mxnet_weights': 'mxnet_weights',
867             'tf_model': 'tf_model',
868             'device': 'cpu',
869             '_models_prefix': 'prefix'
870         }
871
872         with pytest.raises(ConfigError):
873             DLSDKLauncher(config, dummy_adapter)
874
875
876 @pytest.mark.usefixtures('mock_path_exists', 'mock_inputs', 'mock_inference_engine')
877 class TestDLSDKLauncherConfig:
878     def setup(self):
879         self.launcher = {
880             'model': 'foo.xml',
881             'weights': 'foo.bin',
882             'device': 'CPU',
883             'framework': 'dlsdk',
884             'adapter': 'classification',
885             '_models_prefix': 'prefix'
886         }
887         self.config = DLSDKLauncherConfig('dlsdk_launcher')
888
889     def test_hetero_correct(self):
890         self.config.validate(update_dict(self.launcher, device='HETERO:CPU'))
891         self.config.validate(update_dict(self.launcher, device='HETERO:CPU,FPGA'))
892
893     def test_hetero_endswith_comma(self):
894         with pytest.raises(ConfigError):
895             self.config.validate(update_dict(self.launcher, device='HETERO:CPU,FPGA,'))
896
897     def test_normal_multiple_devices(self):
898         with pytest.raises(ConfigError):
899             self.config.validate(update_dict(self.launcher, device='CPU,FPGA'))
900
901     def test_hetero_empty(self):
902         with pytest.raises(ConfigError):
903             self.config.validate(update_dict(self.launcher, device='HETERO:'))
904
905     def test_normal(self):
906         self.config.validate(update_dict(self.launcher, device='CPU'))
907
908     def test_missed_model_in_create_dlsdk_launcher_raises_config_error_exception(self):
909         config = {'framework': 'dlsdk', 'weights': 'custom', 'adapter': 'classification', 'device': 'cpu'}
910
911         with pytest.raises(ConfigError):
912             create_launcher(config)
913
914     def test_missed_weights_in_create_dlsdk_launcher_raises_config_error_exception(self):
915         launcher = {'framework': 'dlsdk', 'model': 'custom', 'adapter': 'ssd', 'device': 'cpu'}
916
917         with pytest.raises(ConfigError):
918             create_launcher(launcher)
919
920     def test_missed_adapter_in_create_dlsdk_launcher_raises_config_error_exception(self):
921         launcher_config = {'framework': 'dlsdk', 'model': 'custom', 'weights': 'custom'}
922
923         with pytest.raises(ConfigError):
924             create_launcher(launcher_config)
925
926     def test_undefined_str_adapter_in_create_dlsdk_launcher_raises_config_error_exception(self):
927         launcher_config = {'framework': 'dlsdk', 'model': 'custom', 'weights': 'custom', 'adapter': 'undefined_str'}
928
929         with pytest.raises(ConfigError):
930             create_launcher(launcher_config)
931
932     def test_empty_dir_adapter_in_create_dlsdk_launcher_raises_config_error_exception(self):
933         launcher_config = {'framework': 'dlsdk', 'model': 'custom', 'weights': 'custom', 'adapter': {}}
934
935         with pytest.raises(ConfigError):
936             create_launcher(launcher_config)
937
938     def test_missed_type_in_dir_adapter_in_create_dlsdk_launcher_raises_config_error_exception(self):
939         launcher_config = {'framework': 'dlsdk', 'model': 'custom', 'weights': 'custom', 'adapter': {'key': 'val'}}
940
941         with pytest.raises(ConfigError):
942             create_launcher(launcher_config)
943
944     def test_undefined_type_in_dir_adapter_in_create_dlsdk_launcher_raises_config_error_exception(self):
945         launcher_config = {
946             'framework': 'dlsdk',
947             'model': 'custom',
948             'weights': 'custom',
949             'adapter': {'type': 'undefined'}
950         }
951
952         with pytest.raises(ConfigError):
953             create_launcher(launcher_config)
954
955     def test_dlsdk_launcher(self):
956         launcher = {
957             'framework': 'dlsdk', 'model': 'custom', 'weights': 'custom', 'adapter': 'ssd', 'device': 'cpu',
958             '_models_prefix': 'models'
959         }
960         create_launcher(launcher)
961
962     def test_dlsdk_launcher_model_with_several_image_inputs_raise_value_error(self, mocker):
963         launcher_config = {'framework': 'dlsdk', 'model': 'custom', 'weights': 'custom', 'adapter': {'key': 'val'}}
964
965         with pytest.raises(ValueError):
966             mocker.patch(
967                 'accuracy_checker.launcher.dlsdk_launcher.DLSDKLauncher.inputs',
968                 new_callable=PropertyMock(return_value={'data1': [3, 227, 227], 'data2': [3, 227, 227]})
969             )
970             create_launcher(launcher_config)
971
972     def test_dlsdk_launcher_model_no_image_inputs_raise_value_error(self):
973         launcher_config = {'framework': 'dlsdk', 'model': 'custom', 'weights': 'custom', 'adapter': {'key': 'val'}}
974
975         with pytest.raises(ValueError):
976             create_launcher(launcher_config)
977
978
979 def dummy_adapter():
980     pass