Download export_and_quantize.sh from Intel/object-classification: direct link, hf CLI and curl.
- Browser
- Download file 4 kB
-
https://huggingface.co/Intel/object-classification/resolve/main/export_and_quantize.sh
- Command line
-
hf download hf://Intel/object-classification/export_and_quantize.sh
-
curl -L -o export_and_quantize.sh https://huggingface.co/Intel/object-classification/resolve/main/export_and_quantize.sh
4 kB
| # SPDX-License-Identifier: MIT | |
| # Copyright (C) Intel Corporation | |
| # | |
| # Export a YOLO26 detector to OpenVINO IR for the object-classification use case. | |
| # The accompanying samples group detected objects into higher-level city | |
| # categories (People, Vehicles) for traffic situational awareness. | |
| # Usage: ./export_and_quantize.sh [MODEL_VARIANT] [PRECISION] | |
| # Example: ./export_and_quantize.sh yolo26n FP16 | |
| # | |
| # Supported precisions: | |
| # FP32 -- Full-precision floating-point weights | |
| # FP16 -- Half-precision floating-point weights (default) | |
| # INT8 -- Quantized 8-bit integer weights (requires NNCF) | |
| # | |
| # Precision / device compatibility: | |
| # | Precision | CPU | GPU | NPU | | |
| # |-----------|-----|-----|-----| | |
| # | FP32 | Yes | Yes | No | | |
| # | FP16 | Yes | Yes | Yes | | |
| # | INT8 | Yes | Yes | Yes | | |
| set -euo pipefail | |
| MODEL_NAME="${1:-yolo26n}" | |
| PRECISION="${2:-FP16}" | |
| PRECISION="$(echo "${PRECISION}" | tr '[:lower:]' '[:upper:]')" | |
| if [[ "${PRECISION}" != "FP32" && "${PRECISION}" != "FP16" && "${PRECISION}" != "INT8" ]]; then | |
| echo "ERROR: unsupported precision '${PRECISION}'. Choose FP32, FP16, or INT8." >&2 | |
| exit 1 | |
| fi | |
| echo "--- Installing dependencies ---" | |
| if [[ "${PRECISION}" == "INT8" ]]; then | |
| pip install -qU openvino nncf ultralytics | |
| else | |
| pip install -qU openvino ultralytics | |
| fi | |
| # Ask for approval before downloading models and sample files | |
| echo "" | |
| echo "This script will download:" | |
| echo " - Model weights and a sample traffic video" | |
| echo "" | |
| read -p "Continue with downloads? (yes/no): " APPROVAL | |
| if [[ "${APPROVAL}" != "yes" ]]; then | |
| echo "Download cancelled by user." | |
| exit 0 | |
| fi | |
| # Ping the HuggingFace repo to register a tracked download of config.json. | |
| # This is best-effort: a failure (offline, or before the repo is published) | |
| # must not stop the export. | |
| echo "--- Registering HuggingFace download (tracking ping) ---" | |
| HF_REPO_ID="Intel/object-classification" | |
| HF_CONFIG_URL="https://huggingface.co/${HF_REPO_ID}/resolve/main/config.json" | |
| if curl -fsSL -o /dev/null "${HF_CONFIG_URL}"; then | |
| echo "Registered HuggingFace download for ${HF_REPO_ID}" | |
| else | |
| echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2 | |
| fi | |
| echo "" | |
| echo "--- Downloading sample test video (urban roundabout) ---" | |
| if [[ ! -f test_video.mp4 ]]; then | |
| wget -q -O test_video.mp4 \ | |
| "https://www.pexels.com/download/video/30119018/?fps=59.94&h=720&w=1280" | |
| echo "Downloaded: test_video.mp4" | |
| else | |
| echo "Already present: test_video.mp4" | |
| fi | |
| if [[ "${PRECISION}" == "FP32" ]]; then | |
| HALF_FLAG="False" | |
| EXPORT_LABEL="FP32" | |
| else | |
| HALF_FLAG="True" | |
| EXPORT_LABEL="FP16" | |
| fi | |
| echo "--- Exporting ${MODEL_NAME} to OpenVINO IR (${EXPORT_LABEL}) ---" | |
| python3 -c " | |
| from ultralytics import YOLO | |
| model = YOLO('${MODEL_NAME}.pt') | |
| model.export(format='openvino', half=${HALF_FLAG}, dynamic=False, imgsz=640) | |
| print('Export complete: ${MODEL_NAME}_openvino_model/') | |
| " | |
| if [[ "${PRECISION}" == "INT8" ]]; then | |
| echo "--- Quantizing to INT8 with NNCF ---" | |
| python3 -c " | |
| import nncf | |
| import openvino as ov | |
| import numpy as np | |
| import cv2 | |
| core = ov.Core() | |
| model = core.read_model('${MODEL_NAME}_openvino_model/${MODEL_NAME}.xml') | |
| # Calibrate on a representative frame from the sample traffic video. | |
| cap = cv2.VideoCapture('test_video.mp4') | |
| ok, frame = cap.read() | |
| cap.release() | |
| if not ok: | |
| raise SystemExit('Could not read a calibration frame from test_video.mp4') | |
| img = cv2.resize(frame, (640, 640)) | |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0 | |
| img = img.transpose(2, 0, 1)[np.newaxis, ...] # NCHW | |
| def transform_fn(data_item): | |
| return img | |
| calibration_dataset = nncf.Dataset(list(range(300)), transform_fn) | |
| quantized = nncf.quantize( | |
| model, | |
| calibration_dataset, | |
| preset=nncf.QuantizationPreset.MIXED, | |
| subset_size=300, | |
| ) | |
| ov.save_model(quantized, '${MODEL_NAME}_objcls_int8.xml') | |
| print('Quantization complete: ${MODEL_NAME}_objcls_int8.xml') | |
| " | |
| fi | |
| echo "--- Done ---" | |