lukemelas / EfficientNet-PyTorch

A PyTorch implementation of EfficientNet
Apache License 2.0
7.95k stars 1.53k forks source link

transfer learning using efficientnet NotImplementedError #142

Open victorzhucompass opened 4 years ago

victorzhucompass commented 4 years ago

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

bsridatta commented 4 years ago

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)
Yapeng-Wang commented 4 years ago

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)
bsridatta commented 4 years ago

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

Yapeng-Wang commented 4 years ago

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"

musimab commented 3 years ago

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)

rover-debug commented 3 years ago

Did you figure this out @musimab

saransh09 commented 3 years ago

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

MrDongdongLin commented 3 years ago

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.

GKalliatakis commented 2 years ago

Have you solved the issue @Yapeng-Wang ?