-
Notifications
You must be signed in to change notification settings - Fork 4
/
graph.go
204 lines (187 loc) · 4.51 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package mxnet
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/awalterschulze/gographviz"
"github.com/fatih/set"
)
// color map
var fillcolors = []string{
"#8dd3c7",
"#fb8072",
"#ffffb3",
"#bebada",
"#80b1d3",
"#fdb462",
"#b3de69",
"#fccde5",
}
var edgecolors = []string{
"#245b51",
"#941305",
"#999900",
"#3b3564",
"#275372",
"#975102",
"#597d1c",
"#90094e",
}
func (g *Graph) ToDotGraph() (*gographviz.Escape, error) {
tuples := func(s string) []string {
re := regexp.MustCompile(`\d+`)
return re.FindAllString(s, -1)
}
makeDefaultAttributes := func() map[string]string {
return map[string]string{
"shape": "box",
"fixedsize": "true",
"width": "1.3",
"height": "0.8034",
"style": "filled",
}
}
isLikeWeight := func(name string) bool {
if strings.HasSuffix(name, "_weight") {
return true
}
if strings.HasSuffix(name, "_bias") {
return true
}
if strings.HasSuffix(name, "_beta") ||
strings.HasSuffix(name, "_gamma") ||
strings.HasSuffix(name, "_moving_var") ||
strings.HasSuffix(name, "_moving_mean") {
return true
}
return false
}
hideWeights := true // TODO: should be an option
drawShape := true // TODO: should be an option
graphName := "mxnet" // TODO: should be an option
layout := "horizontal" // TODO: should be an option
dg := gographviz.NewEscape()
dg.SetName(graphName)
dg.SetDir(true)
dg.AddAttr(graphName, "nodesep", "1")
dg.AddAttr(graphName, "ranksep", "1.5 equally")
switch layout {
case "vertical":
dg.AddAttr(graphName, "rankdir", "TB")
case "horizontal":
dg.AddAttr(graphName, "rankdir", "RL")
default:
}
hiddenNodes := set.New(set.NonThreadSafe)
// make nodes
for _, node := range g.Nodes {
op := node.Op
name := node.Name
attrs := makeDefaultAttributes()
label := op
switch op {
case "null":
if isLikeWeight(name) {
if hideWeights {
hiddenNodes.Add(name)
continue
}
}
attrs["shape"] = "oval"
attrs["fillcolor"] = fillcolors[0]
label = name
case "Convolution":
if val, ok := node.Param["stride"]; ok {
strideInfo := tuples(val)
label = fmt.Sprintf("Convolution\n%s/%s, %s",
strings.Join(tuples(node.Param["kernel"]), "x"),
strings.Join(strideInfo, "x"),
node.Param["num_filter"],
)
} else {
label = fmt.Sprintf("Convolution\n%s/%s, %s",
strings.Join(tuples(node.Param["kernel"]), "x"),
"1",
node.Param["num_filter"],
)
}
attrs["fillcolor"] = fillcolors[1]
case "FullyConnected":
label = fmt.Sprintf("FullyConnected\n%s", node.Param["num_hidden"])
attrs["fillcolor"] = fillcolors[1]
case "BatchNorm":
attrs["fillcolor"] = fillcolors[3]
case "Activation", "LeakyReLU":
label = fmt.Sprintf("%s\n%s", op, node.Param["act_type"])
attrs["fillcolor"] = fillcolors[2]
case "Pooling":
if val, ok := node.Param["stride"]; ok {
strideInfo := tuples(val)
label = fmt.Sprintf("Pooling\n%s, %s/%s",
node.Param["pool_type"],
strings.Join(tuples(node.Param["kernel"]), "x"),
strings.Join(strideInfo, "x"),
)
} else {
label = fmt.Sprintf("Pooling\n%s, %s/%s",
node.Param["pool_type"],
strings.Join(tuples(node.Param["kernel"]), "x"),
"1",
)
}
attrs["fillcolor"] = fillcolors[4]
case "Concat", "Flatten", "Reshape":
attrs["fillcolor"] = fillcolors[5]
case "Softmax":
attrs["fillcolor"] = fillcolors[6]
default:
attrs["fillcolor"] = fillcolors[7]
if op == "Custom" {
label = node.Param["op_type"]
}
attrs["label"] = label
}
dg.AddNode(graphName, name, attrs)
}
// make edges
for _, node := range g.Nodes {
op := node.Op
name := node.Name
if op == "null" {
continue
}
inputs := node.Inputs
for _, item := range inputs {
inputNode := g.Nodes[item.NodeId]
inputName := inputNode.Name
if hiddenNodes.Has(inputName) {
continue
}
attrs := map[string]string{
"dir": "back",
"arrowtail": "open",
}
if drawShape {
key := inputName
if inputNode.Op != "null" {
key += "_output"
if val, ok := inputNode.Param["num_outputs"]; ok {
numOutputs, err := strconv.Atoi(val)
if err != nil {
continue
}
key += strconv.Itoa(numOutputs - 1)
inputNode.Param["num_outputs"] = strconv.Itoa(numOutputs - 1)
}
}
//shape = shape_dict[key][1:]
//label = "x".join([str(x) for x in shape])
//attrs["label"] = label
}
dg.AddEdge(name, inputName, true, attrs)
}
}
return dg, nil
}
type ModelGraphAttributes map[string][]string