forked from szagoruyko/wide-residual-networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.lua
180 lines (148 loc) · 5.07 KB
/
train.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
-- Code for Wide Residual Networks http://arxiv.org/abs/1605.07146
-- (c) Sergey Zagoruyko, 2016
require 'xlua'
require 'optim'
require 'image'
require 'cunn'
require 'cudnn'
local c = require 'trepl.colorize'
local json = require 'cjson'
paths.dofile'augmentation.lua'
-- for memory optimizations and graph generation
local optnet = require 'optnet'
local graphgen = require 'optnet.graphgen'
local iterm = require 'iterm'
require 'iterm.dot'
opt = {
dataset = './datasets/cifar10_whitened.t7',
save = 'logs',
batchSize = 128,
learningRate = 0.1,
learningRateDecay = 0,
learningRateDecayRatio = 0.2,
weightDecay = 0.0005,
dampening = 0,
momentum = 0.9,
epoch_step = "80",
max_epoch = 300,
model = 'nin',
optimMethod = 'sgd',
init_value = 10,
depth = 50,
shortcutType = 'A',
nesterov = false,
dropout = 0,
hflip = true,
randomcrop = 4,
imageSize = 32,
randomcrop_type = 'zero',
cudnn_fastest = true,
cudnn_deterministic = false,
optnet_optimize = true,
generate_graph = false,
multiply_input_factor = 1,
widen_factor = 1,
}
opt = xlua.envparams(opt)
opt.epoch_step = tonumber(opt.epoch_step) or loadstring('return '..opt.epoch_step)()
print(opt)
print(c.blue '==>' ..' loading data')
local provider = torch.load(opt.dataset)
opt.num_classes = provider.testData.labels:max()
print(c.blue '==>' ..' configuring model')
local model = nn.Sequential()
local net = dofile('models/'..opt.model..'.lua'):cuda()
do
local function add(flag, module) if flag then model:add(module) end end
add(opt.hflip, nn.BatchFlip():float())
add(opt.randomcrop > 0, nn.RandomCrop(opt.randomcrop, opt.randomcrop_type):float())
model:add(nn.Copy('torch.FloatTensor','torch.CudaTensor'):cuda())
add(opt.multiply_input_factor ~= 1, nn.MulConstant(opt.multiply_input_factor):cuda())
model:add(net)
cudnn.convert(net, cudnn)
cudnn.benchmark = true
if opt.cudnn_fastest then
for i,v in ipairs(net:findModules'cudnn.SpatialConvolution') do v:fastest() end
end
if opt.cudnn_deterministic then
model:apply(function(m) if m.setMode then m:setMode(1,1,1) end end)
end
print(net)
print('Network has', #model:findModules'cudnn.SpatialConvolution', 'convolutions')
local sample_input = torch.randn(8,3,opt.imageSize,opt.imageSize):cuda()
if opt.generate_graph then
iterm.dot(graphgen(net, sample_input), opt.save..'/graph.pdf')
end
if opt.optnet_optimize then
optnet.optimizeMemory(net, sample_input, {inplace = false, mode = 'training'})
end
end
local function log(t) print('json_stats: '..json.encode(tablex.merge(t,opt,true))) end
print('Will save at '..opt.save)
paths.mkdir(opt.save)
local parameters,gradParameters = model:getParameters()
opt.n_parameters = parameters:numel()
print('Network has ', parameters:numel(), 'parameters')
print(c.blue'==>' ..' setting criterion')
local criterion = nn.CrossEntropyCriterion():cuda()
-- a-la autograd
local f = function(inputs, targets)
model:forward(inputs)
local loss = criterion:forward(model.output, targets)
local df_do = criterion:backward(model.output, targets)
model:backward(inputs, df_do)
return loss
end
print(c.blue'==>' ..' configuring optimizer')
local optimState = tablex.deepcopy(opt)
function train()
model:training()
local targets = torch.CudaTensor(opt.batchSize)
local indices = torch.randperm(provider.trainData.data:size(1)):long():split(opt.batchSize)
-- remove last element so that all minibatches have equal size
indices[#indices] = nil
local loss = 0
for t,v in ipairs(indices) do
local inputs = provider.trainData.data:index(1,v)
targets:copy(provider.trainData.labels:index(1,v))
optim[opt.optimMethod](function(x)
if x ~= parameters then parameters:copy(x) end
gradParameters:zero()
loss = loss + f(inputs, targets)
return f,gradParameters
end, parameters, optimState)
end
return loss / #indices
end
function test()
model:evaluate()
local confusion = optim.ConfusionMatrix(opt.num_classes)
local data_split = provider.testData.data:split(opt.batchSize,1)
local labels_split = provider.testData.labels:split(opt.batchSize,1)
for i,v in ipairs(data_split) do
confusion:batchAdd(model:forward(v), labels_split[i])
end
confusion:updateValids()
return confusion.totalValid * 100
end
for epoch=1,opt.max_epoch do
print('==>'.." online epoch # " .. epoch .. ' [batchSize = ' .. opt.batchSize .. ']')
-- drop learning rate and reset momentum vector
if torch.type(opt.epoch_step) == 'number' and epoch % opt.epoch_step == 0 or
torch.type(opt.epoch_step) == 'table' and tablex.find(opt.epoch_step, epoch) then
opt.learningRate = opt.learningRate * opt.learningRateDecayRatio
optimState = tablex.deepcopy(opt)
end
local function t(f) local s = torch.Timer(); return f(), s:time().real end
local loss, train_time = t(train)
local test_acc, test_time = t(test)
log{
loss = loss,
epoch = epoch,
test_acc = test_acc,
lr = opt.learningRate,
train_time = train_time,
test_time = test_time,
}
end
torch.save(opt.save..'/model.t7', net:clearState())