-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeat_eval_nounphrase.lua
79 lines (65 loc) · 2.14 KB
/
feat_eval_nounphrase.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
require 'torch'
require 'nn'
require 'nngraph'
require 'hdf5'
local utils = require 'misc.utils'
local net_utils = require 'misc.net_utils'
require 'misc.optim_updates'
-------------------------------------------------------------------------------
-- Input arguments and options
-------------------------------------------------------------------------------
cmd = torch.CmdLine()
cmd:text()
cmd:text('Eval')
cmd:text()
cmd:text('Options')
-- Data input settings
cmd:option('-imgfeat_h5', '', '')
cmd:option('-nounphrase_h5', '', '')
cmd:option('-nounphraser', '', '')
cmd:option('-target_h5', 'tmp.h5', '')
-- misc
cmd:option('-backend', 'cudnn', 'nn|cudnn')
cmd:option('-seed', 123, 'random number generator seed to use')
cmd:option('-gpuid', 0, 'which gpu to use. -1 = use CPU')
cmd:option('-id', "1", '')
cmd:text()
-------------------------------------------------------------------------------
-- Basic Torch initializations
-------------------------------------------------------------------------------
local cfg = cmd:parse(arg)
--torch.manualSeed(cfg.seed)
torch.setdefaulttensortype('torch.FloatTensor') -- for CPU
if cfg.gpuid >= 0 then
require 'cutorch'
require 'cunn'
if cfg.backend == 'cudnn' then require 'cudnn' end
--cutorch.manualSeed(cfg.seed)
cutorch.setDevice(cfg.gpuid + 1) -- note +1 because lua is 1-indexed
end
cfg.batch_size = 1
local protos = {}
if cfg.nounphraser ~= '' then
print("load nounphraser from " .. cfg.nounphraser)
local loaded_checkpoint = torch.load(cfg.nounphraser)
protos.nounphraser = loaded_checkpoint.predictor:cuda()
else
error("need a nounphraser")
end
protos.nounphraser:evaluate()
local h5_file = hdf5.open(cfg.nounphrase_h5, "r")
local feat_file = hdf5.open(cfg.imgfeat_h5, "r")
label_size = h5_file:read("/nouns"):dataspaceSize()
n = label_size[1]
m = label_size[2]
local pred = torch.FloatTensor(n, m):zero()
for i = 1, n do
if i % 2000 == 0 then
print(i .. " / " .. n)
end
local img_feat = feat_file:read("feats"):partial({i, i}, {1, 2048}):cuda()
pred[i] = protos.nounphraser:forward(img_feat):float()
end
local cacheFile = hdf5.open(cfg.target_h5, "w")
cacheFile:write("/pred", pred)
cacheFile:close()