Caffe学习笔记(2)--spyder 下绘制网络结构

直接使用Caffe中的python脚本绘制网络结构的方法请参照链接:http://www.cnblogs.com/denny402/p/5106764.html。因为本人在学习caffe 的时候希望在anaconda的环境下区编辑,所以这里介绍如何在spyder中编写python 程序来绘制网络结构图。程序如下:


#将caffe包含到路径中
import sys
caffe_home = '/home/kelly/DL/caffe-master/'
sys.path.insert(0, caffe_home + 'python')
#引用库
import caffe
import caffe.draw
from caffe.proto import caffe_pb2
from google.protobuf import text_format

'''
#核心函数的解释
def draw_net_to_file(caffe_net, filename, rankdir='LR', phase=None):
    """Draws a caffe net, and saves it to file using the format given as the
    file extension. Use '.raw' to output raw text that you can manually feed
    to graphviz to draw graphs.

    Parameters
    ----------
    caffe_net : a caffe.proto.caffe_pb2.NetParameter protocol buffer.
    filename : string
        The path to a file where the networks visualization will be stored.
    rankdir : {'LR', 'TB', 'BT'}
        Direction of graph layout.
    phase : {caffe_pb2.Phase.TRAIN, caffe_pb2.Phase.TEST, None} optional
        Include layers from this network phase.  If None, include all layers.
        (the default is None)
    """
'''
#参数配置 具体可参照上方的注释
# 网络结构文件
input_net_proto_file = '/home/kelly/DL/caffe-master/examples/mnist/lenet.prototxt'
# 输出图片文件
output_image_file = '/home/kelly/DL/caffe-master/cnn_net/lenet-5.jpg'
# 网络结构排列方式:LR、TB、RL等
rankdir = 'TB'
#读取网络
net = caffe_pb2.NetParameter()
text_format.Merge(open(input_net_proto_file).read(), net)
#绘制网络
print('Drawing net to %s' % output_image_file)
caffe.draw.draw_net_to_file(net, output_image_file, rankdir) 
print('done...')

运行程序,网络结构图变保存在相应的路径下啦!

你可能感兴趣的:(caffe学习笔记,python,caffe,spyder,网络结构)