Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to load some parameters of pre-training Model in Pytorch

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "Pytorch how to load some pre-training model parameters", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Pytorch how to load part of the pre-training model parameters" this article.

Directly load the pre-selected face model

If the model we use is exactly the same as the pre-training model, then we can load other people's models directly, and in another case, we suddenly break down in the process of training our own models. but as long as we save the parameters of the previous model, we can also use the following code to load our saved model directly to continue training, without having to start from scratch.

Model=DPN (* args, * * kwargs) model.load_state_dict (torch.load ("DPN.pth"))

This loading method is based on the model storage method used by Pytorch:

Torch.save (DPN.state_dict (), "DPN.pth")

Load some pre-training model parameters

In fact, most of the time, the models we put forward according to our own objects are changed on the basis of some public models, in which there is no need for us to train the parameters of the model from scratch, as long as we load the trained model parameters, which helps to improve the accuracy of training and the generalization ability of our model.

Model = DPN (num_init_features=64, k_R=96, Gambi32, kwon sec=), inc_sec= (16pence32), num_classes=1,decoder=args.decoder) http = {'url':' http://data.lip6.fr/cadene/pretrainedmodels/dpn92_extra-b040e4a9b.pth'} pretrained_dict=model_zoo.load_url (http ['url']) model_dict = model.state_dict () pretrained_dict= {k: v for k V in pretrained_dict.items () if k in model_dict} # filter out unnecessary keys model_dict.update (pretrained_dict) model.load_state_dict (model_dict) model = torch.nn.DataParallel (model) .cuda ()

Because you need to delete the mismatched keys in the pre-training model, that is, the name of the layer.

These are all the contents of the article "how to load some parameters of the pre-training model for Pytorch". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report