【问题标题】:How to load or infer onnx models in edge devices like raspberry pi?如何在树莓派等边缘设备中加载或推断 onnx 模型?
【发布时间】:2020-01-13 11:12:44
【问题描述】:

我只想在树莓派中加载 onnx 模型。如何在边缘设备中加载 onnx 模型?

【问题讨论】:

  • 投票结束这个问题,因为有一个dedicated rasperry pi forum
  • @Chris32,Raspberry PI 是典型的 Linux 系统,因此问题的答案将是“以与在 PC 上加载它们相同的方式加载它们”。但是问题缺少关于运行时的任何细节(python、c++、rust、go 等),所以我认为它太宽泛了。
  • 使用了 Rust 和 c++ 运行时

标签: raspberry-pi iot onnx


【解决方案1】:

您可以在 Raspberry Pi 中使用 ONNX 运行时进行 ONNX 模型推理。它支持 Arm32v7l 架构。截至 2020 年 1 月 14 日,不提供预构建二进制文件。所以你需要从源代码构建它。说明如下。 https://github.com/microsoft/onnxruntime/blob/master/dockerfiles/README.md#arm-32v7

  1. 按照说明here

  2. 在您的开发机器上安装DockerCE
  3. 创建一个空的本地目录

mkdir onnx-build
cd onnx-build
  1. 将 Dockerfile 保存到新目录

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" ]
  1. 运行 docker build

这将首先构建所有依赖项,然后构建 ONNX 运行时及其 Python 绑定。这需要几个小时。

docker build -t onnxruntime-arm32v7 -f Dockerfile.arm32v7 .
  1. 注意 .whl 文件的完整路径

    • 在构建结束时报告,在 # Build Output 行之后。
    • 应该遵循 onnxruntime-0.3.0-cp35-cp35m-linux_armv7l.whl 格式,但版本号可能已更改。稍后您将使用此路径来提取轮文件。
  2. 检查构建是否成功

    • 完成后,您应该会在 docker 图像列表中看到标记为 onnxruntime-arm32v7 的图像:
docker images
  1. 从 docker 镜像中提取 Python Wheel 文件

(将 .whl 文件的路径/版本更新为步骤 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

这会将 wheel 文件 onnxruntime-0.3.0-cp35-cp35m-linux_armv7l.whl 的副本保存到您主机上的工作目录中。

  1. 将 wheel 文件 (onnxruntime-0.3.0-cp35-cp35m-linux_armv7l.whl) 复制到您的 Raspberry Pi 或其他 ARM 设备

  2. 在设备上,安装 ONNX Runtime wheel 文件

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
  1. 按照说明here测试安装

【讨论】:

  • 虽然链接可能会回答问题,但请您自己概述说明,以防链接将来停止工作?
猜你喜欢
  • 1970-01-01
  • 2019-02-14
  • 2017-11-26
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多