Skip to content

Commit d999489

Browse files
committed
initial commit
1 parent c16448c commit d999489

7 files changed

+161
-17
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
build

CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)
3+
CMAKE_POLICY(VERSION 2.6)
4+
IF(LUAROCKS_PREFIX)
5+
MESSAGE(STATUS "Installing Torch through Luarocks")
6+
STRING(REGEX REPLACE "(.*)lib/luarocks/rocks.*" "\\1" CMAKE_INSTALL_PREFIX "${LUAROCKS_PREFIX}")
7+
MESSAGE(STATUS "Prefix inferred from Luarocks: ${CMAKE_INSTALL_PREFIX}")
8+
ENDIF()
9+
FIND_PACKAGE(Torch REQUIRED)
10+
11+
SET(src)
12+
FILE(GLOB luasrc *.lua)
13+
SET(luasrc ${luasrc} test/test.lua)
14+
ADD_TORCH_PACKAGE(cbp "${src}" "${luasrc}" "Compact Bilinear Pooling")

CompactBilinearPooling.lua

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
local CompactBilinearPooling, parent = torch.class('nn.CompactBilinearPooling', 'nn.Module')
2+
3+
-- Reference:
4+
-- Multimodal Compact Bilinear Pooling for Visual Question Answering and Visual Grounding
5+
-- Fukui et al. (2016) http://arxiv.org/abs/1606.01847
6+
function CompactBilinearPooling:__init(outputSize)
7+
self.outputSize = outputSize
8+
self:reset()
9+
end
10+
11+
function CompactBilinearPooling:reset()
12+
self.h = torch.LongTensor()
13+
self.s = torch.IntTensor()
14+
self.v = torch.Tensor()
15+
self.output = torch.Tensor()
16+
end
17+
18+
function CompactBilinearPooling:sample()
19+
for i=1,2 do
20+
for k=1,self.h[i]:size(#self.h[i]:size()) do
21+
self.h[i][k] = torch.random(1,self.outputSize) -- sample from (1,..,C)
22+
self.s[i][k] = torch.random(0,1)*2-1 -- sample from (-1,1)
23+
end
24+
end
25+
end
26+
27+
function CompactBilinearPooling:psi()
28+
self.v:zero()
29+
for i=1,2 do
30+
if 1 == #self.h[i]:size() then -- no batch
31+
for k=1,self.h[i]:size(#self.h[i]:size()) do
32+
self.v[i][self.h[i][k]]:add(self.s[i][k]*self.input[i][k])
33+
end
34+
else -- batch
35+
for k=1,self.h[i]:size(#self.h[i]:size()) do
36+
self.v[i][{{},{k}}]:add(self.s[i][k]*self.input[i][{{},{k}}])
37+
end
38+
end
39+
end
40+
end
41+
42+
function CompactBilinearPooling:updateOutput(input)
43+
self.input = input
44+
local inputSizes1 = input[1]:size()
45+
local inputSizes2 = input[2]:size()
46+
local sizes1 = inputSizes1[#inputSizes1]
47+
local sizes2 = inputSizes2[#inputSizes2]
48+
self.h:resize(2, sizes1)
49+
self.s:resize(2, sizes1)
50+
self:sample()
51+
52+
if 2 > #inputSizes1 then -- no batch
53+
self.v:resize(2, self.outputSize)
54+
elseif 2 == #inputSize1 then -- batch
55+
local batchSize = inputSizes1[1]
56+
self.v:resize(2, batchSize, self.outputSize)
57+
else
58+
assert(false, '# of dimensions > 2')
59+
end
60+
self:psi()
61+
end

LICENSE

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
Copyright (c) 2016, Jin Hwa Kim
1+
Copyright (c) 2016 NAVER Corp. and Seoul National University R&DB Foundation
22
All rights reserved.
33

4+
Author: [email protected] (Jin-Hwa Kim)
5+
46
Redistribution and use in source and binary forms, with or without
57
modification, are permitted provided that the following conditions are met:
68

7-
* Redistributions of source code must retain the above copyright notice, this
8-
list of conditions and the following disclaimer.
9+
1. Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
911

10-
* Redistributions in binary form must reproduce the above copyright notice,
11-
this list of conditions and the following disclaimer in the documentation
12-
and/or other materials provided with the distribution.
12+
2. Redistributions in binary form must reproduce the above copyright
13+
notice, this list of conditions and the following disclaimer in the
14+
documentation and/or other materials provided with the distribution.
1315

14-
* Neither the name of cbp nor the names of its
15-
contributors may be used to endorse or promote products derived from
16-
this software without specific prior written permission.
16+
3. Neither the names of NAVER Corp. and Seoul National University R&DB
17+
Foundation nor the names of its contributors may be used to endorse or
18+
promote products derived from this software without specific prior
19+
written permission.
1720

1821
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1922
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
POSSIBILITY OF SUCH DAMAGE.

init.lua

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'nn'
2+
3+
-- create global rnn table:
4+
cbp = {}
5+
cbp.version = 1
6+
7+
unpack = unpack or table.unpack
8+
9+
-- for testing:
10+
torch.include('cbp', 'test.lua')
11+
12+
-- support modules
13+
torch.include('cbp', 'CompactBilnearPooling.lua')
14+
15+
-- prevent likely name conflicts
16+
nn.cbp = cbp

rocks/cbp-scm-1.rockspec

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package = "cbp"
2+
version = "scm-1"
3+
4+
source = {
5+
url = "git://github.com/jnhwkim/cbp",
6+
tag = "master"
7+
}
8+
9+
description = {
10+
summary = "Compact Bilinear Pooling for Torch7 nn",
11+
detailed = [[
12+
Torch7 Implementation of Compact Bilnear Pooling
13+
]],
14+
homepage = "https://github.com/jnhwkim/cbp",
15+
license = "BSD-3 Clause"
16+
}
17+
18+
dependencies = {
19+
"torch >= 7.0",
20+
"nn >= 1.0"
21+
}
22+
23+
build = {
24+
type = "command",
25+
build_command = [[
26+
cmake -E make_directory build;
27+
cd build;
28+
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)";
29+
$(MAKE)
30+
]],
31+
install_command = "cd build && $(MAKE) install"
32+
}

test/test.lua

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'nn'
2+
3+
local cbptest = torch.TestSuite()
4+
local precision = 1e-5
5+
6+
function cbptest.testPsi()
7+
assert(true, 'Example')
8+
end
9+
10+
function cbp.test(tests)
11+
mytester = torch.Tester()
12+
mytester:add(cbptest)
13+
math.randomseed(os.time())
14+
mytester:run(tests)
15+
end

0 commit comments

Comments
 (0)