2016-03-02 41 views
9

与Caffe框架类似,可以在CNNs训练期间观察学习过滤器,并通过输入图像进行卷积,我想知道是否可以用TensorFlow做同样的事情?如何在张量流中可视化学习过滤器

一个来自Caffe例如可以在这个链接查看:

http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb

感谢您的帮助!

+0

参见[如何可视化tensorflow卷积过滤器?](http://stackoverflow.com/q/39361943/562769) –

+0

[我如何可视化重量(变量)在cnn中的Tensorflow?]( http://stackoverflow.com/questions/33783672/how-can-i-visualize-the-weightsvariables-in-cnn-in-tensorflow) –

+0

你可以使用[tensorflow调试器](https://github.com/ ericjang/tdb)工具 – fabrizioM

回答

10

要看到的只是几个CONV1过滤器Tensorboard,您可以使用此代码(它为cifar10)

# this should be a part of the inference(images) function in cifar10.py file 

# conv1 
with tf.variable_scope('conv1') as scope: 
    kernel = _variable_with_weight_decay('weights', shape=[5, 5, 3, 64], 
             stddev=1e-4, 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)) 
    bias = tf.nn.bias_add(conv, biases) 
    conv1 = tf.nn.relu(bias, 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.image_summary('conv1/filters', kernel_transposed, max_images=3) 

我也写了一个简单gist在网格中显示所有64个CONV1过滤器。

+0

您是否将这段代码放入cifar 10脚本的“推理”函数中? – Twimnox

+0

我没有,但这是一个好主意:)我刚刚更新了代码 – etoropov

+0

Works!谢谢!我在“convert_image_dtype”时出错,所以我将'tf.image.convert_image_dtype(kernel_0_to_1,dtype = tf.uint8)'改为'kernel_0_to_255_uint8 = tf.cast(kernel_0_to_1,dtype = tf.float32)'。 – Twimnox