-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Add finetuning #58
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
Add finetuning #58
Conversation
imagenet/main.py
Outdated
def forward(self, x): | ||
f = self.features(x) | ||
if self.modelName == 'alexnet' : | ||
f = f.view(f.size(0), 256 * 6 * 6) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
imagenet/main.py
Outdated
@@ -48,18 +49,66 @@ | |||
help='evaluate model on validation set') | |||
parser.add_argument('--pretrained', dest='pretrained', action='store_true', | |||
help='use pre-trained model') | |||
parser.add_argument('--finetune', dest='finetune', action='store_true', |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
imagenet/main.py
Outdated
|
||
if arch.startswith('alexnet') : | ||
self.features = original_model.features | ||
self.classifier = nn.Sequential( |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
@sanealytics because vgg has pretrained weights now, can you add fine-tune support for vgg? |
Very helpful script. Although I noticed one problem for resnets. It works fine for resnet18 and 34 because their last conv layers are 512 in depth. However, for resnet50, 101, and 152 this should be increased to 2048. The offending line is: |
…ort for more models
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my other username...
Requesting @fmassa to review changes. |
@sanealytics It would be good to have -predict option to make predictions on test datasets. Currently, I see an option for evaluating using validation dataset. Option to predict on test dataset and right output back to CSV file containing test images and probability for each class would be great functionality. |
When finetuning with ResNet 50, it seems that
should be
|
When finetuning with inception_v3 model, there is an error:
|
# Everything except the last linear layer | ||
self.features = nn.Sequential(*list(original_model.children())[:-1]) | ||
self.classifier = nn.Sequential( | ||
nn.Linear(512, num_classes) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
@saurabhRTR May you test the inception_v3 model finetune? I still can't fintune the inception_v3 model. |
@panovr will do, expect an update this week |
@sanealytics Thanks! May you also add finetune for densenet? |
Here is my fine-tuning code for DenseNet-121. It must be parameterized for all DenseNets. Also it only gave me class DenseNet121(nn.Module):
def __init__(self, num_classes):
super(DenseNet121, self).__init__()
original_model = models.densenet121(pretrained=True)
# Everything except the last linear layer
self.features = nn.Sequential(*list(original_model.children())[:-1])
# Get number of features of last layer
num_feats = original_model.classifier.in_features
# Plug our classifier
self.classifier = nn.Sequential(
nn.Linear(num_feats, num_classes)
)
# Init of last layer
for m in self.classifier:
kaiming_normal(m.weight)
# Freeze weights
# for p in self.features.parameters():
# p.requires_grad = False
def forward(self, x):
f = self.features(x)
out = F.relu(f, inplace=True)
out = F.avg_pool2d(out, kernel_size=7).view(f.size(0), -1)
out = self.classifier(out)
return out |
@mratsim Thanks for the code, and I will test it on my custom dataset tomorrow. |
@mratsim Thanks for the code. I have tested it, and that was so good. However, I can't finetune 'SqueezeNet 1.0' model. would you please help me. In fact, my code for fine tuning the squeezenet1_0, is as follows:
|
@mratsim |
@fmassa , thanks for your reference. I have one question. how to fullfil finetune in imagenet/main.py ? |
Just say |
@saurabhRTR , thank you. |
May you add code for fine-tuning squeezenet1_0 and squeezenet1_1? |
The finetune option is used to freeze features layers on a pretrained network refactored and updated code from pytorch#58
Follow updates on #446 |
... INCORPORATING SUGGESTIONS ...
Finetune for alexnet and resnet. It does not work for vgg yet (no pretrained weights).
This will pick up the number of classes from the training directory and finetune the classification layer.
This does 'hard' finetune by freezing feature layers. Another option is 'soft' finetune where small changes to higher layers are allowed. This PR only does the former.
Used hints from https://discuss.pytorch.org/t/how-to-extract-features-of-an-image-from-a-trained-model/119/3 and @apaszke