Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / tests / test_caffe_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 pytest
18 pytest.importorskip('accuracy_checker.launcher.caffe_launcher')
19
20 import cv2
21 import numpy as np
22
23 from accuracy_checker.launcher.launcher import create_launcher
24 from accuracy_checker.config import ConfigError
25 from accuracy_checker.dataset import DataRepresentation
26
27
28 def get_caffe_test_model(models_dir):
29     config = {
30         "framework": "caffe",
31         "weights": str(models_dir / "SampLeNet.caffemodel"),
32         "model": str(models_dir / "SampLeNet.prototxt"),
33         "adapter": 'classification',
34         "device": "cpu"
35     }
36
37     return create_launcher(config)
38
39
40 class TestCaffeLauncher:
41     def test_launcher_creates(self, models_dir):
42         assert get_caffe_test_model(models_dir).inputs['data'] == (3, 32, 32)
43
44     def test_infer(self, data_dir, models_dir):
45         caffe_test_model = get_caffe_test_model(models_dir)
46         c, h, w = caffe_test_model.inputs['data']
47         img_raw = cv2.imread(str(data_dir / '1.jpg'))
48         img_resized = cv2.resize(img_raw, (w, h))
49         res = caffe_test_model.predict(['1.jpg'], [DataRepresentation(img_resized)])
50
51         assert res[0].label == 6
52
53     def test_caffe_launcher_provide_input_shape_to_adapter(self, mocker, models_dir):
54         mocker.patch('caffe.Net.forward', return_value={'fc3': 0})
55         adapter_mock = mocker.patch('accuracy_checker.adapters.ClassificationAdapter.process')
56         launcher = get_caffe_test_model(models_dir)
57         launcher.predict(['1.png'], [DataRepresentation(np.zeros((32, 32, 3)))])
58         adapter_mock.assert_called_once_with([{'fc3': 0}], ['1.png'], [{'input_shape': {'data': (3, 32, 32)}, 'image_size': (32, 32, 3)}])
59
60
61
62 def test_missed_model_in_create_caffe_launcher_raises_config_error_exception():
63     launcher = {'framework': 'caffe', 'weights': 'custom', 'adapter': 'classification'}
64
65     with pytest.raises(ConfigError):
66         create_launcher(launcher)
67
68
69 def test_missed_weights_in_create_caffe_launcher_raises_config_error_exception():
70     launcher = {'framework': 'caffe', 'model': 'custom', 'adapter': 'ssd'}
71
72     with pytest.raises(ConfigError):
73         create_launcher(launcher)
74
75
76 def dummy_adapter():
77     pass