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 | |
| 5 | import pyarmnn as ann |
| 6 | import numpy as np |
Pavel Macenauer | 09daef8 | 2020-06-02 11:54:59 +0000 | [diff] [blame] | 7 | import os |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 8 | from PIL import Image |
| 9 | import example_utils as eu |
| 10 | |
| 11 | |
| 12 | def preprocess_onnx(img: Image, width: int, height: int, data_type, scale: float, mean: list, |
| 13 | stddev: list): |
| 14 | """Preprocessing function for ONNX imagenet models based on: |
| 15 | https://github.com/onnx/models/blob/master/vision/classification/imagenet_inference.ipynb |
| 16 | |
| 17 | Args: |
| 18 | img (PIL.Image): Loaded PIL.Image |
| 19 | width (int): Target image width |
| 20 | height (int): Target image height |
| 21 | data_type: Image datatype (np.uint8 or np.float32) |
| 22 | scale (float): Scaling factor |
| 23 | mean: RGB mean values |
| 24 | stddev: RGB standard deviation |
| 25 | |
| 26 | Returns: |
| 27 | np.array: Preprocess image as Numpy array |
| 28 | """ |
| 29 | img = img.resize((256, 256), Image.BILINEAR) |
| 30 | # first rescale to 256,256 and then center crop |
| 31 | left = (256 - width) / 2 |
| 32 | top = (256 - height) / 2 |
| 33 | right = (256 + width) / 2 |
| 34 | bottom = (256 + height) / 2 |
| 35 | img = img.crop((left, top, right, bottom)) |
| 36 | img = img.convert('RGB') |
| 37 | img = np.array(img) |
| 38 | img = np.reshape(img, (-1, 3)) # reshape to [RGB][RGB]... |
| 39 | img = ((img / scale) - mean) / stddev |
| 40 | # NHWC to NCHW conversion, by default NHWC is expected |
| 41 | # image is loaded as [RGB][RGB][RGB]... transposing it makes it [RRR...][GGG...][BBB...] |
| 42 | img = np.transpose(img) |
| 43 | img = img.flatten().astype(data_type) # flatten into a 1D tensor and convert to float32 |
| 44 | return img |
| 45 | |
| 46 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 47 | if __name__ == "__main__": |
| 48 | args = eu.parse_command_line() |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 49 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 50 | model_filename = 'mobilenetv2-1.0.onnx' |
| 51 | labels_filename = 'synset.txt' |
| 52 | archive_filename = 'mobilenetv2-1.0.zip' |
| 53 | labels_url = 'https://s3.amazonaws.com/onnx-model-zoo/' + labels_filename |
| 54 | model_url = 'https://s3.amazonaws.com/onnx-model-zoo/mobilenet/mobilenetv2-1.0/' + model_filename |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 55 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 56 | # Download resources |
| 57 | image_filenames = eu.get_images(args.data_dir) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 58 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 59 | model_filename, labels_filename = eu.get_model_and_labels(args.model_dir, model_filename, labels_filename, |
| 60 | archive_filename, |
| 61 | [model_url, labels_url]) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 62 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 63 | # all 3 resources must exist to proceed further |
| 64 | assert os.path.exists(labels_filename) |
| 65 | assert os.path.exists(model_filename) |
| 66 | assert image_filenames |
| 67 | for im in image_filenames: |
| 68 | assert (os.path.exists(im)) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 69 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 70 | # Create a network from a model file |
| 71 | net_id, parser, runtime = eu.create_onnx_network(model_filename) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 72 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 73 | # Load input information from the model and create input tensors |
| 74 | input_binding_info = parser.GetNetworkInputBindingInfo("data") |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 75 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 76 | # Load output information from the model and create output tensors |
| 77 | output_binding_info = parser.GetNetworkOutputBindingInfo("mobilenetv20_output_flatten0_reshape0") |
| 78 | output_tensors = ann.make_output_tensors([output_binding_info]) |
Pavel Macenauer | d0fedae | 2020-04-15 14:52:57 +0000 | [diff] [blame] | 79 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 80 | # Load labels |
| 81 | labels = eu.load_labels(labels_filename) |
Pavel Macenauer | 09daef8 | 2020-06-02 11:54:59 +0000 | [diff] [blame] | 82 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 83 | # Load images and resize to expected size |
| 84 | images = eu.load_images(image_filenames, |
| 85 | 224, 224, |
| 86 | np.float32, |
| 87 | 255.0, |
| 88 | [0.485, 0.456, 0.406], |
| 89 | [0.229, 0.224, 0.225], |
| 90 | preprocess_onnx) |
Pavel Macenauer | 09daef8 | 2020-06-02 11:54:59 +0000 | [diff] [blame] | 91 | |
Éanna Ó Catháin | 145c88f | 2020-11-16 14:12:11 +0000 | [diff] [blame] | 92 | eu.run_inference(runtime, net_id, images, labels, input_binding_info, output_binding_info) |