In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about the sample analysis of loading (.pth) format in pytorch. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
There are some very popular networks such as resnet, squeezenet, densenet and so on in pytorch, including network structure and trained models.
Pytorch comes with a model website: https://pytorch-cn.readthedocs.io/zh/latest/torchvision/torchvision-models/
Load the pre-trained model on the official website:
Import torchvision.models as models # pretrained=True can use the pre-trained model resnet18 = models.resnet18 (pretrained=True) print (resnet18)
The error is as follows:
Requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError (10060,' the connection attempt failed because the connecting party did not reply correctly or the connected host did not respond after a period of time.' , None, 10060, None))
Mainly because the code will remotely download the parameters of the model, but the domestic network is generally not connected, this is that we need to manually download the pre-training network you want.
The address can be obtained in two ways by downloading the address:
1. From the error message, the following line of information appears when the above code is running:
Downloading: "https://download.pytorch.org/models/resnet18-5c106cde.pth" to C:\ Users\ Luo/.torch\ models\ resnet18-5c106cde.pth
Copy this URL to the browser, it may not open, remove the https://, and directly enter download.pytorch.org/models/resnet18-5c106cde.pth can be downloaded.
two。 Look for the address of the model from pytorch's github: https://github.com/pytorch/vision/tree/master/torchvision/models
Find the corresponding model name and go in to find the address.
Save it by yourself after downloading it. I will store it directly in pytorch models.
The next step is to run the .pth file. First of all, to determine whether to save the entire network structure plus parameters, or only saved parameters, you can test it. This is my model is squeezenet1_1, you can test the model you downloaded.
Import torchpthfile = ringing E:\ anaconda\ app\ envs\ luo\ Lib\ site-packages\ torchvision\ models\ squeezenet1_1.pth'net = torch.load (pthfile) print (net)
The result is
Obviously only the parameters are saved, so we're going to load the model in a different way.
Import torchimport torchvision.models as models # pretrained=True can use the pre-trained model net = models.squeezenet1_1 (pretrained=False) pthfile = ritual E:\ anaconda\ app\ envs\ luo\ Lib\ site-packages\ torchvision\ models\ squeezenet1_1.pth'net.load_state_dict (torch.load (pthfile)) print (net)
The result
Thank you for reading! This is the end of this article on "sample analysis of loading (.pth) format in pytorch". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.