-
Notifications
You must be signed in to change notification settings - Fork 1
/
stn_L3.lua
55 lines (45 loc) · 1.59 KB
/
stn_L3.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
require 'stn'
require 'image'
spanet=nn.Sequential()
local concat=nn.ConcatTable()
-- first branch is there to transpose inputs to BHWD, for the bilinear sampler
tranet=nn.Sequential()
tranet:add(nn.Identity())
tranet:add(nn.Transpose({2,3},{3,4}))
-- second branch is the localization network
local locnet = nn.Sequential()
--locnet:add(nn.SpatialContrastiveNormalization(3,image.gaussian(5)))
locnet:add(cudnn.SpatialMaxPooling(2,2,2,2))
locnet:add(cudnn.SpatialConvolution(128,64,5,5))
locnet:add(cudnn.ReLU(true))
locnet:add(cudnn.SpatialMaxPooling(2,2,2,2))
locnet:add(cudnn.SpatialConvolution(64,32,3,3))
locnet:add(cudnn.ReLU(true))
locnet:add(cudnn.SpatialMaxPooling(2,2,2,2))
locnet:add(cudnn.SpatialConvolution(32,20,5,5))
locnet:add(cudnn.ReLU(true))
locnet:add(cudnn.SpatialMaxPooling(2,2,2,2))
locnet:add(cudnn.SpatialConvolution(20,20,3,3))
locnet:add(cudnn.ReLU(true))
locnet:add(nn.View(20*3*3))
locnet:add(nn.Linear(20*3*3,20))
locnet:add(cudnn.ReLU(true))
-- we initialize the output layer so it gives the identity transform
local outLayer = nn.Linear(20,6)
outLayer.weight:fill(0)
local bias = torch.FloatTensor(6):fill(0)
bias[1]=1
bias[5]=1
outLayer.bias:copy(bias)
locnet:add(outLayer)
-- there we generate the grids
locnet:add(nn.View(2,3))
locnet:add(nn.AffineGridGeneratorBHWD(128,128))
-- we need a table input for the bilinear sampler, so we use concattable
concat:add(tranet)
concat:add(locnet)
spanet:add(concat)
spanet:add(nn.BilinearSamplerBHWD())
-- and we transpose back to standard BDHW format for subsequent processing by nn modules
spanet:add(nn.Transpose({3,4},{2,3}))
return spanet