AttributeError Traceback (most recent call last)
<ipython-input-24-0cf9bfd4fe52> in <module>
20 img = torch.zeros((1, 3) + img_size).cuda().half()
21
---> 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
243 io = p.clone()
--> 244 io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid_xy
245
246 io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh
/usr/local/lib/python3.6/dist-packages/torch2trt/torch2trt.py in wrapper(*args, **kwargs)
200
201
--> 202 converter['converter'](ctx)
203
204
/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
AttributeError: 'Tensor' object has no attribute '_trt'
初步判定:
- tensorRT不支持YOLO层,或者说是torch2trt没实现,那么YOLO层的实现就只能手动实现,也就是吧YOLO从网络结构中摘出来,自定义,不参与模型转换。
测试(占坑)
结论(占坑)
- 结论是torch2trt不支持YOLO层,我对比过两个项目的源码(一个实现torch2trt一个报错),他们在Darknet上读取的区别是:YOLO及相关层是否马上读取,实现了的项目是不读取的,因为torch2trt(或者说tensorrt)不支持YOLO层,因此需要自己另外定义,就是手动吧YOLO层需要的数据detach出来~