Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

python 3.x - Cannot import "BasicBlock" from torchvision.models.resnet

I am trying to import the class BasicBlock from torchvision.models.resnet by doing this

from torchvision.models.resnet import *

It is giving no error but when I am trying to use BasicBlock class in my code (which is supposed to be imported already), I am getting the error that

NameError: name 'BasicBlock' is not defined

even though BasicBlock is present in torchvision.models.resnet

But it gives no error when I import like this

from torchvision.models.resnet import BasicBlock

and then use it in my code

Why am I getting this error?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

BasicBlock is indeed defined, however it is not exported by the module: see here the definition of __all__. So torchvision/models/resnet.py only exports these: ResNet, resnet18, resnet34, resnet50, resnet101, resnet152, resnext50_32x4d, resnext101_32x8d, wide_resnet50_2, and wide_resnet101_2.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...