安裝GraphViz以供python調用

本文目錄

問題出現

最近在windows7上利用pycaffe的draw_net.py進行.prototxt網絡結構配置文件的繪圖,結果出現瞭如下錯誤:

InvocationException: GraphViz’s executables not found

經過各種google,總算找到了解決辦法,原來,GraphViz是一個軟件(好吧,請原來我的無知),可以下載安裝,我這裏之所以出現這個錯誤,可能是兩個原因導致的

  • 電腦沒有安裝GraphViz
  • 電腦安裝了GraphViz,但是,並沒有將它的bin目錄加入到環境變量PATH中,所以,其他應用程序無法找到它

仔細想想,自己之前好像確實沒有安裝過它,所以,果斷判斷爲是第一個原因,下面開始安裝GraphViz

下面這段話引用Stackoverflow網站關於該問題的一個回答,本文的解決方案主要來自於它
這裏寫圖片描述

安裝Graphviz

下載Graphviz

下載地址:graphviz官網

  • 進入下載頁,找到Download
    這裏寫圖片描述

  • 點擊Download,在彈出的下載頁中找到windows的下載鏈接,並點擊
    這裏寫圖片描述

  • 在彈出的windows版本下載界面中選擇需要下載的版本,這裏我選擇了.zip文件
    這裏寫圖片描述

解壓縮文件

  • 文件下載完成後,將它解壓到某一路徑下,例如,我將它解壓到了D:\software\caffe-vs\graphviz-2.38,下圖展示瞭解壓後的文件結構,可以看到,解壓後的文件中只包含一個Release文件夾
    這裏寫圖片描述

添加環境變量

  • 將剛剛解壓縮得到的文件夾下的Release下的bin文件夾路徑添加到系統的環境變量PATH中,這樣,其他程序便可以找到graphviz了
  • 例如,按照上面的解壓縮路徑,需要將路徑:D:\software\caffe-vs\graphviz-2.38\release\bin添加到環境變量PATH

重啓python IDE

完成上述所有操作後,重啓python IDE,發現draw_net.py終於可以使用了!

注:下面是’draw_net.py’的代碼,來自於’caffe/python/draw_net.py’
使用方法:

  • ‘draw_net.py’使用方法(在cmd中輸入如下命令)
python /caffe_root/python/draw_net.py file.prototxt file.jpg
  • ‘draw_net.py’代碼
#!/usr/bin/env python
"""
Draw a graph of the net architecture.
"""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from google.protobuf import text_format

import caffe
import caffe.draw
from caffe.proto import caffe_pb2


def parse_args():
    """Parse input arguments
    """

    parser = ArgumentParser(description=__doc__,
                            formatter_class=ArgumentDefaultsHelpFormatter)

    parser.add_argument('input_net_proto_file',
                        help='Input network prototxt file')
    parser.add_argument('output_image_file',
                        help='Output image file')
    parser.add_argument('--rankdir',
                        help=('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')

    args = parser.parse_args()
    return args


def main():
    args = parse_args()
    net = caffe_pb2.NetParameter()
    text_format.Merge(open(args.input_net_proto_file).read(), net)
    print('Drawing net to %s' % args.output_image_file)
    caffe.draw.draw_net_to_file(net, args.output_image_file, args.rankdir)


if __name__ == '__main__':
    main()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章