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中类的继承代码实例
Oct 28 Python
用Python实现一个简单的多线程TCP服务器的教程
May 05 Python
Python实现将数据库一键导出为Excel表格的实例
Dec 30 Python
python简单商城购物车实例代码
Mar 15 Python
matplotlib.pyplot画图 图片的二进制流的获取方法
May 24 Python
python保存文件方法小结
Jul 27 Python
5分钟 Pipenv 上手指南
Dec 20 Python
Python3简单实现串口通信的方法
Jun 12 Python
详解Django CAS 解决方案
Oct 30 Python
Python3通过chmod修改目录或文件权限的方法示例
Jun 08 Python
pytorch中index_select()的用法详解
Jan 06 Python
4种方法python批量修改替换列表中元素
Apr 07 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
磨咖啡豆的密诀
2021/03/03 冲泡冲煮
php class中self,parent,this的区别以及实例介绍
2013/04/24 PHP
php实现webservice实例
2014/11/06 PHP
Codeigniter购物车类不能添加中文的解决方法
2014/11/29 PHP
php实现俄罗斯乘法实例
2015/03/07 PHP
php实现的debug log日志操作类实例
2016/07/12 PHP
js如何获取file控件的完整路径具体实现代码
2013/05/15 Javascript
jQuery实现可收缩展开的级联菜单实例代码
2013/11/27 Javascript
jQuery中noconflict函数的实现原理分解
2015/02/03 Javascript
Javascript中的arguments对象
2016/06/20 Javascript
详解Node.js实现301、302重定向服务
2017/04/07 Javascript
jQuery Ajax自定义分页组件(jquery.loehpagerv1.0)实例详解
2017/05/01 jQuery
jQuery实现radio第一次点击选中第二次点击取消功能
2017/05/15 jQuery
浅谈原型对象的常用开发模式
2017/07/22 Javascript
js如何编写简单的ajax方法库
2017/08/02 Javascript
一个简易时钟效果js实现代码
2020/03/25 Javascript
浅谈从React渲染流程分析Diff算法
2018/09/08 Javascript
JS实现简单的文字无缝上下滚动功能示例
2019/06/22 Javascript
原生js实现自定义滚动条
2021/01/20 Javascript
[48:54]VGJ.T vs infamous Supermajor小组赛D组败者组第一轮 BO3 第二场 6.3
2018/06/04 DOTA
python调用cmd复制文件代码分享
2013/12/27 Python
浅析Git版本控制器使用
2017/12/10 Python
Django重置migrations文件的方法步骤
2019/05/01 Python
3种python调用其他脚本的方法
2020/01/06 Python
基于python3生成标签云代码解析
2020/02/18 Python
Python 自由定制表格的实现示例
2020/03/20 Python
美国一站式电动和手动工具商店:International Tool
2020/11/26 全球购物
历史专业毕业生的自我鉴定
2013/11/15 职场文书
酒店个人培训自我鉴定
2013/12/11 职场文书
退休教师欢送会主持词
2014/03/31 职场文书
中学清明节活动总结
2014/07/04 职场文书
公务员年度考核登记表个人总结
2015/02/12 职场文书
放射科岗位职责
2015/02/14 职场文书
pytorch中Schedule与warmup_steps的用法说明
2021/05/24 Python
教你漂亮打印Pandas DataFrames和Series
2021/05/29 Python
mybatis 解决从列名到属性名的自动映射失败问题
2021/06/30 Java/Android