-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathretrieval_sje_tcnn.lua
165 lines (149 loc) · 5.59 KB
/
retrieval_sje_tcnn.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
-- Retrieve images based on captions.
require('nn')
require('nngraph')
require('cutorch')
require('cunn')
require('cudnn')
require('util.util_retrieval')
local model_utils = require('util.model_utils')
local alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{} "
local dict = {}
for i = 1,#alphabet do
dict[alphabet:sub(i,i)] = i
end
-------------------------------------------------
cmd = torch.CmdLine()
cmd:option('-data_dir','data','data directory.')
cmd:option('-savefile','sje_tcnn','filename to autosave the checkpont to. Will be inside checkpoint_dir/')
cmd:option('-checkpoint_dir', 'cv', 'output directory where checkpoints get written')
cmd:option('-symmetric',1,'symmetric sje')
cmd:option('-learning_rate',0.0001,'learning rate')
cmd:option('-testclasses', 'testclasses.txt', 'validation or test classes to be used in evaluation')
cmd:option('-ids_file', 'trainvalids.txt', 'file specifying which class labels were used for training.')
cmd:option('-model','','model to load. If blank then above options will be used.')
cmd:option('-txt_limit',0,'if 0 then use all available text. Otherwise limit the number of documents per class')
cmd:option('-num_caption',10,'numner of captions per image to be used for training')
cmd:option('-outfile', 'results/roc.csv', 'output csv file with ROC curves.')
cmd:option('-ttype','char','word|char')
opt = cmd:parse(arg)
local model
if opt.model ~= '' then
model = torch.load(opt.model)
else
model = torch.load(string.format('%s/lm_%s_%.5f_%.0f_%.0f_%s.t7', opt.checkpoint_dir, opt.savefile, opt.learning_rate, opt.symmetric, opt.num_caption, opt.ids_file))
end
-----------------------------------------------------------
local doc_length = model.opt.doc_length
local protos = model.protos
protos.enc_doc:evaluate()
protos.enc_image:evaluate()
--print(model.opt)
function extract_img(filename)
local fea = torch.load(filename)[{{},{},1}]
fea = fea:float():cuda()
local out = protos.enc_image:forward(fea):clone()
return out:cuda()
end
function extract_txt(filename)
if opt.ttype == 'word' then
return extract_txt_word(filename)
else -- char
return extract_txt_char(filename)
end
end
function extract_txt_word(filename)
-- average all text features together.
local txt = torch.load(filename):permute(1,3,2)
txt = txt:reshape(txt:size(1)*txt:size(2),txt:size(3)):float():cuda()
if opt.txt_limit > 0 then
local actual_limit = math.min(txt:size(1), opt.txt_limit)
txt_order = torch.randperm(txt:size(1)):sub(1,actual_limit)
local tmp = txt:clone()
for i = 1,actual_limit do
txt[{i,{}}]:copy(tmp[{txt_order[i],{}}])
end
txt = txt:narrow(1,1,actual_limit)
end
if (model.opt.num_repl ~= nil) then
tmp = txt:clone()
txt = torch.ones(txt:size(1),model.opt.num_repl*txt:size(2))
for i = 1,txt:size(1) do
local cur_sen = torch.squeeze(tmp[{i,{}}]):clone()
local cur_len = cur_sen:size(1) - cur_sen:eq(1):sum()
local txt_ix = 1
for j = 1,cur_len do
for k = 1,model.opt.num_repl do
txt[{i,txt_ix}] = cur_sen[j]
txt_ix = txt_ix + 1
end
end
end
end
local txt_mat = torch.zeros(txt:size(1), txt:size(2), vocab_size+1)
for i = 1,txt:size(1) do
for j = 1,txt:size(2) do
local on_ix = txt[{i, j}]
if on_ix == 0 then
break
end
txt_mat[{i, j, on_ix}] = 1
end
end
txt_mat = txt_mat:float():cuda()
local out = protos.enc_doc:forward(txt_mat):clone()
out = torch.mean(out,1)
return out
end
function extract_txt_char(filename)
-- average all text features together.
local txt = torch.load(filename):permute(1,3,2)
txt = txt:reshape(txt:size(1)*txt:size(2),txt:size(3)):float():cuda()
if opt.txt_limit > 0 then
local actual_limit = math.min(txt:size(1), opt.txt_limit)
txt_order = torch.randperm(txt:size(1)):sub(1,actual_limit)
local tmp = txt:clone()
for i = 1,actual_limit do
txt[{i,{}}]:copy(tmp[{txt_order[i],{}}])
end
txt = txt:narrow(1,1,actual_limit)
end
local txt_mat = torch.zeros(txt:size(1), txt:size(2), #alphabet)
for i = 1,txt:size(1) do
for j = 1,txt:size(2) do
local on_ix = txt[{i, j}]
if on_ix == 0 then
break
end
txt_mat[{i, j, on_ix}] = 1
end
end
txt_mat = txt_mat:float():cuda()
local out = protos.enc_doc:forward(txt_mat):clone()
return torch.mean(out,1)
end
local txt_dir
if opt.ttype == 'char' then
txt_dir = string.format('%s/text_c%d', opt.data_dir, opt.num_caption)
else -- word
txt_dir = string.format('%s/word_c%d', opt.data_dir, opt.num_caption)
vocab_size = 0
for k,v in pairs(model.vocab) do
vocab_size = vocab_size + 1
end
end
local img_dir = string.format('%s/images', opt.data_dir)
local testcls = string.format('%s/%s', opt.data_dir, opt.testclasses)
local kvals = { 1, 5, 10, 50 }
local res = retrieval_at_k(txt_dir, img_dir, testcls, kvals, torch.dot, 1)
--print(string.format('mAP@1: %6.4f\n', res[1]))
--print(string.format('mAP@5: %6.4f\n', res[5]))
--print(string.format('mAP@10: %6.4f\n', res[10]))
print(string.format('mAP@50: %6.4f\n', res[50]))
--file = io.open(opt.outfile, "w")
--io.output(file)
io.write(string.format('%.4f,%.4f,%.4f,%.4f\n',
res[1],
res[5],
res[10],
res[50]))
io.close(file)