Search code examples
pythonubuntu-12.04pydotcaffe

Drawing network in Caffe causes pydot to throw End of Line errors


So I just pulled the latest revision of Caffe from the master branch, and went through all the initialization steps. As a quick test, I was trying to run the python/draw_net.py script provided, in order to visualize the MNIST Autoencoder example network. On executing the following command:

./python/draw_net.py examples/mnist/mnist_autoencoder.prototxt trial_viz.png

Pydot complained, and threw the following error:

Drawing net to trial_viz.png
Traceback (most recent call last):
  File "./python/draw_net.py", line 44, in <module>
    main()
  File "./python/draw_net.py", line 40, in main
    caffe.draw.draw_net_to_file(net, args.output_image_file, args.rankdir)
  File "/home/username/3rdparty/caffe/python/caffe/draw.py", line 165, in draw_net_to_file
    fid.write(draw_net(caffe_net, rankdir, ext))
  File "/home/username/3rdparty/caffe/python/caffe/draw.py", line 156, in draw_net
    return get_pydot_graph(caffe_net, rankdir).create(format=ext)
  File "/usr/lib/pymodules/python2.7/pydot.py", line 1796, in create
    status, stderr_output) )
pydot.InvocationException: Program terminated with status: 1. stderr follows: Warning: /tmp/tmpjqPQBC:5: string ran past end of line
Error: /tmp/tmpjqPQBC:6: syntax error near line 6
context:  >>> ( <<< Sigmoid)" [shape=record, style=filled, fillcolor="#6495ED"];
Warning: /tmp/tmpjqPQBC:6: ambiguous "6495ED" splits into two names: "6495" and "ED"
Warning: /tmp/tmpjqPQBC:6: string ran past end of line
Warning: /tmp/tmpjqPQBC:9: string ran past end of line
Warning: /tmp/tmpjqPQBC:10: string ran past end of line
Warning: /tmp/tmpjqPQBC:12: string ran past end of line
Warning: /tmp/tmpjqPQBC:13: ambiguous "6495ED" splits into two names: "6495" and "ED"
Warning: /tmp/tmpjqPQBC:13: string ran past end of line
Warning: /tmp/tmpjqPQBC:14: string ran past end of line
Warning: /tmp/tmpjqPQBC:15: string ran past end of line
Warning: /tmp/tmpjqPQBC:17: string ran past end of line
Warning: /tmp/tmpjqPQBC:18: ambiguous "6495ED" splits into two names: "6495" and "ED"

I see many more Warning messages like the ones shown above, and my error log was getting too big, so I didn't post the entire log. This post, seems to be seeing the same error as me, so I tried to replicate their solution, and changed all the strings in the get_pydot_graph() method in draw.py to raw strings. But that didn't seem to work.

Any suggestions on how I can sort this issue?

Thanks!! :)


Solution

  • I think the key is in the determine_node_label_by_layertype function. This is a block of code that should look something like this (or at least it does in my current version of the repository):

    def determine_node_label_by_layertype(layer, layertype, rankdir):
    """Define node label based on layer type
    """
    
        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'
    

    Replace the separater = '\n' with separater = r"\n" and it seemed to work for me.