ONNX (Open Neural Network Exchange) is an open-source format for representing machine learning models, designed to enable interoperability between different deep learning frameworks
FROMnvidia/cuda:12.6.0-cudnn-runtime-ubuntu22.04ARGUSERNAME=user
ARGUSER_UID=1000ARGUSER_GID=$USER_UID# Check if "ubuntu" user exists, delete it if it does, then create the desired userRUNifgetentpasswdubuntu>/dev/null2>&1;then\userdel-rubuntu&&\echo"Deleted existing ubuntu user";\fi&&\groupadd--gid$USER_GID$USERNAME&&\useradd-s/bin/bash--uid$USER_UID--gid$USER_GID-m$USERNAME&&\echo"Created new user $USERNAME"# Add sudo support for the non-root userRUNapt-getupdate&&apt-getinstall-ysudo\&&echo$USERNAMEALL=\(root\)NOPASSWD:ALL>/etc/sudoers.d/$USERNAME\&&chmod0440/etc/sudoers.d/$USERNAME\&&rm-rf/var/lib/apt/lists/*
# Install Python and pipRUNapt-getupdate\&&apt-get-yinstall--no-install-recommends\python3-pip\python3-dev\&&apt-getclean-y\&&rm-rf/var/lib/apt/lists/*
# Copy and install requirementsCOPYrequirements.txt/tmp/requirements.txt
RUNpip3install--no-cache-dir-r/tmp/requirements.txt
# RUN pip3 install https://github.com/ultralytics/assets/releases/download/v0.0.0/onnxruntime_gpu-1.20.0-cp310-cp310-linux_aarch64.whlRUNpip3installhttps://pypi.jetson-ai-lab.io/jp6/cu126/+f/e1e/9e3dc2f4d5551/onnxruntime_gpu-1.23.0-cp310-cp310-linux_aarch64.whl#sha256=e1e9e3dc2f4d5551c5f0b5554cf490d141cd0d339a5a7f4826ac0e04f20a35fc
# RUN apt-get update \# && apt-get -y install --no-install-recommends \# wget \# libgl1 \# libglib2.0-0 \# libsm6 \# libxext6 \# libxrender-dev \# && apt-get clean -y \# && rm -rf /var/lib/apt/lists/*
importonnxruntimeasort# Show all available providers on your machineprint("Available providers:",ort.get_available_providers())print("ONNX Runtime version:",ort.__version__)print("Build info:",ort.get_device())
importonnxruntimeasortimportnumpyasnpimportcv2# import matplotlib.pyplot as plt# --- Load ONNX model ---# "CPUExecutionProvider"# CUDAExecutionProvider# TensorrtExecutionProvidersession=ort.InferenceSession("model/mnist-8.onnx",providers=["CUDAExecutionProvider"])# --- Load and preprocess an image (28×28 grayscale) ---# You can replace 'digit.png' with your own file.img=cv2.imread("images/six_1.png",cv2.IMREAD_GRAYSCALE)ifimgisNone:# fallback: create a synthetic "3"-like shape for demoimg=np.zeros((28,28),np.uint8)cv2.putText(img,"3",(4,24),cv2.FONT_HERSHEY_SIMPLEX,0.9,255,2)# Normalize to [0,1]img=img.astype(np.float32)/255.0# Reshape to NCHW = (1,1,28,28)img=img.reshape(1,1,28,28)# --- Run inference ---input_name=session.get_inputs()[0].nameoutput_name=session.get_outputs()[0].namepred=session.run([output_name],{input_name:img})[0]digit=int(np.argmax(pred))print(f"Predicted digit: {digit}")