-
Notifications
You must be signed in to change notification settings - Fork 2
/
2_model.lua
172 lines (143 loc) · 5.62 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
----------------------------------------------------------------------
-- This script demonstrates how to define a couple of different
-- models:
-- + linear
-- + 2-layer neural network (MLP)
-- + convolutional network (ConvNet)
--
-- It's a good idea to run this script with the interactive mode:
-- $ th -i 2_model.lua
-- this will give you a Torch interpreter at the end, that you
-- can use to play with the model.
--
-- Clement Farabet
----------------------------------------------------------------------
require 'torch' -- torch
require 'image' -- for image transforms
require 'nn' -- provides all sorts of trainable modules/layers
----------------------------------------------------------------------
-- 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
-- number of hidden units (for MLP only):
nhiddens = ninputs / 2
-- hidden units, filter sizes (for ConvNet only):
nstates = {64,64,64,1000,256}
filtsize = 5
poolsize = 2
normkernel = image.gaussian1D(7)
----------------------------------------------------------------------
print '==> construct model'
if opt.model == 'linear' then
-- Simple linear model
model = nn.Sequential()
model:add(nn.Reshape(ninputs))
model:add(nn.Linear(ninputs,noutputs))
elseif opt.model == 'mlp' then
-- Simple 2-layer neural network, with tanh hidden units
model = nn.Sequential()
model:add(nn.Reshape(ninputs))
model:add(nn.Linear(ninputs,nhiddens))
model:add(nn.Tanh())
model:add(nn.Linear(nhiddens,noutputs))
elseif opt.model == 'convnet' then
if opt.type == 'cuda' then
--[[
-- a typical modern convolution network (conv+relu+pool)
model = nn.Sequential()
-- stage 1 : filter bank -> squashing -> L2 pooling -> normalization
model:add(nn.SpatialConvolutionMM(nfeats, nstates[1], filtsize, filtsize))
model:add(nn.ReLU())
model:add(nn.SpatialMaxPooling(poolsize,poolsize,poolsize,poolsize))
-- stage 2 : filter bank -> squashing -> L2 pooling -> normalization
model:add(nn.SpatialConvolutionMM(nstates[1], nstates[2], filtsize, filtsize))
model:add(nn.ReLU())
model:add(nn.SpatialMaxPooling(poolsize,poolsize,poolsize,poolsize))
-- stage 3 : standard 2-layer neural network
model:add(nn.View(nstates[2]*filtsize*filtsize))
model:add(nn.Dropout(0.5))
model:add(nn.Linear(nstates[2]*filtsize*filtsize, nstates[3]))
model:add(nn.ReLU())
model:add(nn.Linear(nstates[3], noutputs))
--]]
else
-- 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
filtsize = 5
poolsize = 2
model:add(nn.SpatialConvolutionMM(nfeats, nstates[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
filtsize = 3
poolsize = 2
model:add(nn.SpatialConvolutionMM(nstates[1], nstates[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 : filter bank -> squashing
filtsize = 3
model:add(nn.SpatialConvolutionMM(nstates[2], nstates[3], filtsize, filtsize))
model:add(nn.Tanh())
-- stage 4 : filter bank -> squashing
filtsize = 3
model:add(nn.SpatialConvolutionMM(nstates[3], nstates[4], filtsize, filtsize))
model:add(nn.Tanh())
-- stage 5&6 : 3-layer neural network
model:add(nn.Reshape(nstates[4]*filtsize*filtsize))
model:add(nn.Linear(nstates[4]*filtsize*filtsize, nstates[5]))
model:add(nn.Tanh())
model:add(nn.Reshape(nstates[5]))
model:add(nn.Linear(nstates[5],nstates[6]))
model:add(nn.Tanh())
model:add(nn.Linear(nstates[6], noutputs))
end
else
error('unknown -model')
end
----------------------------------------------------------------------
print '==> here is the model:'
print(model)
----------------------------------------------------------------------
-- Visualization is quite easy, using itorch.image().
if opt.visualize then
if opt.model == 'convnet' then
if itorch then
print '==> visualizing ConvNet filters'
print('Layer 1 filters:')
itorch.image(model:get(1).weight)
print('Layer 2 filters:')
itorch.image(model:get(5).weight)
else
print '==> To visualize filters, start the script in itorch notebook'
end
end
end