-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support more flexible ways to instantiate models in torchvision.models
, e.g., remove fc layers, support for pretrained=True
and num_classes!=1000
#2200
Comments
Hi, Thanks for opening the issue to start the discussion! I have a few worries about the current approach:
For those reasons, I inclined to say that we shouldn't be providing those high-level model manipulation blocks (which are limited in scope), but instead encourage users to modify the models as their framework / application need. Thoughts? |
Yes, I do understand your worries. But in my opinions:
There are already some discussions for this: |
torchvision.models
, torchvision.models
, e.g., remove fc layers, support for pretrained=True
and num_classes!=1000
Another issue that is related: #2152 I think that adding those extra arguments like One could arguably say that they would want to control the number of fc layers (maybe adding more than the initial one?). I think that given the generality of the task (model surgery), I would prefer to keep the implementation simple as it facilitates the users modifying the code themselves. This is because PyTorch model definition couples the layers (in the |
@fmassa OK. I will fork and do modifications in my own repository. |
@ljqcava the way I would recommend doing it is to instead of forking torchvision (and having to install your forked version) to either: Subclass the model that you want to extend (preferred)Here is an example: class MyResNet(torchvision.models.resnet.ResNet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
del self.fc
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
return x Copy-paste the whole
|
🚀 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:
load pretrained weights when class number doesn't equals to 1000(imagenet):
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 whenpretrained=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, likeinclude_top=False
in Keras.Pitch
A possible solution is to modify some codes in models constructions. At least two more features should be realized:
num_classes!=1000
, weights can still be loaded except for last FC layer.include_top
) is set toFalse
, the last layer(FC layer) will be moved.We can apply these modifications to many basic models in
torchvision.models
.The text was updated successfully, but these errors were encountered: