1. 安装cuda,tensor-rt
apt install cuda-11-4
apt install libfreeimage-dev libcudnn8 libcudnn8-dev libcudnn8-samples
apt install tensorrt tensorrt-dev tensorrt-libs python3-libnvinfer python3-libnvinfer-dev uff-converter-tf onnx-graphsurgeon graphsurgeon-tf libatlas-base-dev
2. 安装推理环境
onnxruntime 仅支持 CPU 推理,onnxruntime-gpu 支持在 GPU 上进行推理。
Nvidia 提供了一个专门优化的 ONNX Runtime 版本,支持 TensorRT 加速,你可以在以下网站下载与编译的,也可以下载这个来自己编译。
https://elinux.org/Jetson_Zoo#ONNX_Runtime
我是使用自己编译的,已经将编译好的,放在如下连接:onnxruntime_gpu-1.16.3-cp38-cp38-linux_aarch64.whl
2.1 编译 onnxruntime 推理框架
自己编译的方法如下:
git clone --depth 1 --branch v1.16.3 https://github.com/microsoft/onnxruntime.git
cd onnxruntime
运行编译命令
./build.sh --config Release --update --build --parallel --build_wheel \
--use_tensorrt --cuda_home /usr/local/cuda --cudnn_home /usr/lib/aarch64-linux-gnu \
--tensorrt_home /usr/lib/aarch64-linux-gnu --allow_running_as_root
2.2 安装
你可以使用 Nvidia 的 PyPI 仓库 安装它:
pip3 uninstall onnxruntime
pip3 install onnxruntime_gpu-1.16.3-cp38-cp38-linux_aarch64.whl
而apt install onnxruntime 通常是 CPU 版本,不包含 TensorRT 加速。
2.3 检查onnxruntime 是否正确安装
python3 -c "import onnxruntime; print(onnxruntime.get_device()); print(onnxruntime.__version__)"
如果出现GPU,说明安装Ok,如果是CPU 说明没有安装成功。
3. 配置MLperf
Build and install the benchmark:
cd ../../loadgen; CFLAGS="-std=c++14" python setup.py develop --user; cd ../vision/classification_and_detection
python setup.py develop
# 转换模型
python3 onnxruntime/tools/python/remove_initializer_from_input.py --input inference/vision/classification_and_detection/mobilenet_v1_1.0_224.onnx --output mobilenet_v1_1.0_224.onnx
4. 运行测试
$ cd vision/classification_and_detection
$ export MODEL_DIR=${PWD}
$ export DATA_DIR=${PWD}/fake_imagenet
## 运行在GPU 上
$ ./run_local.sh onnxruntime mobilenet gpu --scenario Offline
## 运行在CPU 上
./run_local.sh onnxruntime mobilenet cpu --scenario Offline
##or
./run_local.sh onnxruntime mobilenet --scenario Offline
export CUDA_VISIBLE_DEVICES=“” 的作用是 禁用所有 CUDA 设备,即使系统上有可用的 GPU,程序也不会使用它们,而是回退到 CPU 进行计算。