This repository has been archived by the owner on Oct 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 665
/
preresnet.lua
237 lines (214 loc) · 7.82 KB
/
preresnet.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
--
-- Copyright (c) 2016, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the BSD-style license found in the
-- LICENSE file in the root directory of this source tree. An additional grant
-- of patent rights can be found in the PATENTS file in the same directory.
--
-- The full pre-activation ResNet variation from the technical report
-- "Identity Mappings in Deep Residual Networks" (http://arxiv.org/abs/1603.05027)
--
local nn = require 'nn'
require 'cunn'
local Convolution = cudnn.SpatialConvolution
local Avg = cudnn.SpatialAveragePooling
local ReLU = cudnn.ReLU
local Max = nn.SpatialMaxPooling
local SBatchNorm = nn.SpatialBatchNormalization
local function createModel(opt)
local depth = opt.depth
local shortcutType = opt.shortcutType or 'B'
local iChannels
-- The shortcut layer is either identity or 1x1 convolution
local function shortcut(nInputPlane, nOutputPlane, stride)
local useConv = shortcutType == 'C' or
(shortcutType == 'B' and nInputPlane ~= nOutputPlane)
if useConv then
-- 1x1 convolution
return nn.Sequential()
:add(Convolution(nInputPlane, nOutputPlane, 1, 1, stride, stride))
elseif nInputPlane ~= nOutputPlane then
-- Strided, zero-padded identity shortcut
return nn.Sequential()
:add(nn.SpatialAveragePooling(1, 1, stride, stride))
:add(nn.Concat(2)
:add(nn.Identity())
:add(nn.MulConstant(0)))
else
return nn.Identity()
end
end
-- Typically shareGradInput uses the same gradInput storage for all modules
-- of the same type. This is incorrect for some SpatialBatchNormalization
-- modules in this network b/c of the in-place CAddTable. This marks the
-- module so that it's shared only with other modules with the same key
local function ShareGradInput(module, key)
assert(key)
module.__shareGradInputKey = key
return module
end
-- The basic residual layer block for 18 and 34 layer network, and the
-- CIFAR networks
local function basicblock(n, stride, type)
local nInputPlane = iChannels
iChannels = n
local block = nn.Sequential()
local s = nn.Sequential()
if type == 'both_preact' then
block:add(ShareGradInput(SBatchNorm(nInputPlane), 'preact'))
block:add(ReLU(true))
elseif type ~= 'no_preact' then
s:add(SBatchNorm(nInputPlane))
s:add(ReLU(true))
end
s:add(Convolution(nInputPlane,n,3,3,stride,stride,1,1))
s:add(SBatchNorm(n))
s:add(ReLU(true))
s:add(Convolution(n,n,3,3,1,1,1,1))
return block
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, n, stride)))
:add(nn.CAddTable(true))
end
-- The bottleneck residual layer for 50, 101, and 152 layer networks
local function bottleneck(n, stride, type)
local nInputPlane = iChannels
iChannels = n * 4
local block = nn.Sequential()
local s = nn.Sequential()
if type == 'both_preact' then
block:add(ShareGradInput(SBatchNorm(nInputPlane), 'preact'))
block:add(ReLU(true))
elseif type ~= 'no_preact' then
s:add(SBatchNorm(nInputPlane))
s:add(ReLU(true))
end
s:add(Convolution(nInputPlane,n,1,1,1,1,0,0))
s:add(SBatchNorm(n))
s:add(ReLU(true))
s:add(Convolution(n,n,3,3,stride,stride,1,1))
s:add(SBatchNorm(n))
s:add(ReLU(true))
s:add(Convolution(n,n*4,1,1,1,1,0,0))
return block
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, n * 4, stride)))
:add(nn.CAddTable(true))
end
-- Creates count residual blocks with specified number of features
local function layer(block, features, count, stride, type)
local s = nn.Sequential()
if count < 1 then
return s
end
s:add(block(features, stride,
type == 'first' and 'no_preact' or 'both_preact'))
for i=2,count do
s:add(block(features, 1))
end
return s
end
local model = nn.Sequential()
if opt.dataset == 'imagenet' then
-- Configurations for ResNet:
-- num. residual blocks, num features, residual block function
local cfg = {
[18] = {{2, 2, 2, 2}, 512, basicblock},
[34] = {{3, 4, 6, 3}, 512, basicblock},
[50] = {{3, 4, 6, 3}, 2048, bottleneck},
[101] = {{3, 4, 23, 3}, 2048, bottleneck},
[152] = {{3, 8, 36, 3}, 2048, bottleneck},
[200] = {{3, 24, 36, 3}, 2048, bottleneck},
}
assert(cfg[depth], 'Invalid depth: ' .. tostring(depth))
local def, nFeatures, block = table.unpack(cfg[depth])
iChannels = 64
print(' | ResNet-' .. depth .. ' ImageNet')
-- The ResNet ImageNet model
model:add(Convolution(3,64,7,7,2,2,3,3))
model:add(SBatchNorm(64))
model:add(ReLU(true))
model:add(Max(3,3,2,2,1,1))
model:add(layer(block, 64, def[1], 1, 'first'))
model:add(layer(block, 128, def[2], 2))
model:add(layer(block, 256, def[3], 2))
model:add(layer(block, 512, def[4], 2))
model:add(ShareGradInput(SBatchNorm(iChannels), 'last'))
model:add(ReLU(true))
model:add(Avg(7, 7, 1, 1))
model:add(nn.View(nFeatures):setNumInputDims(3))
model:add(nn.Linear(nFeatures, 1000))
elseif opt.dataset == 'cifar10' then
-- Model type specifies number of layers for CIFAR-10 model
assert((depth - 2) % 6 == 0, 'depth should be one of 20, 32, 44, 56, 110, 1202')
local n = (depth - 2) / 6
iChannels = 16
print(' | ResNet-' .. depth .. ' CIFAR-10')
-- The ResNet CIFAR-10 model
model:add(Convolution(3,16,3,3,1,1,1,1))
model:add(layer(basicblock, 16, n, 1))
model:add(layer(basicblock, 32, n, 2))
model:add(layer(basicblock, 64, n, 2))
model:add(ShareGradInput(SBatchNorm(iChannels), 'last'))
model:add(ReLU(true))
model:add(Avg(8, 8, 1, 1))
model:add(nn.View(64):setNumInputDims(3))
model:add(nn.Linear(64, 10))
elseif opt.dataset == 'cifar100' then
-- Model type specifies number of layers for CIFAR-100 model
assert((depth - 2) % 6 == 0, 'depth should be one of 20, 32, 44, 56, 110, 1202')
local n = (depth - 2) / 6
iChannels = 16
print(' | ResNet-' .. depth .. ' CIFAR-100')
-- The ResNet CIFAR-100 model
model:add(Convolution(3,16,3,3,1,1,1,1))
model:add(layer(basicblock, 16, n, 1))
model:add(layer(basicblock, 32, n, 2))
model:add(layer(basicblock, 64, n, 2))
model:add(ShareGradInput(SBatchNorm(iChannels), 'last'))
model:add(ReLU(true))
model:add(Avg(8, 8, 1, 1))
model:add(nn.View(64):setNumInputDims(3))
model:add(nn.Linear(64, 100))
else
error('invalid dataset: ' .. opt.dataset)
end
local function ConvInit(name)
for k,v in pairs(model:findModules(name)) do
local n = v.kW*v.kH*v.nOutputPlane
v.weight:normal(0,math.sqrt(2/n))
if cudnn.version >= 4000 then
v.bias = nil
v.gradBias = nil
else
v.bias:zero()
end
end
end
local function BNInit(name)
for k,v in pairs(model:findModules(name)) do
v.weight:fill(1)
v.bias:zero()
end
end
ConvInit('cudnn.SpatialConvolution')
ConvInit('nn.SpatialConvolution')
BNInit('fbnn.SpatialBatchNormalization')
BNInit('cudnn.SpatialBatchNormalization')
BNInit('nn.SpatialBatchNormalization')
for k,v in pairs(model:findModules('nn.Linear')) do
v.bias:zero()
end
model:type(opt.tensorType)
if opt.cudnn == 'deterministic' then
model:apply(function(m)
if m.setMode then m:setMode(1,1,1) end
end)
end
model:get(1).gradInput = nil
return model
end
return createModel