Convolution.lua 4.35 KB
Newer Older
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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
-- Copyright 2016-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the license found in the
-- LICENSE file in the root directory of this source tree.

return function(sparseconvnet)
  local C = sparseconvnet.C

  local Convolution, parent = torch.class(
    'sparseconvnet.Convolution', 'nn.Module', sparseconvnet)

  function Convolution:__init(dimension, nInputPlanes, nOutputPlanes,
      filterSize, filterStride, bias)
    parent.__init(self)
    self.dimension = dimension
    self.nInputPlanes = nInputPlanes
    self.nOutputPlanes = nOutputPlanes
    self.filterSize = sparseconvnet.toLongTensor(filterSize,dimension)
    self.filterStride = sparseconvnet.toLongTensor(filterStride,dimension)
    self.filterVolume = self.filterSize:prod()
    self.weight = torch.Tensor(nInputPlanes*self.filterVolume,nOutputPlanes)
    self.gradWeight = torch.Tensor(nInputPlanes*self.filterVolume,nOutputPlanes)
    if (type(bias) ~= 'boolean') or bias then
      self.bias = torch.Tensor(nOutputPlanes)
      self.gradBias = torch.Tensor(nOutputPlanes)
    end
    self.output = {
      features = torch.Tensor(),
    }
    self.gradInput = {
      features = torch.Tensor()
    }
    self:reset()
  end

  function Convolution:reset()
    local stdv = math.sqrt(2/self.nInputPlanes/self.filterVolume)
    self.weight:normal(0, stdv)
    if self.bias then
      self.bias:zero()
    end
    return self
  end

  function Convolution:updateOutput(input)
    assert(input.features:size(2)==self.nInputPlanes)
    self.output.metadata = input.metadata
    self.output.spatialSize =
    torch.cdiv(input.spatialSize-self.filterSize,self.filterStride)+1
    self.shared.forwardPassMultiplyAddCount=
    self.shared.forwardPassMultiplyAddCount+
    C.dimTypedFn(self.dimension, self._type, 'Convolution_updateOutput')(
      input.spatialSize:cdata(),
      self.output.spatialSize:cdata(),
      self.filterSize:cdata(),
      self.filterStride:cdata(),
      input.metadata.ffi,
      input.features:cdata(),
      self.output.features:cdata(),
      self.weight:cdata(),
      self.bias and self.bias:cdata(),
      self.filterVolume,
      self.shared.rulesBuffer and self.shared.rulesBuffer:cdata())
    self.shared.forwardPassHiddenStates=
    self.shared.forwardPassHiddenStates + self.output.features:nElement()
    return self.output
  end

  function Convolution:backward(input, gradOutput)
    C.dimTypedFn(self.dimension, self._type, 'Convolution_backward')(
      input.spatialSize:cdata(),
      self.output.spatialSize:cdata(),
      self.filterSize:cdata(),
      self.filterStride:cdata(),
      input.metadata.ffi,
      input.features:cdata(),
      self.gradInput.features:cdata(),
      gradOutput.features:cdata(),
      self.weight:cdata(),
      self.gradWeight:cdata(),
      self.gradBias and self.gradBias:cdata(),
      self.filterVolume,
      self.shared.rulesBuffer and self.shared.rulesBuffer:cdata())
    return self.gradInput
  end

  function Convolution:type(type,tensorCache)
    if type==nil then
      return self._type
    else
      self._type=type
      self.weight = self.weight:type(type)
      self.gradWeight =self.gradWeight:type(type)
      if self.bias then
        self.bias = self.bias:type(type)
        self.gradBias =self.gradBias:type(type)
      end
      self.output.features=self.output.features:type(type)
      self.gradInput.features=self.gradInput.features:type(type)
    end
  end

  function Convolution:__tostring()
    local s = 'Convolution ' .. self.nInputPlanes .. '->' .. self.nOutputPlanes..' C'
    if self.filterSize:max()==self.filterSize:min() and
    self.filterStride:max()==self.filterStride:min() then
      s=s..self.filterSize[1] ..(self.filterStride[1]==1 and
        '' or '/'..self.filterStride[1])
    else
      s=s..'('..self.filterSize[1]
      for i=2,self.dimension do
        s=s..','..self.filterSize[i]
      end
      s=s..')/('..self.filterStride[1]
      for i=2,self.dimension do
        s=s..','..self.filterStride[i]
      end
      s=s..')'
    end
    return s
  end

  function Convolution:clearState()
    self.output={features=self.output.features:set()}
    self.gradInput={features=self.gradInput.features:set()}
    self.rules=nil
  end

  function Convolution:suggestInputSize(nOut)
    if self.valid then
      return nOut
    else
      return torch.cmul(nOut-1,self.filterStride)+self.filterSize
    end
  end
end