From [1]: "CLIP (Contrastive Language-Image Pre-Training) is a neural network trained on a variety of (image, text) pairs. It can be instructed in natural language to predict the most relevant text snippet, given an image, without directly optimizing for the task, similarly to the zero-shot capabilities of GPT-2 and 3."
CLIP has two encoders:
The purpose of these two encoders is to map both images and text into a shared embedding space, where the cosine similarity between an image and its corresponding textual description can be measured. By aligning image representations and their corresponding captions, CLIP can generalize across different modalities (text and images).
This repository focuses on the CLIP image embedding, which provides a tensor representation of the
image features for the corresponding image-text pair prediction. It is built upon the original ResNet50
float32 CLIP vision encoder [1]. Here NXP quantizes only the vision model to int8 and compiles it for
NXP i.MX platforms (i.MX 8M Plus, i.MX 93, i.MX 95 and i.MX 952); the text encoder is kept in its original
float32 format for host/CPU inference. It also contains the scripts used to regenerate these assets
(recipe.sh) and an inference example (example.py).
This repository contains pre-compiled model files optimized for NXP i.MX processors.
This model is a derivative work with the following changes from the original:
lf-6.18.20_2.0.0 and lf-6.18.37_2.1.0.3.1.3, 3.2.1 and 3.2.3.Note: all model information below refers only to the int8 quantized vision model with ResNet50 architecture.
Original model available at: https://github.com/openai/CLIP.
| Information | Value |
|---|---|
| Input shape | RGB image crop (1, 224, 224, 3), int8 |
| Input example | (Public domain picture) |
| Output shape | Image feature embedding, shape (1, 1024), int8 quantized; dequantize with the output tensor's scale / zero point (see example.py) |
| Output example | Output tensor: [[43, 45, 38, ..., 40, 42, 41]] |
| FLOPS | 4 GOPS |
| Number of parameters | 58,832,602 |
| File size (int8) | 37.4 MB (assets/quantized_tflite_model/image_clip_quant_int8.tflite) |
| Source framework | ONNX (originally PyTorch, exported to ONNX upstream) |
All assets below run with the eIQ LiteRT (TensorFlow Lite) runtime and the corresponding NPU delegate (VX for i.MX 8M Plus, Ethos-U for i.MX 93, Neutron for i.MX 95 / i.MX 952). The same information is captured in release_manifest.json.
| Asset | Runtime | Precision | HW target | BSP & SDK versions | Link to asset |
|---|---|---|---|---|---|
| i.MX 8M Plus Quantized | LiteRT | INT8 | i.MX 8M Plus | - | Download |
| i.MX 93 Precompiled | LiteRT | INT8 | i.MX 93 | BSP lf-6.18.20_2.0.0, VELA_TAG = lf-6.18.20_2.0.0 | Download |
| i.MX 93 Precompiled | LiteRT | INT8 | i.MX 93 | BSP lf-6.18.37_2.1.0, VELA_TAG = lf-6.18.37_2.1.0 | Download |
| i.MX 95 Precompiled | LiteRT | INT8 | i.MX 95 | BSP lf-6.18.20_2.0.0, Neutron SDK v3.1.3 | Download |
| i.MX 95 Precompiled | LiteRT | INT8 | i.MX 95 | BSP lf-6.18.37_2.1.0, Neutron SDK v3.2.1 | Download |
| i.MX 95 Precompiled | LiteRT | INT8 | i.MX 95 | BSP lf-6.18.37_2.1.0, Neutron SDK v3.2.3 | Download |
| i.MX 952 Precompiled | LiteRT | INT8 | i.MX 952 | BSP lf-6.18.20_2.0.0, Neutron SDK v3.1.3 | Download |
| i.MX 952 Precompiled | LiteRT | INT8 | i.MX 952 | BSP lf-6.18.37_2.1.0, Neutron SDK v3.2.1 | Download |
| i.MX 952 Precompiled | LiteRT | INT8 | i.MX 952 | BSP lf-6.18.37_2.1.0, Neutron SDK v3.2.3 | Download |
The hardware-independent int8 vision model and the float32 text encoder are also provided for host (x86)
inference in assets/quantized_tflite_model/:
image_clip_quant_int8.tflite (identical to the assets/imx8mplus/ asset above) and
textual_clip_float32.tflite (int32 input / float32 output, not compiled for any NPU).
The original ResNet50 CLIP vision encoder was pretrained by OpenAI on a large, web-scale set of (image, text) pairs referred to in [1] as "WIT" (WebImageText). NXP did not retrain the network; NXP only quantized and compiled the existing float32 vision encoder as described in Modifications above.
The recipe installs the eIQ Neutron SDK from the NXP eIQ PyPI Distribution Portal. Configure pip once to use it:
python -m pip config --user set global.index https://eiq.nxp.com/repository
python -m pip config --user set global.index-url https://eiq.nxp.com/repository
python -m pip config --user set global.trusted-host eiq.nxp.com
python -m pip config --user set global.extra-index-url https://pypi.org/simple
python -m pip config --user set global.find-links https://eiq.nxp.com/repository/
To reproduce the quantized and compiled model files from scratch, run:
bash recipe.sh [VELA_TAG] [NEUTRON_VERSION]
# e.g. default: bash recipe.sh lf-6.18.20_2.0.0 3.2.3
# e.g. other: bash recipe.sh lf-6.18.37_2.1.0 3.2.1
Set DATASET_PATH in recipe.sh to your local ImageNetV2 subset before running.
This produces:
assets/quantized_tflite_model/image_clip_quant_int8.tflite - int8 vision model for i.MX 8M Plus (CPU/NPU)assets/quantized_tflite_model/textual_clip_float32.tflite - float32 text encoder (CPU)assets/imx93/<vela-tag>/image_clip_quant_int8_vela.tflite - Vela-compiled for i.MX 93 Ethos-U65assets/imx95/<neutron-version>/image_clip_quant_int8_converted.tflite - Neutron-compiled for i.MX 95assets/imx952/<neutron-version>/image_clip_quant_int8_converted.tflite - Neutron-compiled for i.MX 952An example of how to run inference is provided in example.py (run on a PC):
pip install tensorflow numpy pillow tqdm ftfy regex tabulate
python example.py --model assets/quantized_tflite_model/image_clip_quant_int8.tflite --text-model assets/quantized_tflite_model/textual_clip_float32.tflite --input images/flowers.jpg images/heavy-industry.jpg images/black_car.jpg
example.py also accepts -o/--output to write results to a text file and -k/--top-k to change how
many of the closest text matches are reported (default 5).
Note: BSP >= LF6.1.36_2.1.0 supports the Ethos-U Delegate on i.MX 93, which performs Vela compilation online. If using an older BSP version, compile the quantized TFLite model with the Vela compiler before use.
Please refer to the i.MX Machine Learning User Guide for details on running models on each target platform.
This model, including the quantized and compiled assets and the scripts in this repository, is released under the MIT License (see LICENSE). The original OpenAI CLIP model is also released under the MIT License (see MODEL_LICENSE).
If you use this model, please cite both this work and the original model:
@inproceedings{radford2021learning,
title={Learning transferable visual models from natural language supervision},
author={Radford, Alec and Kim, Jong Wook and Hallacy, Chris and Ramesh, Aditya and Goh, Gabriel and Agarwal, Sandhini and Sastry, Girish and Askell, Amanda and Mishkin, Pamela and Clark, Jack and others},
booktitle={International conference on machine learning},
pages={8748--8763},
year={2021},
organization={PmLR}
}
mlunar/clip-variants