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 相关文章推荐
Python读写Redis数据库操作示例
Mar 18 Python
使用相同的Apache实例来运行Django和Media文件
Jul 22 Python
python socket多线程通讯实例分析(聊天室)
Apr 06 Python
python写一个md5解密器示例
Feb 23 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
Apr 02 Python
Python使用到第三方库PyMuPDF图片与pdf相互转换
May 03 Python
Python 200行代码实现一个滑动验证码过程详解
Jul 11 Python
python将数组n等分的实例
Dec 02 Python
python字典setdefault方法和get方法使用实例
Dec 25 Python
Python %r和%s区别代码实例解析
Apr 03 Python
python如何利用traceback获取详细的异常信息
Jun 05 Python
PYTHON基于Pyecharts绘制常见的直角坐标系图表
Apr 28 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
substr()函数中文版
2006/10/09 PHP
PHP实现求连续子数组最大和问题2种解决方法
2017/12/26 PHP
PHP仿tp实现mvc框架基本设计思路与实现方法分析
2018/05/23 PHP
jQuery 表单验证扩展(三)
2010/10/20 Javascript
jQuery 数据缓存模块进化史详细介绍
2012/11/19 Javascript
JavaScript数组常用方法
2015/03/02 Javascript
javascript获取本机操作系统类型的方法
2015/08/13 Javascript
javascript每日必学之运算符
2016/02/16 Javascript
JavaScript中offsetWidth的bug及解决方法
2017/05/17 Javascript
基于jquery trigger函数无法触发a标签的两种解决方法
2018/01/06 jQuery
基于Vue实现关键词实时搜索高亮显示关键词
2018/07/21 Javascript
基于vue实现滚动条滚动到指定位置对应位置数字进行tween特效
2019/04/18 Javascript
Layui动态生成select下拉选择框不显示的解决方法
2019/09/24 Javascript
解决vue.js提交数组时出现数组下标的问题
2019/11/05 Javascript
Vue中rem与postcss-pxtorem的应用详解
2019/11/20 Javascript
[43:49]LGD vs CHAOS 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
Python中pow()和math.pow()函数用法示例
2018/02/11 Python
Linux下Pycharm、Anaconda环境配置及使用踩坑
2018/12/19 Python
对python中url参数编码与解码的实例详解
2019/07/25 Python
Python列表删除元素del、pop()和remove()的区别小结
2019/09/11 Python
python修改文件内容的3种方法详解
2019/11/15 Python
Python文件操作函数用法实例详解
2019/12/24 Python
python中前缀运算符 *和 **的用法示例详解
2020/05/28 Python
python空元组在all中返回结果详解
2020/12/15 Python
python实现PolynomialFeatures多项式的方法
2021/01/06 Python
CSS3混合模式mix-blend-mode/background-blend-mode简介
2018/03/15 HTML / CSS
纯CSS3单页切换导航菜单界面设计的简单实现
2016/08/16 HTML / CSS
Hush Puppies澳大利亚官网:舒适的男女休闲和正装鞋
2019/08/24 全球购物
Sandro法国官网:法国成衣品牌
2019/08/28 全球购物
宿舍违规检讨书
2014/01/12 职场文书
房地产项目策划书
2014/02/05 职场文书
大学生简短的自我评价分享
2014/02/20 职场文书
主管竞聘书范文
2014/03/31 职场文书
《灰椋鸟》教学反思
2014/04/27 职场文书
英语专业求职信
2014/07/08 职场文书
2019XX公司员工考核管理制度!
2019/08/07 职场文书