-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpatialSAD.lua
61 lines (51 loc) · 1.63 KB
/
SpatialSAD.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 SpatialSAD, parent = torch.class('nn.SpatialSAD', 'nn.Module')
function SpatialSAD:__init(nInputPlane, nOutputPlane, kW, kH, dW, dH)
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.weight = torch.Tensor(nOutputPlane, nInputPlane, kH, kW)
self.bias = torch.Tensor(nOutputPlane)
self.gradWeight = torch.Tensor(nOutputPlane, nInputPlane, kH, kW)
self.gradBias = torch.Tensor(nOutputPlane)
self:reset()
end
function SpatialSAD:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1/math.sqrt(self.kW*self.kH*self.nInputPlane)
end
self.weight:apply(function()
return torch.uniform(-stdv, stdv)
end)
self.bias:apply(function()
return torch.uniform(-stdv, stdv)
end)
end
function SpatialSAD:templates(templates)
if templates then
if templates:size(1) ~= self.nOutputPlane then
error('templates() you must provide <nOutputPlane> templates')
end
self.weight:copy(templates)
return self
end
return self.weight
end
function SpatialSAD:updateOutput(input)
return input.nn.SpatialSAD_updateOutput(self, input)
end
function SpatialSAD:updateGradInput(input, gradOutput)
if self.gradInput then
return input.nn.SpatialSAD_updateGradInput(self, input, gradOutput)
end
end
function SpatialSAD:accGradParameters(input, gradOutput, scale)
return input.nn.SpatialSAD_accGradParameters(self, input, gradOutput, scale)
end