最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch 可視化feature map的示例代碼

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

之前做的一些項(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加速的,不然會(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的示例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析

    python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析

    這篇文章主要介紹了python爬蟲之你好,李煥英電影票房數(shù)據(jù)分析,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python爬蟲的小伙伴們有一定的幫助,需要的朋友可以參考下
    2021-04-04
  • Python 變量類型詳解

    Python 變量類型詳解

    變量可以指定不同的數(shù)據(jù)類型,這些變量可以存儲(chǔ)整數(shù),小數(shù)或字符。這篇文章主要介紹了Python 變量類型詳解,需要的朋友可以參考下
    2018-10-10
  • python的turtle庫使用詳解

    python的turtle庫使用詳解

    在本篇文章里小編給大家分享了關(guān)于python的turtle庫相關(guān)知識(shí)點(diǎn)以及使用方法,需要的朋友們跟著學(xué)習(xí)下。
    2019-05-05
  • Python中使用haystack實(shí)現(xiàn)django全文檢索搜索引擎功能

    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ù)操作示例匯總

    這篇文章主要為大家介紹了科學(xué)計(jì)算NumPy之Ndarray運(yùn)算函數(shù)操作示例匯總,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的方法

    利用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常見問題解決

    這篇文章主要介紹了Python for循環(huán)搭配else常見問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Python的缺點(diǎn)和劣勢分析

    Python的缺點(diǎn)和劣勢分析

    在本篇文章里小編給大家整理了關(guān)于Python的缺點(diǎn)和劣勢總結(jié),有興趣的朋友們可以學(xué)習(xí)下。
    2019-11-11
  • Google colab中從kaggle中接入數(shù)據(jù)的操作方法

    Google colab中從kaggle中接入數(shù)據(jù)的操作方法

    這篇文章主要介紹了Google colab中如何從kaggle中接入數(shù)據(jù),本文涉及到兩大平臺(tái)內(nèi)容,所以我默認(rèn)你已經(jīng)擁有了,并且使用過了一段時(shí)間的google賬號(hào)和kaggle賬號(hào),需要的朋友可以參考下
    2024-03-03
  • django celery redis使用具體實(shí)踐

    django celery redis使用具體實(shí)踐

    這篇文章主要介紹了django celery redis使用具體實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論

台中市| 南投县| 贵德县| 西华县| 玉田县| 南昌县| 铜梁县| 新龙县| 托里县| 太仓市| 柳河县| 田林县| 紫金县| 宜丰县| 云霄县| 芜湖县| 彩票| 西盟| 兴安县| 灵川县| 额尔古纳市| 江门市| 于都县| 巨鹿县| 丰都县| 丹阳市| 名山县| 吕梁市| 佛山市| 民和| 陆川县| 平泉县| 昌都县| 抚松县| 津市市| 涿州市| 克山县| 炎陵县| 平利县| 东乌珠穆沁旗| 祁连县|