用TensorFlow可视化卷积层的方法
来源:互联网 发布:淘宝邮箱注册页面 编辑:程序博客网 时间:2024/05/15 20:08
深度学习中对于卷积层的可视化可以帮助理解卷积层的工作原理与训练状态,然而卷积层可视化的方法不只一种。最简单的方法即直接输出卷积核和卷积后的filter通道,成为图片。然而也有一些方法试图通过反卷积(转置卷积)了解卷积层究竟看到了什么。
在TensorFlow中,即使是最简单的直接输出卷积层的方法,网上的讲解也参差不齐,David 9 今天要把可运行的方法告诉大家,以免大家受到误导。
废话少说,最简单的方法在此:
如果你有一个卷积层,我们以Tensorflow自带的cifar-10训练为例子:
- with tf.variable_scope('conv1')as scope:
- kernel = _variable_with_weight_decay('weights',
- shape=[5,5,3,64],
- stddev=5e-2,
- wd=0.0)
- conv = tf.nn.conv2d(images, kernel, [1,1,1,1], padding='SAME')
- biases = _variable_on_cpu('biases',[64], tf.constant_initializer(0.0))
- pre_activation = tf.nn.bias_add(conv, biases)
- conv1 = tf.nn.relu(pre_activation, name=scope.name)
- _activation_summary(conv1)
不出所料的话你一定会有以上代码,这是第一层卷积层conv1的TensorFlow流图定义。显然这里conv1对象是卷积层的激活输出。我们要做的就是直接可视化输出。在这个scope中加上如下代码:
- with tf.variable_scope('visualization'):
- # scale weights to [0 1], type is still float
- x_min = tf.reduce_min(kernel)
- x_max = tf.reduce_max(kernel)
- kernel_0_to_1 =(kernel - x_min) / (x_max - x_min)
- # to tf.image_summary format [batch_size, height, width, channels]
- kernel_transposed = tf.transpose(kernel_0_to_1,[3,0,1,2])
- # this will display random 3 filters from the 64 in conv1
- tf.summary.image('conv1/filters', kernel_transposed, max_outputs=3)
- layer1_image1 = conv1[0:1, :, :, 0:16]
- layer1_image1 = tf.transpose(layer1_image1, perm=[3,1,2,0])
- tf.summary.image("filtered_images_layer1", layer1_image1, max_outputs=16)
即总体变为:
- with tf.variable_scope('conv1')as scope:
- kernel = _variable_with_weight_decay('weights',
- shape=[5,5,3,64],
- stddev=5e-2,
- wd=0.0)
- conv = tf.nn.conv2d(images, kernel, [1,1,1,1], padding='SAME')
- biases = _variable_on_cpu('biases',[64], tf.constant_initializer(0.0))
- pre_activation = tf.nn.bias_add(conv, biases)
- conv1 = tf.nn.relu(pre_activation, name=scope.name)
- _activation_summary(conv1)
- with tf.variable_scope('visualization'):
- # scale weights to [0 1], type is still float
- x_min = tf.reduce_min(kernel)
- x_max = tf.reduce_max(kernel)
- kernel_0_to_1 =(kernel - x_min) / (x_max - x_min)
- # to tf.image_summary format [batch_size, height, width, channels]
- kernel_transposed = tf.transpose(kernel_0_to_1,[3,0,1,2])
- # this will display random 3 filters from the 64 in conv1
- tf.summary.image('conv1/filters', kernel_transposed, max_outputs=3)
- layer1_image1 = conv1[0:1, :, :, 0:16]
- layer1_image1 = tf.transpose(layer1_image1, perm=[3,1,2,0])
- tf.summary.image("filtered_images_layer1", layer1_image1, max_outputs=16)
加入的功能是在TensorBoard中随机显示3张卷积核,并且,显示16张卷积后的输出filter通道。
知道讲解的是,这里的tf.transpose()方法,是转置方法。
- tf.transpose(layer1_image1, perm=[3,1,2,0])
这句代码表示把第0维和第3维调换,因为图片输出函数
- tf.summary.image()
需要输入维度的格式是(batch数,长,宽,彩色通道),而刚才卷积输出得到的是(batch数,长,宽,卷积通道), 现在的彩色通道是应该是空,现在batch数应该是刚才卷积输出的彩色通道数。
总之加了以上visualization 的scope之后,就能实时跑了。亲测可用。输出样例如下:
参考文献:
- http://stackoverflow.com/questions/35759220/how-to-visualize-learned-filters-on-tensorflow
- https://github.com/tensorflow/tensorflow/issues/842
- https://github.com/yosinski/deep-visualization-toolbox
- https://github.com/tensorflow/tensorflow/issues/908
- https://medium.com/@awjuliani/visualizing-neural-network-layer-activation-tensorflow-tutorial-d45f8bf7bbc4
- https://gist.github.com/kukuruza/03731dc494603ceab0c5
source: http://nooverfit.com/wp/%E7%94%A8tensorflow%E5%8F%AF%E8%A7%86%E5%8C%96%E5%8D%B7%E7%A7%AF%E5%B1%82%E7%9A%84%E6%96%B9%E6%B3%95/#comment-900
- 用TensorFlow可视化卷积层的方法
- Tensorflow 卷积层
- TensorFlow 卷积层
- TensorFlow:简单的卷积层、池化层(采样层)示例
- cnn神经网络卷积层可视化
- caffemodel的卷积层可视化(Python接口)
- Tensorflow中卷积神经网络之卷积层
- 卷积神经网络CNN:Tensorflow实现(以及对卷积特征的可视化)
- 4用于cifar10的卷积神经网络-4.24Tensorflow中LRN层的工作原理以及添加方法
- tensorflow tutorials(九):卷积神经网络可视化
- 深度学习Caffe实战笔记(17)MATLAB实现卷积层卷积核权重的可视化
- 卷积神经网络的可视化
- 卷积的可视化学习
- tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图
- Tensorflow基础:卷积层和池化层
- Tensorflow的可视化操作
- TensorFlow的可视化工具
- 学习用tensorflow实现卷积神经网络中的卷积层随笔
- 不做手机控
- Android Studio之bash: gradlew: command not found
- J2EE框架(四)核心设计模式
- 实现可拖拽移动的悬浮按钮
- SpringBoot+AngularJS导出数据库数据到excel
- 用TensorFlow可视化卷积层的方法
- Spring AOP底层实现- JDK动态代理和CGLIB动态代理
- 浅谈 MVP in Android
- css父元素透明度(opacity)对子元素的影响
- 08_c++访问控制和继承
- 工具类
- vivado(1)——创建工程
- SimpleDateFormat类时间转换BUG
- a'a