blob: 344ec7ca139a495fa76d673492ea55b9c754a025 [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 parser = ann.ITfLiteParser()
16 parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'mock_model.tflite'))
17
18 yield parser
19
20
21def test_tflite_parser_swig_destroy():
22 assert ann.ITfLiteParser.__swig_destroy__, "There is a swig python destructor defined"
23 assert ann.ITfLiteParser.__swig_destroy__.__name__ == "delete_ITfLiteParser"
24
25
26def test_check_tflite_parser_swig_ownership(parser):
27 # Check to see that SWIG has ownership for parser. This instructs SWIG to take
28 # ownership of the return value. This allows the value to be automatically
29 # garbage-collected when it is no longer in use
30 assert parser.thisown
31
32
33def test_tflite_get_sub_graph_count(parser):
34 graphs_count = parser.GetSubgraphCount()
35 assert graphs_count == 1
36
37
38def test_tflite_get_network_input_binding_info(parser):
39 graphs_count = parser.GetSubgraphCount()
40 graph_id = graphs_count - 1
41
42 input_names = parser.GetSubgraphInputTensorNames(graph_id)
43
44 input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0])
45
46 tensor = input_binding_info[1]
47 assert tensor.GetDataType() == 2
48 assert tensor.GetNumDimensions() == 4
49 assert tensor.GetNumElements() == 784
50 assert tensor.GetQuantizationOffset() == 128
51 assert tensor.GetQuantizationScale() == 0.007843137718737125
52
53
54def test_tflite_get_network_output_binding_info(parser):
55 graphs_count = parser.GetSubgraphCount()
56 graph_id = graphs_count - 1
57
58 output_names = parser.GetSubgraphOutputTensorNames(graph_id)
59
60 output_binding_info1 = parser.GetNetworkOutputBindingInfo(graph_id, output_names[0])
61
62 # Check the tensor info retrieved from GetNetworkOutputBindingInfo
63 tensor1 = output_binding_info1[1]
64
65 assert tensor1.GetDataType() == 2
66 assert tensor1.GetNumDimensions() == 2
67 assert tensor1.GetNumElements() == 10
68 assert tensor1.GetQuantizationOffset() == 0
69 assert tensor1.GetQuantizationScale() == 0.00390625
70
71
72def test_tflite_get_subgraph_input_tensor_names(parser):
73 graphs_count = parser.GetSubgraphCount()
74 graph_id = graphs_count - 1
75
76 input_names = parser.GetSubgraphInputTensorNames(graph_id)
77
78 assert input_names == ('input_1',)
79
80
81def test_tflite_get_subgraph_output_tensor_names(parser):
82 graphs_count = parser.GetSubgraphCount()
83 graph_id = graphs_count - 1
84
85 output_names = parser.GetSubgraphOutputTensorNames(graph_id)
86
87 assert output_names[0] == 'dense/Softmax'
88
89
90def test_tflite_filenotfound_exception(shared_data_folder):
91 parser = ann.ITfLiteParser()
92
93 with pytest.raises(RuntimeError) as err:
94 parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'some_unknown_network.tflite'))
95
96 # Only check for part of the exception since the exception returns
97 # absolute path which will change on different machines.
98 assert 'Cannot find the file' in str(err.value)
99
100
101def test_tflite_parser_end_to_end(shared_data_folder):
102 parser = ann.ITfLiteParser()
103
104 network = parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, "mock_model.tflite"))
105
106 graphs_count = parser.GetSubgraphCount()
107 graph_id = graphs_count - 1
108
109 input_names = parser.GetSubgraphInputTensorNames(graph_id)
110 input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0])
111
112 output_names = parser.GetSubgraphOutputTensorNames(graph_id)
113
114 preferred_backends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')]
115
116 options = ann.CreationOptions()
117 runtime = ann.IRuntime(options)
118
119 opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions())
120 assert 0 == len(messages)
121
122 net_id, messages = runtime.LoadNetwork(opt_network)
123 assert "" == messages
124
125 # Load test image data stored in input_lite.npy
126 input_tensor_data = np.load(os.path.join(shared_data_folder, 'tflite_parser/input_lite.npy'))
127 input_tensors = ann.make_input_tensors([input_binding_info], [input_tensor_data])
128
129 output_tensors = []
130 for index, output_name in enumerate(output_names):
131 out_bind_info = parser.GetNetworkOutputBindingInfo(graph_id, output_name)
132 out_tensor_info = out_bind_info[1]
133 out_tensor_id = out_bind_info[0]
134 output_tensors.append((out_tensor_id,
135 ann.Tensor(out_tensor_info)))
136
137 runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)
138
139 output_vectors = []
140 for index, out_tensor in enumerate(output_tensors):
141 output_vectors.append(out_tensor[1].get_memory_area())
142
143 # Load golden output file for result comparison.
144 expected_outputs = np.load(os.path.join(shared_data_folder, 'tflite_parser/golden_output_lite.npy'))
145
146 # Check that output matches golden output
147 assert (expected_outputs == output_vectors[0]).all()