溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

pytorch 可視化feature map的示例代碼

發(fā)布時(shí)間:2020-09-20 00:15:36 來源:腳本之家 閱讀:376 作者:牛丸4 欄目:開發(fā)技術(shù)

之前做的一些項(xiàng)目中涉及到feature map 可視化的問題,一個(gè)層中feature map的數(shù)量往往就是當(dāng)前層out_channels的值,我們可以通過以下代碼可視化自己網(wǎng)絡(luò)中某層的feature map,個(gè)人感覺可視化feature map對(duì)調(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í)很簡(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())是用來提取網(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)來了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的示例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI