delivery-package-verification / export_and_quantize.sh
vagheshpatel's picture
Sync delivery-package-verification from metro-analytics-catalog
a8485ae verified
Raw
History Blame Contribute Delete
3.5 kB
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# Export a YOLO26 detector to OpenVINO IR for the delivery-package-verification
# use case (COCO backpack/handbag/suitcase relabeled to "package" at runtime).
# Usage: ./export_and_quantize.sh [MODEL_VARIANT] [PRECISION]
# Example: ./export_and_quantize.sh yolo26n FP16
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/or sample files"
echo ""
read -p "Continue with downloads? (yes/no): " APPROVAL
if [[ "${APPROVAL}" != "yes" ]]; then
echo "Download cancelled by user."
exit 0
fi
echo ""
echo "--- Downloading sample test image ---"
if [[ ! -f test.jpg ]]; then
wget -q -O test.jpg https://ultralytics.com/images/bus.jpg
echo "Downloaded: test.jpg"
else
echo "Already present: test.jpg"
fi
echo ""
echo "--- Downloading sample test video ---"
if [[ ! -f test_video.mp4 ]]; then
wget -q -O test_video.mp4 \
"https://www.pexels.com/download/video/6170052/?fps=25&w=540&h=960"
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/')
"
echo "--- Writing package label map (relabels backpack/handbag/suitcase -> package) ---"
python3 - "${MODEL_NAME}" <<'PY'
import sys
import yaml
name = sys.argv[1]
with open(f"{name}_openvino_model/metadata.yaml") as f:
meta = yaml.safe_load(f)
names = meta["names"]
labels = [names[i] for i in range(len(names))]
for i in (24, 26, 28): # backpack, handbag, suitcase -> package
labels[i] = "package"
with open("coco_package_labels.txt", "w") as f:
f.write("\n".join(labels) + "\n")
print(f"Wrote coco_package_labels.txt ({len(labels)} labels)")
PY
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')
img = cv2.imread('test.jpg')
img = cv2.resize(img, (640, 640))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
img = img.transpose(2, 0, 1)[np.newaxis, ...]
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}_package_int8.xml')
print('Quantization complete: ${MODEL_NAME}_package_int8.xml')
"
fi
echo "--- Done ---"