WongKinYiu / yolov7

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
GNU General Public License v3.0
13.15k stars 4.16k forks source link

AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor' #647

Open pritam-dey3 opened 1 year ago

pritam-dey3 commented 1 year ago

Hi, I tried to clone mask branch and reproduce the results in tools/instance.ipynb. And got the following error

Traceback (most recent call last):
  File "C:\Users\Pritam Dey\AppData\Local\Temp\ipykernel_17988\339931518.py", line 3, in <cell line: 2>
    output = model(image)
  File "d:\softwares\anaconda\envs\visionduo\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "e:\programms\python\bundesliga_data_shootout\VisionDuo\yolov7\models\yolo.py", line 696, in forward
    return self.forward_once(x, profile)  # single-scale inference, train
  File "e:\programms\python\bundesliga_data_shootout\VisionDuo\yolov7\models\yolo.py", line 722, in forward_once
    x = m(x)  # run
  File "d:\softwares\anaconda\envs\visionduo\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "d:\softwares\anaconda\envs\visionduo\lib\site-packages\torch\nn\modules\upsampling.py", line 154, in forward
    recompute_scale_factor=self.recompute_scale_factor)
  File "d:\softwares\anaconda\envs\visionduo\lib\site-packages\torch\nn\modules\module.py", line 1207, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'

torch version: 1.12.1 cuda version: 11.7

This issue could be duplicate of #520

pritam-dey3 commented 1 year ago

Following this reply, I added two lines in yolo.py

if isinstance(m, nn.Upsample):
    m.recompute_scale_factor = False

And it solved the issue.

hamzanaeem1999 commented 1 year ago

Following this reply, I added two lines in yolo.py

if isinstance(m, nn.Upsample):
    m.recompute_scale_factor = False

And it solved the issue. @pritam-dey3 You should tell that where to put these lines.

afzixhaikh commented 1 year ago

I downgraded my pytorch version to 1.11.0 but still faced this error, I downgraded pytorch to 1.10.1 and it solved the error. I used this command specifically: "conda install pytorch==1.10.1 torchvision==0.11.2 torchaudio==0.10.1 cudatoolkit=11.3 -c pytorch -c conda-forge"

getarobo commented 1 year ago

@hamzanaeem1999

I install pytorch under follow command

# CUDA 11.3
pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113

this torch version had a bug(?) so had to fix it

go to your anaconda env and fix torch/nn/modules/upsampling.py e.g. /home/gene/anaconda3/envs/yolov7/lib/python3.8/site-packages/torch/nn/modules/upsampling.py

line 152~154 shoud like this now

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

p.s. got this fix from TheCodingBug

ohjho commented 1 year ago

@hamzanaeem1999 I'd put

if isinstance(m, nn.Upsample):
                m.recompute_scale_factor = False

on line 722 in models/yolo.py before the x = m(x) statement

that way you can keep the latest torch version (i'm using 1.13.1) and run things from the mask branch