Matthias
Matthias

Reputation: 10369

How to use the Inception model for transfer learning in PyTorch?

I have created a PyTorch torchvision model for transfer learning, using the pre-built ResNet50 base model, like this:

        # Create base model from torchvision.models
        model = resnet50(pretrained=True)
        num_features = model.fc.in_features

        # Define the network head and attach it to the model
        model_head = nn.Sequential(
            nn.Linear(num_features, 512),
            nn.ReLU(),
            nn.Dropout(0.25),
            nn.Linear(512, 256),
            nn.ReLU(),
            nn.Dropout(0.5),
            nn.Linear(256, num_classes),
        )
        model.fc = model_head

Now I wanted to use the Ineception v3 model instead as base, so I switched from resnet50() above to inception_v3(), the rest stayed as is. However, during training I get the following error:

TypeError: cross_entropy_loss(): argument 'input' (position 1) must be Tensor, not InceptionOutputs

So how can one use the Inception v3 model from torchvision.models as base model for transfer learning?

Upvotes: 2

Views: 4620

Answers (1)

caumente
caumente

Reputation: 58

From PyTorch documentation about Inceptionv3 architecture:

This network is unique because it has two output layers when training. The primary output is a linear layer at the end of the network. The second output is known as an auxiliary output and is contained in the AuxLogits part of the network.

Have a look at this tutorial: https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html#inception-v3 There you can find how to use transfer learning for several models, included ResNet and Inception.

Upvotes: 1

Related Questions