Matthew Bentham | 245d64c | 2019-12-02 12:59:43 +0000 | [diff] [blame] | 1 | # Copyright © 2019 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 | |
| 10 | @pytest.fixture() |
| 11 | def parser(shared_data_folder): |
| 12 | """ |
| 13 | Parse and setup the test network (ssd_mobilenetv1) to be used for the tests below |
| 14 | """ |
| 15 | parser = ann.ITfLiteParser() |
| 16 | parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'ssd_mobilenetv1.tflite')) |
| 17 | |
| 18 | yield parser |
| 19 | |
| 20 | |
| 21 | def 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 | |
| 26 | def 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 | def test_tflite_get_sub_graph_count(parser): |
| 33 | graphs_count = parser.GetSubgraphCount() |
| 34 | assert graphs_count == 1 |
| 35 | |
| 36 | |
| 37 | def test_tflite_get_network_input_binding_info(parser): |
| 38 | graphs_count = parser.GetSubgraphCount() |
| 39 | graph_id = graphs_count - 1 |
| 40 | |
| 41 | input_names = parser.GetSubgraphInputTensorNames(graph_id) |
| 42 | |
| 43 | input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0]) |
| 44 | |
| 45 | tensor = input_binding_info[1] |
| 46 | assert tensor.GetDataType() == 2 |
| 47 | assert tensor.GetNumDimensions() == 4 |
| 48 | assert tensor.GetNumElements() == 270000 |
| 49 | assert tensor.GetQuantizationOffset() == 128 |
| 50 | assert tensor.GetQuantizationScale() == 0.007874015718698502 |
| 51 | |
| 52 | |
| 53 | def test_tflite_get_network_output_binding_info(parser): |
| 54 | graphs_count = parser.GetSubgraphCount() |
| 55 | graph_id = graphs_count - 1 |
| 56 | |
| 57 | output_names = parser.GetSubgraphOutputTensorNames(graph_id) |
| 58 | |
| 59 | output_binding_info1 = parser.GetNetworkOutputBindingInfo(graph_id, output_names[0]) |
| 60 | output_binding_info2 = parser.GetNetworkOutputBindingInfo(graph_id, output_names[1]) |
| 61 | output_binding_info3 = parser.GetNetworkOutputBindingInfo(graph_id, output_names[2]) |
| 62 | output_binding_info4 = parser.GetNetworkOutputBindingInfo(graph_id, output_names[3]) |
| 63 | |
| 64 | # Check the tensor info retrieved from GetNetworkOutputBindingInfo |
| 65 | tensor1 = output_binding_info1[1] |
| 66 | tensor2 = output_binding_info2[1] |
| 67 | tensor3 = output_binding_info3[1] |
| 68 | tensor4 = output_binding_info4[1] |
| 69 | |
| 70 | assert tensor1.GetDataType() == 1 |
| 71 | assert tensor1.GetNumDimensions() == 3 |
| 72 | assert tensor1.GetNumElements() == 40 |
| 73 | assert tensor1.GetQuantizationOffset() == 0 |
| 74 | assert tensor1.GetQuantizationScale() == 0.0 |
| 75 | |
| 76 | assert tensor2.GetDataType() == 1 |
| 77 | assert tensor2.GetNumDimensions() == 2 |
| 78 | assert tensor2.GetNumElements() == 10 |
| 79 | assert tensor2.GetQuantizationOffset() == 0 |
| 80 | assert tensor2.GetQuantizationScale() == 0.0 |
| 81 | |
| 82 | assert tensor3.GetDataType() == 1 |
| 83 | assert tensor3.GetNumDimensions() == 2 |
| 84 | assert tensor3.GetNumElements() == 10 |
| 85 | assert tensor3.GetQuantizationOffset() == 0 |
| 86 | assert tensor3.GetQuantizationScale() == 0.0 |
| 87 | |
| 88 | assert tensor4.GetDataType() == 1 |
| 89 | assert tensor4.GetNumDimensions() == 1 |
| 90 | assert tensor4.GetNumElements() == 1 |
| 91 | assert tensor4.GetQuantizationOffset() == 0 |
| 92 | assert tensor4.GetQuantizationScale() == 0.0 |
| 93 | |
| 94 | |
| 95 | def test_tflite_get_subgraph_input_tensor_names(parser): |
| 96 | graphs_count = parser.GetSubgraphCount() |
| 97 | graph_id = graphs_count - 1 |
| 98 | |
| 99 | input_names = parser.GetSubgraphInputTensorNames(graph_id) |
| 100 | |
| 101 | assert input_names == ('normalized_input_image_tensor',) |
| 102 | |
| 103 | |
| 104 | def test_tflite_get_subgraph_output_tensor_names(parser): |
| 105 | graphs_count = parser.GetSubgraphCount() |
| 106 | graph_id = graphs_count - 1 |
| 107 | |
| 108 | output_names = parser.GetSubgraphOutputTensorNames(graph_id) |
| 109 | |
| 110 | assert output_names[0] == 'TFLite_Detection_PostProcess' |
| 111 | assert output_names[1] == 'TFLite_Detection_PostProcess:1' |
| 112 | assert output_names[2] == 'TFLite_Detection_PostProcess:2' |
| 113 | assert output_names[3] == 'TFLite_Detection_PostProcess:3' |
| 114 | |
| 115 | |
| 116 | def test_tflite_filenotfound_exception(shared_data_folder): |
| 117 | parser = ann.ITfLiteParser() |
| 118 | |
| 119 | with pytest.raises(RuntimeError) as err: |
| 120 | parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'some_unknown_network.tflite')) |
| 121 | |
| 122 | # Only check for part of the exception since the exception returns |
| 123 | # absolute path which will change on different machines. |
| 124 | assert 'Cannot find the file' in str(err.value) |
| 125 | |
| 126 | |
| 127 | def test_tflite_parser_end_to_end(shared_data_folder): |
| 128 | parser = ann.ITfLiteParser() |
| 129 | |
| 130 | network = parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder,"inception_v3_quant.tflite")) |
| 131 | |
| 132 | graphs_count = parser.GetSubgraphCount() |
| 133 | graph_id = graphs_count - 1 |
| 134 | |
| 135 | input_names = parser.GetSubgraphInputTensorNames(graph_id) |
| 136 | input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0]) |
| 137 | |
| 138 | output_names = parser.GetSubgraphOutputTensorNames(graph_id) |
| 139 | |
| 140 | preferred_backends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')] |
| 141 | |
| 142 | options = ann.CreationOptions() |
| 143 | runtime = ann.IRuntime(options) |
| 144 | |
| 145 | opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions()) |
| 146 | assert 0 == len(messages) |
| 147 | |
| 148 | net_id, messages = runtime.LoadNetwork(opt_network) |
| 149 | assert "" == messages |
| 150 | |
| 151 | # Load test image data stored in input.npy |
| 152 | input_tensor_data = np.load(os.path.join(shared_data_folder, 'tflite_parser/inceptionv3_golden_input.npy')) |
| 153 | input_tensors = ann.make_input_tensors([input_binding_info], [input_tensor_data]) |
| 154 | |
| 155 | output_tensors = [] |
| 156 | for index, output_name in enumerate(output_names): |
| 157 | out_bind_info = parser.GetNetworkOutputBindingInfo(graph_id, output_name) |
| 158 | out_tensor_info = out_bind_info[1] |
| 159 | out_tensor_id = out_bind_info[0] |
| 160 | output_tensors.append((out_tensor_id, |
| 161 | ann.Tensor(out_tensor_info))) |
| 162 | |
| 163 | runtime.EnqueueWorkload(net_id, input_tensors, output_tensors) |
| 164 | |
| 165 | output_vectors = [] |
| 166 | for index, out_tensor in enumerate(output_tensors): |
| 167 | output_vectors.append(out_tensor[1].get_memory_area()) |
| 168 | |
| 169 | # Load golden output file to compare the output results with |
| 170 | expected_outputs = np.load(os.path.join(shared_data_folder, 'tflite_parser/inceptionv3_golden_output.npy')) |
| 171 | |
| 172 | # Check that output matches golden output |
| 173 | np.testing.assert_allclose(output_vectors, expected_outputs, 0.08) |