【深度学习】关于解决caffe中draw_net无法使用的问题

  • 问题1 Couldnt import dot_parser问题
    • 原因分析
    • 解决方案
    • 参考资料
  • 问题2 pydotInvocationException GraphVizs executables not found
    • 原因分析
    • 解决方案
    • 参考资料
  • 问题3 Draw Net Problem d format a number is required 3201
    • 原因分析
    • 参考资料
  • 使用方法与效果


问题1: Couldnt import dot_parser问题

1. 原因分析

谷歌后的结果是dot_parser所依赖的pyparsing的版本太高了,而dot_parser一直没有修正,结果造成了如下问题。

2. 解决方案

将pyparsing降级到2.0以下就可以了:

$ sudo pip uninstall pyparsing
$ sudo pip install -Iv https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz#md5=9be0fcdcc595199c646ab317c1d9a709
$ sudo pip install pydot

注意,sudo权限不可少。

3. 参考资料

http://stackoverflow.com/questions/15951748/pydot-and-graphviz-error-couldnt-import-dot-parser-loading-of-dot-files-will


问题2: pydot.InvocationException: GraphVizs executables not found

具体的log如下:

File "coco.py", line 18, in graph.write_pdf("iris.pdf")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pydot.py", line 1602, in lambda path, f=frmt, prog=self.prog : self.write(path, format=f, prog=prog))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pydot.py", line 1696, in write dot_fd.write(self.create(prog, format))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pydot.py", line 1727, in create 'GraphViz\'s executables not found' ) pydot.InvocationException: GraphViz's executables not found

1. 原因分析

缺少GraphViz,虽然python中有这个包,但其实只是包装以下,让你好用的,真正的GraphViz是一个二进制的binary,还需要你自己去装。

2. 解决方案

使用apt-get包管理器安装一下就好了。

$ sudo apt-get install graphviz

3. 参考资料:

http://stackoverflow.com/questions/27666846/pydot-invocationexception-graphvizs-executables-not-found


问题3: Draw Net Problem (%d format: a number is required) #3201

具体的log如下:

root@0e28b3340f95:/opt/caffe# python ./python/draw_net.py ./foo.proto /data/net.png
libdc1394 error: Failed to initialize libdc1394
Couldn't import dot_parser, loading of dot files will not be possible.
Drawing net to /data/net.png
Traceback (most recent call last):
  File "./python/draw_net.py", line 45, in 
    main()
  File "./python/draw_net.py", line 41, in main
    caffe.draw.draw_net_to_file(net, args.output_image_file, args.rankdir)
  File "/opt/caffe/python/caffe/draw.py", line 213, in draw_net_to_file
    fid.write(draw_net(caffe_net, rankdir, ext))
  File "/opt/caffe/python/caffe/draw.py", line 195, in draw_net return
    get_pydot_graph(caffe_net, rankdir).create(format=ext)
  File "/opt/caffe/python/caffe/draw.py", line 142, in get_pydot_graph
    node_label = get_layer_label(layer, rankdir)
  File "/opt/caffe/python/caffe/draw.py", line 89, in get_layer_label
    layer.convolution_param.pad)
TypeError: %d format: a number is required, not RepeatedScalarFieldContainer

1. 原因分析

这个应该是caffe内部设计的一个bug,我在caffe的github代码仓库的issue中找到了相关的解释。具体的原因我还不是很清楚,但需要修改$CAFFE_ROOT/python/caffe/draw.py源代码,请注意红色部分代码:

def get_layer_label(layer, rankdir):
    """Define node label based on layer type.

    Parameters
    ----------
    layer : ?
    rankdir : {'LR', 'TB', 'BT'}
        Direction of graph layout.

    Returns
    -------
    string :
        A label for the current layer
    """

    if rankdir in ('TB', 'BT'):
        # If graph orientation is vertical, horizontal space is free and
        # vertical space is not; separate words with spaces
        separator = ' '
    else:
        # If graph orientation is horizontal, vertical space is free and
        # horizontal space is not; separate words with newlines
        separator = '\\n'

    if layer.type == 'Convolution' or layer.type == 'Deconvolution':
        # Outer double quotes needed or else colon characters don't parse
        # properly
        param = layer.convolution_param
        node_label = '"%s%s(%s)%skernel size: %d%sstride: %d%spad: %d"' %\
                    (layer.name,
                     separator,
                     layer.type,
                     separator,
                     layer.convolution_param.kernel_size[0] if len(layer.convolution_param.kernel_size._values) else 1,
                     separator,
                     layer.convolution_param.stride[0] if len(layer.convolution_param.stride._values) else 1,
                     separator,
                     layer.convolution_param.pad[0] if len(layer.convolution_param.pad._values) else 0)
    elif layer.type == 'Pooling':
        pooling_types_dict = get_pooling_types_dict()
        node_label = '"%s%s(%s %s)%skernel size: %d%sstride: %d%spad: %d"' %\
                     (layer.name,
                      separator,
                      pooling_types_dict[layer.pooling_param.pool],
                      layer.type,
                      separator,
                      layer.pooling_param.kernel_size,
                      separator,
                      layer.pooling_param.stride,
                      separator,
                      layer.pooling_param.pad)
    else:
        node_label = '"%s%s(%s)"' % (layer.name, separator, layer.type)
    return node_label

2. 参考资料:

https://github.com/BVLC/caffe/issues/3201


使用方法与效果

在caffe中可以使用draw_net.py轻松地绘制卷积神经网络(CNN,Convolutional Neural Networks)的架构图。这个工具对于我们理解、学习甚至查错都有很大的帮助。

draw_net.py的使用方法如下:

usage: draw_net.py [-h] [--rankdir RANKDIR]
                   input_net_proto_file output_image_file

Draw a graph of the net architecture.

positional arguments:
  input_net_proto_file  Input network prototxt file
  output_image_file     Output image file

optional arguments:
  -h, --help            show this help message and exit
  --rankdir RANKDIR     One of TB (top-bottom, i.e., vertical), RL (right-
                        left, i.e., horizontal), or another valid dot option;
                        see
                        http://www.graphviz.org/doc/info/attrs.html#k:rankdir
                        (default: LR)

其中,–rankdir选项确定了输出图片的摆放位置,如TB (Top-Bottom) 就是网络从上到下拜访,同理RL (Right-Left)就是从右向左,LR (Left-Right) 就是从左向右摆放。默认使用的是LR方式。

下面的代码输出了卷积神经网络经典的LeNet,LeNet可以说是深度学习的一个奠基石一样的东西,一个绕不过去的东西。建议大家有时间多多去阅读LeCun在98年发表的这篇文章。

$ cd caffe/python/
$ python draw_net.py --rankdir TB ../examples/mnist/lenet.prototxt ../examples/mnist/lenet_structure.jpg

输出结果如下所示:

【深度学习】关于解决caffe中draw_net无法使用的问题_第1张图片


2016/1/4 于 浙大

你可能感兴趣的:(计算机视觉,深度学习,深度学习,caffe)