AttributeError: module 'torch.nn.init' has no attribute 'kaiming_normal_'


#1

When I run this cell in lesson1.ipynb on my windows PC,

arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
learn = ConvLearner.pretrained(arch, data, precompute=True)
learn.fit(0.01, 2)

I get this error.

AttributeError Traceback (most recent call last)
in
1 arch=resnet34
2 data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
----> 3 learn = ConvLearner.pretrained(arch, data, precompute=True)
4 learn.fit(0.01, 2)

~\Anaconda3\envs\fastai\lib\site-packages\fastai\conv_learner.py in pretrained(cls, f, data, ps, xtra_fc, xtra_cut, custom_head, precompute, pretrained, **kwargs)
111 pretrained=True, **kwargs):
112 models = ConvnetBuilder(f, data.c, data.is_multi, data.is_reg,
–> 113 ps=ps, xtra_fc=xtra_fc, xtra_cut=xtra_cut, custom_head=custom_head, pretrained=pretrained)
114 return cls(data, models, precompute, **kwargs)
115

~\Anaconda3\envs\fastai\lib\site-packages\fastai\conv_learner.py in init(self, f, c, is_multi, is_reg, ps, xtra_fc, xtra_cut, custom_head, pretrained)
38 else: cut,self.lr_cut = 0,0
39 cut-=xtra_cut
—> 40 layers = cut_model(f(pretrained), cut)
41 self.nf = model_features[f] if f in model_features else (num_features(layers)*2)
42 if not custom_head: layers += [AdaptiveConcatPool2d(), Flatten()]

~\Anaconda3\envs\fastai\lib\site-packages\torchvision\models\resnet.py in resnet34(pretrained, **kwargs)
171 pretrained (bool): If True, returns a model pre-trained on ImageNet
172 “”"
–> 173 model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
174 if pretrained:
175 model.load_state_dict(model_zoo.load_url(model_urls[‘resnet34’]))

~\Anaconda3\envs\fastai\lib\site-packages\torchvision\models\resnet.py in init(self, block, layers, num_classes)
113 for m in self.modules():
114 if isinstance(m, nn.Conv2d):
–> 115 nn.init.kaiming_normal_(m.weight, mode=‘fan_out’, nonlinearity=‘relu’)
116 elif isinstance(m, nn.BatchNorm2d):
117 nn.init.constant_(m.weight, 1)

AttributeError: module ‘torch.nn.init’ has no attribute ‘kaiming_normal_’

What is the cause?


#2

Are you running python 2 or 3?


#3

Yes, I run this in python3.


(Adedoja Adedamola) #4

HI, Please do you have a fix yet?

I am having the same error coming up on my PC.


#5

I believe this error is caused by the wrong version of torch. Try upgrading pytorch >=.4 ?

see here: https://github.com/Randl/MobileNetV2-pytorch/issues/4

and here: https://github.com/hezhangsprinter/DCPDN/issues/8


#6

I also had this problem and was able to resolve it by running the following commands in the fastai directory.

conda install -c anaconda mkl
conda install -c pytorch pytorch torchvision


#7

It works well.
Thank you very match!


#8

I solved this by this reply.


(Adedoja Adedamola) #9

Thanks. I just updated my pytorch to >=0.4 and it worked.


(Adedoja Adedamola) #10

Thanks


(fareeha saeed) #12

for me
conda install -c pytorch pytorch torchvision
wasnt working
gave me the following error

PackagesNotFoundError: The following packages are not available from current channels:

  • torchvision

so instead this series if commands worked for me
conda install -c anaconda mkl
conda install -c pytorch pytorch-cpu


#13

I’d be cautious of updating to pytorch v 0.4, since it appears that the fastai library uses pytorch < 0.4. Updating to 0.4 caused other errors for me. Instead, try pip install torchvision==0.1.9. It appears that the fastai library uses pytorch < 0.4, but torchvision >= 1.9, which may be causing conflicts. I’m going to try and bring this up in github in the fastai library.