Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added demo/.DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# DEMO

1. First download and unzip the file from the following [link](https://docs.google.com/a/umich.edu/uc?id=0B_jnlzs7cGbjVWZvanVUejdVTG8&export=download) into your desired location

2. Use the following to install the proper dependencies:
```luarocks install luasocket```
```luarocks install json```

3. Type the following into a terminal:
```th kp_demo.lua <path_to_data> ```

4. Direct your browser to `localhost:8080`
71 changes: 71 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<html>
<head>
<!-- Material Design Lite -->
<script src="https://storage.googleapis.com/code.getmdl.io/1.1.3/material.min.js"></script>
<link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.1.3/material.indigo-pink.min.css">
<!-- Material Design icon font -->
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" type="text/css" href="resources/css/style_kp_sam.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<!-- Neural Net Stuff -->
<script type="text/javascript" src="resources/javascript/DragDisk.js"></script>
<script type="text/javascript" src="resources/javascript/generate_kp_sam.js"></script>
</head>

<body>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<header class="mdl-layout__header">
<!-- Tabs -->
<div class="mdl-layout__tab-bar mdl-js-ripple-effect" style="text-align:center;">
<a href="#scroll-tab-1" id="pp_tab" class="mdl-layout__tab is-active">(Keypoint, txt) -> Image</a>
</div>
</header>

<main class="mdl-layout__content">
<!-- Pick parts -->
<!--<section class="mdl-layout__tab-panel" id="scroll-tab-1">-->
<section>
<div class="page-content">
<table style="padding-top: 50px; width: 150px; margin: 0 auto;">
<tr>
<td class="canvas_container">
<img class="placeholder" src="resources/images/placeholder.jpg" height="512px" width="512px">
<canvas id="pp_canvas" class="canvas_class" style="border:1px solid #000000;" height="512px" width="768px">
</canvas>
</td>
</tr>
<tr>
<td>
<form action="/" onsubmit="get_pp_prediction()">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="pp_description">
<label class="mdl-textfield__label" for="sample3" style="color:black;">Click here to describe a bird...</label>

</div>
<!-- thanks to www.tutorialspoint.com/materialdesignlite/materialdesignlite_checkboxes.htm -->
<!--<label class="mdl-checkbox mdl-js-checkbox" for="checkbox0">-->
<input type="checkbox" class="mdl-checkbox__input" id="pp_showkps">
<span class="mdl-checkbox__label">Show keypoints?</span>
<!--</label>-->
</form>
</td>
</tr>
<tr style="width: 125px;">
<td>
<button style="float:left;" type="button" id="pp_clear_btn" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">
Clear Inputs
</button>
</td>
<td>
<button style="float:right;" type="button" id="pp_generate_btn" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">
Generate Image!
</button>
</td>
</tr>
</table>
</div>
</section>
</main>
</div>
</body>
</html>
130 changes: 130 additions & 0 deletions demo/kp_demo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
local orbiter = require 'orbiter'

local hello = orbiter.new()
local mime = require("mime")
local util = paths.dofile('../util.lua')
require('image')
require('nn')
require('nngraph')
require('image')
require('json')

-- Fill in appropriate data path variable
if arg[1] == nil then
print('Please enter path to folder containing data')
else
DATA_PATH = arg[1]
end

net_txt = torch.load(DATA_PATH .. 'lm_sje_nc4_cub_hybrid_gru18_a1_c512_0.00070_1_10_trainvalids.txt_iter30000.t7_cpu.t7')
net_txt:evaluate()
net_gen = torch.load(DATA_PATH .. 'cub_part_stn_kd16_bs16_ngf128_ndf128_600_net_G.t7_cpu.t7')
net_gen:evaluate()
net_kp = torch.load(DATA_PATH .. 'cub_kptxt2kp_large_bs64_ngf128_ndf128_200_net_G.t7_cpu.t7')
net_kp:evaluate()

torch.setdefaulttensortype('torch.FloatTensor')

local alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{} "
local dict = {}
for i = 1,#alphabet do
dict[alphabet:sub(i,i)] = i
end
ivocab = {}
for k,v in pairs(dict) do
ivocab[v] = k
end

opt = {}
opt.keypoint_dim = 16
opt.num_elt = 15
opt.doc_length = 201
opt.batchSize = 1
opt.nz = 100
opt.txtSize = 1024
opt.noisetype = 'normal'

function hello:index(web)
local f = assert(io.open('index.html', 'r'))
local rtrn = f:read('*all')
f:close()
return rtrn
end

function hello:request(web)
local data = json.decode(web.POST.data)
local desc = data.description
local showkps = data.showkps
-- prepare noise
noise = torch.Tensor(opt.batchSize, opt.nz)
if opt.noisetype == 'uniform' then
noise:uniform(-1, 1)
elseif opt.noisetype == 'normal' then
noise:normal(0, 1)
end
-- prepare text
local txt_mat = torch.zeros(1,opt.doc_length,#alphabet)
for t = 1,opt.doc_length do
local ch = desc:sub(t,t)
local on_ix = dict[ch]
if (on_ix ~= 0 and on_ix ~= nil) then
txt_mat[{1, t, on_ix}] = 1
end
end
local fea_txt = net_txt:forward(txt_mat):clone()
-- prepare keypoints
local fea_loc_inp = torch.zeros(opt.batchSize, opt.num_elt, 3)
for n = 1,#data.keypoints do
local id = data.keypoints[n].part_id
local x = data.keypoints[n].x / 256.0
local y = data.keypoints[n].y / 256.0
fea_loc_inp[{1,id,1}] = x
fea_loc_inp[{1,id,2}] = y
fea_loc_inp[{1,id,3}] = 1.0
end
fea_loc = net_kp:forward{noise, fea_txt, fea_loc_inp}:clone()

local data_loc = torch.zeros(opt.batchSize, opt.num_elt,
opt.keypoint_dim, opt.keypoint_dim)
for b = 1,opt.batchSize do
for s = 1,opt.num_elt do
local point = fea_loc[{b,s,{}}]
if point[3] > 0.5 then
local x = math.min(opt.keypoint_dim,
math.max(1,torch.round(point[1] * opt.keypoint_dim)))
local y = math.min(opt.keypoint_dim,
math.max(1,torch.round(point[2] * opt.keypoint_dim)))
data_loc[{b,s,y,x}] = 1
end
end
end
local images = net_gen:forward({ { noise, fea_txt }, data_loc }):clone()
images:add(1):mul(0.5)
local img = images:select(1,1)

local locs_tmp = fea_loc:clone()
locs_tmp:narrow(3,1,2):mul(128)

if showkps==1 then
print(showkps)
print('drawing keypoints...')
img = util.draw_keypoints(img, locs_tmp[1], 0.03)
end

print(desc)

local tmp_fname = '/tmp/tmp_bbox.jpeg'
image.save(tmp_fname, img)
local f = assert(io.open(tmp_fname, "rb"))
local img_binary = f:read("*all")
local img_b64 = 'data:image/jpeg;base64,' .. mime.b64(img_binary)
return img_b64,'image/jpeg'
end

hello:dispatch_get(hello.index,'/','/index')
hello:dispatch_post(hello.request, '/request')
hello:dispatch_static '/resources/images/.+'
hello:dispatch_static '/resources/css/.+'
hello:dispatch_static '/resources/javascript/.+'

hello:run(...)
Binary file added demo/orbiter/.DS_Store
Binary file not shown.
38 changes: 38 additions & 0 deletions demo/orbiter/bridge.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Orbiter, a personal web application framework
-- orbiter.orbit acts as a bridge for registering static dispatches
-- that works with both Orbiter and Orbit

local _M = {}

local app

function _M.new(app_)
app = app_
end

function _M.dispatch_static(...)
-- remember to strip off the starting @
local path = debug.getinfo(2, "S").source:sub(2):gsub('\\','/')
if path:find '/' then
path = path:gsub('/[%w_]+%.lua$','')
else -- invoked just as script name
path = '.'
end
if orbit then
local function static_handler(web)
local fpath = path..web.path_info
return app:serve_static(web,fpath)
end
app:dispatch_get(static_handler,...)
return app
else
local obj = require 'orbiter'. new()
obj.root = path
obj:dispatch_static(...)
return obj
end
end

return _M


41 changes: 41 additions & 0 deletions demo/orbiter/controls/calendar.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

-- http://www.softcomplex.com/products/tigra_calendar/

local html = require 'orbiter.html'

-- if extensions use the bridge dispatch_static, then Orbit applications can
-- use this as well!
local bridge = require 'orbiter.bridge'
bridge.dispatch_static('/resources/javascript/calendar.+',
'/resources/css/calendar.+',
'/resources/images/calendar.+')

local _M = {}
_M.mode = 'us'

html.set_defaults {
scripts = '/resources/javascript/calendar.js',
styles = '/resources/css/calendar.css'
}

function _M.set_mode(mode)
_M.mode = mode
end

function _M.calendar(form,control,mode)
return html.script ( ([[
new tcal ({
formname: '%s',
controlname: '%s',
mode: '%s'
});
]]):format(form,control,mode) )
end

local input = html.tags 'input'

function _M.date(form,control)
return input{type='text',name=control},_M.calendar(form,control,_M.mode)
end

return _M
Loading