-
Notifications
You must be signed in to change notification settings - Fork 1
/
ResNetDetNet.lua
214 lines (189 loc) · 6.27 KB
/
ResNetDetNet.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
require 'nn';
require 'cunn';
require 'cudnn';
require 'cudnnorn'
optnet = require 'optnet'
nninit = require 'nninit'
torch.setdefaulttensortype('torch.CudaTensor')
--[[
classe = 18
con = {
bpc = {6},
imgshape = 336
}--]]
local Convolution = cudnn.SpatialConvolution
local Avg = cudnn.SpatialAveragePooling
local ReLU = nn.LeakyReLU(0.1)
local Max = nn.SpatialMaxPooling
local SBatchNorm = nn.SpatialBatchNormalization
local function createModel(classes, conf)
local shortcutType = 'B'
local iChannels
-- xavier initilization and bias zero
local function xconv(ic,oc,kw,kh,sw,sh,pw,ph,type,dw,dh,relu)
local conv
use_relu = relu
if type == 'N' then
conv = cudnn.SpatialConvolution(ic, oc, kw, kh, sw, sh, pw, ph):init('weight', nninit.xavier, {dist='uniform', gain=1.1})
elseif type == 'D' then
local karnel = torch.randn(oc, ic, kw, kh)
conv = nn.SpatialDilatedConvolution(ic, oc, kw, kh, sw, sh, pw, ph, pw, ph)
nninit.xavier(nn.SpatialConvolution(ic, oc, kw, kh, sw, sh, pw, ph), karnel, {dist='uniform', gain=1.1})
conv.weight:copy(karnel)
end
if cudnn.version >= 4000 then
conv.bias = nil
conv.gradBias = nil
else
conv.bias:zero()
end
if use_relu then
return nn.Sequential():add(conv):add(nn.SpatialBatchNormalization(oc)):add(nn.LeakyReLU(0.1))
else
return conv
end
end
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))
:add(SBatchNorm(nOutputPlane))
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
-- The basic residual layer block for 18 and 34 layer network, and the
-- CIFAR networks
local function basicblock(n, stride)
local nInputPlane = iChannels
iChannels = n
local s = nn.Sequential()
s:add(Convolution(nInputPlane,n,3,3,stride,stride,1,1))
s:add(SBatchNorm(n))
s:add(ReLU)
s:add(Convolution(n,n,3,3,1,1,1,1))
s:add(SBatchNorm(n))
return nn.Sequential()
:add(nn.ConcatTable()
:add(s)
:add(shortcut(nInputPlane, n, stride)))
:add(nn.CAddTable(true))
:add(ReLU)
end
local function ADilatedbottleneck()
local s = nn.Sequential()
s:add(Convolution(1024, 256, 1, 1, 1, 1, 0, 0))
s:add(SBatchNorm(256))
s:add(nn.LeakyReLU(0.1))
s:add(nn.ORConv(32, 32, 8, 3, 3, 1, 1, 1, 1))
s:add(SBatchNorm(256))
s:add(nn.LeakyReLU(0.1))
s:add(Convolution(256, 1024, 1, 1, 1, 1, 0, 0))
s:add(SBatchNorm(256 * 4))
return nn.Sequential()
:add(nn.ConcatTable()
:add(s)
:add(nn.Identity()))
:add(nn.CAddTable(true))
:add(nn.LeakyReLU(0.1))
end
local function BDilatedbottleneck()
local s = nn.Sequential()
s:add(Convolution(1024, 256, 1, 1, 1, 1, 0, 0))
s:add(SBatchNorm(256))
s:add(nn.LeakyReLU(0.1))
s:add(nn.ORConv(32, 32, 8, 3, 3, 1, 1, 1, 1))
s:add(SBatchNorm(256))
s:add(nn.LeakyReLU(0.1))
s:add(Convolution(256, 1024, 1, 1, 1, 1, 0, 0))
s:add(SBatchNorm(256 * 4))
return nn.Sequential()
:add(nn.ConcatTable()
:add(s)
:add(nn.Sequential()
:add(Convolution(1024, 1024, 1, 1, 1, 1))
:add(SBatchNorm(1024))))
:add(nn.CAddTable(true))
:add(nn.LeakyReLU(0.1))
end
local function layer(block, features, count, stride)
local s = nn.Sequential()
for i=1,count do
s:add(block(features, i == 1 and stride or 1))
end
return s
end
local main
local branch = nn.Sequential()
if pretrain ~= nil then
main = torch.load(pretrain)
print('load pretrained net from '..pretrain )
else
iChannels = 64
main = nn.Sequential()
main:add(Convolution(3,64,7,7,2,2,3,3)) -- 224*224 -> 112*112 2
main:add(SBatchNorm(64))
main:add(ReLU)
main:add(Max(3,3,2,2,1,1)) -- 112*112 -> 56*56 4
main:add(layer(basicblock, 64, 2))
main:add(layer(basicblock, 128, 2, 2)) -- 56*56 -> 28*28 8
main:add(layer(basicblock, 256, 2, 2)) -- 28*28 -> 14*14 16
--main:add(layer(basicblock, 512, 2, 2)) -- 14*14 -> 7*7
end
main:add(BDilatedbottleneck())
--main:add(ADilatedbottleneck())
main:add(ADilatedbottleneck())
main:add(ADilatedbottleneck())
main:add(BDilatedbottleneck())
--main:add(ADilatedbottleneck())
main:add(ADilatedbottleneck())
main:add(ADilatedbottleneck())
branch:add(nn.ConcatTable()
:add(nn.Sequential():add(xconv(1024, 5*conf.bpc[1], 3, 3, 1, 1, 1, 1, 'N', 0, 0, false))
:add(nn.Transpose({2,3},{3,4}))
:add(nn.Reshape(-1, 5)))
:add(nn.Sequential():add(xconv(1024, conf.classes*conf.bpc[1], 3, 3, 1, 1, 1, 1, 'N', 0, 0, false))
:add(nn.Transpose({2,3},{3,4}))
:add(nn.Reshape(-1, 2))))
main:add(branch)
local function ConvInit(name)
for k,v in pairs(main: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(main:findModules(name)) do
v.weight:fill(1)
v.bias:zero()
end
end
if pretrain == nil then
ConvInit('cudnn.SpatialConvolution')
ConvInit('nn.SpatialConvolution')
BNInit('cudnn.SpatialBatchNormalization')
BNInit('nn.SpatialBatchNormalization')
end
main = main:cuda()
local inp = torch.randn(1, 3, conf.imgshape, conf.imgshape):cuda()
local opts = {inplace=true, mode='training'}
optnet.optimizeMemory(main, inp, opts)
return main
end
return createModel