pytorch实现从本地加载 .pth 格式模型

作者:卢开毅 时间:2021-07-01 18:32:03 

可以从官网加载预训练好的模型:


import torchvision.models as models

model = models.vgg16(pretrained = True)
print(model)

但是经常会出现因为下载速度太慢而出现requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError(10060, '由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。', None, 10060, None))这种错误,因此需要我们手动去下载 .pth 文件(百度云也很慢,如果你是SVIP,当我没说;迅雷的速度也还可以),然后从本地加载。

从本地加载只需要把上面的代码换成如下:


import torchvision.models as models

model = models.vgg16(pretrained=False)
pre=torch.load(r'.\kaggle_dog_vs_cat\pretrain\vgg16-397923af.pth')
model.load_state_dict(pre)

如果你模型不是用的vgg16,而是用的vgg11或者vgg13,只需要修改语句 model = models.vgg16(pretrained=False) 为对应模型的函数即可。

来源:https://blog.csdn.net/TomorrowAndTuture/article/details/100219240

标签:pytorch,加载,pth
0
投稿

猜你喜欢

  • 利用Tkinter和matplotlib两种方式画饼状图的实例

    2021-09-22 18:10:35
  • python 实现手机自动拨打电话的方法(通话压力测试)

    2021-03-19 08:10:34
  • Pytorch参数注册和nn.ModuleList nn.ModuleDict的问题

    2021-02-10 23:43:06
  • python实现智能语音天气预报

    2023-05-13 14:10:27
  • Python 设计模式中命令模式

    2022-10-14 20:28:36
  • 解决pycharm工程启动卡住没反应的问题

    2021-04-05 09:54:02
  • 对python修改xml文件的节点值方法详解

    2021-02-21 19:54:43
  • Discuz7 的提示效果如何实现

    2010-01-13 13:10:00
  • DIV+CSS设计时容易犯的一些错误

    2007-11-13 12:42:00
  • 让Python程序定时执行的8种方法整理

    2023-05-16 01:32:22
  • Python3.x检查内存可用大小的两种实现

    2022-03-24 07:17:03
  • python3.3教程之模拟百度登陆代码分享

    2023-08-02 23:08:23
  • CSS网页布局编码小技巧整理

    2009-12-30 16:50:00
  • Python实现人脸识别并进行视频跟踪打码

    2022-08-04 22:57:25
  • 对Python中小整数对象池和大整数对象池的使用详解

    2023-02-17 18:13:49
  • Python RuntimeError: thread.__init__() not called解决方法

    2022-12-22 17:11:46
  • python实现密码验证合格程序的思路详解

    2022-12-10 05:07:38
  • Django REST 异常处理详解

    2023-06-19 02:40:34
  • Python实现模拟锟斤拷等各类乱码详解

    2022-08-23 04:31:22
  • python去除字符串中的空格、特殊字符和指定字符的三种方法

    2022-06-12 20:21:31
  • asp之家 网络编程 m.aspxhome.com