Caffe: Draw Net Problem (%d format: a number is required)

Created on 15 Oct 2015  路  3Comments  路  Source: BVLC/caffe

This seem similar to #1709. In the master branch, when running ./python/draw_net.py, I get this traceback. Seems like this is an area that regresses frequently, what would a good test look like to prevent this from happening again?

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 <module>
    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

Most helpful comment

change the corresponding function as following in 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

All 3 comments

change the corresponding function as following in 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

Seems like this is an area that regresses frequently, what would a good test look like to prevent this from happening again?

A minimal but helpful improvement would be to add a Python test that imports caffe.draw and calls draw_net() to check that it at least runs without exception. That could be a good PR to encourage further testing.

I have a similar problem for the Pooling layer type. I fixed and published #5010.

Was this page helpful?
0 / 5 - 0 ratings