blob: be28b585ba4c998cc7fd107317acd8d66f48a40c [file] [log] [blame]
Pavel Macenauer59e057f2020-04-15 14:17:26 +00001#!/usr/bin/env python3
Éanna Ó Catháin6c3dee42020-09-10 13:02:37 +01002# Copyright © 2020 NXP and Contributors. All rights reserved.
Pavel Macenauerd0fedae2020-04-15 14:52:57 +00003# SPDX-License-Identifier: MIT
4
5import pyarmnn as ann
6import numpy as np
Pavel Macenauer09daef82020-06-02 11:54:59 +00007import os
Pavel Macenauerd0fedae2020-04-15 14:52:57 +00008from PIL import Image
9import example_utils as eu
10
11
12def 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áin145c88f2020-11-16 14:12:11 +000047if __name__ == "__main__":
48 args = eu.parse_command_line()
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000049
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000050 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 Macenauerd0fedae2020-04-15 14:52:57 +000055
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000056 # Download resources
57 image_filenames = eu.get_images(args.data_dir)
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000058
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000059 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 Macenauerd0fedae2020-04-15 14:52:57 +000062
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000063 # 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 Macenauerd0fedae2020-04-15 14:52:57 +000069
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000070 # Create a network from a model file
71 net_id, parser, runtime = eu.create_onnx_network(model_filename)
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000072
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000073 # Load input information from the model and create input tensors
74 input_binding_info = parser.GetNetworkInputBindingInfo("data")
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000075
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000076 # 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 Macenauerd0fedae2020-04-15 14:52:57 +000079
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000080 # Load labels
81 labels = eu.load_labels(labels_filename)
Pavel Macenauer09daef82020-06-02 11:54:59 +000082
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000083 # 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 Macenauer09daef82020-06-02 11:54:59 +000091
Éanna Ó Catháin145c88f2020-11-16 14:12:11 +000092 eu.run_inference(runtime, net_id, images, labels, input_binding_info, output_binding_info)