|
| 1 | +require 'graph' |
| 2 | + |
| 3 | +-- taken from http://www.graphviz.org/doc/info/colors.html |
| 4 | +local colorNames = { |
| 5 | + "aliceblue","antiquewhite","antiquewhite1","antiquewhite2","antiquewhite3", |
| 6 | + "antiquewhite4","aquamarine","aquamarine1","aquamarine2","aquamarine3", |
| 7 | + "aquamarine4","azure","azure1","azure2","azure3", |
| 8 | + "azure4","beige","bisque","bisque1","bisque2", |
| 9 | + "bisque3","bisque4","black","blanchedalmond","blue", |
| 10 | + "blue1","blue2","blue3","blue4","blueviolet", |
| 11 | + "brown","brown1","brown2","brown3","brown4", |
| 12 | + "burlywood","burlywood1","burlywood2","burlywood3","burlywood4", |
| 13 | + "cadetblue","cadetblue1","cadetblue2","cadetblue3","cadetblue4", |
| 14 | + "chartreuse","chartreuse1","chartreuse2","chartreuse3","chartreuse4", |
| 15 | + "chocolate","chocolate1","chocolate2","chocolate3","chocolate4", |
| 16 | + "coral","coral1","coral2","coral3","coral4", |
| 17 | + "cornflowerblue","cornsilk","cornsilk1","cornsilk2","cornsilk3", |
| 18 | + "cornsilk4","crimson","cyan","cyan1","cyan2", |
| 19 | + "cyan3","cyan4","darkgoldenrod","darkgoldenrod1","darkgoldenrod2", |
| 20 | + "darkgoldenrod3","darkgoldenrod4","darkgreen","darkkhaki","darkolivegreen", |
| 21 | + "darkolivegreen1","darkolivegreen2","darkolivegreen3","darkolivegreen4","darkorange", |
| 22 | + "darkorange1","darkorange2","darkorange3","darkorange4","darkorchid", |
| 23 | + "darkorchid1","darkorchid2","darkorchid3","darkorchid4","darksalmon", |
| 24 | + "darkseagreen","darkseagreen1","darkseagreen2","darkseagreen3","darkseagreen4", |
| 25 | + "darkslateblue","darkslategray","darkslategray1","darkslategray2","darkslategray3", |
| 26 | + "darkslategray4","darkslategrey","darkturquoise","darkviolet","deeppink", |
| 27 | + "deeppink1","deeppink2","deeppink3","deeppink4","deepskyblue", |
| 28 | + "deepskyblue1","deepskyblue2","deepskyblue3","deepskyblue4","dimgray", |
| 29 | + "dimgrey","dodgerblue","dodgerblue1","dodgerblue2","dodgerblue3", |
| 30 | + "dodgerblue4","firebrick","firebrick1","firebrick2","firebrick3", |
| 31 | + "firebrick4","floralwhite","forestgreen","gainsboro","ghostwhite", |
| 32 | + "gold","gold1","gold2","gold3","gold4", |
| 33 | + "goldenrod","goldenrod1","goldenrod2","goldenrod3","goldenrod4" |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +local function generateGraph(net, input, opts) |
| 38 | + |
| 39 | + local storageHash = {} |
| 40 | + local nodes = {} |
| 41 | + |
| 42 | + local g = graph.Graph() |
| 43 | + |
| 44 | + -- basic function for creating an annotated nn.Node to suit our purposes |
| 45 | + -- gives the same color for the same storage. |
| 46 | + -- note that two colors being the same does not imply the same storage |
| 47 | + -- as we have a limited number of colors |
| 48 | + local function createNode(name, tensor) |
| 49 | + local data = torch.pointer(tensor:storage()) |
| 50 | + local storageId |
| 51 | + if not storageHash[data] then |
| 52 | + storageHash[data] = torch.random(1,#colorNames) |
| 53 | + table.insert(storageHash, data) |
| 54 | + end |
| 55 | + for k, v in ipairs(storageHash) do |
| 56 | + if v == data then |
| 57 | + storageId = k |
| 58 | + end |
| 59 | + end |
| 60 | + local node = graph.Node("Storage id: "..storageId) |
| 61 | + function node:graphNodeName() |
| 62 | + return name |
| 63 | + end |
| 64 | + function node:graphNodeAttributes() |
| 65 | + return {color=colorNames[storageHash[data]]} |
| 66 | + end |
| 67 | + return node |
| 68 | + end |
| 69 | + |
| 70 | + -- generate input/output nodes |
| 71 | + local function createBoundaryNode(input, name) |
| 72 | + if torch.isTensor(input) then |
| 73 | + local ptr = torch.pointer(input) |
| 74 | + nodes[ptr] = createNode(name,input) |
| 75 | + else |
| 76 | + for k,v in ipairs(input) do |
| 77 | + createBoundaryNode(nodes, v, name..' '..k) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + -- create edge "from" -> "to", creating "to" on the way with "name" |
| 83 | + -- the edges can be seen as linking modules, but in fact it links the output |
| 84 | + -- tensor of each module |
| 85 | + local function addEdge(from, to, name) |
| 86 | + if torch.isTensor(to) and torch.isTensor(from) then |
| 87 | + local fromPtr = torch.pointer(from) |
| 88 | + local toPtr = torch.pointer(to) |
| 89 | + |
| 90 | + nodes[toPtr] = nodes[toPtr] or createNode(name,to) |
| 91 | + |
| 92 | + assert(nodes[fromPtr], 'Parent node inexistant for module '.. name) |
| 93 | + |
| 94 | + -- insert edge |
| 95 | + g:add(graph.Edge(nodes[fromPtr],nodes[toPtr])) |
| 96 | + |
| 97 | + elseif torch.isTensor(from) then |
| 98 | + for k,v in ipairs(to) do |
| 99 | + addEdge(from, v, name) |
| 100 | + end |
| 101 | + else |
| 102 | + for k,v in ipairs(from) do |
| 103 | + addEdge(v, to, name) |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + -- go over the network keeping track of the input/output for each module |
| 109 | + -- we overwrite the updateOutput for that. |
| 110 | + local function apply_func(m) |
| 111 | + local basefunc = m.updateOutput |
| 112 | + m.updateOutput = function(self, input) |
| 113 | + if not m.modules then |
| 114 | + local name = tostring(m) |
| 115 | + if m.inplace then -- handle it differently ? |
| 116 | + addEdge(input,self.output,name) |
| 117 | + else |
| 118 | + addEdge(input,self.output,name) |
| 119 | + end |
| 120 | + elseif torch.typename(m) == 'nn.Concat' or |
| 121 | + torch.typename(m) == 'nn.Parallel' or |
| 122 | + torch.typename(m) == 'nn.DepthConcat' then |
| 123 | + -- those containers effectively do some computation, so they have their |
| 124 | + -- place in the graph |
| 125 | + for i,branch in ipairs(m.modules) do |
| 126 | + local last_module = branch:get(branch:size()) |
| 127 | + local out = last_module.output |
| 128 | + local ptr = torch.pointer(out) |
| 129 | + |
| 130 | + local name = torch.typename(last_module) |
| 131 | + nodes[ptr] = nodes[ptr] or createNode(name,out) |
| 132 | + addEdge(out, self.output, torch.typename(m)) |
| 133 | + end |
| 134 | + end |
| 135 | + return basefunc(self, input) |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + createBoundaryNode(input, 'Input') |
| 140 | + |
| 141 | + -- fill the states from each tensor |
| 142 | + net:forward(input) |
| 143 | + |
| 144 | + --createInputNode(nodes, net.output, 'Output') |
| 145 | + |
| 146 | + -- overwriting the standard functions to generate our graph |
| 147 | + net:apply(apply_func) |
| 148 | + -- generate the graph |
| 149 | + net:forward(input) |
| 150 | + |
| 151 | + -- clean up the modified function |
| 152 | + net:apply(function(x) |
| 153 | + x.updateOutput = nil |
| 154 | + end) |
| 155 | + |
| 156 | + return g |
| 157 | +end |
| 158 | + |
| 159 | +return generateGraph |
| 160 | + |
0 commit comments