pytorch 获取层权重,对特定层注入hook, 提取中间层输出的方法
如下所示:
#获取模型权重 fork,vinmodel_2.state_dict().iteritems(): print("Layer{}".format(k)) print(v)
#获取模型权重 forlayerinmodel_2.modules(): ifisinstance(layer,nn.Linear): print(layer.weight)
#将一个模型权重载入另一个模型 model=VGG(make_layers(cfg['E']),**kwargs) ifpretrained: load=torch.load('/home/huangqk/.torch/models/vgg19-dcbb9e9d.pth') load_state={k:vfork,vinload.items()ifknotin['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) returnmodel
#对特定层注入hook defhook_layers(model): defhook_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 forlinvgg.features.modules(): x=l(x) modulelist=list(vgg.features.modules()) forlinmodulelist[:5]: x=l(x) keep=x forlinmodulelist[5:]: x=l(x)
#提取vgg模型的中间层输出 #coding:utf8 importtorch importtorch.nnasnn fromtorchvision.modelsimportvgg16 fromcollectionsimportnamedtuple classVgg16(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() defforward(self,x): results=[] forii,modelinenumerate(self.features): x=model(x) ifiiin{3,8,15,22}: results.append(x) vgg_outputs=namedtuple("VggOutputs",['relu1_2','relu2_2','relu3_3','relu4_3']) returnvgg_outputs(*results)
以上这篇pytorch获取层权重,对特定层注入hook,提取中间层输出的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。