File size: 3,047 Bytes
9590012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# Copyright 2023-2024,2026 NXP
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

import argparse

import cv2
import numpy as np
import tensorflow as tf

from labels import CIFAR10_LABELS

INPUT_SIZE = 32


def inference(model_path, input_path, output_path=None):
    interpreter = tf.lite.Interpreter(model_path=model_path)
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    print("Loaded model:", model_path)

    img = cv2.imread(input_path, cv2.IMREAD_COLOR)
    if img is None:
        raise RuntimeError("Could not read input image: %s" % input_path)
    img = cv2.resize(img, (INPUT_SIZE, INPUT_SIZE))

    tensor = img.astype(np.float32)

    input_dtype = input_details[0]["dtype"]
    if input_dtype in (np.int8, np.uint8):
        # Quantize the input using the model input scale/zero-point.
        in_scale, in_zero_point = input_details[0]["quantization"]
        tensor = np.round(tensor / in_scale + in_zero_point)
        tensor = tensor.astype(input_dtype)

    tensor = np.expand_dims(tensor, axis=0)

    interpreter.set_tensor(input_details[0]["index"], tensor)
    interpreter.invoke()

    out = interpreter.get_tensor(output_details[0]["index"])

    # Dequantize the output if the model returns a quantized tensor.
    out_scale, out_zero_point = output_details[0]["quantization"]
    if out_scale != 0:
        out = out_scale * (out.astype(np.float32) - out_zero_point)

    probs = out[0].astype(np.float32)
    top = int(probs.argmax())

    label = CIFAR10_LABELS.get(top, "unknown")
    line = "Top class: [{}] {}  (score: {:.4f})".format(top, label, probs[top])
    print(line)

    if output_path:
        with open(output_path, "w") as f:
            f.write(line + "\n")
        print("Wrote prediction to", output_path)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Tiny ResNet (CIFAR-10) inference example")
    parser.add_argument(
        "-m",
        "--model",
        default="assets/quantized_tflite_model/pretrainedResnet_quant.tflite",
        help="Path to the TFLite model file.",
    )
    parser.add_argument(
        "-i",
        "--input",
        default="example_input.jpg",
        help="Path to the input image.",
    )
    parser.add_argument(
        "-o",
        "--output",
        default=None,
        help="Optional path to write the prediction as text.",
    )
    args = parser.parse_args()

    inference(args.model, args.input, args.output)