EfficientNet-PyTorch icon indicating copy to clipboard operation
EfficientNet-PyTorch copied to clipboard

transfer learning using efficientnet NotImplementedError

Open victorzhucompass opened this issue 5 years ago • 9 comments

in fastai, if we convert loaded model to sequential:

mode = EfficientNet.from_pretrained('efficientnet-b0') model1 = nn.Sequential(*list(chiuldren(model)))

not able to train using learn.fit_one_cycle.

reported error is NotImplementedError

victorzhucompass avatar Feb 10 '20 22:02 victorzhucompass

Is the NotImplemened error for the Forward pass? IMO, This is because the module is has nested modulelist and forward is not simply chaining the outputs

check the pytorch discussion here https://discuss.pytorch.org/t/forward-not-implemented-error/51162/4

To use a certain part of the model you would do something like below if you want all layers except last 5 or so. I havent tried myself but you would have to create another model and define the forward for that.

backbone_model = EfficientNet.from_name('efficientnet-b5')
backbone_layers = torch.nn.ModuleList(backbone_model.children())[:-5]
self.features = torch.nn.Sequential(*backbone_layers)

If you simply want to remove the last FCs, you can do like below (also in the readme)

model = EfficientNet.from_pretrained('efficientnet-b0')
features = model.extract_features(img)

bsridatta avatar Mar 09 '20 19:03 bsridatta

I encountered the same problem in transfer learning. Despite I adopted these three lines code, I didn't solve it.Have you solved it?

backbone_model = EfficientNet.from_name('efficientneyot-b5')
backbone_layers = torch.nn.ModuleList(backbone_model.children())[:-5]
self.features = torch.nn.Sequential(*backbone_layers)

Yapeng-Wang avatar May 29 '20 09:05 Yapeng-Wang

The model name is wrong, corrected it now. What is the error that you get?

bsridatta avatar May 29 '20 10:05 bsridatta

The model name is wrong, corrected it now. What is the error that you get?

I wanted to use the pre-training model for transfer learning, so I intercepted the network before the classifier from the B0 model and added some new layers. However, I met a mistake in the training, I got the same hint as at the beginning, namely"NotImplementedError"

Yapeng-Wang avatar Jun 01 '20 13:06 Yapeng-Wang

hi I am getting TypeError: forward() takes 1 positional argument but 2 were given How can I solve this problem

Efficient_b0 = EfficientNet.from_pretrained('efficientnet-b0') Efficient_b0 = nn.Sequential(*(torch.nn.ModuleList(Efficient_b0.children())[:-4]))

x = torch.randn(4,3,224,224) print(Efficient_b0(x).shape)

musimab avatar Jan 03 '21 13:01 musimab

Did you figure this out @musimab

rover-debug avatar Jan 25 '21 06:01 rover-debug

Same problem. The model is not getting wrapped around the Sequential properly for some reason. Is there any other workaround this?

saransh09 avatar Jan 29 '21 07:01 saransh09

hi I am getting TypeError: forward() takes 1 positional argument but 2 were given How can I solve this problem

Efficient_b0 = EfficientNet.from_pretrained('efficientnet-b0') Efficient_b0 = nn.Sequential(*(torch.nn.ModuleList(Efficient_b0.children())[:-4]))

x = torch.randn(4,3,224,224) print(Efficient_b0(x).shape)

I got the same problem. If I move the truncated operation, the model works.

MrDongdongLin avatar May 03 '21 09:05 MrDongdongLin

Have you solved the issue @Yapeng-Wang ?

GKalliatakis avatar Mar 15 '22 16:03 GKalliatakis