forked from szagoruyko/cifar.torch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.lua
184 lines (146 loc) · 5.13 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
181
182
require 'xlua'
require 'optim'
require 'cutorch'
dofile './provider.lua'
local c = require 'trepl.colorize'
opt = lapp[[
-s,--save (default "logs") subdirectory to save logs
-b,--batchSize (default 128) batch size
-r,--learningRate (default 1) learning rate
--learningRateDecay (default 1e-7) learning rate decay
--weightDecay (default 0.0005) weightDecay
-m,--momentum (default 0.9) momentum
--epoch_step (default 25) epoch step
--model (default vgg_bn_drop) model name
--max_epoch (default 300) maximum number of iterations
]]
print(opt)
do -- data augmentation module
local BatchFlip,parent = torch.class('nn.BatchFlip', 'nn.Module')
function BatchFlip:__init()
parent.__init(self)
self.train = true
end
function BatchFlip:updateOutput(input)
if self.train then
local bs = input:size(1)
local flip_mask = torch.randperm(bs):le(bs/2)
for i=1,input:size(1) do
if flip_mask[i] == 1 then image.hflip(input[i], input[i]) end
end
end
self.output = input
return self.output
end
end
print(c.blue '==>' ..' configuring model')
local model = nn.Sequential()
model:add(nn.BatchFlip():float())
model:add(nn.Copy('torch.FloatTensor','torch.CudaTensor'):cuda())
model:add(nn.Sequential():add(dofile('models/'..opt.model..'.lua'):cuda()))
model:get(2).updateGradInput = function(input) return end
print(model)
print(c.blue '==>' ..' loading data')
provider = torch.load 'provider.t7'
confusion = optim.ConfusionMatrix(10)
print('Will save at '..opt.save)
paths.mkdir(opt.save)
testLogger = optim.Logger(paths.concat(opt.save, 'test.log'))
testLogger:setNames{'% mean class accuracy (train set)', '% mean class accuracy (test set)'}
parameters,gradParameters = model:getParameters()
print(c.blue'==>' ..' setting criterion')
criterion = nn.CrossEntropyCriterion():cuda()
print(c.blue'==>' ..' configuring optimizer')
optimState = {
learningRate = opt.learningRate,
weightDecay = opt.weightDecay,
momentum = opt.momentum,
learningRateDecay = opt.learningRateDecay,
}
optimMethod = optim.sgd
function train()
model:training()
epoch = epoch or 1
-- drop learning rate every "epoch_step" epochs
if epoch % opt.epoch_step == 0 then optimState.learningRate = optimState.learningRate/2 end
print(c.blue '==>'.." online epoch # " .. epoch .. ' [batchSize = ' .. opt.batchSize .. ']')
local targets = torch.CudaTensor(opt.batchSize)
local indices = torch.randperm(provider.trainData.data:size(1)):long():split(opt.batchSize)
indices[#indices] = nil
local tic = torch.tic()
for t,v in ipairs(indices) do
xlua.progress(t, #indices)
inputs = provider.trainData.data:index(1,v)
targets:copy(provider.trainData.labels:index(1,v))
local feval = function(x)
if x ~= parameters then parameters:copy(x) end
gradParameters:zero()
local outputs = model:forward(inputs)
local f = criterion:forward(outputs, targets)
local df_do = criterion:backward(outputs, targets)
model:backward(inputs, df_do)
confusion:batchAdd(outputs, targets)
return f,gradParameters
end
optim.sgd(feval, parameters, optimState)
end
confusion:updateValids()
print(('Train accuracy: '..c.cyan'%.2f'..' %%\t time: %.2f s'):format(
confusion.totalValid * 100, torch.toc(tic)))
train_acc = confusion.totalValid * 100
confusion:zero()
epoch = epoch + 1
end
function test()
-- disable flips, dropouts and batch normalization
model:evaluate()
print(c.blue '==>'.." testing")
local n = provider.testData.data:size(1)
local bs = 125
for i=1,n,bs do
_,y = model:forward(provider.testData.data:narrow(1,i,bs)):max(2)
confusion:batchAdd(y,provider.testData.labels:narrow(1,i,bs))
end
confusion:updateValids()
print('Test accuracy:', confusion.totalValid * 100)
if testLogger then
paths.mkdir(opt.save)
testLogger:add{train_acc, confusion.totalValid * 100}
testLogger:style{'-','-'}
testLogger:plot()
local file = io.open(opt.save..'/report.html','w')
local header = [[
<!DOCTYPE html>
<html>
<body>
<title>$SAVE - $EPOCH</title>
<img src="test.png">
]]
header = header:gsub('$SAVE',opt.save):gsub('$EPOCH',epoch)
file:write(header)
file:write'<h4>optimState:</h4>\n'
file:write'<table>\n'
for k,v in pairs(optimState) do
if torch.type(v) == 'number' then
file:write('<tr><td>'..k..'</td><td>'..v..'</td></tr>\n')
end
end
file:write'</table><pre>\n'
file:write(tostring(confusion)..'\n')
file:write(tostring(model)..'\n')
file:write("</pre></body></html>")
file:close()
os.execute('convert -density 200 '..opt.save..'/test.log.eps '..opt.save..'/test.png')
end
-- save model every 50 epochs
if epoch % 50 == 0 then
local filename = paths.concat(opt.save, 'model.net')
print('==> saving model to '..filename)
torch.save(filename, model:get(3))
end
confusion:zero()
end
for i=1,opt.max_epoch do
train()
test()
end