blob: 05bfd7b4151efaf83fae22b9074f7bc4e803cdf2 [file] [log] [blame]
Pavel Macenauer59e057f2020-04-15 14:17:26 +00001#!/usr/bin/env python3
Pavel Macenauerd0fedae2020-04-15 14:52:57 +00002# Copyright 2020 NXP
3# 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
Pavel Macenauer09daef82020-06-02 11:54:59 +000047args = eu.parse_command_line()
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000048
Pavel Macenauer09daef82020-06-02 11:54:59 +000049model_filename = 'mobilenetv2-1.0.onnx'
50labels_filename = 'synset.txt'
51archive_filename = 'mobilenetv2-1.0.zip'
52labels_url = 'https://s3.amazonaws.com/onnx-model-zoo/' + labels_filename
53model_url = 'https://s3.amazonaws.com/onnx-model-zoo/mobilenet/mobilenetv2-1.0/' + model_filename
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000054
Pavel Macenauer09daef82020-06-02 11:54:59 +000055# Download resources
56image_filenames = eu.get_images(args.data_dir)
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000057
Pavel Macenauer09daef82020-06-02 11:54:59 +000058model_filename, labels_filename = eu.get_model_and_labels(args.model_dir, model_filename, labels_filename,
59 archive_filename,
60 [model_url, labels_url])
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000061
Pavel Macenauer09daef82020-06-02 11:54:59 +000062# all 3 resources must exist to proceed further
63assert os.path.exists(labels_filename)
64assert os.path.exists(model_filename)
65assert image_filenames
66for im in image_filenames:
67 assert (os.path.exists(im))
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000068
Pavel Macenauer09daef82020-06-02 11:54:59 +000069# Create a network from a model file
70net_id, parser, runtime = eu.create_onnx_network(model_filename)
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000071
Pavel Macenauer09daef82020-06-02 11:54:59 +000072# Load input information from the model and create input tensors
73input_binding_info = parser.GetNetworkInputBindingInfo("data")
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000074
Pavel Macenauer09daef82020-06-02 11:54:59 +000075# Load output information from the model and create output tensors
76output_binding_info = parser.GetNetworkOutputBindingInfo("mobilenetv20_output_flatten0_reshape0")
77output_tensors = ann.make_output_tensors([output_binding_info])
Pavel Macenauerd0fedae2020-04-15 14:52:57 +000078
Pavel Macenauer09daef82020-06-02 11:54:59 +000079# Load labels
80labels = eu.load_labels(labels_filename)
81
82# Load images and resize to expected size
83images = eu.load_images(image_filenames,
84 224, 224,
85 np.float32,
86 255.0,
87 [0.485, 0.456, 0.406],
88 [0.229, 0.224, 0.225],
89 preprocess_onnx)
90
91eu.run_inference(runtime, net_id, images, labels, input_binding_info, output_binding_info)