forked from szagoruyko/imagine-nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpatialPyramidPooling.lua
61 lines (45 loc) · 1.32 KB
/
SpatialPyramidPooling.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
local SpatialPyramidPooling, parent = torch.class('inn.SpatialPyramidPooling', 'nn.Module')
function SpatialPyramidPooling:__init(pyr)
parent.__init(self)
self.pyr = pyr
self.dimd = 1
self.module = nn.Concat(self.dimd)
for i=1,#self.pyr do
local t = nn.Sequential()
t:add(nn.SpatialAdaptiveMaxPooling(self.pyr[i][1],self.pyr[i][2]))
t:add(nn.View(-1):setNumInputDims(3))
self.module:add(t)
end
self.output = torch.Tensor()
end
function SpatialPyramidPooling:updateOutput(input)
local dimd = 1
if input:nDimension() == 4 then
dimd = dimd + 1
end
if self.dimd ~= dimd then
self.dimd = dimd
self.module.dimension = dimd
end
assert(input:type()==self.output:type(),'Wrong input type!')
self.output = self.module:updateOutput(input)
return self.output
end
function SpatialPyramidPooling:updateGradInput(input, gradOutput)
local dimd = 1
if input:nDimension() == 4 then
dimd = dimd + 1
end
if self.dimd ~= dimd then
self.dimd = dimd
self.module.dimension = dimd
end
assert(input:type()==self.gradInput:type(),'Wrong input type!')
self.gradInput = self.module:updateGradInput(input,gradOutput)
return self.gradInput
end
function SpatialPyramidPooling:type(type)
parent.type(self,type)
self.module:type(type)
return self
end