Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active November 24, 2016 14:24
Show Gist options
  • Save vadimkantorov/7789eac44dd5b19903fc1f430461bc39 to your computer and use it in GitHub Desktop.
Save vadimkantorov/7789eac44dd5b19903fc1f430461bc39 to your computer and use it in GitHub Desktop.
Torch routine for generating HTML tables with embedded images
require 'image'
require 'base64' -- luarocks install lbase64
function vis_html_table(vis_path)
return {
file = io.open(vis_path, 'w'):write('<html><body><table>'),
close = function(self)
self.file:write('</tr></table></body></html>'):close()
end,
figure = function(self, img, top_legend, bottom_legend, rescale, width, height)
width = width or 200
height = height or 150
self.file:write(('<td><figure><figure style="margin:0; width: %dpx"><figcaption style="font-size: 0.7em">%s</figcaption></figure><img src="data:image/jpeg;base64,%s" width="%d" height="%d" /><figcaption style="background: %s">%s</figcaption></figure></td>\n'):format(width, top_legend or '', base64.encode(image.compressJPG(rescale ~= false and image.scale(img, width, height, 'simple') or img):storage():string()), width, height, type(bottom_legend) == 'table' and bottom_legend[2] or 'white', type(bottom_legend) == 'table' and bottom_legend[1] or bottom_legend or '&nbsp;'))
return self
end,
heatmap = function(self, tensor, ...)
return self:figure(image.y2jet(image.minmax{tensor = tensor:double()} * 255 + 1), ...)
end,
text = function(self, ...)
self.file:write(('<td>%s</td>'):format(...))
return self
end,
row = function(self, exampleIdx, ...)
self.numRows = (self.numRows or 0) + 1
self.file:write(self.numRows == 1 and '<tr>' or '</tr><tr>')
self:text('#' .. self.numRows)
if exampleIdx then
self:text(exampleIdx)
end
return self
end,
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment