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引用(import)文件夹下的py文件的方法
Aug 26 Python
Python迭代用法实例教程
Sep 08 Python
python email smtplib模块发送邮件代码实例
Apr 26 Python
django_orm查询性能优化方法
Aug 20 Python
元组列表字典(莫烦python基础)
Apr 03 Python
pandas 如何分割字符的实现方法
Jul 29 Python
Python分割训练集和测试集的方法示例
Sep 19 Python
使用Python对Dicom文件进行读取与写入的实现
Apr 20 Python
解决pycharm编辑区显示yaml文件层级结构遇中文乱码问题
Apr 27 Python
Python使用re模块验证危险字符
May 21 Python
python实现在列表中查找某个元素的下标示例
Nov 16 Python
Django项目配置Memcached和Redis, 缓存选择哪个更有优势
Apr 06 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
40年前的这部特摄片恐龙特级克塞号80后的共同回忆
2020/03/08 日漫
PHP网页游戏学习之Xnova(ogame)源码解读(十六)
2014/06/30 PHP
PHP实现搜索相似图片
2015/09/22 PHP
使用jQuery的将桌面应用程序引入浏览器
2010/11/19 Javascript
js中onload与onunload的使用示例
2013/08/25 Javascript
firefox下jquery ajax返回object XMLDocument处理方法
2014/01/26 Javascript
JavaScript常用脚本汇总(一)
2015/03/04 Javascript
JS上传组件FileUpload自定义模板的使用方法
2016/05/10 Javascript
基于JS实现类似支付宝支付密码输入框
2016/09/02 Javascript
详解微信小程序 wx.uploadFile 的编码坑
2017/01/23 Javascript
Bootstrap 3浏览器兼容性问题及解决方案
2017/04/11 Javascript
Webpack实现按需打包Lodash的几种方法详解
2017/05/08 Javascript
详解vue-cli项目中用json-sever搭建mock服务器
2017/11/02 Javascript
Vue+Vux项目实践完整代码
2017/11/30 Javascript
vue 使用Jade模板写html,stylus写css的方法
2018/02/23 Javascript
浅谈VUE监听窗口变化事件的问题
2018/02/24 Javascript
解决Vue打包上线之后部分CSS不生效的问题
2019/11/12 Javascript
python实现中文分词FMM算法实例
2015/07/10 Python
Python配置mysql的教程(推荐)
2017/10/13 Python
python中urlparse模块介绍与使用示例
2017/11/19 Python
Python实现的文本对比报告生成工具示例
2018/05/22 Python
Python3使用pandas模块读写excel操作示例
2018/07/03 Python
Python生成MD5值的两种方法实例分析
2019/04/26 Python
浅谈Python3中strip()、lstrip()、rstrip()用法详解
2019/04/29 Python
numpy库与pandas库axis=0,axis= 1轴的用法详解
2019/05/27 Python
python 输出列表元素实例(以空格/逗号为分隔符)
2019/12/25 Python
CSS3过渡transition效果实例介绍
2016/05/03 HTML / CSS
HTML页面中添加Canvas标签示例
2015/01/01 HTML / CSS
美国滑雪和滑雪板商店:Buckman
2018/03/03 全球购物
采用冷却技术的超自然舒适度:GhostBed床垫
2018/09/18 全球购物
给海归自荐信的建议
2013/12/13 职场文书
求职自荐信的格式
2014/04/07 职场文书
如何写求职信
2014/05/24 职场文书
文员岗位职责范本
2015/04/16 职场文书
golang elasticsearch Client的使用详解
2021/05/05 Golang
python中 Flask Web 表单的使用方法
2022/05/20 Python