-
Notifications
You must be signed in to change notification settings - Fork 958
/
Copy pathVolumetricConvolution.lua
169 lines (157 loc) · 5 KB
/
VolumetricConvolution.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
165
166
167
168
169
local THNN = require 'nn.THNN'
local VolumetricConvolution, parent = torch.class('nn.VolumetricConvolution', 'nn.Module')
function VolumetricConvolution:__init(nInputPlane, nOutputPlane, kT, kW, kH, dT, dW, dH, padT, padW, padH)
parent.__init(self)
dT = dT or 1
dW = dW or 1
dH = dH or 1
self.nInputPlane = nInputPlane
self.nOutputPlane = nOutputPlane
self.kT = kT
self.kW = kW
self.kH = kH
self.dT = dT
self.dW = dW
self.dH = dH
self.padT = padT or 0
self.padW = padW or self.padT
self.padH = padH or self.padW
self.weight = torch.Tensor(nOutputPlane, nInputPlane, kT, kH, kW)
self.bias = torch.Tensor(nOutputPlane)
self.gradWeight = torch.Tensor(nOutputPlane, nInputPlane, kT, kH, kW)
self.gradBias = torch.Tensor(nOutputPlane)
self:reset()
end
function VolumetricConvolution:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1/math.sqrt(self.kT*self.kW*self.kH*self.nInputPlane)
end
if nn.oldSeed then
self.weight:apply(function()
return torch.uniform(-stdv, stdv)
end)
if self.bias then
self.bias:apply(function()
return torch.uniform(-stdv, stdv)
end)
end
else
self.weight:uniform(-stdv, stdv)
if self.bias then
self.bias:uniform(-stdv, stdv)
end
end
end
function VolumetricConvolution:noBias()
self.bias = nil
self.gradBias = nil
return self
end
function VolumetricConvolution:updateOutput(input)
self.finput = self.finput or input.new()
self.fgradInput = self.fgradInput or input.new()
if torch.typename(input):find('torch%.Cuda.*Tensor') then
input.THNN.VolumetricConvolution_updateOutput(
input:cdata(),
self.output:cdata(),
self.weight:cdata(),
THNN.optionalTensor(self.bias),
self.finput:cdata(),
self.fgradInput:cdata(),
self.dT, self.dW, self.dH,
self.padT, self.padW, self.padH
)
else
input.THNN.VolumetricConvolutionMM_updateOutput(
input:cdata(),
self.output:cdata(),
self.weight:cdata(),
THNN.optionalTensor(self.bias),
self.finput:cdata(),
self.kT, self.kW, self.kH,
self.dT, self.dW, self.dH,
self.padT, self.padW, self.padH
)
end
return self.output
end
function VolumetricConvolution:updateGradInput(input, gradOutput)
if torch.typename(input):find('torch%.Cuda.*Tensor') then
input.THNN.VolumetricConvolution_updateGradInput(
input:cdata(),
gradOutput:cdata(),
self.gradInput:cdata(),
self.weight:cdata(),
self.finput:cdata(),
self.dT, self.dW, self.dH,
self.padT, self.padW, self.padH
)
return self.gradInput
else
if self.gradInput then
input.THNN.VolumetricConvolutionMM_updateGradInput(
input:cdata(),
gradOutput:cdata(),
self.gradInput:cdata(),
self.weight:cdata(),
self.finput:cdata(),
self.fgradInput:cdata(),
self.kT, self.kW, self.kH,
self.dT, self.dW, self.dH,
self.padT, self.padW, self.padH
)
return self.gradInput
end
end
end
function VolumetricConvolution:accGradParameters(input, gradOutput, scale)
if torch.typename(input):find('torch%.Cuda.*Tensor') then
input.THNN.VolumetricConvolution_accGradParameters(
input:cdata(),
gradOutput:cdata(),
self.gradWeight:cdata(),
THNN.optionalTensor(self.gradBias),
self.finput:cdata(),
self.fgradInput:cdata(),
self.dT, self.dW, self.dH,
self.padT, self.padW, self.padH,
scale or 1
)
else
input.THNN.VolumetricConvolutionMM_accGradParameters(
input:cdata(),
gradOutput:cdata(),
self.gradWeight:cdata(),
THNN.optionalTensor(self.gradBias),
self.finput:cdata(),
self.kT, self.kW, self.kH,
self.dT, self.dW, self.dH,
self.padT, self.padW, self.padH,
scale or 1
)
end
end
function VolumetricConvolution:type(type, tensorCache)
if self.finput then self.finput:set() end
if self.fgradInput then self.fgradInput:set() end
return parent.type(self, type, tensorCache)
end
function VolumetricConvolution:clearState()
nn.utils.clear(self, 'finput', 'fgradInput', '_input', '_gradOutput')
return parent.clearState(self)
end
function VolumetricConvolution:__tostring__()
local s = string.format('%s(%d -> %d, %dx%dx%d', torch.type(self),
self.nInputPlane, self.nOutputPlane, self.kT, self.kW, self.kH)
if self.dT ~= 1 or self.dW ~= 1 or self.dH ~= 1 or
self.padT ~= 0 or self.padW ~= 0 or self.padH ~= 0 then
s = s .. string.format(', %d,%d,%d', self.dT, self.dW, self.dH)
end
if (self.padT or self.padW or self.padH) and
(self.padT ~=0 or self.padW ~= 0 or self.padH ~= 0) then
s = s .. ', ' .. self.padT .. ',' .. self.padW .. ',' .. self.padH
end
return s .. ')'
end