-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.lua
189 lines (160 loc) · 4.91 KB
/
models.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
require "nn"
require "nngraph"
local Convolution = nn.SpatialConvolution
local Pool = nn.SpatialMaxPooling
local fmp = nn.SpatialFractionalMaxPooling
local UpSample = nn.SpatialUpSamplingNearest
local SBN = nn.SpatialBatchNormalization
local Dropout = nn.Dropout
local af = nn.ReLU
local Linear = nn.Linear
local Dropout = nn.Dropout
local layers = dofile("/home/msmith/torchFunctions/layers.lua")
models = {}
function initParamsEg()
params = {}
params.kernelSize = 3
params.nFeats = 22
params.nDown = 7
params.nUp = 3
model = nn.Sequential()
end
--initParamsEg()
local nFeats = params.nFeats
local nFeatsInc = params.nFeats
local nOutputs
local nInputs
local kS = 3
local pad = torch.floor((kS-1)/2)
function shortcut(nInputPlane, nOutputPlane, stride)
return nn.Sequential()
:add(Convolution(nInputPlane,nOutputPlane,1,1,stride,stride,0,0))
:add(SBN(nOutputPlane))
--:add(nn.Identity())
end
function basicblock(nInputPlane, n, stride)
local s = nn.Sequential()
s:add(Convolution(nInputPlane,n,3,3,1,1,1,1))
s:add(SBN(n))
s:add(af())
return nn.Sequential()
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, n, stride)))
:add(nn.CAddTable(true))
:add(af())
end
function models.model1()
local model = nn.Sequential()
local nInputs
local nOutputs
for i =1, 8 do
if i == 1 then nInputs = 3; else nInputs = nOutputs; end
if i == 1 then nOutputs = nFeats; else nOutputs = nOutputs; end
model:add(basicblock(nInputs,nOutputs,1))
model:add(fmp(2,2,0.8,0.8))
--model:add(Dropout(0.3))
end
model:add(Convolution(nInputs,1,3,3,1,1,1,1))
nInputs = nOutputs
outputBeforeReshape = model:cuda():forward(torch.rand(1,3,params.inH,params.inW):cuda()):size()
nOutputsBeforeReshape = outputBeforeReshape[2]*outputBeforeReshape[3]*outputBeforeReshape[4]
model:add(nn.Reshape(nOutputsBeforeReshape))
model:add(nn.Linear(nOutputsBeforeReshape,100))
model:add(nn.BatchNormalization(100))
model:add(af())
--model:add(Dropout(0.5))
model:add(nn.Linear(100,10))
layers.init(model)
return model
end
function models.model2()
local model = nn.Sequential()
local nInputs
local nOutputs
for i =1, params.nDown do
if i == 1 then nInputs = 3; else nInputs = nOutputs; end
if i == 1 then nOutputs = nFeats; else nOutputs = nOutputs + torch.floor(nFeatsInc/2); end
model:add(Convolution(nInputs,nOutputs,3,3,1,1,1,1))
model:add(SBN(nOutputs))
model:add(af())
model:add(fmp(2,2,0.7,0.7))
end
model:add(Convolution(nOutputs,nOutputs,3,3,1,1,1,1))
model:add(SBN(nOutputs))
model:add(af())
outputBeforeReshape = model:cuda():forward(torch.rand(1,3,params.inH,params.inW):cuda()):size()
nOutputsBeforeReshape = outputBeforeReshape[2]*outputBeforeReshape[3]*outputBeforeReshape[4]
print(outputBeforeReshape)
model:add(nn.Reshape(nOutputsBeforeReshape))
model:add(nn.Linear(nOutputsBeforeReshape,500))
model:add(nn.BatchNormalization(500))
model:add(af())
model:add(Dropout(0.5))
model:add(nn.Linear(500,10))
layers.init(model)
return model
end
function models.vgg()
local vgg = nn.Sequential()
-- building block
local function ConvBNReLU(nInputPlane, nOutputPlane)
vgg:add(nn.SpatialConvolution(nInputPlane, nOutputPlane, 3,3, 1,1, 1,1))
vgg:add(nn.SpatialBatchNormalization(nOutputPlane,1e-3))
vgg:add(nn.ReLU(true))
return vgg
end
-- Will use "ceil" MaxPooling because we want to save as much
-- space as we can
local MaxPooling = nn.SpatialMaxPooling
ConvBNReLU(3,64):add(nn.Dropout(0.3))
ConvBNReLU(64,64)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(64,128):add(nn.Dropout(0.4))
ConvBNReLU(128,128)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(128,256):add(nn.Dropout(0.4))
ConvBNReLU(256,256):add(nn.Dropout(0.4))
ConvBNReLU(256,256)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(256,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512)
vgg:add(MaxPooling(2,2,2,2):ceil())
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512):add(nn.Dropout(0.4))
ConvBNReLU(512,512)
vgg:add(MaxPooling(2,2,2,2):ceil())
vgg:add(nn.View(512))
classifier = nn.Sequential()
classifier:add(nn.Dropout(0.5))
classifier:add(nn.Linear(512,512))
classifier:add(nn.BatchNormalization(512))
classifier:add(nn.ReLU(true))
classifier:add(nn.Dropout(0.5))
classifier:add(nn.Linear(512,10))
vgg:add(classifier)
-- initialization from MSR
local function MSRinit(net)
local function init(name)
for k,v in pairs(net:findModules(name)) do
local n = v.kW*v.kH*v.nOutputPlane
v.weight:normal(0,math.sqrt(2/n))
v.bias:zero()
end
end
-- have to do for both backends
init'nn.SpatialConvolution'
end
MSRinit(vgg)
return vgg
end
return models