Skip to content

Support more flexible ways to instantiate models in torchvision.models, e.g., remove fc layers, support for pretrained=True and num_classes!=1000 #2200

Closed
@lijiaqi

Description

@lijiaqi

🚀 Feature

Support more flexible ways to instantiate models in torchvision.models,

e.g., instantiate ResNet-50 only for feature extraction without FC layer like Keras style:

torchvision.models.resnet50(include_top=False)

load pretrained weights when class number doesn't equals to 1000(imagenet):

torchvision.models.resnet50(pretrained=True, num_classes=10)

Motivation

In many situations, we need more flexiable ways to instantiate models in torchvision.models.

For examples, when finetuning a ResNet-50 classification model on a dataset of 10 classes, we need torchvision.models.resnet50(pretrained=True, num_classes=10), but it is not supported now. In current implementation, num_classes should be 1000 when pretrained=True. To implement this case, we should support partial copy from pretrained weights(load weights except for FC layer).

Another example: sometimes we need instantiate the models in torchvision.models as backbone, which means the FC layer is no more needed. Implementation in this case is also needed. We can support this case with an additional argument, like include_top=False in Keras.

Pitch

A possible solution is to modify some codes in models constructions. At least two more features should be realized:

  1. Support for loading partial weights: when num_classes!=1000, weights can still be loaded except for last FC layer.
  2. Support for backbone mode(FC layer moved): when an argument (like include_top) is set to False, the last layer(FC layer) will be moved.

We can apply these modifications to many basic models in torchvision.models.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions