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