I hope you are well I have a question can I train transfer learning like pre-trained model 'vgg16' trained on ImageNet in my customer data and save weight to train another customer data? How I can do this, please Thanks for ur time
Asked
Active
Viewed 36 times
-1
-
Of cause you can do that. Depend on your framework you use. Now, almost frameworks support load pre-trained weight by its parameter – AnhPC03 Jul 24 '22 at 13:35
-
thanks for ur time, can u give me example code? – Asma Hekal Jul 24 '22 at 13:39
1 Answers
0
I do example with Pytorch framework.
When you train any model, you have to define training strategy.
There are two ways to save and load trained weight for transfer learning in Pytorch.
The first is
load state dict
- only save and load the weight (paramemters) (recommended).While training, if condition you defined satisfied, let's save the trained weight by this command (refer link)
torch.save(model.state_dict(), PATH)
And then when you train VGG16 model with other custom dataset and want to transfer learning from previous trained weight, use this
model = VGG16(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
model.eval()
The second way is load the whole model (model structure included)
To save model, use (refer link)
torch.save(model, PATH)
And to load it
# Model class must be defined somewhere
model = torch.load(PATH)
model.eval()

AnhPC03
- 622
- 5
- 15