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