question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in package models: `TypeError: __init__() got an unexpected keyword argument 'pretrained'`

See original GitHub issue

🐛 Describe the bug

Bug

I was going through the vision source code and I found a bug in function resnet_fpn_backbone in the module torchvision/models/detection/backbone_utils.py.

According to the docstring of resnet_fpn_backbone, one of the possible values for the parameter backbone_name is 'ResNet'.

However, passing 'ResNet' to the function results in the following run time error:

============================= test session starts ==============================
collecting ... collected 1 item

test_found_n1.py::test_resnet_fpn_backbone FAILED                        [100%]
test_mohrez/test_found_n1.py:4 (test_resnet_fpn_backbone)
Traceback (most recent call last):
  File "/home/moe/vision/test_mohrez/test_found_n1.py", line 7, in test_resnet_fpn_backbone
    y = resnet_fpn_backbone(backbone_name='ResNet', pretrained=False)(x)
  File "/home/moe/vision/torchvision/models/detection/backbone_utils.py", line 103, in resnet_fpn_backbone
    backbone = resnet.__dict__[backbone_name](
TypeError: __init__() got an unexpected keyword argument 'pretrained'

======================== 1 failed, 2 warnings in 1.03s =========================

Process finished with exit code 1

To Reproduce

Running the following test reproduces the bug.

import torch
from torchvision.models.detection.backbone_utils import resnet_fpn_backbone

def test_resnet_fpn_backbone():
    x = torch.rand(1, 3, 300, 300, dtype=torch.float32, device='cpu')
    y = resnet_fpn_backbone(backbone_name='ResNet', pretrained=False)(x)

Reason

I think the reason behind this bug is that the constructor of the class ResNet in module torchvision/models/resnet.py does not have a parameter named pretrained. Therefore, function resnet_fpn_backbone should check the value of the parameter backbone_name, before reaching line 92. If it is ‘ResNet’, it should act differently.

Versions

Collecting environment information…

PyTorch version: 1.10.1 Is debug build: False CUDA used to build PyTorch: 11.3 ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64) GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 Clang version: Could not collect CMake version: Could not collect Libc version: glibc-2.31

Python version: 3.8.12 (default, Oct 12 2021, 13:49:34) [GCC 7.5.0] (64-bit runtime) Python platform: Linux-5.11.0-46-generic-x86_64-with-glibc2.17 Is CUDA available: False CUDA runtime version: No CUDA GPU models and configuration: No CUDA Nvidia driver version: No CUDA cuDNN version: No CUDA HIP runtime version: N/A MIOpen runtime version: N/A

Versions of relevant libraries: [pip3] mypy==0.931 [pip3] mypy-extensions==0.4.3 [pip3] numpy==1.21.2 [pip3] torch==1.10.1 [pip3] torchaudio==0.10.1 [pip3] torchvision==0.11.0a0+e7ec7e2 [conda] blas 1.0 mkl
[conda] cudatoolkit 11.3.1 h2bc3f7f_2
[conda] mkl 2021.4.0 h06a4308_640
[conda] mkl-service 2.4.0 py38h7f8727e_0
[conda] mkl_fft 1.3.1 py38hd3c417c_0
[conda] mkl_random 1.2.2 py38h51133e4_0
[conda] mypy 0.931 pypi_0 pypi [conda] mypy-extensions 0.4.3 pypi_0 pypi [conda] numpy 1.21.2 py38h20f2e39_0
[conda] numpy-base 1.21.2 py38h79a1101_0
[conda] pytorch 1.10.1 py3.8_cuda11.3_cudnn8.2.0_0 pytorch [conda] pytorch-mutex 1.0 cuda pytorch [conda] torchaudio 0.10.1 py38_cu113 pytorch [conda] torchvision 0.11.0a0+e7ec7e2 dev_0 <develop>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
datumboxcommented, Jan 20, 2022

@mohrez86 @abhi-glitchhg Thanks to both of you for offering your help!

To avoid having duplicate PRs for the same issue, shall I suggest that Mohammad fixes this issue since he reported it and Abhijit to help us with the assertion improvements that @oke-aditya proposed (Oke could you create a ticket)?

3reactions
datumboxcommented, Jan 19, 2022

@mohrez86 Thanks for reporting.

The problem is on the documentation. The ResNet should not have been included in the list. The intention is for model builders to be provided, all of which support the pretrained parameter. Could you send a PR to fix it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

__init__() got an unexpected keyword argument 'pretrained ...
Describe the bug init() got an unexpected keyword argument 'pretrained' Environment Ubuntu 18.04 CUDA 10.1 mmdet 2.8.0 mmcv-full 1.2.7 ...
Read more >
TypeError: __init__() got an unexpected keyword argument ...
I have been trying to train the model using one of my own model as a pretrained model and every time I run,...
Read more >
typeerror: __init__() got an unexpected keyword argument ...
Assuming this form: from django import forms from .models import MyModel class MyForm (forms.ModelForm): class Meta: model = MyModel def __init__ (self, user_id ......
Read more >
trax-ml/community - Gitter
@lukaszkaiser I installed the package but I cannot import it. I get this error: TypeError: configurable() got an unexpected keyword argument 'denylist'.
Read more >
MASKRCNN, Error while loading the model - vision
/usr/local/lib/python3.6/site-packages/torchvision/models/detection/mask_rcnn.py in ... TypeError: init() got an unexpected keyword argument ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found