blob: b6d5d8c61eaf5d3a40c42aa5928b94e01ccf735d [file] [log] [blame]
Pavel Macenauerd0fedae2020-04-15 14:52:57 +00001# Copyright 2020 NXP
2# SPDX-License-Identifier: MIT
3
4import pyarmnn as ann
5import numpy as np
6from PIL import Image
7import example_utils as eu
8
9
10def preprocess_onnx(img: Image, width: int, height: int, data_type, scale: float, mean: list,
11 stddev: list):
12 """Preprocessing function for ONNX imagenet models based on:
13 https://github.com/onnx/models/blob/master/vision/classification/imagenet_inference.ipynb
14
15 Args:
16 img (PIL.Image): Loaded PIL.Image
17 width (int): Target image width
18 height (int): Target image height
19 data_type: Image datatype (np.uint8 or np.float32)
20 scale (float): Scaling factor
21 mean: RGB mean values
22 stddev: RGB standard deviation
23
24 Returns:
25 np.array: Preprocess image as Numpy array
26 """
27 img = img.resize((256, 256), Image.BILINEAR)
28 # first rescale to 256,256 and then center crop
29 left = (256 - width) / 2
30 top = (256 - height) / 2
31 right = (256 + width) / 2
32 bottom = (256 + height) / 2
33 img = img.crop((left, top, right, bottom))
34 img = img.convert('RGB')
35 img = np.array(img)
36 img = np.reshape(img, (-1, 3)) # reshape to [RGB][RGB]...
37 img = ((img / scale) - mean) / stddev
38 # NHWC to NCHW conversion, by default NHWC is expected
39 # image is loaded as [RGB][RGB][RGB]... transposing it makes it [RRR...][GGG...][BBB...]
40 img = np.transpose(img)
41 img = img.flatten().astype(data_type) # flatten into a 1D tensor and convert to float32
42 return img
43
44
45if __name__ == "__main__":
46 # Download resources
47 kitten_filename = eu.download_file('https://s3.amazonaws.com/model-server/inputs/kitten.jpg')
48 labels_filename = eu.download_file('https://s3.amazonaws.com/onnx-model-zoo/synset.txt')
49 model_filename = eu.download_file(
50 'https://s3.amazonaws.com/onnx-model-zoo/mobilenet/mobilenetv2-1.0/mobilenetv2-1.0.onnx')
51
52 # Create a network from a model file
53 net_id, parser, runtime = eu.create_onnx_network(model_filename)
54
55 # Load input information from the model and create input tensors
56 input_binding_info = parser.GetNetworkInputBindingInfo("data")
57
58 # Load output information from the model and create output tensors
59 output_binding_info = parser.GetNetworkOutputBindingInfo("mobilenetv20_output_flatten0_reshape0")
60 output_tensors = ann.make_output_tensors([output_binding_info])
61
62 # Load labels
63 labels = eu.load_labels(labels_filename)
64
65 # Load images and resize to expected size
66 image_names = [kitten_filename]
67 images = eu.load_images(image_names,
68 224, 224,
69 np.float32,
70 255.0,
71 [0.485, 0.456, 0.406],
72 [0.229, 0.224, 0.225],
73 preprocess_onnx)
74
75 for idx, im in enumerate(images):
76 # Create input tensors
77 input_tensors = ann.make_input_tensors([input_binding_info], [im])
78
79 # Run inference
80 print("Running inference on '{0}' ...".format(image_names[idx]))
81 runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)
82
83 # Process output
84 out_tensor = ann.workload_tensors_to_ndarray(output_tensors)[0][0]
85 results = np.argsort(out_tensor)[::-1]
86 eu.print_top_n(5, results, labels, out_tensor)