当前位置:网站首页>Torchvision loads the weight of RESNET except the full connection layer
Torchvision loads the weight of RESNET except the full connection layer
2022-06-30 08:41:00 【Wu lele~】
Simply post how to load torchvision Pre training weight , Or always forget .
import torch
import torch.nn as nn
import torchvision
class ResNet(nn.Module):
def __init__(self):
super(ResNet, self).__init__()
pass
# Go to ResNet Add weight inside
def init_weights(self, pretrained = True):
""" Args: self: Model itself pretrained (bool) """
if pretrained == True:
# obtain ResNet34 Pre training weight of
resnet34 = torchvision.models.resnet34(pretrained=True)
pretrained_dict = resnet34.state_dict()
""" load torchvision The pre training model and parameters in are passed after state_dict() Method extract parameters Or directly from the official model_zoo download : pretrained_dict = model_zoo.load_url(model_urls['resnet152'])"""
# Get the parameter Dictionary of the current model
model_dict = self.state_dict()
# take pretrained_dict It doesn't belong to model_dict Key out of
pretrained_dict = {
k: v for k, v in pretrained_dict.items() if k in model_dict}
# Update existing model_dict
model_dict.update(pretrained_dict)
# Load what we really need state_dict
self.load_state_dict(model_dict)
print(' Successfully loaded pre training weights ')
else:
pass
if __name__ == '__main__':
resnet = ResNet()
resnet.init_weights(pretrained=True)
边栏推荐
- Detailed explanation of pytoch's scatter function
- 【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
- C# ListBox如何获取选中的内容(搜了很多无效的文章)
- Common tools installation, configuration, compilation, link, etc
- MIME类型大全
- [untitled]
- Redis design and Implementation (VI) | cluster (sharding)
- Redis design and Implementation (IV) | master-slave replication
- vim 从嫌弃到依赖(21)——跨文件搜索
- [nvme2.0b 14-7] set features (Part 1)
猜你喜欢

微信公众号第三方平台开发,零基础入门。想学我教你啊

【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
![[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel](/img/98/bb250030f3e31e71d48e3be3ea4698.png)
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel

Self made GIF dynamic graph -gifcam

Summary of common pytoch APIs

Redis设计与实现(三)| 服务器与客户端的交互(事件IO模型)

codeforces每日5题(均1700)-第三天

Flink SQL 自定义 Connector

Flink Exception -- No ExecutorFactory found to execute the application

使用华为性能管理服务,按需配置采样率
随机推荐
codeforces每日5题(均1700)-第三天
CUDA implements matrix replication
Redis design and Implementation (VII) | publish & subscribe
C # listbox how to get the selected content (search many invalid articles)
增强for循环的增删操作 & 迭代器删除集合元素
2021-02-22
[untitled]
Axure make menu bar effect
Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
Swagger use
14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
我们如何拿到自己满意的薪资呢?这些套路还是需要掌握的
2021-02-27
Self made GIF dynamic graph -gifcam
c#获取当前的时间戳
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel
Unity basic lighting model
Flink 数据偶尔数据积压导致checkpoint失败
vite項目require語法兼容問題解决require is not defined
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush