Richard Burton | dc0c6ed | 2020-04-08 16:39:05 +0100 | [diff] [blame] | 1 | # Copyright © 2020 Arm Ltd. All rights reserved. |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | import os |
| 4 | |
| 5 | import pytest |
| 6 | import pyarmnn as ann |
| 7 | import numpy as np |
| 8 | |
| 9 | |
Jan Eilers | 841aca1 | 2020-08-12 14:59:06 +0100 | [diff] [blame] | 10 | def test_TfLiteParserOptions_default_values(): |
| 11 | parserOptions = ann.TfLiteParserOptions() |
| 12 | assert parserOptions.m_InferAndValidate == False |
| 13 | assert parserOptions.m_StandInLayerForUnsupported == False |
| 14 | |
| 15 | |
Richard Burton | dc0c6ed | 2020-04-08 16:39:05 +0100 | [diff] [blame] | 16 | @pytest.fixture() |
| 17 | def parser(shared_data_folder): |
| 18 | """ |
| 19 | Parse and setup the test network to be used for the tests below |
| 20 | """ |
| 21 | parser = ann.ITfLiteParser() |
| 22 | parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'mock_model.tflite')) |
| 23 | |
| 24 | yield parser |
| 25 | |
| 26 | |
| 27 | def test_tflite_parser_swig_destroy(): |
| 28 | assert ann.ITfLiteParser.__swig_destroy__, "There is a swig python destructor defined" |
| 29 | assert ann.ITfLiteParser.__swig_destroy__.__name__ == "delete_ITfLiteParser" |
| 30 | |
| 31 | |
| 32 | def test_check_tflite_parser_swig_ownership(parser): |
| 33 | # Check to see that SWIG has ownership for parser. This instructs SWIG to take |
| 34 | # ownership of the return value. This allows the value to be automatically |
| 35 | # garbage-collected when it is no longer in use |
| 36 | assert parser.thisown |
| 37 | |
| 38 | |
Jan Eilers | 841aca1 | 2020-08-12 14:59:06 +0100 | [diff] [blame] | 39 | def test_tflite_parser_with_optional_options(): |
| 40 | parserOptions = ann.TfLiteParserOptions() |
| 41 | parserOptions.m_InferAndValidate = True |
| 42 | parser = ann.ITfLiteParser(parserOptions) |
| 43 | assert parser.thisown |
| 44 | |
| 45 | |
| 46 | def create_with_opt() : |
| 47 | parserOptions = ann.TfLiteParserOptions() |
| 48 | parserOptions.m_InferAndValidate = True |
| 49 | return ann.ITfLiteParser(parserOptions) |
| 50 | |
| 51 | def test_tflite_parser_with_optional_options_out_of_scope(shared_data_folder): |
| 52 | parser = create_with_opt() |
| 53 | network = parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, "mock_model.tflite")) |
| 54 | |
| 55 | graphs_count = parser.GetSubgraphCount() |
| 56 | graph_id = graphs_count - 1 |
| 57 | |
| 58 | input_names = parser.GetSubgraphInputTensorNames(graph_id) |
| 59 | input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0]) |
| 60 | |
| 61 | output_names = parser.GetSubgraphOutputTensorNames(graph_id) |
| 62 | |
| 63 | preferred_backends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')] |
| 64 | |
| 65 | options = ann.CreationOptions() |
| 66 | runtime = ann.IRuntime(options) |
| 67 | |
| 68 | opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions()) |
| 69 | assert 0 == len(messages) |
| 70 | |
| 71 | net_id, messages = runtime.LoadNetwork(opt_network) |
| 72 | assert "" == messages |
| 73 | |
| 74 | |
Richard Burton | dc0c6ed | 2020-04-08 16:39:05 +0100 | [diff] [blame] | 75 | def test_tflite_get_sub_graph_count(parser): |
| 76 | graphs_count = parser.GetSubgraphCount() |
| 77 | assert graphs_count == 1 |
| 78 | |
| 79 | |
| 80 | def test_tflite_get_network_input_binding_info(parser): |
| 81 | graphs_count = parser.GetSubgraphCount() |
| 82 | graph_id = graphs_count - 1 |
| 83 | |
| 84 | input_names = parser.GetSubgraphInputTensorNames(graph_id) |
| 85 | |
| 86 | input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0]) |
| 87 | |
| 88 | tensor = input_binding_info[1] |
| 89 | assert tensor.GetDataType() == 2 |
| 90 | assert tensor.GetNumDimensions() == 4 |
| 91 | assert tensor.GetNumElements() == 784 |
| 92 | assert tensor.GetQuantizationOffset() == 128 |
| 93 | assert tensor.GetQuantizationScale() == 0.007843137718737125 |
| 94 | |
| 95 | |
| 96 | def test_tflite_get_network_output_binding_info(parser): |
| 97 | graphs_count = parser.GetSubgraphCount() |
| 98 | graph_id = graphs_count - 1 |
| 99 | |
| 100 | output_names = parser.GetSubgraphOutputTensorNames(graph_id) |
| 101 | |
| 102 | output_binding_info1 = parser.GetNetworkOutputBindingInfo(graph_id, output_names[0]) |
| 103 | |
| 104 | # Check the tensor info retrieved from GetNetworkOutputBindingInfo |
| 105 | tensor1 = output_binding_info1[1] |
| 106 | |
| 107 | assert tensor1.GetDataType() == 2 |
| 108 | assert tensor1.GetNumDimensions() == 2 |
| 109 | assert tensor1.GetNumElements() == 10 |
| 110 | assert tensor1.GetQuantizationOffset() == 0 |
| 111 | assert tensor1.GetQuantizationScale() == 0.00390625 |
| 112 | |
| 113 | |
| 114 | def test_tflite_get_subgraph_input_tensor_names(parser): |
| 115 | graphs_count = parser.GetSubgraphCount() |
| 116 | graph_id = graphs_count - 1 |
| 117 | |
| 118 | input_names = parser.GetSubgraphInputTensorNames(graph_id) |
| 119 | |
| 120 | assert input_names == ('input_1',) |
| 121 | |
| 122 | |
| 123 | def test_tflite_get_subgraph_output_tensor_names(parser): |
| 124 | graphs_count = parser.GetSubgraphCount() |
| 125 | graph_id = graphs_count - 1 |
| 126 | |
| 127 | output_names = parser.GetSubgraphOutputTensorNames(graph_id) |
| 128 | |
| 129 | assert output_names[0] == 'dense/Softmax' |
| 130 | |
| 131 | |
| 132 | def test_tflite_filenotfound_exception(shared_data_folder): |
| 133 | parser = ann.ITfLiteParser() |
| 134 | |
| 135 | with pytest.raises(RuntimeError) as err: |
| 136 | parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'some_unknown_network.tflite')) |
| 137 | |
| 138 | # Only check for part of the exception since the exception returns |
| 139 | # absolute path which will change on different machines. |
| 140 | assert 'Cannot find the file' in str(err.value) |
| 141 | |
| 142 | |
| 143 | def test_tflite_parser_end_to_end(shared_data_folder): |
| 144 | parser = ann.ITfLiteParser() |
| 145 | |
| 146 | network = parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, "mock_model.tflite")) |
| 147 | |
| 148 | graphs_count = parser.GetSubgraphCount() |
| 149 | graph_id = graphs_count - 1 |
| 150 | |
| 151 | input_names = parser.GetSubgraphInputTensorNames(graph_id) |
| 152 | input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0]) |
| 153 | |
| 154 | output_names = parser.GetSubgraphOutputTensorNames(graph_id) |
| 155 | |
| 156 | preferred_backends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')] |
| 157 | |
| 158 | options = ann.CreationOptions() |
| 159 | runtime = ann.IRuntime(options) |
| 160 | |
| 161 | opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions()) |
| 162 | assert 0 == len(messages) |
| 163 | |
| 164 | net_id, messages = runtime.LoadNetwork(opt_network) |
| 165 | assert "" == messages |
| 166 | |
| 167 | # Load test image data stored in input_lite.npy |
| 168 | input_tensor_data = np.load(os.path.join(shared_data_folder, 'tflite_parser/input_lite.npy')) |
| 169 | input_tensors = ann.make_input_tensors([input_binding_info], [input_tensor_data]) |
| 170 | |
| 171 | output_tensors = [] |
| 172 | for index, output_name in enumerate(output_names): |
| 173 | out_bind_info = parser.GetNetworkOutputBindingInfo(graph_id, output_name) |
| 174 | out_tensor_info = out_bind_info[1] |
| 175 | out_tensor_id = out_bind_info[0] |
| 176 | output_tensors.append((out_tensor_id, |
| 177 | ann.Tensor(out_tensor_info))) |
| 178 | |
| 179 | runtime.EnqueueWorkload(net_id, input_tensors, output_tensors) |
| 180 | |
| 181 | output_vectors = [] |
| 182 | for index, out_tensor in enumerate(output_tensors): |
| 183 | output_vectors.append(out_tensor[1].get_memory_area()) |
| 184 | |
| 185 | # Load golden output file for result comparison. |
| 186 | expected_outputs = np.load(os.path.join(shared_data_folder, 'tflite_parser/golden_output_lite.npy')) |
| 187 | |
| 188 | # Check that output matches golden output |
| 189 | assert (expected_outputs == output_vectors[0]).all() |