Jetson安装Python3.8 tensorrt库


背景

我在运行项目:https://github.com/wang-xinyu/tensorrtx/tree/master/yolov7/yolov7_trt.py时,遇到报错:

Traceback (most recent call last):
  File "yolov7_trt.py", line 17, in <module>
    import tensorrt as trt
ModuleNotFoundError: No module named 'tensorrt'

在执行完:

sudo apt update
sudo apt install nvidia-tensorrt

还是出现如下类似错误:

Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com
Collecting nvidia-tensorrt
  Downloading nvidia-tensorrt-0.0.1.dev5.tar.gz (7.9 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [17 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-t6193kze/nvidia-tensorrt_0e4a9b978c63427fb703eefcc65ff71c/setup.py", line 150, in <module>
          raise RuntimeError(open("ERROR.txt", "r").read())
      RuntimeError:
      ###########################################################################################
      The package you are trying to install is only a placeholder project on PyPI.org repository.
      This package is hosted on NVIDIA Python Package Index.
      
      This package can be installed as:
      ```
      $ pip install nvidia-pyindex
      $ pip install nvidia-tensorrt
      ```
      ###########################################################################################
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

查了资料知道,NVIDIA仅提供默认的 Python 版本包,对于其他 Python 版本,需要从源构建绑定。

本次记录:Python 3.8.19、TensorRT 8.5.2.2
在这里插入图片描述


1. 安装必要的依赖

sudo apt-get update
sudo apt-get install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev protobuf-compiler libprotobuf-dev libcurl4-openssl-dev

2.编译python3.8.19

下载Python源码:

wget https://www.python.org/ftp/python/3.8.19/Python-3.8.19.tar.xz
tar xvf Python-3.8.19.tar.xz Python-3.8.19/

编译:

mkdir build-python-3.8.19
cd build-python-3.8.19/
../Python-3.8.19/configure --enable-optimizations

在这里插入图片描述

make -j $(nproc)

在这里插入图片描述

sudo -H make altinstall

在这里插入图片描述

3.准备Python头文件

sudo apt-get install libpython3.8-dev

以上步骤都执行完,会在/usr/include/aarch64-linux-gnu下生成一个python3.8文件夹
在这里插入图片描述
确保Python头文件和其他必要文件在正确的位置:

mkdir python3.8
mkdir python3.8/include
cp ./usr/include/aarch64-linux-gnu/python3.8/pyconfig.h python3.8/include/
cp -r Python-3.8.19/Include/* python3.8/include/

在这里插入图片描述

4.构建 TensorRT pybinding

git clone https://github.com/pybind/pybind11.git
git clone -b release/8.5 https://github.com/NVIDIA/TensorRT.git
cd TensorRT
git submodule update --init --recursive

5.构建Python Wheel

返回到TensorRT的python目录修改并运行build.sh脚本:

cd python/
TRT_OSSPATH=${PWD}/.. EXT_PATH=${PWD}/../.. ./build.sh

build.sh如下:

#
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

PYTHON_MAJOR_VERSION=${PYTHON_MAJOR_VERSION:-3}
PYTHON_MINOR_VERSION=${PYTHON_MINOR_VERSION:-8}
TARGET=${TARGET_ARCHITECTURE:-aarch64}
CUDA_ROOT=${CUDA_ROOT:-/usr/local/cuda}
ROOT_PATH=${TRT_OSSPATH}
EXT_PATH=${EXT_PATH}
WHEEL_OUTPUT_DIR=${ROOT_PATH}/python/build
HEADER_PATH=/usr/include/aarch64-linux-gnu

mkdir -p ${WHEEL_OUTPUT_DIR}
pushd ${WHEEL_OUTPUT_DIR}

# Generate tensorrt.so
cmake .. -DCMAKE_BUILD_TYPE=Release \
         -DTARGET=${TARGET} \
         -DPYTHON_MAJOR_VERSION=${PYTHON_MAJOR_VERSION} \
         -DPYTHON_MINOR_VERSION=${PYTHON_MINOR_VERSION} \
         -DEXT_PATH=${EXT_PATH} \
         -DCUDA_INCLUDE_DIRS=${CUDA_ROOT}/include \
         -DTENSORRT_ROOT=${ROOT_PATH} \
         -DTENSORRT_BUILD=${ROOT_PATH}/build/
make -j12

# Generate wheel
TRT_MAJOR=$(awk '/^\#define NV_TENSORRT_MAJOR/ {print $3}' ${HEADER_PATH}/NvInferVersion.h)
TRT_MINOR=$(awk '/^\#define NV_TENSORRT_MINOR/ {print $3}' ${HEADER_PATH}/NvInferVersion.h)
TRT_PATCH=$(awk '/^\#define NV_TENSORRT_PATCH/ {print $3}' ${HEADER_PATH}/NvInferVersion.h)
TRT_BUILD=$(awk '/^\#define NV_TENSORRT_BUILD/ {print $3}' ${HEADER_PATH}/NvInferVersion.h)
TRT_VERSION=${TRT_MAJOR}.${TRT_MINOR}.${TRT_PATCH}.${TRT_BUILD}
TRT_MAJMINPATCH=${TRT_MAJOR}.${TRT_MINOR}.${TRT_PATCH}

echo "Generating python ${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION} bindings for TensorRT ${TRT_VERSION}"

expand_vars_cp () {
    test -f ${1} || (echo "ERROR: File: ${1} does not exist!" && exit 1); \
    sed -e "s|\#\#TENSORRT_VERSION\#\#|${TRT_VERSION}|g" \
        -e "s|\#\#TENSORRT_MAJMINPATCH\#\#|${TRT_MAJMINPATCH}|g" \
        ${1} > ${2}
}


pushd ${ROOT_PATH}/python/packaging
for dir in $(find . -type d); do mkdir -p ${WHEEL_OUTPUT_DIR}/$dir; done
for file in $(find . -type f); do expand_vars_cp $file ${WHEEL_OUTPUT_DIR}/${file}; done
popd
python3 setup.py -q bdist_wheel --python-tag=cp${PYTHON_MAJOR_VERSION}${PYTHON_MINOR_VERSION} --plat-name=linux_${TARGET}

popd

执行完就会在python/build/dist目录下生成tensorrt-8.5.2.2-cp38-none-linux_aarch64.whl文件
在这里插入图片描述

6.安装与验证Tensorrt包

进入自己的虚拟环境

pip install build/dist/tensorrt-8.5.2.2-cp38-none-linux_aarch64.whl

启动python环境,验证TensorRT是否正确安装:

import tensorrt as trt
print(trt.__version__)

在这里插入图片描述
出现上图,则说明安装成功!!!


总结

Jetson安装Python3.8 tensorrt库,需要确保正确Python与TensorRT版本,你的编译环境与目标平台(ARM架构)一致,避免使用不兼容的x86_64编译配置。
后续使用python调用tensorrt与cuda进行推理速度还是很快的,能够达到50帧,这在板端也是很炸裂的存在!!!
在这里插入图片描述

参考文档:
https://github.com/wang-xinyu/tensorrtx/issues/344
https://forums.developer.nvidia.com/t/tensorrt-on-jetson-with-python-3-9/196131/9
https://arcanesciencelab.wordpress.com/2021/02/14/building-python-3-9-1-on-jetpack-4-5-and-the-jetson-xavier-nx/
https://forums.developer.nvidia.com/t/unable-to-install-python-tensorrt/246445
https://github.com/NVIDIA/TensorRT/tree/release/8.2/python

如果阅读本文对你有用,欢迎一键三连呀!!!
2024年8月9日12:23:36
在这里插入图片描述

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI小笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值