-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathzeiler.lua
86 lines (73 loc) · 2.33 KB
/
zeiler.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
-- Zeiler network
local function createModel(backend)
local matio = require 'matio'
local maxpooling
local spatialconv
local relu
if backend == 'nn' then
maxpooling = nn.SpatialMaxPooling
spatialconv = nn.SpatialConvolution
relu = nn.ReLU
elseif backend == 'cudnn' then
maxpooling = cudnn.SpatialMaxPooling
spatialconv = cudnn.SpatialConvolution
relu = cudnn.ReLU
end
local features = nn.Sequential()
local fS = {96, 256, 384, 384, 256}
fS[0] = 3
local ks = {7,5,3,3,3}
local stride = {2,2,1,1,1}
local pad = {1,0,1,1,1}
local lrn = {true,true,false,false,false}
local pool = {true,true,false,false,false}
for i=1,#fS do
features:add(spatialconv(fS[i-1],fS[i],ks[i],ks[i],stride[i],stride[i],pad[i],pad[i]))
features:add(relu(true))
if lrn[i] then
features:add(inn.SpatialSameResponseNormalization(3,0.00005,0.75))
end
if pool[i] then
features:add(maxpooling(3,3,2,2):ceil())
end
end
local classifier = nn.Sequential()
local fS = {4096,4096,1000}
fS[0] = 256*50
for i=1,#fS do
classifier:add(nn.Linear(fS[i-1],fS[i]))
if i < #fS then
classifier:add(relu(true))
classifier:add(nn.Dropout(0.5,true))
end
end
local modelsdir = 'data/models'
local modelpath = paths.concat(modelsdir,'Zeiler_imagenet_weights.mat')
assert(paths.filep(modelpath),
'Parameters file not found: '..modelpath)
local mat = matio.load(modelpath)
local idx = 1
for i=1,features:size() do
if torch.typename(features:get(i))=='nn.SpatialConvolutionMM' or
torch.typename(features:get(i))=='nn.SpatialConvolution' or
torch.typename(features:get(i))=='cudnn.SpatialConvolution' then
features:get(i).weight:copy(mat['conv'..idx..'_w']:transpose(1,4):transpose(2,3))
features:get(i).bias:copy(mat['conv'..idx..'_b'])
idx = idx + 1
end
end
local idx = 6
for i=1,classifier:size() do
if torch.typename(classifier:get(i))=='nn.Linear' then
classifier:get(i).weight:copy(mat['fc'..idx..'_w']:transpose(1,2))
classifier:get(i).bias:copy(mat['fc'..idx..'_b'])
idx = idx + 1
end
end
local model = nn.Sequential()
model:add(features)
model:add(inn.SpatialPyramidPooling({{1,1},{2,2},{3,3},{6,6}}))
model:add(classifier)
return model
end
return createModel