This repository was archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpatialConvolution.lua
More file actions
152 lines (134 loc) · 4.16 KB
/
SpatialConvolution.lua
File metadata and controls
152 lines (134 loc) · 4.16 KB
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
local SpatialConvolution, parent = torch.class('mklnn.SpatialConvolution', 'nn.Module')
local wrapper = mklnn.wrapper
local getType = mklnn.getType
function SpatialConvolution:__init(nInputPlane, nOutputPlane, kW, kH, dW, dH, padW, padH,group)
parent.__init(self)
dW = dW or 1
dH = dH or 1
self.nInputPlane = nInputPlane
self.nOutputPlane = nOutputPlane
self.kW = kW
self.kH = kH
self.dW = dW
self.dH = dH
self.padW = padW or 0
self.padH = padH or self.padW
self.group = group or 1
self.weight = torch.Tensor(nOutputPlane, nInputPlane*kH*kW/self.group)
self.bias = torch.Tensor(nOutputPlane)
self.gradWeight = torch.Tensor(nOutputPlane, nInputPlane*kH*kW/self.group)
self.gradBias = torch.Tensor(nOutputPlane)
self:reset()
end
function SpatialConvolution:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1/math.sqrt(self.kW*self.kH*self.nInputPlane)
end
if nn.oldSeed then
self.weight:apply(function()
return torch.uniform(-stdv, stdv)
end)
self.bias:apply(function()
return torch.uniform(-stdv, stdv)
end)
else
self.weight:uniform(-stdv, stdv)
self.bias:uniform(-stdv, stdv)
end
end
local function makeContiguous(self, input, gradOutput)
--[[
if not input:isContiguous() then
self._input = self._input or input.new()
self._input:resizeAs(input):copy(input)
input = self._input
end
if gradOutput then
if not gradOutput:isContiguous() then
self._gradOutput = self._gradOutput or gradOutput.new()
self._gradOutput:resizeAs(gradOutput):copy(gradOutput)
gradOutput = self._gradOutput
end
end
]]--
return input, gradOutput
end
function SpatialConvolution:updateOutput(input)
if self.dnnPrimitives then
self.mkldnnInitOk = 1
else
self.mkldnnInitOk = 0
end
self.dnnPrimitives = self.dnnPrimitives or torch.LongTensor(30)
self.output = self.output:mkl()
self.gradInput = self.gradInput:mkl()
if self.padding then
self.padW = self.padding
self.padH = self.padding
self.padding = nil
end
input = makeContiguous(self, input)
wrapper(getType(input),'SpatialConvolution_forward',
input:cdata(),
self.output:cdata(),
self.weight:cdata(),
self.bias:cdata(),
self.dnnPrimitives:cdata(),self.mkldnnInitOk,
self.kW, self.kH,
self.dW, self.dH,
self.padW, self.padH,self.group
)
return self.output
end
function SpatialConvolution:updateGradInput(input, gradOutput)
if self.gradInput then
input, gradOutput = makeContiguous(self, input, gradOutput)
wrapper(getType(input),'SpatialConvolution_bwdData',
input:cdata(),
gradOutput:cdata(),
self.gradInput:cdata(),
self.weight:cdata(),
self.bias:cdata(),
self.dnnPrimitives:cdata(),self.mkldnnInitOk,
self.kW, self.kH,
self.dW, self.dH,
self.padW, self.padH,self.group
)
return self.gradInput
end
end
function SpatialConvolution:accGradParameters(input, gradOutput, scale)
scale = scale or 1
input, gradOutput = makeContiguous(self, input, gradOutput)
wrapper(getType(input),'SpatialConvolution_bwdFilter',
input:cdata(),
gradOutput:cdata(),
self.gradWeight:cdata(),
self.gradBias:cdata(),
self.dnnPrimitives:cdata(),self.mkldnnInitOk,
self.kW, self.kH,
self.dW, self.dH,
self.padW, self.padH,
scale,self.group
)
end
function SpatialConvolution:type(type,tensorCache)
return parent.type(self,type,tensorCache)
end
function SpatialConvolution:__tostring__()
local s = string.format('%s(%d -> %d, %dx%d', torch.type(self),
self.nInputPlane, self.nOutputPlane, self.kW, self.kH)
if self.dW ~= 1 or self.dH ~= 1 or self.padW ~= 0 or self.padH ~= 0 then
s = s .. string.format(', %d,%d', self.dW, self.dH)
end
if (self.padW or self.padH) and (self.padW ~= 0 or self.padH ~= 0) then
s = s .. ', ' .. self.padW .. ',' .. self.padH
end
return s .. ')'
end
function SpatialConvolution:clearState()
nn.utils.clear(self, '_input', '_gradOutput')
return parent.clearState(self)
end