Pytorch离线下载并使用torchvision.models预训练模型

Pytorch离线下载并使用torchvision.models预训练模型

原本直接在IDE中执行models.alexnet(pretrained=True)就行了,但是一直报错,搞得我好不难受
在这里插入图片描述

不用说,肯定是由于网络不好导致模型下载失败,能不能离线下载完之后在本地直接使用?答案是肯定的

其实步骤很简单(但是对于新手要命),就和我们下载软件一样,详细步骤如下:

  1. 复制需要下载的模型地址,粘贴到浏览器地址栏中下载,各种模型的下载地址如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1. Resnet:
model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}

2. inception:
model_urls = {
Inception v3 ported from TensorFlow
'inception_v3_google': 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth',
}

3. Densenet:
model_urls = {
'densenet121': 'https://download.pytorch.org/models/densenet121-a639ec97.pth',
'densenet169': 'https://download.pytorch.org/models/densenet169-b2777c0a.pth',
'densenet201': 'https://download.pytorch.org/models/densenet201-c1103571.pth',
'densenet161': 'https://download.pytorch.org/models/densenet161-8d451a50.pth',
}

4. Alexnet:
model_urls = {
'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
}

5. vggnet:
model_urls = {
'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
'vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth',
'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
'vgg13_bn': 'https://download.pytorch.org/models/vgg13_bn-abd245e5.pth',
'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
}
  1. 这里以Alexnet为例,复制https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth浏览器地址栏中下载
    在这里插入图片描述

大约233M,慢慢下吧,友情提示一下,在手机上下载每秒几M,在电脑下载每秒几kb,推荐在手机上下载好之后,再发送到电脑上

  1. 将下载好的文件剪切到torch缓存文件夹下即可,windos和linux的torch缓存文件夹分别如下:
  • windows:C:\Users\wang1\.cache\torch\checkpoints (wang1是你的电脑用户名)
  • linux:/root/.cache/torch/hub/checkpoints/alexnet-owt-4df8aa71.pth

如下所示:
在这里插入图片描述
4. 然后执行models.alexnet(pretrained=True),发现OK,大功告成!
在这里插入图片描述