- 这个问题出现在torch2rt与tensorRT之间的调用
- 类似报错还有:
'Tensor' object has no attribute '_trt'
,这个我在debug的时候也遇到了 - 我的环境:
- 硬件:Jetson Nano
- 系统:Ubuntu 18.04 aarch
- 环境:pytorch1.4-aarch ,Jetpack4.2(Tensorrt5.0.6),torch2trt-github编译版
- 我的Traceback:
Traceback (most recent call last):
File "/home/jetsonnano/Desktop/Yolov3/4_torch2trt_detect_custom.py", line 149, in <module>
model_trt = torch2trt(model_backbone, [x], fp16_mode=True)
File "/usr/local/lib/python3.6/dist-packages/torch2trt/torch2trt.py", line 377, in torch2trt
outputs = module(*inputs)
File "/home/jetsonnano/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "/home/jetsonnano/Desktop/Yolov3/models_for_trt.py", line 377, in forward
x = module(x)
File "/home/jetsonnano/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "/home/jetsonnano/.local/lib/python3.6/site-packages/torch/nn/modules/container.py", line 100, in forward
input = module(input)
File "/home/jetsonnano/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "/home/jetsonnano/.local/lib/python3.6/site-packages/torch/nn/modules/activation.py", line 559, in forward
return F.leaky_relu(input, self.negative_slope, self.inplace)
File "/usr/local/lib/python3.6/dist-packages/torch2trt/torch2trt.py", line 202, in wrapper
converter['converter'](ctx)
File "/usr/local/lib/python3.6/dist-packages/torch2trt/converters/activation.py", line 22, in convert_leaky_relu
layer = ctx.network.add_activation(input_trt, trt.ActivationType.LEAKY_RELU)
AttributeError: type object 'tensorrt.tensorrt.ActivationType' has no attribute 'LEAKY_RELU'
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/jetsonnano/.vscode-server-insiders/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/no_wheels/ptvsd/__main__.py", line 45, in <module>
cli.main()
File "/home/jetsonnano/.vscode-server-insiders/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/no_wheels/ptvsd/../ptvsd/server/cli.py", line 361, in main
run()
File "/home/jetsonnano/.vscode-server-insiders/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/no_wheels/ptvsd/../ptvsd/server/cli.py", line 203, in run_file
runpy.run_path(options.target, run_name="__main__")
File "/usr/lib/python3.6/runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "/usr/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/jetsonnano/Desktop/Yolov3/4_torch2trt_detect_custom.py", line 149, in <module>
model_trt = torch2trt(model_backbone, [x], fp16_mode=True)
File "/usr/local/lib/python3.6/dist-packages/torch2trt/torch2trt.py", line 381, in torch2trt
ctx.mark_outputs(outputs, output_names)
File "/usr/local/lib/python3.6/dist-packages/torch2trt/torch2trt.py", line 282, in mark_outputs
trt_tensor = torch_output._trt
AttributeError: 'Tensor' object has no attribute '_trt'
解决思路
- 我首先去torch2trt的github上查了查,发现两个类似的issues,如下图所示:
- 他们的链接是:
- https://github.com/NVIDIA-AI-IOT/torch2trt/issues/226
- https://github.com/NVIDIA-AI-IOT/torch2trt/issues/15
- 俩好像都没有解决,但是多少给了我们一些思路
- 首先,我们也可以肯定的是,torch2trt这个库是支持Leaky_relu,这个函数,我进源码看了一眼,是调用的tensorrt.tensorrt.ActivationType.LEAKY_RELU,也就是说,当前版本不支持leaky_relu的时候,就会有这个错误
- 可以用这个查查torch2trt都支持什么CONVERTERS
import torch2trt
print(torch2trt.CONVERTERS)
-
输出是个Json,格式化后可以看到当前torch2trt支持leaky_relu
-
那么也就是当前tensorRT不支持,最直接的验证方法是,直接打出来,看看有没有这个函数
-
我现在是tensorRT6.0版本是有了,之前5.0是没有的,而当版本更新到6.0的时候,原来的错误自然而然消失了
-
还有一种更加直观的方法——去文档查看,以下是文档地址
-
这个是5.0.2.1的API文档,通过搜索ActivationTpye可以清楚看到它支持的函数只有三个
-
对比6.0版本的文档,可以发现6.0新增了许多激活函数,期中就包括了LEAKY_RELU
-
顺便查了查7版本的文档,发现激活函数部分没有更新,那暂时用6也就够了
解决办法
-
因此我的解决思路是,将tensorRT升级到6.0 版本
-
于是,第一反应就是去官网下载,于是瞅到了这个
-
我发现,想要给我的Jetson Nano 更新TensorRT版本,就必须直接更新整体的Jetpack,目前最新Jetpack4.3是包含tensorRT6.0
-
而改变Jetpack版本,根据官网说法,有两种方法
- 第一种是直接重烧4.3的系统:就是下载最新镜像,然后重新装一遍系统
- 第二种是用NVIDIA 的SDK Manager刷Jetpack版本:推荐这位博主刷机教程,虽然我没成功
- 第三种,我是没有尝试过的,思路是直接从github上源码编译,有可能可以,因为我在那篇文档里面瞟到tensorrt7.0似乎支持Jetson Nano这种aarch的系统架构
-
另外,附上Jetpack 4.3的镜像(如果选择自己下,迅雷可能会快一些):
链接:https://pan.baidu.com/s/1qiXhP-6xFLyRwBIIWa0zCg
提取码:6brj
复制这段内容后打开百度网盘手机App,操作更方便哦
椰子奶糖
发布了78 篇原创文章 · 获赞 23 · 访问量 9万+
私信
关注