如果是你自己构建的模型,那么可以再forward函数中,返回特定层的输出特征图。
下面是介绍针对预训练模型,获取指定层的输出的方法。
如果你只想得到模型最后全连接层之前的输出,那么只需要将最后一个全连接层去掉:
import torchvision
import torch
net = torchvision.models.resnet18(pretrained=False)
print("model ", net)
net.fc = nn.Sequential([])
当然,对于vgg19网络,如果你想获得vgg19, classifier子模块中第一个全连接层的输出,则可以只更改其classifier子模块。
import torchvision
import torch
net = models.vgg19_bn(pretrained=False).cuda()
net.classifier = nn.Sequential(*list(net.classifier.children())[:-6])
# 只保留第一个全连接层, 输出特征为4096
接下来是一些通用方法:
方法1:
对于简单的模型,可以采用直接遍历子模块的方法
import torchvision
import torch
net = torchvision.models.resnet18(pretrained=False)
print("model ", net)
out = []
x = torch.randn(1, 3, 224, 224)
return_layer = "maxpool"
for name, module in net.named_children():
print(name)
# print(module)
x = module(x)
print(x.shape)
if name == return_layer:
out.append(x.data)
break
print(out[0].shape)
该方法的缺点在于,只能得到其子模块的输出,而对于使用nn.Sequensial()中包含很多层的模型,无法获得其指定层的输出。
方法2:
使用torchvison提供内置方法,参考:Pytorch获取中间层输出的几种方法 - 知乎 (zhihu.com)
该方法与方法1 存在一样的问题。 不能获得其子模块内部特定层的输出。
from collections import OrderedDict
import torch
from torch import nn
class IntermediateLayerGetter(nn.ModuleDict):
"""
Module wrapper that returns intermediate layers from a model
It has a strong assumption that the modules have been registered
into the model in the same order as they are used.
This means that one should **not** reuse the same nn.Module
twice in the forward if you want this to work.
Additionally, it is only able to query submodules that are directly
assigned to the model. So if `model` is passed, `model.feature1` can
be returned, but not `model.feature1.layer2`.
Arguments:
model (nn.Module): model on which we will extract the features
return_layers (Dict[name, new_name]): a dict containing the names
of the modules for which the activations will be returned as
the key of the dict, and the value of the dict is the name
of the returned activation (which the user can specify).
"""
def __init__(self, model, return_layers):
if not set(return_layers).issubset([name for name, _ in model.named_children()]):
raise ValueError("return_layers are not present in model")
orig_return_layers = return_layers
return_layers = {k: v for k, v in return_layers.items()}
layers = OrderedDict()
for name, module in model.named_children():
layers[name] = module
if name in return_layers:
del return_layers[name]
if not return_layers:
break
super(IntermediateLayerGetter, self).__init__(layers)
self.return_layers = orig_return_layers
def forward(self, x):
out = OrderedDict()
for name, module in self.named_children():
x = module(x)
if name in self.return_layers:
out_name = self.return_layers[name]
out[out_name] = x
return out
# example
m = torchvision.models.resnet18(pretrained=True)
# extract layer1 and layer3, giving as names `feat1` and feat2`
new_m = torchvision.models._utils.IntermediateLayerGetter(m,{'layer1': 'feat1', 'layer3': 'feat2'})
out = new_m(torch.rand(1, 3, 224, 224))
print([(k, v.shape) for k, v in out.items()])
# [('feat1', torch.Size([1, 64, 56, 56])), ('feat2', torch.Size([1, 256, 14, 14]))]
补充:
使用 create_feature_extractor方法,创建一个新的模块,该模块将给定模型中的中间节点作为字典返回,用户指定的键作为字符串,请求的输出作为值。
该方法比 IntermediateLayerGetter方法更通用, 不局限于获得模型第一层子模块的输出。因此推荐使用create_feature_extractor方法。
# Feature extraction with resnet
from torchvision.models.feature_extraction import create_feature_extractor
model = torchvision.models.resnet18()
# extract layer1 and layer3, giving as names `feat1` and feat2`
model = create_feature_extractor(
model, {'layer1': 'feat1', 'layer3': 'feat2'})
out = model(torch.rand(1, 3, 224, 224))
print([(k, v.shape) for k, v in out.items()])
提取vgg16,features子模块下的特征层:
# vgg16
backbone = torchvision.models.vgg16_bn(pretrained=True)
# print(backbone)
backbone = create_feature_extractor(backbone, return_nodes={"features.42": "0"}) #“0”字典的key
out = backbone(torch.rand(1, 3, 224, 224))
print(out["0"].shape)
方法3:
使用hook函数,获取任意层的输出。
import torchvision
import torch
from torch import nn
from torchvision.models import resnet50, resnet18
resnet = resnet18()
print(resnet)
features_in_hook = []
features_out_hook = []
# 使用 hook 函数
def hook(module, fea_in, fea_out):
features_in_hook.append(fea_in.data) # 勾的是指定层的输入
# 只取前向传播的数值
features_out_hook.append(fea_out.data) # 勾的是指定层的输出
return None
layer_name = 'avgpool'
for (name, module) in resnet.named_modules():
print(name)
if name == layer_name:
module.register_forward_hook(hook=hook)
# 测试
x = torch.randn(1, 3, 224, 224)
resnet(x)
# print(features_in_hook) # 勾的是指定层的输入
print(features_out_hook[0].shape) # 勾的是指定层的输出 # 1, 64, 56, 56
print(features_out_hook[0])
方法3的优点在于:
通过遍历resnet.named_modules()可以获取任意中间层的输入和输出。
比较通过方法2和方法3获得的指定层输出是否相等。,结果为True,说明两种方法获得的结果相同。
new_m = torchvision.models._utils.IntermediateLayerGetter(resnet, {'avgpool': "feat1"})
out = new_m(x)
print(out['feat1'].data)
# print([(k, v.shape) for k, v in out.items()])
print(torch.equal(features_out_hook[0], out['feat1'].data)) # True
补充:使用net._modules可以获得子模块内的层,但对于复杂模型,使用起来太过繁琐。
for name, module in resnet._modules['layer1']._modules.items():
print(name)
冻结模型指定子模块的权重:
net = torchvision.models.vgg19_bn(pretrained=False)
for param in net.features.parameters():
param.requires_grad=False
# define optimizer
params = [p for p in net.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=0.005,
momentum=0.9, weight_decay=0.0005)
获取模型的子模块,并保存其权重:
import torchvision
import torch
from torch import nn
from torchvision.models import resnet50, resnet18
resnet = resnet18()
layer1 = resnet.get_submodule("layer1")
torch.save(layer1.state_dict(), './layer1.pth')
# 子模块载入相应权重
layer1.load_state_dict(torch.load("./layer1.pth"))
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs