执行YOLOv5时报错,解决:AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

查了很多,说是pytorch和torchvision的问题,但是实在不想卸载了。

版本信息:Jetson NX、Jetpack5.0.2、Python3.8、Pytorch1.12.0、TorchVision0.13.0、CUDA11.4、Cudnn8.4.1、Tensorrt8.4.1。

执行yolov5时报错如下:

Traceback (most recent call last):
  File "detect.py", line 307, in 
    main(opt)
  File "detect.py", line 302, in main
    run(**vars(opt))
  File "/home/efy/mambaforge/envs/py38/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "detect.py", line 133, in run
    model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.parameters())))  # run once
  File "/home/efy/mambaforge/envs/py38/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1111, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/efy/tensorrtx_yolov5/yolov5-6.0/models/yolo.py", line 126, in forward
    return self._forward_once(x, profile, visualize)  # single-scale inference, train
  File "/home/efy/tensorrtx_yolov5/yolov5-6.0/models/yolo.py", line 149, in _forward_once
    x = m(x)  # run
  File "/home/efy/mambaforge/envs/py38/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1111, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/efy/mambaforge/envs/py38/lib/python3.8/site-packages/torch/nn/modules/upsampling.py", line 154, in forward
    recompute_scale_factor=self.recompute_scale_factor)
  File "/home/efy/mambaforge/envs/py38/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1186, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'

解决方式:修改Pytorch源码,我的目录在/home/efy/mambaforge/envs/py38/lib/python3.8/site-packages/torch/nn/modules/upsampling.py,大概是153行的位置。

    def forward(self, input: Tensor) -> Tensor:
        # return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners,
        #                      recompute_scale_factor=self.recompute_scale_factor)
        return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners)

注释掉原来的,添加新的。

我也不知道为啥,不过确实解决了问题。

你可能感兴趣的:(深度学习,python,人工智能)