keras得到每层的系数方式


Posted in Python onJune 15, 2020

使用keras搭建好一个模型,训练好,怎么得到每层的系数呢:

weights = np.array(model.get_weights())
print(weights)
print(weights[0].shape)
print(weights[1].shape)

这样系数就被存放到一个np中了。

补充知识:使用keras框架编写的深度模型 输出及每一层的特征可视化

使用训练好的模型进行预测的时候,为分析效果,通常需要对特征提取过程中的特征映射做可视化操作

本文以keras为例,对特征可视化操作进行详解。

一、首先,对模型的最后输出层进行特征可视化

from keras import models
#使用matlpotlib模块进行绘图的操作
import matplotlib.pylot as plt
#images是一个batch的输入图像,batch_input[batch图像数量,尺寸高,尺寸宽,3(rgb通道数量)]
#model是训练好的模型
#model = load_model('path')
nb_images = len(images)
batch_input = np.zeros((nb_images, net_h, net_w, 3))

# preprocess the input
for i in range(nb_images):
 batch_input[i] = preprocess_input(images[i], net_h, net_w)  

# run the prediction
#batch_output为一个样本的所有通道输出特征映射,本文应用特征金字塔结构,有三个维度的特征提取层
#batch_output[0]是第一个维度的特征提取层所有通道的输出特征映射,四维,本文例子中为[1, 52, 52, 72]
#[一个样本,尺寸,尺寸,通道数]

#也可以是batch_output = model.predict(batch_input)
batch_output = model.predict_on_batch(batch_input)
batch_boxes = [None]*nb_images
print(batch_output[0].shape)
#display feature map

#下面为归一化到0-255空间内
xx = batch_output[0]
max = np.max(xx)
print(max,"max value is :")
X_output = X_output .astype("float32") / max * 255

#下面的30为第30个通道
X_output = xx[0,:,:,30]

#使用matplotlib显示图像
plt.figure()
plt.imshow(X_output, cmap='viridis')
plt.show()

#输出结果

原始图像

keras得到每层的系数方式

输出层的特征可视化

keras得到每层的系数方式

二、可视化某一层的特征映射

from keras import backend as k
from keras import models
import matplotlib.pylot as plt
model = load_model('...')
layer_1 =k.function([model.layers[0].input], [model.layers[1].output])

#第2个model,layers[]改成你想显示的层数
f1 = layer_1[input_image][0]
f1.image = f1[0,:,:,channel]
plt,matshow(f1.image, camp='viridis')
plt.show()

示例:

from keras import models
import matplotlib.pylot as plt
from keras import backend as k
#images是一个batch的输入图像,batch_input[batch图像数量,尺寸高,尺寸宽,3(rgb通道数量)]
#model是训练好的模型
#model = load_model('path')
nb_images = len(images)
batch_input = np.zeros((nb_images, net_h, net_w, 3))

# preprocess the input
for i in range(nb_images):
 batch_input[i] = preprocess_input(images[i], net_h, net_w)  
#display feature map

#可视化第一层的特征映射
layer_1 = K.function([model.layers[0].input], [model.layers[1].output])
f1 = layer_1([batch_input])[0]
print(f1.shape)
max = np.max(f1)
f1 =f1.astype("float32") / max * 255
plt.figure()

#显示第一层网络前5个通道的特征映射
for i in range(5):
 plt.subplot(2, 3, i+1)
 plt.imshow(f1[0,:,:,i], cmap='viridis')
plt.show()

输出结果:

keras得到每层的系数方式

以上这篇keras得到每层的系数方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python获取脚本所在目录的正确方法
Apr 15 Python
python实现颜色rgb和hex相互转换的函数
Mar 19 Python
Python 实现 贪吃蛇大作战 代码分享
Sep 07 Python
Python利用递归和walk()遍历目录文件的方法示例
Jul 14 Python
详解python 拆包可迭代数据如tuple, list
Dec 29 Python
tensorflow实现对图片的读取的示例代码
Feb 12 Python
Python Pywavelet 小波阈值实例
Jan 09 Python
Python修改文件往指定行插入内容的实例
Jan 30 Python
Python datetime包函数简单介绍
Aug 28 Python
SELENIUM自动化模拟键盘快捷键操作实现解析
Oct 28 Python
Django之全局使用request.user.username的实例详解
May 14 Python
python压包的概念及实例详解
Feb 17 Python
Python类及获取对象属性方法解析
Jun 15 #Python
在Keras中实现保存和加载权重及模型结构
Jun 15 #Python
简单了解Python多态与属性运行原理
Jun 15 #Python
Python类super()及私有属性原理解析
Jun 15 #Python
Keras 实现加载预训练模型并冻结网络的层
Jun 15 #Python
Python StringIO及BytesIO包使用方法解析
Jun 15 #Python
Python smtp邮件发送模块用法教程
Jun 15 #Python
You might like
十天学会php之第十天
2006/10/09 PHP
PHP常用函数小技巧
2008/09/11 PHP
php之对抗Web扫描器的脚本技巧
2008/10/01 PHP
PHP 存储文本换行实现方法
2010/01/05 PHP
PHP中几种常见的超时处理全面总结
2012/09/11 PHP
解决PhpMyAdmin中导入2M以上大文件限制的方法分享
2014/06/06 PHP
php实现的Timer页面运行时间监测类
2014/09/24 PHP
php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证
2016/05/04 PHP
JSON PHP中,Json字符串反序列化成对象/数组的方法
2018/05/31 PHP
laravel 数据迁移与 Eloquent ORM的实现方法
2019/04/12 PHP
javascript Array.remove() 数组删除
2009/08/06 Javascript
js中if语句的几种优化代码写法
2011/03/12 Javascript
jquery.blockUI.js上传滚动等待效果实现思路及代码
2013/03/18 Javascript
理解js回收机制通俗易懂版
2016/02/29 Javascript
JS简单循环遍历json数组的方法
2016/04/22 Javascript
利用JS轻松实现获取表单数据
2016/12/06 Javascript
JS Input里添加小图标的两种方法
2017/11/11 Javascript
Vue v-model组件封装(类似弹窗组件)
2020/01/08 Javascript
python简单读取大文件的方法
2016/07/01 Python
Python单例模式的两种实现方法
2017/08/14 Python
Python 异常处理的实例详解
2017/09/11 Python
安装python3的时候就是输入python3死活没有反应的解决方法
2018/01/24 Python
解决pycharm 误删掉项目文件的处理方法
2018/10/22 Python
Python 创建新文件时避免覆盖已有的同名文件的解决方法
2018/11/16 Python
Python中psutil的介绍与用法
2019/05/02 Python
Python实现二叉树前序、中序、后序及层次遍历示例代码
2019/05/18 Python
Python 脚本拉取 Docker 镜像问题
2019/11/10 Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
2020/04/20 Python
python编写softmax函数、交叉熵函数实例
2020/06/11 Python
德国机场停车位比较和预订网站:Ich-parke-billiger
2018/01/08 全球购物
新闻专业学生的自我评价
2014/02/13 职场文书
代理人委托书
2014/09/16 职场文书
2015年网管个人工作总结
2015/05/22 职场文书
2016年“世界环境日”校园广播稿
2015/12/18 职场文书
Pyqt5将多个类组合在一个界面显示的完整示例
2021/09/04 Python
Nginx防盗链与服务优化配置的全过程
2022/01/18 Servers