File size: 3,455 Bytes
e4846ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
#!/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 numpy as np
import tensorflow as tf
from PIL import Image

from labels import IMAGENET_LABELS

INPUT_SIZE = 224

# The EfficientNet-lite TFLite model outputs 1001 classes: index 0 is a
# "background" class, so the 1000 ImageNet labels are offset by 1.
LABEL_OFFSET = 1


def inference(model_path, input_path, output_path=None, top_k=5):
    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 = Image.open(input_path).convert("RGB")
    img = img.resize((INPUT_SIZE, INPUT_SIZE))
    img = np.asarray(img)

    input_dtype = input_details[0]["dtype"]
    if input_dtype == np.uint8:
        # uint8-input model: feed raw [0, 255] pixels.
        tensor = img.astype(np.uint8)
    elif input_dtype == np.int8:
        # int8-input model: shift the [0, 255] pixel range to [-128, 127].
        tensor = (img.astype(np.int32) - 128).astype(np.int8)
    else:
        # float32-input model: EfficientNet-lite expects pixels in [-1, 1].
        tensor = (img.astype(np.float32) / 127.5) - 1.0

    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 = probs.argsort()[::-1][:top_k]

    print("Top {} predictions:".format(top_k))
    lines = []
    for rank, idx in enumerate(top):
        label = IMAGENET_LABELS.get(int(idx) - LABEL_OFFSET, "unknown")
        line = "  {}. [{}] {}  (score: {:.4f})".format(rank + 1, idx, label, probs[idx])
        print(line)
        lines.append(line)

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


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="EfficientNet-lite0 inference example")
    parser.add_argument(
        "-m",
        "--model",
        default="assets/quantized_tflite_model/efficientnet-lite0-int8.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 top predictions as text.",
    )
    args = parser.parse_args()

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