pytorch 获取层权重,对特定层注入hook, 提取中间层输出的方法
作者:青盏 时间:2021-05-20 07:01:01
如下所示:
#获取模型权重
for k, v in model_2.state_dict().iteritems():
print("Layer {}".format(k))
print(v)
#获取模型权重
for layer in model_2.modules():
if isinstance(layer, nn.Linear):
print(layer.weight)
#将一个模型权重载入另一个模型
model = VGG(make_layers(cfg['E']), **kwargs)
if pretrained:
load = torch.load('/home/huangqk/.torch/models/vgg19-dcbb9e9d.pth')
load_state = {k: v for k, v in load.items() if k not in ['classifier.0.weight', 'classifier.0.bias', 'classifier.3.weight', 'classifier.3.bias', 'classifier.6.weight', 'classifier.6.bias']}
model_state = model.state_dict()
model_state.update(load_state)
model.load_state_dict(model_state)
return model
# 对特定层注入hook
def hook_layers(model):
def hook_function(module, inputs, outputs):
recreate_image(inputs[0])
print(model.features._modules)
first_layer = list(model.features._modules.items())[0][1]
first_layer.register_forward_hook(hook_function)
#获取层
x = someinput
for l in vgg.features.modules():
x = l(x)
modulelist = list(vgg.features.modules())
for l in modulelist[:5]:
x = l(x)
keep = x
for l in modulelist[5:]:
x = l(x)
# 提取vgg模型的中间层输出
# coding:utf8
import torch
import torch.nn as nn
from torchvision.models import vgg16
from collections import namedtuple
class Vgg16(torch.nn.Module):
def __init__(self):
super(Vgg16, self).__init__()
features = list(vgg16(pretrained=True).features)[:23]
# features的第3,8,15,22层分别是: relu1_2,relu2_2,relu3_3,relu4_3
self.features = nn.ModuleList(features).eval()
def forward(self, x):
results = []
for ii, model in enumerate(self.features):
x = model(x)
if ii in {3, 8, 15, 22}:
results.append(x)
vgg_outputs = namedtuple("VggOutputs", ['relu1_2', 'relu2_2', 'relu3_3', 'relu4_3'])
return vgg_outputs(*results)
来源:https://blog.csdn.net/qq_16234613/article/details/80217851
标签:pytorch,层权重,hook,中间层
0
投稿
猜你喜欢
ASP验证码的实现原理及源码
2007-10-02 12:14:00
python获取点击的坐标画图形的方法
2023-04-23 09:59:28
ASp使用AspJpeg组件制作透明文字水印
2008-01-15 20:25:00
golang mysql的连接池的具体使用
2024-01-14 11:52:10
JavaScript 图片变换效果(ie only)
2010-01-20 12:40:00
浅谈Python对内存的使用(深浅拷贝)
2021-06-08 03:07:35
如何在pyqt中实现全局事件实战记录
2023-07-01 02:25:46
Python流行ORM框架sqlalchemy安装与使用教程
2022-10-20 06:12:20
Python压缩解压缩zip文件及破解zip文件密码的方法
2023-04-20 10:30:30
一小时学会TensorFlow2之Fashion Mnist
2023-01-27 12:08:16
python删除列表内容
2023-05-23 02:47:49
JavaScript对象属性操作实例解析
2024-05-13 09:18:47
从MySQL4.0向MySQL5迁移数据
2007-11-19 13:11:00
python+playwright微软自动化工具的使用
2022-10-18 08:52:29
MySql 5.7.17压缩包免安装的配置过程图解
2024-01-27 20:02:14
基于Python编写一个语音合成系统
2021-10-14 03:28:16
在VScode中引用自定义模块问题
2023-08-13 03:50:37
Python try except异常捕获机制原理解析
2023-04-12 20:00:35
简评:JavaScript将成Silverlight的最大对手?
2008-10-17 10:29:00
asp下以Json获取中国天气网天气的代码
2011-03-06 11:01:00