pytorch快速加载预训练模型参数的方式
https://github.com/pytorch/vision/tree/master/torchvision/models
常用预训练模型在这里面
总结下各种模型的下载地址:
1 Resnet:
2
3 model_urls = {
4 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
5 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
6 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
7 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
8 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
9 }
10
11 inception:
12
13 model_urls = {
14 # Inception v3 ported from TensorFlow
15 'inception_v3_google': 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth',
16 }
17
18 Densenet:
19
20 model_urls = {
21 'densenet121': 'https://download.pytorch.org/models/densenet121-a639ec97.pth',
22 'densenet169': 'https://download.pytorch.org/models/densenet169-b2777c0a.pth',
23 'densenet201': 'https://download.pytorch.org/models/densenet201-c1103571.pth',
24 'densenet161': 'https://download.pytorch.org/models/densenet161-8d451a50.pth',
25 }
26
27
28
29 Alexnet:
30
31 model_urls = {
32 'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
33 }
34
35 vggnet:
36
37 model_urls = {
38 'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
39 'vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth',
40 'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
41 'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
42 'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
43 'vgg13_bn': 'https://download.pytorch.org/models/vgg13_bn-abd245e5.pth',
44 'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
45 'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
46 }
解决下载速度慢的方法:
1.换移动网络,有些公司网、校园网对于pytorch网站有很大的限速。
2.翻墙(有时不翻墙也可)先下载下来,放入文件夹中,方法如下两种(推荐第二种)
针对的预训练模型是通用的模型,也可以是自定义模型,大多是vgg16 , resnet50 , resnet101 , 等,从官网加载太慢
1.直接修改源码,改为本地地址
直接使用默认程序里的下载方式,往往比较慢;
通过修改源代码,使得模型加载已经下载好的参数,修改地方如下:
通过查找自己代码里所调用网络的类,使用pycharm自带的函数查找功能(ctrl+鼠标左键),查看此网络的加载方法,修改model.load_state_dict()函数。
例如:已经下载好的resnet50的参数文件:放在model_urls里面,这样就可以提前下载直接使用。
model_urls = {
'resnet50': '/home/huihua/NewDisk1/pretrain_parameter/resnet50-19c8e357.pth',
}
2.把模型权重下载至torch的缓存文件夹
由于torch在加载模型时候首先检查本地缓存是否已经存在模型,所以在本用户目录下,预先下载放入可快速加载模型。
cd .cache/torch/checkpoints
cd /home/team/.torch/models
两种方式,常常是用第二种作为torch模型的缓存文件夹
进入文件夹把所需模型权重放入即可自动加载,相比第一种方法简单点。
来源:oschina
链接:https://my.oschina.net/u/4399909/blog/4463351