欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch 可視化feature map的示例代碼

 更新時(shí)間:2019年08月20日 10:10:32   作者:牛丸4  
今天小編就為大家分享一篇pytorch 可視化feature map的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

之前做的一些項(xiàng)目中涉及到feature map 可視化的問題,一個(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]])

以上這篇pytorch 可視化feature map的示例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python字典取值的幾種方法總結(jié)

    python字典取值的幾種方法總結(jié)

    這篇文章主要介紹了python字典取值的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Python 數(shù)據(jù)結(jié)構(gòu)之隊(duì)列的實(shí)現(xiàn)

    Python 數(shù)據(jù)結(jié)構(gòu)之隊(duì)列的實(shí)現(xiàn)

    這篇文章主要介紹了Python 數(shù)據(jù)結(jié)構(gòu)之隊(duì)列的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • python學(xué)習(xí)筆記之多進(jìn)程

    python學(xué)習(xí)筆記之多進(jìn)程

    這篇文章主要介紹了python多進(jìn)程的的相關(guān)資料,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下
    2020-08-08
  • 關(guān)于Numpy之repeat、tile的用法總結(jié)

    關(guān)于Numpy之repeat、tile的用法總結(jié)

    這篇文章主要介紹了關(guān)于Numpy之repeat、tile的用法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python字符串類型及格式化問題

    Python字符串類型及格式化問題

    這篇文章主要介紹了Python字符串類型及格式化問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • python調(diào)用API實(shí)現(xiàn)智能回復(fù)機(jī)器人

    python調(diào)用API實(shí)現(xiàn)智能回復(fù)機(jī)器人

    這篇文章主要為大家詳細(xì)介紹了python調(diào)用API實(shí)現(xiàn)智能回復(fù)機(jī)器人,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python創(chuàng)建一個(gè)自定義視頻播放器的實(shí)現(xiàn)

    Python創(chuàng)建一個(gè)自定義視頻播放器的實(shí)現(xiàn)

    本文主要介紹了Python創(chuàng)建一個(gè)自定義視頻播放器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Django Form常用功能及代碼示例

    Django Form常用功能及代碼示例

    這篇文章主要介紹了Django Form常用功能及代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • python用BeautifulSoup庫(kù)簡(jiǎn)單爬蟲實(shí)例分析

    python用BeautifulSoup庫(kù)簡(jiǎn)單爬蟲實(shí)例分析

    文章給大家分享了關(guān)于python爬蟲的相關(guān)實(shí)例以及相關(guān)代碼,有興趣的朋友們參考下。
    2018-07-07
  • 利用python爬取軟考試題之ip自動(dòng)

    利用python爬取軟考試題之ip自動(dòng)

    最近為了考試打算抓取網(wǎng)上的軟考試題,在抓取中遇到一些問題,下面這篇文章主要介紹的是利用python爬取軟考試題之ip自動(dòng)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。
    2017-03-03

最新評(píng)論