pytorch 獲取層權(quán)重,對(duì)特定層注入hook, 提取中間層輸出的方法
如下所示:
#獲取模型權(quán)重
for k, v in model_2.state_dict().iteritems():
print("Layer {}".format(k))
print(v)
#獲取模型權(quán)重 for layer in model_2.modules(): if isinstance(layer, nn.Linear): print(layer.weight)
#將一個(gè)模型權(quán)重載入另一個(gè)模型
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
# 對(duì)特定層注入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 獲取層權(quán)重,對(duì)特定層注入hook, 提取中間層輸出的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)某考試系統(tǒng)生成word試卷
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)某考試系統(tǒng)生成word試卷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
如何利用Python獲取鼠標(biāo)的實(shí)時(shí)位置
這篇文章主要給大家介紹了關(guān)于如何利用Python獲取鼠標(biāo)的實(shí)時(shí)位置的相關(guān)資料,主要利用的是pyautogui,一個(gè)自動(dòng)化鍵鼠操作的Python類庫,需要的朋友可以參考下2022-01-01
Autopep8的使用(python自動(dòng)編排工具)
這篇文章主要介紹了Autopep8的使用(python自動(dòng)編排工具),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
解決Vscode中jupyter出現(xiàn)kernel dead問題
遇到VSCode中Jupyter Kernel Dead時(shí),可通過Anaconda Prompt安裝ipykernel解決,首先使用jupyter kernelspec list命令查看內(nèi)核,若發(fā)現(xiàn)缺少ipykernel,激活相應(yīng)虛擬環(huán)境,使用conda install ipykernel命令安裝,操作后,VSCode中Jupyter應(yīng)能正常運(yùn)行2024-09-09
Python中免驗(yàn)證跳轉(zhuǎn)到內(nèi)容頁的實(shí)例代碼
在本篇文章里小編給大家整理的是一篇關(guān)于Python中免驗(yàn)證跳轉(zhuǎn)到內(nèi)容頁的實(shí)例代碼,有興趣的朋友們可以學(xué)習(xí)分享下。2020-10-10
基于數(shù)據(jù)歸一化以及Python實(shí)現(xiàn)方式
今天小編就為大家分享一篇基于數(shù)據(jù)歸一化以及Python實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Python復(fù)制文件的9個(gè)方法小結(jié)
本文主要介紹了Python復(fù)制文件的9個(gè)方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01

