Image Classification
LiteRT
LiteRT
vision
imx8mplus
imx93
imx95
imx952
resnet
cifar10
tinyml
quantized
int8
Instructions to use nxp/tiny-resnet-imx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use nxp/tiny-resnet-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/tiny-resnet-imx: direct link, hf CLI and curl.
- Browser
- Download file 3.05 kB
-
https://huggingface.co/nxp/tiny-resnet-imx/resolve/main/example.py
- Command line
-
hf download hf://nxp/tiny-resnet-imx/example.py
-
curl -L -o example.py https://huggingface.co/nxp/tiny-resnet-imx/resolve/main/example.py
3.05 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 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) | |