之前做的一些項(xiàng)目中涉及到feature map 可視化的問(wèn)題,一個(gè)層中feature map的數(shù)量往往就是當(dāng)前層out_channels的值,我們可以通過(guò)以下代碼可視化自己網(wǎng)絡(luò)中某層的feature map,個(gè)人感覺可視化feature map對(duì)調(diào)參還是很有用的。
不多說(shuō)了,直接看代碼:
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í)很簡(jiǎn)單 ''' 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())是用來(lái)提取網(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è)必須是對(duì)應(yīng)的,網(wǎng)絡(luò)的參數(shù)weight都是用cuda加速的,不然會(huì)報(bào)錯(cuò) model = model.cuda() output1,output2 = get_features(model,x)## model是訓(xùn)練好的model,前面已經(jīng)import 進(jìn)來(lái)了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]])
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站題目:pytorch可視化featuremap的示例代碼-創(chuàng)新互聯(lián)
本文地址:http://chinadenli.net/article40/dgjdeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、面包屑導(dǎo)航、做網(wǎng)站、ChatGPT、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容