question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Finetune on multi-GPUs

See original GitHub issue

I want to finetune the models on multi-GPUs, as following the office documents, I try this: model = make_model( args.model_name, pretrained=True, num_classes=len(classes), dropout_p=args.dropout_p, use_original_classifier=True ) model = nn.DataParallel(model) model = model.to(device)

But get error as: Traceback (most recent call last): File "pg_cls.py", line 83, in <module> mean=model.original_model_info.mean, File "/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __getattr__ type(self).__name__, name)) AttributeError: 'DataParallel' object has no attribute 'original_model_info'

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
creafzcommented, Jul 19, 2018

For now, you can use standard PyTorch methods to freeze some layers:

Create a new model:

model = make_model('resnet18', num_classes=10, pretrained=True)

To freeze all feature extracting layers and finetune only the classifer:

for param in model._features.parameters():
    param.requires_grad = False

To freeze only some layers of the feature extractor:

# The number of feature extracting layers that should be finetuned. 
# Here we want to finetune the 2 last layers of the feature extractor and the classifying layers
NUM_LAYERS_TO_UNFREEZE = 2

# Just to be sure, print those layers that will be frozen...
print(list(model._features.children())[:-NUM_LAYERS_TO_UNFREEZE])

# ... and those layers that we want to finetune
print(list(model._features.children())[-NUM_LAYERS_TO_UNFREEZE:])

# Finally freeze layers
for module in list(model._features.children())[:-NUM_LAYERS_TO_UNFREEZE]:
    for param in module.parameters():
        param.requires_grad = False

# Print parameters that will be updated during training.
print([name for (name, param) in model.named_parameters() if param.requires_grad])

You will also need to pass only parameters from ‘unfrozen’ layers to an optimizer or you will get an exception

optimizer = optim.SGD((param for param in model.parameters() if param.requires_grad), lr=..., momentum=...)

I will think about how this functionality could be added to the library.

0reactions
Ken2yLiucommented, Sep 30, 2018

@creafz sorry for replying late, your answers help a lot, then I would close this issue now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wav2vec fine-tuning with multiGPU - Hugging Face Forums
I'm fine-tuning wav2vec model with Fine-Tune XLSR-Wav2Vec2 for low-resource ASR with Transformers at local machine with 4xT4 GPU (16Gb)
Read more >
How to Fine-Tune YOLOv5 on Multiple GPUs
Simple screen finetuning. The simplest way to search for hyper-parameters is to run the training with an enabled evolution --evolve <number> argument. But ......
Read more >
How to parallel in GPU when finetuning - PyTorch Forums
In my Finetune models , I wanna parallel my model, in multi-gpus, my code is shown below: class FinetuneModel(nn.Module): def __init__(self, ...
Read more >
Multi-gpus — bio-transformers v0.1.14
Note that ray parallelization is only used for inference function. finetune method uses pytorch-lightning with its built-in function to train a model.
Read more >
Niels Rogge on Twitter: "(m)LUKE by @StudioOusia is a ...
Accelerate, a great solution for folks that want to write their own training loop but have it run instantly on (multi) GPUs or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found