pytorch 可視化feature map的示例代碼
之前做的一些項(xiàng)目中涉及到feature map 可視化的問題,一個(gè)層中feature map的數(shù)量往往就是當(dāng)前層out_channels的值,我們可以通過以下代碼可視化自己網(wǎng)絡(luò)中某層的feature map,個(gè)人感覺可視化feature map對調(diào)參還是很有用的。
不多說了,直接看代碼:
import torch
from torch.autograd import Variable
import torch.nn as nn
import pickle
from sys import path
path.append('/residual model path')
import residual_model
from residual_model import Residual_Model
model = Residual_Model()
model.load_state_dict(torch.load('./model.pkl'))
class myNet(nn.Module):
def __init__(self,pretrained_model,layers):
super(myNet,self).__init__()
self.net1 = nn.Sequential(*list(pretrained_model.children())[:layers[0]])
self.net2 = nn.Sequential(*list(pretrained_model.children())[:layers[1]])
self.net3 = nn.Sequential(*list(pretrained_model.children())[:layers[2]])
def forward(self,x):
out1 = self.net1(x)
out2 = self.net(out1)
out3 = self.net(out2)
return out1,out2,out3
def get_features(pretrained_model, x, layers = [3, 4, 9]): ## get_features 其實(shí)很簡單
'''
1.首先import model
2.將weights load 進(jìn)model
3.熟悉model的每一層的位置,提前知道要輸出feature map的網(wǎng)絡(luò)層是處于網(wǎng)絡(luò)的那一層
4.直接將test_x輸入網(wǎng)絡(luò),*list(model.chidren())是用來提取網(wǎng)絡(luò)的每一層的結(jié)構(gòu)的。net1 = nn.Sequential(*list(pretrained_model.children())[:layers[0]]) ,就是第三層前的所有層。
'''
net1 = nn.Sequential(*list(pretrained_model.children())[:layers[0]])
# print net1
out1 = net1(x)
net2 = nn.Sequential(*list(pretrained_model.children())[layers[0]:layers[1]])
# print net2
out2 = net2(out1)
#net3 = nn.Sequential(*list(pretrained_model.children())[layers[1]:layers[2]])
#out3 = net3(out2)
return out1, out2
with open('test.pickle','rb') as f:
data = pickle.load(f)
x = data['test_mains'][0]
x = Variable(torch.from_numpy(x)).view(1,1,128,1) ## test_x必須為Varibable
#x = Variable(torch.randn(1,1,128,1))
if torch.cuda.is_available():
x = x.cuda() # 如果模型的訓(xùn)練是用cuda加速的話,輸入的變量也必須是cuda加速的,兩個(gè)必須是對應(yīng)的,網(wǎng)絡(luò)的參數(shù)weight都是用cuda加速的,不然會報(bào)錯(cuò)
model = model.cuda()
output1,output2 = get_features(model,x)## model是訓(xùn)練好的model,前面已經(jīng)import 進(jìn)來了Residual model
print('output1.shape:',output1.shape)
print('output2.shape:',output2.shape)
#print('output3.shape:',output3.shape)
output_1 = torch.squeeze(output2,dim = 0)
output_1_arr = output_1.data.cpu().numpy() # 得到的cuda加速的輸出不能直接轉(zhuǎn)變成numpy格式的,當(dāng)時(shí)根據(jù)報(bào)錯(cuò)的信息首先將變量轉(zhuǎn)換為cpu的,然后轉(zhuǎn)換為numpy的格式
output_1_arr = output_1_arr.reshape([output_1_arr.shape[0],output_1_arr.shape[1]])
以上這篇pytorch 可視化feature map的示例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析
這篇文章主要介紹了python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python爬蟲的小伙伴們有一定的幫助,需要的朋友可以參考下2021-04-04
Python中使用haystack實(shí)現(xiàn)django全文檢索搜索引擎功能
django是python語言的一個(gè)web框架,功能強(qiáng)大。配合一些插件可為web網(wǎng)站很方便地添加搜索功能。下面通過本文給大家分享Python中使用haystack實(shí)現(xiàn)django全文檢索搜索引擎功能,感興趣的朋友一起看看吧2017-08-08
科學(xué)計(jì)算NumPy之Ndarray運(yùn)算函數(shù)操作示例匯總
這篇文章主要為大家介紹了科學(xué)計(jì)算NumPy之Ndarray運(yùn)算函數(shù)操作示例匯總,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的方法
這篇文章主要給大家介紹了關(guān)于利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python for循環(huán)搭配else常見問題解決
這篇文章主要介紹了Python for循環(huán)搭配else常見問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Google colab中從kaggle中接入數(shù)據(jù)的操作方法
這篇文章主要介紹了Google colab中如何從kaggle中接入數(shù)據(jù),本文涉及到兩大平臺內(nèi)容,所以我默認(rèn)你已經(jīng)擁有了,并且使用過了一段時(shí)間的google賬號和kaggle賬號,需要的朋友可以參考下2024-03-03
django celery redis使用具體實(shí)踐
這篇文章主要介紹了django celery redis使用具體實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

