pytorch 可视化feature map的示例代码


Posted in Python onAugust 20, 2019

之前做的一些项目中涉及到feature map 可视化的问题,一个层中feature map的数量往往就是当前层out_channels的值,我们可以通过以下代码可视化自己网络中某层的feature map,个人感觉可视化feature map对调参还是很有用的。

不多说了,直接看代码:

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 其实很简单
'''
1.首先import model 
2.将weights load 进model
3.熟悉model的每一层的位置,提前知道要输出feature map的网络层是处于网络的那一层
4.直接将test_x输入网络,*list(model.chidren())是用来提取网络的每一层的结构的。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() # 如果模型的训练是用cuda加速的话,输入的变量也必须是cuda加速的,两个必须是对应的,网络的参数weight都是用cuda加速的,不然会报错
  model = model.cuda()
output1,output2 = get_features(model,x)## model是训练好的model,前面已经import 进来了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加速的输出不能直接转变成numpy格式的,当时根据报错的信息首先将变量转换为cpu的,然后转换为numpy的格式
output_1_arr = output_1_arr.reshape([output_1_arr.shape[0],output_1_arr.shape[1]])

以上这篇pytorch 可视化feature map的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
tensorflow训练中出现nan问题的解决
Feb 10 Python
Python3.5 处理文本txt,删除不需要的行方法
Dec 10 Python
Python实现的多进程拷贝文件并显示百分比功能示例
Apr 09 Python
在PyCharm的 Terminal(终端)切换Python版本的方法
Aug 02 Python
Python 中pandas索引切片读取数据缺失数据处理问题
Oct 09 Python
如何使用Python脚本实现文件拷贝
Nov 20 Python
python打印n位数“水仙花数”(实例代码)
Dec 25 Python
python 消除 futureWarning问题的解决
Dec 25 Python
Python通过Tesseract库实现文字识别
Mar 05 Python
使用Keras预训练好的模型进行目标类别预测详解
Jun 27 Python
matplotlib 三维图表绘制方法简介
Sep 20 Python
python编程学习使用管道Pipe编写优化代码
Nov 20 Python
python爬虫 基于requests模块的get请求实现详解
Aug 20 #Python
python爬虫 urllib模块url编码处理详解
Aug 20 #Python
pytorch实现用Resnet提取特征并保存为txt文件的方法
Aug 20 #Python
python web框架 django wsgi原理解析
Aug 20 #Python
opencv转换颜色空间更改图片背景
Aug 20 #Python
pytorch 预训练层的使用方法
Aug 20 #Python
python爬虫 urllib模块反爬虫机制UA详解
Aug 20 #Python
You might like
php 计算两个时间戳相隔的时间的函数(小时)
2009/12/18 PHP
PHP下通过QRCode类库创建中间带网站LOGO的二维码
2014/07/12 PHP
php程序内部post数据的方法
2015/03/31 PHP
jquery easyui的tabs使用时的问题
2010/03/23 Javascript
浅析Js(Jquery)中,字符串与JSON格式互相转换的示例(直接运行实例)
2013/07/09 Javascript
JQueryiframe页面操作父页面中的元素与方法(实例讲解)
2013/11/19 Javascript
Extjs4中tree的拖拽功能(可以两棵树之间拖拽) 简单实例
2013/12/08 Javascript
JS获取select的value和text值的简单实例
2014/02/26 Javascript
javascript文件中引用依赖的js文件的方法
2014/03/17 Javascript
详解Javascript 装载和执行
2014/11/17 Javascript
浅谈Sticky组件的改进实现
2016/03/22 Javascript
IE8 内存泄露(内存一直增长 )的原因及解决办法
2016/04/06 Javascript
javascript实现文字无缝滚动效果
2017/08/26 Javascript
JSONP原理及应用实例详解
2018/09/13 Javascript
angularjs手动识别字符串中的换行符方法
2018/10/02 Javascript
小程序scroll-view安卓机隐藏横向滚动条的实现详解
2019/05/16 Javascript
使用konva和vue-konva库实现拖拽滑块验证功能
2020/04/27 Javascript
[01:26]神话结束了,却也刚刚开始——DOTA2新英雄玛尔斯驾临战场
2019/03/10 DOTA
[52:05]EG vs OG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/18 DOTA
Python实现打印螺旋矩阵功能的方法
2017/11/21 Python
python实现图书管理系统
2018/03/12 Python
Python cv2 图像自适应灰度直方图均衡化处理方法
2018/12/07 Python
python 猴子补丁(monkey patch)
2019/06/26 Python
澳大利亚领先的在线药房:Pharmacy Online(有中文站)
2020/02/22 全球购物
千元咖啡店的创业计划书范文
2013/12/29 职场文书
生日寄语大全
2014/04/08 职场文书
电子装配专业毕业生求职信
2014/04/23 职场文书
初中学生评语大全
2014/04/24 职场文书
航海技术专业毕业生推荐信
2014/07/09 职场文书
本溪关门山导游词
2015/02/09 职场文书
2015年党建工作目标责任书
2015/05/08 职场文书
公文写作指导之倡议书!
2019/07/03 职场文书
提升Nginx性能的一些建议
2021/03/31 Servers
JS实现扫雷项目总结
2021/05/19 Javascript
springboot临时文件存储目录配置方式
2021/07/01 Java/Android
Python字符串格式化方式
2022/04/07 Python