Ubuntu20.04安装onnx与onnx_tensorrt

描述

onnx和onnx_tensorrt:库,用于推理onnx格式的模型

Ubuntu20.04系统安装onnx与onnx_tensorrt

验证是否成功安装onnx与onnx_tensorrt

运行命令python

import onnx
import onnx_tensorrt

查看是否报错

安装onnx(C++)

  1. 安装onnx_tensorrt需要的protobuf库

    sudo apt-get install libprotobuf-dev protobuf-compiler
    
  2. 拉取代码

    git clone --recursive https://github.com/onnx/onnx.git
    
  3. 更改版本

    cd onnx
    vim VERSION_NUMBER
    

    我的是

    1.12.0  # 版本号的文本只有一行
    

    改成

    1.8.0
    

    wq保存

  4. 编译onnx

    mkdir build
    cd build
    cmake ..
    make -j8
    sudo make install
    
  5. 验证安装是否成功

    python
    >>> import onnx
    >>> onnx.__version__
    '1.8.0'
    

安装onnx(python)

cd onnx
python setup.py build
python setup.py install

安装onnx_tensorrt(C++)

  1. 从Github下载源码,这里我们选择下载仓库中的某个tag,tag名为release/8.0
    git clone --recursive -b release/8.0 https://github.com/onnx/onnx-tensorrt.git
    
  2. 编译并安装
    cd onnx-tensorrt
    mkdir build
    cd build
    # /path/to/TensorRT-8.0.0.3改成自己的TensorRT绝对路径
    cmake .. -DTENSORRT_ROOT=/path/to/TensorRT-8.0.0.3
    make -j8
    sudo make install
    
  3. 测试是否安装成功
    onnx2trt
    
    我的终端返回了
    ONNX to TensorRT model parser
    Usage: onnx2trt onnx_model.pb
                [-o engine_file.trt]  (output TensorRT engine)
                [-t onnx_model.pbtxt] (output ONNX text file without weights)
                [-T onnx_model.pbtxt] (output ONNX text file with weights)
                [-m onnx_model_out.pb] (output ONNX model)
                [-b max_batch_size (default 32)]
                [-w max_workspace_size_bytes (default 1 GiB)]
                [-d model_data_type_bit_depth] (32 => float32, 16 => float16)
                [-O passes] (optimize onnx model. Argument is a semicolon-separated list of passes)
                [-p] (list available optimization passes and exit)
                [-l] (list layers and their shapes)
                [-F] (optimize onnx model in fixed mode)
                [-v] (increase verbosity)
                [-q] (decrease verbosity)
                [-V] (show version information)
                [-h] (show help)
    
    没有提示错误,安装成功

安装onnx_tensorrt(python)

cd onnx_tensorrt
python setup.py build
python setup.py install

错误1解决

我在编译我自己的代码时,发生了错误。

错误提示如下

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
NVINFER
    linked by target "pointpillars" in directory /home/chen/CodeBase/centerpoint_tensorrt/pointpillars
NVONNXPARSERS
    linked by target "pointpillars" in directory /home/chen/CodeBase/centerpoint_tensorrt/pointpillars

能够解决该问题的参考链接https://github.com/onnx/onnx#linux

简答来说是因为我没有成功安装onnx,在执行命令sudo make install安装onnx时,我报了一些错误。具体错误链接在onnx的仓库issue里也有人遇到了,https://github.com/onnx/onnx/issues/3746,以下是我的错误提示:

/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libprotobuf.a(arena.o): relocation R_X86_64_TPOFF32 against hidden symbol `_ZN6google8protobuf8internal9ArenaImpl13thread_cache_E' can not be used when making a shared object
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libprotobuf.a(common.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: 最后的链结失败: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/onnx_cpp2py_export.dir/build.make:101:onnx_cpp2py_export.cpython-38-x86_64-linux-gnu.so] 错误 1
make[1]: *** [CMakeFiles/Makefile2:136:CMakeFiles/onnx_cpp2py_export.dir/all] 错误 2
make: *** [Makefile:136:all] 错误 2
Traceback (most recent call last):
  File "setup.py", line 358, in 
    setuptools.setup(
  File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 144, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.8/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.8/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/usr/lib/python3.8/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/usr/lib/python3.8/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "setup.py", line 233, in run
    self.run_command("cmake_build")
  File "/usr/lib/python3.8/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "setup.py", line 227, in run
    subprocess.check_call(build_args)
  File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/bin/cmake', '--build', '.', '--', '-j', '12']' returned non-zero exit status 2.

简单来说,虽然我们安装了Protobuf,但好像在安装过程中并没有找到对应的protobuf库

解决命令

cd onnx
export CMAKE_ARGS=-DONNX_USE_LITE_PROTO=ON
pip install -e .

错误2解决

执行命令python3 setup.py build报错

ModuleNotFoundError: No module named 'pycuda'

这个很好解决

pip3 install pycuda

你可能感兴趣的:(功能安装,ubuntu,linux,python)