Jetson Nano【13】关于torch2trt报错:AttributeError: 'Tensor' object has no attribute '_trt'的一种可能性

  • 尝试转换,报错,日志如下
AttributeError                            Traceback (most recent call last)
<ipython-input-24-0cf9bfd4fe52> in <module>
     20     img = torch.zeros((1, 3) + img_size).cuda().half()
     21 #     x = torch.rand(size=(1, 3,  opt.img_size, opt.img_size)).cuda().half()
---> 22     model_trt = torch2trt(model, [img], fp16_mode=True)
     23 
     24 

/usr/local/lib/python3.6/dist-packages/torch2trt/torch2trt.py in torch2trt(module, inputs, input_names, output_names, log_level, max_batch_size, fp16_mode, max_workspace_size, strict_type_constraints, keep_network, int8_mode, int8_calib_dataset, int8_calib_algorithm)
    375         ctx.add_inputs(inputs, input_names)
    376 
--> 377         outputs = module(*inputs)
    378 
    379         if not isinstance(outputs, tuple) and not isinstance(outputs, list):

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~/Desktop/HelmetWearingDetection20200303/models.py in forward(self, x, var)
    319 
    320         ptr = 0
--> 321         for i, (module_def, module) in enumerate(zip(self.module_defs, self.module_list)):
    322             if i == cutoff:
    323                 break

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~/Desktop/HelmetWearingDetection20200303/models.py in forward(self, p, img_size, var)
    242             # s = 1.5  # scale_xy  (pxy = pxy * s - (s - 1) / 2)
    243             io = p.clone()  # inference output,测试过程输出就是io
--> 244             io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid_xy  # xy
    245             # grid_xy是左上角再加上偏移量io[...:2]代表xy偏移
    246             io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh  # wh yolo method

/usr/local/lib/python3.6/dist-packages/torch2trt/torch2trt.py in wrapper(*args, **kwargs)
    200 
    201 #             print('%s' % (converter.__name__,))
--> 202             converter['converter'](ctx)
    203 
    204             # convert to None so conversion will fail for unsupported layers

/usr/local/lib/python3.6/dist-packages/torch2trt/converters/getitem.py in convert_tensor_getitem(ctx)
     28     output = ctx.method_return
     29 
---> 30     input_trt = input._trt
     31 
     32     # Step 1 - Replace ellipsis with expanded slices

AttributeError: 'Tensor' object has no attribute '_trt'

初步判定:

  • tensorRT不支持YOLO层,或者说是torch2trt没实现,那么YOLO层的实现就只能手动实现,也就是吧YOLO从网络结构中摘出来,自定义,不参与模型转换。

测试(占坑)



  • 看了下源码,还在整理中

结论(占坑)

  • 结论是torch2trt不支持YOLO层,我对比过两个项目的源码(一个实现torch2trt一个报错),他们在Darknet上读取的区别是:YOLO及相关层是否马上读取,实现了的项目是不读取的,因为torch2trt(或者说tensorrt)不支持YOLO层,因此需要自己另外定义,就是手动吧YOLO层需要的数据detach出来~
发布了120 篇原创文章 · 获赞 153 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/symuamua/article/details/105184089