-
Notifications
You must be signed in to change notification settings - Fork 5
/
2_model.lua
executable file
·207 lines (159 loc) · 7.09 KB
/
2_model.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
----------------------------------------------------------------------
-- This script demonstrates how to define a couple of different
-- models for Clustering Learning research
--
-- E. Culurciello modified original from Clement Farabet
-- August 2012
----------------------------------------------------------------------
require 'torch' -- torch
require 'image' -- to visualize the dataset
require 'nnx' -- provides all sorts of trainable modules/layers
require 'eex'
----------------------------------------------------------------------
-- parse command line arguments
if not opt then
print '==> processing options'
cmd = torch.CmdLine()
cmd:text()
cmd:text('SVHN Model Definition')
cmd:text()
cmd:text('Options:')
cmd:option('-model', 'convnet', 'type of model to construct: linear | mlp | convnet')
cmd:option('-visualize', true, 'visualize input data and weights during training')
cmd:text()
opt = cmd:parse(arg or {})
end
----------------------------------------------------------------------
print '==> define parameters'
-- 10-class problem
noutputs = 10
-- input dimensions
nfeats = 3
width = 32
height = 32
ninputs = nfeats*width*height
-- hidden units, filter sizes (for ConvNet only):
nstates = {16,256,128}
fanin = {1,4}
filtsize = 5
poolsize = 2
normkernel = image.gaussian1D(3)
----------------------------------------------------------------------
print '==> construct model'
if opt.model == '1st-layer' then
o1size = trainData.data:size(3) - is + 1 -- size of spatial conv layer output
cvstepsize = 1
poolsize = 2
l1netoutsize = o1size/poolsize/cvstepsize
model = nn.Sequential()
model:add(nn.SpatialConvolution(3, nk1, is, is, cvstepsize, cvstepsize))
--model:add(nn.HardShrink(0.5))
model:add(nn.Tanh())
--model:add(nn.SpatialSubSampling(nk1, poolsize, poolsize, poolsize, poolsize))
--model:add(nn.SpatialMaxPooling(poolsize, poolsize, poolsize, poolsize))
model:add(nn.SpatialLPPooling(nk1, 2, poolsize, poolsize, poolsize, poolsize))
model:add(nn.SpatialSubtractiveNormalization(nk1, normkernel))
elseif opt.model == '2nd-layer' then
o1size = trainData.data:size(3) - is + 1 -- size of spatial conv layer output
cvstepsize = 1
poolsize = 2
l1netoutsize = o1size/poolsize/cvstepsize
model = nn.Sequential()
model:add(nn.SpatialConvolution(nk1, nk2, is, is, cvstepsize, cvstepsize))
--model:add(nn.SpatialConvolutionMap(nn.tables.random(nk1, nk2, 8), is, is))
--model:add(nn.HardShrink(0.5))
model:add(nn.Tanh())
--model:add(nn.SpatialSubSampling(nk2, poolsize, poolsize, poolsize, poolsize))
--model:add(nn.SpatialMaxPooling(poolsize, poolsize, poolsize, poolsize))
model:add(nn.SpatialLPPooling(nk2, 2, poolsize, poolsize, poolsize, poolsize))
model:add(nn.SpatialSubtractiveNormalization(nk2, normkernel))
elseif opt.model == '1st-layer-dist' then
o1size = trainData.data:size(3) - is + 1 -- size of spatial conv layer output
poolsize = 2
l1netoutsize = o1size/poolsize
model = nn.Sequential()
model:add(nn.SpatialSAD(3, nk1, is, is))
model:add(nn.Reshape(nk1*o1size*o1size))
model:add(nn.Mul(nk1*o1size*o1size))
model:add(nn.Reshape(nk1,o1size,o1size))
model:add(nn.SpatialSubtractiveNormalization(nk1, normkernel))
model:add(nn.Reshape(nk1*o1size*o1size))
model:add(nn.Mul(nk1*o1size*o1size))
model:add(nn.Reshape(nk1,o1size,o1size))
model:add(nn.SpatialSubtractiveNormalization(nk1, normkernel))
model:add(nn.Tanh())
model:add(nn.SpatialLPPooling(nk1, 2, poolsize, poolsize, poolsize, poolsize))
model:add(nn.SpatialSubtractiveNormalization(nk1, normkernel))
elseif opt.model == '2nd-layer-dist' then
o1size = trainData.data:size(3) - is + 1 -- size of spatial conv layer output
poolsize = 2
l1netoutsize = o1size/poolsize
model = nn.Sequential()
model:add(nn.SpatialSAD(nk1, nk2, is, is))
model:add(nn.Reshape(nk2*o1size*o1size))
model:add(nn.Mul(nk2*o1size*o1size))
model:add(nn.Reshape(nk2,o1size,o1size))
model:add(nn.SpatialSubtractiveNormalization(nk2, normkernel))
model:add(nn.Reshape(nk2*o1size*o1size))
model:add(nn.Mul(nk2*o1size*o1size))
model:add(nn.Reshape(nk2,o1size,o1size))
model:add(nn.SpatialSubtractiveNormalization(nk2, normkernel))
model:add(nn.Tanh())
model:add(nn.SpatialLPPooling(nk2, 2, poolsize, poolsize, poolsize, poolsize))
model:add(nn.SpatialSubtractiveNormalization(nk2, normkernel))
elseif opt.model == '2mlp-classifier' then
nhiddens = 128
outsize = 10 -- in CIFAR, SVHN datasets
model = nn.Sequential()
model:add(nn.Reshape(nk*l1netoutsize^2))
model:add(nn.Linear(nk*l1netoutsize^2, nhiddens))
model:add(nn.Tanh())
model:add(nn.Linear(nhiddens,outsize))
elseif opt.model == '2mlp-cl-2layers' then
nhiddens = 256
outsize = 10 -- in CIFAR, SVHN datasets
model = nn.Sequential()
model:add(nn.Linear(nk2*l1netoutsize^2 + nk1*l1o^2, nhiddens))
model:add(nn.Tanh())
model:add(nn.Linear(nhiddens,outsize))
elseif opt.model == 'convnet' then
-- a typical convolutional network, with locally-normalized hidden
-- units, and L2-pooling
-- Note: the architecture of this convnet is loosely based on Pierre Sermanet's
-- work on this dataset (http://arxiv.org/abs/1204.3968). In particular
-- the use of LP-pooling (with P=2) has a very positive impact on
-- generalization. Normalization is not done exactly as proposed in
-- the paper, and low-level (first layer) features are not fed to
-- the classifier.
model = nn.Sequential()
-- stage 1 : filter bank -> squashing -> L2 pooling -> normalization
model:add(nn.SpatialConvolutionMap(nn.tables.random(nfeats, nstates[1], fanin[1]), filtsize, filtsize))
model:add(nn.Tanh())
model:add(nn.SpatialLPPooling(nstates[1],2,poolsize,poolsize,poolsize,poolsize))
model:add(nn.SpatialSubtractiveNormalization(nstates[1], normkernel))
-- stage 2 : filter bank -> squashing -> L2 pooling -> normalization
model:add(nn.SpatialConvolutionMap(nn.tables.random(nstates[1], nstates[2], fanin[2]), filtsize, filtsize))
model:add(nn.Tanh())
model:add(nn.SpatialLPPooling(nstates[2],2,poolsize,poolsize,poolsize,poolsize))
model:add(nn.SpatialSubtractiveNormalization(nstates[2], normkernel))
-- stage 3 : standard 2-layer neural network
model:add(nn.Reshape(nstates[2]*filtsize*filtsize))
model:add(nn.Linear(nstates[2]*filtsize*filtsize, nstates[3]))
model:add(nn.Tanh())
model:add(nn.Linear(nstates[3], noutputs))
else
error('unknown -model')
end
----------------------------------------------------------------------
print '==> here is the model:'
print(model)
----------------------------------------------------------------------
-- Visualization is quite easy, using image.display(). Check out:
-- help(image.display), for more info about options.
if opt.visualize then
if opt.model == 'convnet' then
print '==> visualizing ConvNet filters'
image.display{image=model:get(1).weight, padding=2, zoom=4, legend='filters @ layer 1'}
image.display{image=model:get(5).weight, padding=2, zoom=4, nrow=32, legend='filters @ layer 2'}
end
end