使用Python中NetworkX包绘制深度神经网络结构图

Posted junlong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Python中NetworkX包绘制深度神经网络结构图相关的知识,希望对你有一定的参考价值。

技术图片
 1 """ 使用Python中NetworkX包绘制深度神经网络结构图 """
 2 # 导入相应包
 3 import networkx as nx
 4 import matplotlib.pyplot as plt
 5 
 6 # 创建DAG
 7 G = nx.DiGraph()
 8 
 9 # 创建结构图顶点列表
10 vertex_list = [v+str(i) for i in range(1, 22)]
11 
12 # 添加结构图顶点
13 G.add_nodes_from(vertex_list)
14 
15 # 创建边列表
16 edge_list = [
17              (v1, v5), (v1, v6), (v1, v7), (v1, v8), (v1, v9),
18              (v2, v5), (v2, v6), (v2, v7), (v2, v8), (v2, v9),
19              (v3, v5), (v3, v6), (v3, v7), (v3, v8), (v3, v9),
20              (v4, v5), (v4, v6), (v4, v7), (v4, v8), (v4, v9),
21              (v5, v10), (v5, v11), (v5, v12), (v5, v13), (v5, v14), (v5, v15),
22              (v6, v10), (v6, v11), (v6, v12), (v6, v13), (v6, v14), (v6, v15),
23              (v7, v10), (v7, v11), (v7, v12), (v7, v13), (v7, v14), (v7, v15),
24              (v8, v10), (v8, v11), (v8, v12), (v8, v13), (v8, v14), (v8, v15),
25              (v9, v10), (v9, v11), (v9, v12), (v9, v13), (v9, v14), (v9, v15),
26              (v10, v16), (v10, v17), (v10, v18),
27              (v11, v16), (v11, v17), (v11, v18),
28              (v12, v16), (v12, v17), (v12, v18),
29              (v13, v16), (v13, v17), (v13, v18),
30              (v14, v16), (v14, v17), (v14, v18),
31              (v15, v16), (v15, v17), (v15, v18),
32              (v16, v19),
33              (v17, v20),
34              (v18, v21)
35             ]
36 
37 # 通过列表形式来添加边
38 G.add_edges_from(edge_list)
39 
40 # 指定绘制DAG图时每个顶点的位置
41 pos = 
42         v1: (-2, 1.5),
43         v2: (-2, 0.5),
44         v3: (-2, -0.5),
45         v4: (-2, -1.5),
46         v5: (-1, 2),
47         v6: (-1, 1),
48         v7: (-1, 0),
49         v8: (-1, -1),
50         v9: (-1, -2),
51         v10: (0, 2.5),
52         v11: (0, 1.5),
53         v12: (0, 0.5),
54         v13: (0, -0.5),
55         v14: (0, -1.5),
56         v15: (0, -2.5),
57         v16: (1, 1),
58         v17: (1, 0),
59         v18: (1, -1),
60         v19: (2, 1),
61         v20: (2, 0),
62         v21: (2, -1)
63        
64 
65 # 绘制DAG图
66 plt.title(Deep Neural Network Structure)  # 神经网络结构图标题
67 plt.xlim(-2.2, 2.2)  # 设置X轴坐标范围
68 plt.ylim(-3, 3)  # 设置Y轴坐标范围
69 nx.draw(
70         G,
71         pos=pos,  # 点的位置
72         node_color=red,  # 顶点颜色
73         edge_color=black,  # 边的颜色
74         with_labels=False,  # 不显示顶点标签
75         font_size=10,  # 文字大小
76         node_size=300,  # 顶点大小
77        )
78 
79 # 保存图片,图片大小为640*480
80 plt.savefig(E:/data/DNN_chart.png)
81 
82 # 显示图片
83 plt.show()
使用Python中NetworkX包绘制深度神经网络结构图

程序效果展示:2019-07-14 17:24:20

技术图片
技术图片
 1 """ 利用opencv模块对DNN框架添加文字注释 """
 2 import cv2
 3 
 4 # 读取图片
 5 imagepath = E:/data/DNN_chart.png
 6 image = cv2.imread(imagepath, 1)
 7 
 8 # 输入层
 9 cv2.rectangle(image, (85, 130), (120, 360), (255, 0, 0), 2)
10 cv2.putText(image, "Input_Layer", (15, 390), 1, 1.5, (0, 0, 150), 2, 1)
11 
12 # 隐藏层
13 cv2.rectangle(image, (190, 70), (360, 420), (255, 0, 0), 2)
14 cv2.putText(image, "Hidden_Layer", (210, 450), 1, 1.5, (0, 0, 150), 2, 1)
15 
16 # 输出层
17 cv2.rectangle(image, (420, 150), (460, 330), (255, 0, 0), 2)
18 cv2.putText(image, "Output_Layer", (380, 360), 1, 1.5, (0, 0, 150), 2, 1)
19 
20 # sofrmax层
21 cv2.rectangle(image, (530, 150), (570, 330), (255, 0, 0), 2)
22 cv2.putText(image, "softmax", (500, 130), 1, 1.5, (0, 0, 150), 2, 1)
23 
24 # 保存修改后的图片
25 cv2.imwrite(E://data/DNN.jpg, image)
26 
27 plt.show()
利用opencv模块对DNN框架添加文字注释

程序效果展示:2019-07-14 17:25:52

技术图片

以上是关于使用Python中NetworkX包绘制深度神经网络结构图的主要内容,如果未能解决你的问题,请参考以下文章

python networkx绘制图

python networkx:绘制网络图

Python:获取所有节点的度数,然后在networkx中绘制箱线图

python 使用networkx绘制带权无向图和带权有向图,以及标注特定路径

添加边缘权重以在 networkx 中绘制输出

networkx图论Depth First Search深度优先搜索遍历DFS,基于递归,Python