前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于DeepStream的CNN的可视化理解

基于DeepStream的CNN的可视化理解

作者头像
sparkexpert
发布2018-01-09 11:31:20
1.2K0
发布2018-01-09 11:31:20
举报

  对CNN进行可视化是一项重要的工作,有助于理解深度学习模型的黑箱里面到底是什么东西。

有个著名的工具是:deep-visualization-toolbox里面还实现了对各种类别的展示,同时还有个遮挡的实验,令人印象深刻。

  此外,只用tensorflow的deepstream来学习一下CNN的模型到底都有哪些东西。代码可以看:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/deepdream/deepdream.ipynb。

其官网介绍如下:

This directory contains Jupyter notebook that demonstrates a number of Convolutional Neural Network image generation techniques implemented with TensorFlow:

  • visualizing individual feature channels and their combinations to explore the space of patterns learned by the neural network (see GoogLeNet and VGG16 galleries)
  • embedding TensorBoard graph visualizations into Jupyter notebooks
  • producing high-resolution images with tiled computation (example)
  • using Laplacian Pyramid Gradient Normalization to produce smooth and colorful visuals at low cost
  • generating DeepDream-like images with TensorFlow

从官网说明可以看出:DeepStream主要用于查看现有谷歌基于imagenet进行建立模型的内部特征。并可以产生高精度的图像。

代码语言:javascript
复制
def render_deepdream(t_obj, img0=img_noise,
                     iter_n=10, step=1.5, octave_n=4, octave_scale=1.4):
    t_score = tf.reduce_mean(t_obj) # defining the optimization objective
    t_grad = tf.gradients(t_score, t_input)[0] # behold the power of automatic differentiation!

    # split the image into a number of octaves
    img = img0
    octaves = []
    for i in range(octave_n-1):
        hw = img.shape[:2]
        lo = resize(img, np.int32(np.float32(hw)/octave_scale))
        hi = img-resize(lo, hw)
        img = lo
        octaves.append(hi)
    
    # generate details octave by octave
    for octave in range(octave_n):
        if octave>0:
            hi = octaves[-octave]
            img = resize(img, hi.shape[:2])+hi
        for i in range(iter_n):
            g = calc_grad_tiled(img, t_grad)
            img += g*(step / (np.abs(g).mean()+1e-7))
            print('.',end = ' ')
        clear_output()
        showarray(img/255.0)

从上面的deepstream代码可以看出,针对某一个颜色通道,进行逐层展示,其算法的基本介绍如下。

Making the "dream" images is very simple. Essentially it is just a gradient ascent process that tries to maximize the L2 norm of activations of a particular DNN layer. Here are a few simple tricks that we found useful for getting good images:

  • offset image by a random jitter
  • normalize the magnitude of gradient ascent steps
  • apply ascent across multiple scales (octaves)

First we implement a basic gradient ascent step function, applying the first two tricks:

当然DeepDream的测试核心步骤如下:

代码语言:javascript
复制
if __name__ == '__main__':    
    # creating TensorFlow session and loading the model
    graph = tf.Graph()
    sess = tf.InteractiveSession(graph=graph)
    with tf.gfile.FastGFile(model_fn, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    t_input = tf.placeholder(np.float32, name='input') # define the input tensor
    imagenet_mean = 117.0
    t_preprocessed = tf.expand_dims(t_input-imagenet_mean, 0)
    tf.import_graph_def(graph_def, {'input':t_preprocessed})
    
    layers = [op.name for op in graph.get_operations() if op.type=='Conv2D' and 'import/' in op.name]
    print(layers)
    feature_nums = [int(graph.get_tensor_by_name(name+':0').get_shape()[-1]) for name in layers]
    
    print('Number of layers', len(layers))
    print('Total number of feature channels:', sum(feature_nums))
    
    # Picking some internal layer. Note that we use outputs before applying the ReLU nonlinearity
    # to have non-zero gradients for features with negative initial activations.
    layer = 'mixed4d_3x3_bottleneck_pre_relu'
    channel = 139 # picking some feature channel to visualize
    
    # start with a gray image with a little noise
    img_noise = np.random.uniform(size=(224,224,3)) + 100.0
    
    resize = tffunc(np.float32, np.int32)(resize)
    
    
#     render_naive(T(layer)[:,:,:,channel], img_noise)
#     render_multiscale(T(layer)[:,:,:,channel], img_noise)
    img0 = PIL.Image.open('images/tiger.jpg')    
    img0 = np.float32(img0)
#     render_deepdream(tf.square(T('mixed4c')), img0)
    render_deepdream(T(layer)[:,:,:,139], img0)

上面可以基于某个图片上进行绘制逐层的特征抽象,默认只绘制4层,如139层为花朵的特征层。其逐层展示结果如下:

这种展示效果还是非常好的。当然看到说可以对上面的特征层可以再进行排序的,可能会产生意想不到的效果。以后有时间再测试一下。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年07月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档