xref: /aosp_15_r20/external/armnn/python/pyarmnn/examples/image_classification/onnx_mobilenetv2.py (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1#!/usr/bin/env python3
2# Copyright © 2020 NXP and Contributors. All rights reserved.
3# SPDX-License-Identifier: MIT
4
5import pyarmnn as ann
6import numpy as np
7import os
8from 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
47if __name__ == "__main__":
48    args = eu.parse_command_line()
49
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
55
56    # Download resources
57    image_filenames = eu.get_images(args.data_dir)
58
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])
62
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))
69
70    # Create a network from a model file
71    net_id, parser, runtime = eu.create_onnx_network(model_filename)
72
73    # Load input information from the model and create input tensors
74    input_binding_info = parser.GetNetworkInputBindingInfo("data")
75
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])
79
80    # Load labels
81    labels = eu.load_labels(labels_filename)
82
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)
91
92    eu.run_inference(runtime, net_id, images, labels, input_binding_info, output_binding_info)
93