用过caffe的都知道,caffe的网络结构可以用NetScope可视化,但caffe2目前没有这么直接的工具,为了可视化网络结构,有两个思路。
1、对于层数较少的网络,可以输出网络结构,命令行不方便查看的话可以输出到文档
f=open('/home/usr/proto.txt','w')
print(net.Proto(),file=f)
f.close()
2、对于层数较多的网络,只是输出成文字显然很不方便
(1)先安装需要的包
pip install graphviz
sudo apt-get install graphviz
pip install pydot
用别的安装方法可能一开始也不会报错,但后面可能会出现一些问题,比如我的代码就在调用pydot中write_png方法时卡了一天一夜都动不了,按上面的安装方法就能正常运行。
https://blog.youkuaiyun.com/sinat_37998852/article/details/80507536
(2)再用以下代码让网络结构以图形的方式呈现
- 显示全部参数和operators
from caffe2.python import net_drawer
from IPython import display
graph=net_drawer.GetPydotGraph(net.Proto(),rankdir="LR") #LR表示从左到右,也可选择TB —— 从上到下
display.Image(graph.create_png(),width=800) # 显示图片
graph.write_png('proto.png')) # 保存图片
- 只显示operators
from caffe2.python import net_drawer
from IPython import display
graph=net_drawer.GetPydotGraphMinimal(net.Proto(),rankdir="LR", minimal_dependency=True)
display.Image(graph.create_png(),width=800) #显示图片
graph.write_png('proto.png')) # 保存图片