blob: d744b907d42338f6a8da36b61ab066380af7710f [file] [log] [blame]
Richard Burtondc0c6ed2020-04-08 16:39:05 +01001# Copyright © 2020 Arm Ltd. All rights reserved.
2# SPDX-License-Identifier: MIT
3import os
4
5import pytest
6import pyarmnn as ann
7import numpy as np
8
9
10@pytest.fixture()
11def parser(shared_data_folder):
12 """
13 Parse and setup the test network to be used for the tests below
14 """
15
16 # Create caffe parser
17 parser = ann.ICaffeParser()
18
19 # Specify path to model
20 path_to_model = os.path.join(shared_data_folder, 'mock_model.caffemodel')
21
22 # Specify the tensor shape relative to the input [1, 1, 28, 28]
23 tensor_shape = {'Placeholder': ann.TensorShape((1, 1, 28, 28))}
24
25 # Specify the requested_outputs
26 requested_outputs = ["output"]
27
28 # Parse caffe binary & create network
29 parser.CreateNetworkFromBinaryFile(path_to_model, tensor_shape, requested_outputs)
30
31 yield parser
32
33
34def test_caffe_parser_swig_destroy():
35 assert ann.ICaffeParser.__swig_destroy__, "There is a swig python destructor defined"
36 assert ann.ICaffeParser.__swig_destroy__.__name__ == "delete_ICaffeParser"
37
38
39def test_check_caffe_parser_swig_ownership(parser):
40 # Check to see that SWIG has ownership for parser. This instructs SWIG to take
41 # ownership of the return value. This allows the value to be automatically
42 # garbage-collected when it is no longer in use
43 assert parser.thisown
44
45
46def test_get_network_input_binding_info(parser):
47 input_binding_info = parser.GetNetworkInputBindingInfo("Placeholder")
48
49 tensor = input_binding_info[1]
50 assert tensor.GetDataType() == 1
51 assert tensor.GetNumDimensions() == 4
52 assert tensor.GetNumElements() == 784
53
54
55def test_get_network_output_binding_info(parser):
56 output_binding_info1 = parser.GetNetworkOutputBindingInfo("output")
57
58 # Check the tensor info retrieved from GetNetworkOutputBindingInfo
59 tensor1 = output_binding_info1[1]
60
61 assert tensor1.GetDataType() == 1
62 assert tensor1.GetNumDimensions() == 2
63 assert tensor1.GetNumElements() == 10
64
65
66def test_filenotfound_exception(shared_data_folder):
67 parser = ann.ICaffeParser()
68
69 # path to model
70 path_to_model = os.path.join(shared_data_folder, 'some_unknown_network.caffemodel')
71
72 # generic tensor shape [1, 1, 1, 1]
73 tensor_shape = {'data': ann.TensorShape((1, 1, 1, 1))}
74
75 # requested_outputs
76 requested_outputs = [""]
77
78 with pytest.raises(RuntimeError) as err:
79 parser.CreateNetworkFromBinaryFile(path_to_model, tensor_shape, requested_outputs)
80
81 # Only check for part of the exception since the exception returns
82 # absolute path which will change on different machines.
83 assert 'Failed to open graph file' in str(err.value)
84
85
86def test_caffe_parser_end_to_end(shared_data_folder):
87 parser = ann.ICaffeParser = ann.ICaffeParser()
88
89 # Load the network specifying the inputs and outputs
90 input_name = "Placeholder"
91 tensor_shape = {input_name: ann.TensorShape((1, 1, 28, 28))}
92 requested_outputs = ["output"]
93
94 network = parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'mock_model.caffemodel'),
95 tensor_shape, requested_outputs)
96
97 # Specify preferred backend
98 preferred_backends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')]
99
100 input_binding_info = parser.GetNetworkInputBindingInfo(input_name)
101
102 options = ann.CreationOptions()
103 runtime = ann.IRuntime(options)
104
105 opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions())
106
107 assert 0 == len(messages)
108
109 net_id, messages = runtime.LoadNetwork(opt_network)
110
111 assert "" == messages
112
113 # Load test image data stored in input_caffe.npy
114 input_tensor_data = np.load(os.path.join(shared_data_folder, 'caffe_parser/input_caffe.npy')).astype(np.float32)
115 input_tensors = ann.make_input_tensors([input_binding_info], [input_tensor_data])
116
117 # Load output binding info and
118 outputs_binding_info = []
119 for output_name in requested_outputs:
120 outputs_binding_info.append(parser.GetNetworkOutputBindingInfo(output_name))
121 output_tensors = ann.make_output_tensors(outputs_binding_info)
122
123 runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)
124
125 output_vectors = ann.workload_tensors_to_ndarray(output_tensors)
126
127 # Load golden output file for result comparison.
128 expected_output = np.load(os.path.join(shared_data_folder, 'caffe_parser/golden_output_caffe.npy'))
129
130 # Check that output matches golden output to 4 decimal places (there are slight rounding differences after this)
131 np.testing.assert_almost_equal(output_vectors[0], expected_output, 4)