The degirum/dg_n2x_compiler_api Docker image is a lightweight, efficient tool for programmatic model compilation for the N2X runtime, eliminating the need for the Degirum Streamlit UI. This API allows you to compile your models by passing the required configuration parameters directly through the CLI.
onnx-fp32
Supports all four variations:
n2x-orca1-quantn2x-orca1-floatn2x-cpu-quantn2x-cpu-floattflite-fp32
Supports:
n2x-orca1-floatn2x-cpu-floattflite-int8
Supports:
n2x-orca1-quantn2x-cpu-quantn2x-orca1-quantn2x-orca1-floatn2x-cpu-quantn2x-cpu-floatn2x-orca1-quantn2x-orca1-floatn2x-cpu-quantn2x-cpu-floatstretchletterboxcrop-firstcrop-lastnearestbilinearareabicubiclanczosYolov5-DetectYolov8-DetectYolov8-PoseYolov8-SegmentClassificationNone.jpg/.jpeg files or use COCO128 images for quant models.{
"model_name": "model_name",
"model_version": 1,
"image_width": 640,
"image_height": 640,
"calibration_images": "",
"OutputNMSThreshold": "0.6",
"MaxDetectionsPerClass": "100",
"OutputConfThreshold": "0.3",
"MaxDetections": "100",
"UseRegularNMS": true,
"MaxClassesPerDetection": "1",
"upload_url": "https://cs.degirum.com/zoo/v1/public/models/",
"cloud_zoo_url": "degirum/<model_zoo_name>",
"cloud_zoo_token": "<degirum token>",
"device_type": ["n2x-cpu-quant"],
"upload_zip_cloud": false,
"separate_outputs": true,
"output_postprocess_type": "Yolov8-Detect",
"model_format": "onnx-fp32",
"input_pad_method": "letterbox",
"input_resize_method": "bilinear",
"input_img_norm_enabled": true,
"input_norm_mean": "[0.0, 0.0, 0.0]",
"input_norm_std": "[1.0, 1.0, 1.0]"
}
Use the following command to start the Docker container:
docker run -p 8535:8535 --mount type=bind,source=<zoo_folder_path>,target=/output_files degirum/dg_compiler_api
Run the Python API script with custom args:
python3 dg_n2x_compiler_api_usage.py --json_file params.json --model_file checkpoint.(pt, onnx, tflite) --class_file coco.yaml --calib_images_folder ./calib_images_folder
To send a POST request with your model and parameters, run the following command in your terminal:
curl -X POST \
-F "input_params=$(cat params.json)" \
-F "model_file=@../yolov8n_relu6_coco.onnx" \
-F "class_file=@../assets/class_labels/coco.yaml" \
-F "calib_image1=@./calib_images_folder/image1.jpg" \
-F "calib_image2=@./calib_images_folder/image2.jpg" \
http://0.0.0.0:8535/generalcompile
A reference usage script (dg_compiler_api_usage.py) is provided in the repository. This script sends a POST request with your model and configuration parameters. Below is a shortened version of the script:
#!/usr/bin/env python3
import requests
import json
import argparse
import os
def load_json(json_file):
if not os.path.isfile(json_file):
raise FileNotFoundError(f"JSON file '{json_file}' not found.")
with open(json_file, 'r') as f:
return json.load(f)
def prepare_files(model_file, class_file=None, images_folder=None):
files = {}
if model_file:
if not os.path.isfile(model_file):
raise FileNotFoundError(f"Model file '{model_file}' not found.")
files['model_file'] = open(model_file, 'rb')
if class_file:
if not os.path.isfile(class_file):
raise FileNotFoundError(f"Class file '{class_file}' not found.")
files['class_file'] = open(class_file, 'rb')
if images_folder:
if not os.path.isdir(images_folder):
raise FileNotFoundError(f"Images folder '{images_folder}' not found.")
for idx, fname in enumerate(sorted(os.listdir(images_folder)), start=1):
if fname.lower().endswith(('.png', '.jpg', '.jpeg')):
files[f'calib_image{idx}'] = open(os.path.join(images_folder, fname), 'rb')
return files
def send_request(url, input_params, files):
response = requests.post(url, files=files, data={'input_params': json.dumps(input_params)})
for fh in files.values():
fh.close()
print(f"Status Code: {response.status_code}")
try:
print("Response JSON:", response.json())
except json.JSONDecodeError:
print("Response content is not in JSON format:")
print(response.text)
def main():
parser = argparse.ArgumentParser(description="Send a POST request with model and parameters.")
parser.add_argument('--json_file', required=True, help='Path to the JSON file with input parameters.')
parser.add_argument('--model_file', required=True, help='Path to the model file (.onnx, .tflite).')
parser.add_argument('--class_file', help='Optional path to a class file (e.g., YAML).')
parser.add_argument('--calib_images_folder', help='Optional path to a folder of calibration images.')
parser.add_argument('--port', type=int, default=8535, help='Port number of the compiler server. Default is 8535.')
args = parser.parse_args()
try:
url = f"http://0.0.0.0:{args.port}/generalcompile"
print("Using URL:", url)
input_params = load_json(args.json_file)
input_params['model_file_name'] = os.path.basename(args.model_file)
files = prepare_files(args.model_file, args.class_file, args.calib_images_folder)
send_request(url, input_params, files)
except (FileNotFoundError, ValueError) as e:
print("Error:", e)
if __name__ == '__main__':
main()
Content type
Image
Digest
sha256:e8a4df73d…
Size
2.1 GB
Last updated
over 1 year ago
docker pull degirum/dg_n2x_compiler_api