Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--SpatialContrastiveNormalization.lua42
-rw-r--r--SpatialDivisiveNormalization.lua5
-rw-r--r--init.lua1
3 files changed, 45 insertions, 3 deletions
diff --git a/SpatialContrastiveNormalization.lua b/SpatialContrastiveNormalization.lua
new file mode 100644
index 0000000..262d3b1
--- /dev/null
+++ b/SpatialContrastiveNormalization.lua
@@ -0,0 +1,42 @@
+local SpatialContrastiveNormalization, parent = torch.class('nn.SpatialContrastiveNormalization','nn.Module')
+
+function SpatialContrastiveNormalization:__init(nInputPlane, kernel, threshold, thresval)
+ parent.__init(self)
+
+ -- get args
+ self.nInputPlane = nInputPlane or 1
+ self.kernel = kernel or torch.Tensor(9,9):fill(1)
+ self.threshold = threshold or 1e-4
+ self.thresval = thresval or 1e-4
+ local kdim = self.kernel:nDimension()
+
+ -- check args
+ if kdim ~= 2 and kdim ~= 1 then
+ error('<SpatialContrastiveNormalization> averaging kernel must be 2D or 1D')
+ end
+ if (self.kernel:size(1) % 2) == 0 or (kdim == 2 and (self.kernel:size(2) % 2) == 0) then
+ error('<SpatialContrastiveNormalization> averaging kernel must have ODD dimensions')
+ end
+
+ -- instantiate sub+div normalization
+ self.normalizer = nn.Sequential()
+ self.normalizer:add(nn.SpatialSubtractiveNormalization(self.nInputPlane, self.kernel))
+ self.normalizer:add(nn.SpatialDivisiveNormalization(self.nInputPlane, self.kernel,
+ self.threshold, self.threshval))
+end
+
+function SpatialContrastiveNormalization:updateOutput(input)
+ self.output = self.normalizer:forward(input)
+ return self.output
+end
+
+function SpatialContrastiveNormalization:updateGradInput(input, gradOutput)
+ self.gradInput = self.normalizer:backward(input, gradOutput)
+ return self.gradInput
+end
+
+function SpatialContrastiveNormalization:type(type)
+ parent.type(self,type)
+ self.normalizer:type(type)
+ return self
+end
diff --git a/SpatialDivisiveNormalization.lua b/SpatialDivisiveNormalization.lua
index eaf7465..33668e9 100644
--- a/SpatialDivisiveNormalization.lua
+++ b/SpatialDivisiveNormalization.lua
@@ -18,9 +18,6 @@ function SpatialDivisiveNormalization:__init(nInputPlane, kernel, threshold, thr
error('<SpatialDivisiveNormalization> averaging kernel must have ODD dimensions')
end
- -- normalize kernel
- self.kernel:div(self.kernel:sum() * self.nInputPlane)
-
-- padding values
local padH = math.floor(self.kernel:size(1)/2)
local padW = padH
@@ -62,6 +59,7 @@ function SpatialDivisiveNormalization:__init(nInputPlane, kernel, threshold, thr
-- set kernel and bias
if kdim == 2 then
+ self.kernel:div(self.kernel:sum() * self.nInputPlane)
for i = 1,self.nInputPlane do
self.meanestimator.modules[2].weight[i] = self.kernel
self.stdestimator.modules[3].weight[i] = self.kernel
@@ -69,6 +67,7 @@ function SpatialDivisiveNormalization:__init(nInputPlane, kernel, threshold, thr
self.meanestimator.modules[2].bias:zero()
self.stdestimator.modules[3].bias:zero()
else
+ self.kernel:div(self.kernel:sum() * math.sqrt(self.nInputPlane))
for i = 1,self.nInputPlane do
self.meanestimator.modules[2].weight[i]:copy(self.kernel)
self.meanestimator.modules[3].weight[i]:copy(self.kernel)
diff --git a/init.lua b/init.lua
index d589eec..c1c7b79 100644
--- a/init.lua
+++ b/init.lua
@@ -63,6 +63,7 @@ torch.include('nn', 'TemporalConvolution.lua')
torch.include('nn', 'TemporalSubSampling.lua')
torch.include('nn', 'SpatialSubtractiveNormalization.lua')
torch.include('nn', 'SpatialDivisiveNormalization.lua')
+torch.include('nn', 'SpatialContrastiveNormalization.lua')
torch.include('nn', 'SpatialZeroPadding.lua')
torch.include('nn', 'VolumetricConvolution.lua')