Instructions to use nxp/efficientnet-lite0-imx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use nxp/efficientnet-lite0-imx with LiteRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Download example.py from nxp/efficientnet-lite0-imx: direct link, hf CLI and curl.
- Browser
- Download file 3.46 kB
-
https://huggingface.co/nxp/efficientnet-lite0-imx/resolve/main/example.py
- Command line
-
hf download hf://nxp/efficientnet-lite0-imx/example.py
-
curl -L -o example.py https://huggingface.co/nxp/efficientnet-lite0-imx/resolve/main/example.py
3.46 kB
| #!/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) | |