问题
I just want to load onnx models in raspberry pi. How to load onnx models in edge devices?
回答1:
You can use ONNX Runtime for ONNX model inference in Raspberry Pi. It support Arm32v7l architecture. Pre-build binary is not provided as of 2020/1/14. So you need to build it from source code. Instruction is described below. https://github.com/microsoft/onnxruntime/blob/master/dockerfiles/README.md#arm-32v7
Install DockerCE on your development machine by following the instructions here
Create an empty local directory
mkdir onnx-build
cd onnx-build
- Save the Dockerfile to your new directory
Dockerfile.arm32v7
FROM balenalib/raspberrypi3-python:latest-stretch-build
ARG ONNXRUNTIME_REPO=https://github.com/Microsoft/onnxruntime
ARG ONNXRUNTIME_SERVER_BRANCH=master
#Enforces cross-compilation through Quemu
RUN [ "cross-build-start" ]
RUN install_packages \
sudo \
build-essential \
curl \
libcurl4-openssl-dev \
libssl-dev \
wget \
python3 \
python3-pip \
python3-dev \
git \
tar \
libatlas-base-dev
RUN pip3 install --upgrade pip
RUN pip3 install --upgrade setuptools
RUN pip3 install --upgrade wheel
RUN pip3 install numpy
# Build the latest cmake
WORKDIR /code
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.3/cmake-3.14.3.tar.gz
RUN tar zxf cmake-3.14.3.tar.gz
WORKDIR /code/cmake-3.14.3
RUN ./configure --system-curl
RUN make
RUN sudo make install
# Set up build args
ARG BUILDTYPE=MinSizeRel
ARG BUILDARGS="--config ${BUILDTYPE} --arm"
# Prepare onnxruntime Repo
WORKDIR /code
RUN git clone --single-branch --branch ${ONNXRUNTIME_SERVER_BRANCH} --recursive ${ONNXRUNTIME_REPO} onnxruntime
# Start the basic build
WORKDIR /code/onnxruntime
RUN ./build.sh ${BUILDARGS} --update --build
# Build Shared Library
RUN ./build.sh ${BUILDARGS} --build_shared_lib
# Build Python Bindings and Wheel
RUN ./build.sh ${BUILDARGS} --enable_pybind --build_wheel
# Build Output
RUN ls -l /code/onnxruntime/build/Linux/${BUILDTYPE}/*.so
RUN ls -l /code/onnxruntime/build/Linux/${BUILDTYPE}/dist/*.whl
RUN [ "cross-build-end" ]
- Run docker build
This will build all the dependencies first, then build ONNX Runtime and its Python bindings. This will take several hours.
docker build -t onnxruntime-arm32v7 -f Dockerfile.arm32v7 .
Note the full path of the .whl file
- Reported at the end of the build, after the # Build Output line.
- It should follow the format onnxruntime-0.3.0-cp35-cp35m-linux_armv7l.whl, but version number may have changed. You'll use this path to extract the wheel file later.
Check that the build succeeded
- Upon completion, you should see an image tagged onnxruntime-arm32v7 in your list of docker images:
docker images
- Extract the Python wheel file from the docker image
(Update the path/version of the .whl file with the one noted in step 5)
docker create -ti --name onnxruntime_temp onnxruntime-arm32v7 bash
docker cp onnxruntime_temp:/code/onnxruntime/build/Linux/MinSizeRel/dist/onnxruntime-0.3.0-cp35-cp35m-linux_armv7l.whl .
docker rm -fv onnxruntime_temp
This will save a copy of the wheel file, onnxruntime-0.3.0-cp35-cp35m-linux_armv7l.whl, to your working directory on your host machine.
Copy the wheel file (onnxruntime-0.3.0-cp35-cp35m-linux_armv7l.whl) to your Raspberry Pi or other ARM device
On device, install the ONNX Runtime wheel file
sudo apt-get update
sudo apt-get install -y python3 python3-pip
pip3 install numpy
# Install ONNX Runtime
# Important: Update path/version to match the name and location of your .whl file
pip3 install onnxruntime-0.3.0-cp35-cp35m-linux_armv7l.whl
- Test installation by following the instructions here
来源:https://stackoverflow.com/questions/59715507/how-to-load-or-infer-onnx-models-in-edge-devices-like-raspberry-pi