Pavel Macenauer | 59e057f | 2020-04-15 14:17:26 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Éanna Ó Catháin | 6c3dee4 | 2020-09-10 13:02:37 +0100 | [diff] [blame] | 2 | # Copyright © 2020 NXP and Contributors. All rights reserved. |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 3 | # SPDX-License-Identifier: MIT |
| 4 | |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 5 | import example_utils as eu |
| 6 | import os |
| 7 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 8 | if __name__ == "__main__": |
| 9 | args = eu.parse_command_line() |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 10 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 11 | # names of the files in the archive |
| 12 | labels_filename = 'labels_mobilenet_quant_v1_224.txt' |
| 13 | model_filename = 'mobilenet_v1_1.0_224_quant.tflite' |
| 14 | archive_filename = 'mobilenet_v1_1.0_224_quant_and_labels.zip' |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 15 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 16 | archive_url = \ |
| 17 | 'https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_1.0_224_quant_and_labels.zip' |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 18 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 19 | model_filename, labels_filename = eu.get_model_and_labels(args.model_dir, model_filename, labels_filename, |
| 20 | archive_filename, archive_url) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 21 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 22 | image_filenames = eu.get_images(args.data_dir) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 23 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 24 | # all 3 resources must exist to proceed further |
| 25 | assert os.path.exists(labels_filename) |
| 26 | assert os.path.exists(model_filename) |
| 27 | assert image_filenames |
| 28 | for im in image_filenames: |
| 29 | assert(os.path.exists(im)) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 30 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 31 | # Create a network from the model file |
| 32 | net_id, graph_id, parser, runtime = eu.create_tflite_network(model_filename) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 33 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 34 | # Load input information from the model |
| 35 | # tflite has all the need information in the model unlike other formats |
| 36 | input_names = parser.GetSubgraphInputTensorNames(graph_id) |
| 37 | assert len(input_names) == 1 # there should be 1 input tensor in mobilenet |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 38 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 39 | input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0]) |
| 40 | input_width = input_binding_info[1].GetShape()[1] |
| 41 | input_height = input_binding_info[1].GetShape()[2] |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 42 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 43 | # Load output information from the model and create output tensors |
| 44 | output_names = parser.GetSubgraphOutputTensorNames(graph_id) |
| 45 | assert len(output_names) == 1 # and only one output tensor |
| 46 | output_binding_info = parser.GetNetworkOutputBindingInfo(graph_id, output_names[0]) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 47 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 48 | # Load labels file |
| 49 | labels = eu.load_labels(labels_filename) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 50 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 51 | # Load images and resize to expected size |
| 52 | images = eu.load_images(image_filenames, input_width, input_height) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 53 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 54 | eu.run_inference(runtime, net_id, images, labels, input_binding_info, output_binding_info) |