forked from flashlight/wav2letter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.lua
286 lines (264 loc) · 7.77 KB
/
plot.lua
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
-- Copyright (c) 2017-present, Facebook, Inc.
-- All rights reserved.
-- This source code is licensed under the BSD-style license found in the
-- LICENSE file in the root directory of this source tree. An additional grant
-- of patent rights can be found in the PATENTS file in the same directory.
require 'torch'
require 'paths'
require 'gnuplot'
local function usage(arg)
print(string.format("usage: %s [options] <column name> <files>", arg[0]))
print[[
options:
%web -- output path root is $HOME/public_html
%pdf[=<filename>] -- pdf output (optional filename) default: plot.pdf
%png[=<filename>] -- png output (optional filename) default: plot.png
%svg[=<filename>] -- svg output (optional filename) default: plot.svg
%xmin=<value> -- xmin value
%xmax=<value> -- xmax value
%ymin=<value> -- ymin value
%ymax=<value> -- ymax value
+<key>=<value> -- select only experiments where config <key> is equal to <value>
-<key>=<value> -- filter out experiments where config <key> is equal to <value>
]]
return false
end
local opt = {outpath='.'}
local invalidkeys = {
gfsai=true, rundir=true, netspecs=true, dataspecs=true, kw=true,
dw=true, force=true, timestamp=true, reload=true, path=true,
hostname=true, datadir=true, archdir=true, gpu=true, iter=true,
username=true, nclass=true
}
local function convert(value, typename)
if typename == 'boolean' then
if value == 'false' then
value = false
elseif value == 'true' then
value = true
end
elseif typename == 'number' then
value = tonumber(value)
end
return value
end
local title = {}
local titleflag
for i=1,#arg do
local z = arg[i]
if z:match('^%%')
or z:match('^%+')
or z:match('^%-') then
table.insert(title, z)
elseif not titleflag then
titleflag = true
table.insert(title, z)
end
end
table.insert(title, '...')
title = table.concat(title, ' ')
local filters = {}
for i=#arg,1,-1 do
local z = arg[i]
if z:match('^%%pdf') then
local filename = z:match('^%%pdf%=(.+)') or 'plot.pdf'
opt.pdf = filename
table.remove(arg, i)
elseif z:match('^%%png') then
local filename = z:match('^%%png%=(.+)') or 'plot.png'
opt.png = filename
table.remove(arg, i)
elseif z:match('^%%svg') then
local filename = z:match('^%%svg%=(.+)') or 'plot.svg'
opt.svg = filename
table.remove(arg, i)
elseif z:match('^%%web') then
local subpath = z:match('^%%web%=(.+)')
if subpath then
opt.outpath = paths.concat(os.getenv('HOME'), 'public_html', subpath)
else
opt.outpath = paths.concat(os.getenv('HOME'), 'public_html')
end
table.remove(arg, i)
elseif z:match('^%%ymin%=') then
opt.ymin = tonumber(z:match('^%%ymin%=(.+)'))
table.remove(arg, i)
elseif z:match('^%%ymax%=') then
opt.ymax = tonumber(z:match('^%%ymax%=(.+)'))
table.remove(arg, i)
elseif z:match('^%%xmin%=') then
opt.xmin = tonumber(z:match('^%%xmin%=(.+)'))
table.remove(arg, i)
elseif z:match('^%%xmax%=') then
opt.xmax = tonumber(z:match('^%%xmax%=(.+)'))
table.remove(arg, i)
elseif z:match('^%+.+%=.+') then
local key, value = z:match('^%+(.+)%=(.+)')
table.insert(
filters,
function(config)
if config[key] == convert(value, type(config[key])) then
return true
end
end
)
table.remove(arg, i)
elseif z:match('^%-.+%=.+') then
local key, value = z:match('^%-(.+)%=(.+)')
table.insert(
filters,
function(config)
if config[key] ~= convert(value, type(config[key])) then
return true
end
end
)
table.remove(arg, i)
end
end
local column = arg[1]
local ylabel = column
if not column then
assert(usage(arg))
end
-- escapes
column = column:gsub('([%(%)%.%+%-%*%?%[%]%^%$%%])',
function(str)
return '%' .. str
end)
table.remove(arg, 1)
local function loadfile(filename)
local x = {}
local y = {}
local x_ = 0
pcall(
function()
for line in io.lines(filename) do
if not line:match(column .. '^%s*#') then
local y_ = tonumber(line:match(column .. '%s*([^%s|]+)'))
if y_ then
x_ = x_ + 1
table.insert(x, x_)
table.insert(y, y_)
end
end
end
end
)
collectgarbage() -- io.lines()
return torch.Tensor(x), torch.Tensor(y)
end
local files = {}
local avlkeys = {}
for i=1,#arg do
local f
if pcall(
function()
f = torch.DiskFile(paths.concat(arg[i], 'model-last.bin')):binary()
end) then
f:readObject()
local config = f:readObject()
f:close()
local accept = true
for _, filter in ipairs(filters) do
if not filter(config) then
accept = false
break
end
end
if accept then
for k,v in pairs(config) do
if not invalidkeys[k] then
avlkeys[k] = avlkeys[k] or {}
avlkeys[k][v] = avlkeys[k][v] or 1
avlkeys[k][v] = avlkeys[k][v] + 1
end
end
table.insert(files, {path=arg[i], config=config})
end
end
end
local keys = {}
for k,v in pairs(avlkeys) do
local valid = false
local n = 0
local sz = 0
for k,v in pairs(v) do
n = n + 1
sz = math.max(sz, #tostring(k))
end
if n == 1 then
for k,v in pairs(v) do
if v == 1 then
valid = true
end
end
else
valid = true
end
if valid then
table.insert(keys, {name=k, size=sz})
end
end
table.sort(keys, function(a, b) return a.name < b.name end)
local function legend(config, keys)
local legend = {}
for _,k in ipairs(keys) do
local value = tostring(config[k.name])
value = value .. string.rep(' ', k.size-#value)
table.insert(legend, string.format("%s=%s", k.name, value))
end
return table.concat(legend, ' ')
end
local plots = {}
local xmin = math.huge
local xmax = -math.huge
local ymin = math.huge
local ymax = -math.huge
for _, file in ipairs(files) do
local x, y = loadfile(paths.concat(file.path, 'perf'))
if x:nDimension() > 0 and x:size(1) > 0 then
xmin = math.min(xmin, x:min())
xmax = math.max(xmax, x:max())
ymin = math.min(ymin, y:min())
ymax = math.max(ymax, y:max())
table.insert(
plots,
{
legend(file.config, keys),
x,
y,
'+-'
}
)
end
end
if opt.pdf then
os.execute(string.format('mkdir -p "%s"', opt.outpath))
gnuplot.pdffigure(paths.concat(opt.outpath, opt.pdf))
gnuplot.raw('set terminal pdfcairo enhanced size 35cm,25cm')
gnuplot.raw('set key font "Monospace,6"')
elseif opt.png then
os.execute(string.format('mkdir -p "%s"', opt.outpath))
gnuplot.pngfigure(paths.concat(opt.outpath, opt.png))
gnuplot.raw('set terminal pngcairo enhanced size 1400,1050')
gnuplot.raw('set key font "Monospace,8"')
elseif opt.svg then
os.execute(string.format('mkdir -p "%s"', opt.outpath))
gnuplot.svgfigure(paths.concat(opt.outpath, opt.svg))
gnuplot.raw('set terminal svg enhanced dynamic size 1400,1050')
gnuplot.raw('set key font "Monospace,8"')
else
gnuplot.raw('set term wxt enhanced size 1400,1050')
gnuplot.raw('set key font "Monospace,8"')
end
gnuplot.raw('set key outside tmargin')
gnuplot.xlabel("epoch")
gnuplot.ylabel(ylabel)
gnuplot.title(title)
gnuplot.axis({opt.xmin or xmin, opt.xmax or xmax, opt.ymin or ymin, opt.ymax or ymax})
gnuplot.plot(plots)
gnuplot.grid(true)
if opt.pdf or opt.png or opt.svg then
gnuplot.plotflush()
end