幽灵资源网 Design By www.bzswh.com
如下所示:
#获取模型权重 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)
以上这篇pytorch 获取层权重,对特定层注入hook, 提取中间层输出的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。