forked from itayhubara/BinaryNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_BinaryNet_Cifar10.lua
319 lines (267 loc) · 10 KB
/
Main_BinaryNet_Cifar10.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
require 'torch'
require 'xlua'
require 'optim'
require 'gnuplot'
require 'pl'
require 'trepl'
require 'adaMax_binary_clip_shift'
require 'adam_binary_clip_b'
require 'nn'
require 'SqrHingeEmbeddingCriterion'
----------------------------------------------------------------------
cmd = torch.CmdLine()
cmd:addTime()
cmd:text()
cmd:text('Training a convolutional network for visual classification')
cmd:text()
cmd:text('==>Options')
cmd:text('===>Model And Training Regime')
cmd:option('-modelsFolder', './Models/', 'Models Folder')
cmd:option('-network', 'Model.lua', 'Model file - must return valid network.')
cmd:option('-LR', 2^-6, 'learning rate')
cmd:option('-LRDecay', 0, 'learning rate decay (in # samples)')
cmd:option('-weightDecay', 0.0, 'L2 penalty on the weights')
cmd:option('-momentum', 0.0, 'momentum')
cmd:option('-batchSize', 200, 'batch size')
cmd:option('-stcNeurons', true, 'use stochastic binarization for the neurons')
cmd:option('-stcWeights', false, 'use stochastic binarization for the weights')
cmd:option('-optimization', 'adam', 'optimization method')
cmd:option('-SBN', true, 'shift based batch-normalization')
cmd:option('-runningVal', false, 'use running mean and std')
cmd:option('-epoch', -1, 'number of epochs to train, -1 for unbounded')
cmd:text('===>Platform Optimization')
cmd:option('-threads', 8, 'number of threads')
cmd:option('-type', 'cuda', 'float or cuda')
cmd:option('-devid', 1, 'device ID (if using CUDA)')
cmd:option('-nGPU', 1, 'num of gpu devices used')
cmd:option('-constBatchSize', false, 'do not allow varying batch sizes - e.g for ccn2 kernel')
cmd:text('===>Save/Load Options')
cmd:option('-load', '', 'load existing net weights')
cmd:option('-save', os.date():gsub(' ',''), 'save directory')
cmd:text('===>Data Options')
cmd:option('-dataset', 'Cifar10', 'Dataset - Cifar10, Cifar100, STL10, SVHN, MNIST')
cmd:option('-normalization', 'simple', 'simple - whole sample, channel - by image channel, image - mean and std images')
cmd:option('-format', 'rgb', 'rgb or yuv')
cmd:option('-whiten', true, 'whiten data')
cmd:option('-dp_prepro', false, 'preprocessing using dp lib')
cmd:option('-augment', false, 'Augment training data')
cmd:option('-preProcDir', './PreProcData/', 'Data for pre-processing (means,P,invP)')
cmd:text('===>Misc')
cmd:option('-visualize', 1, 'visualizing results')
torch.manualSeed(432)
opt = cmd:parse(arg or {})
opt.network = opt.modelsFolder .. paths.basename(opt.network, '.lua')
opt.save = paths.concat('./Results', opt.save)
opt.preProcDir = paths.concat(opt.preProcDir, opt.dataset .. '/')
-- If you choose to use exponentialy decaying learning rate use uncomment this line
--opt.LRDecay=torch.pow((2e-6/opt.LR),(1./500));
--
os.execute('mk1ir -p ' .. opt.preProcDir)
torch.setnumthreads(opt.threads)
torch.setdefaulttensortype('torch.FloatTensor')
if opt.augment then
require 'image'
end
----------------------------------------------------------------------
-- Model + Loss:
local modelAll = require(opt.network)
model=modelAll.model
GLRvec=modelAll.lrs
clipV=modelAll.clipV
local loss = SqrtHingeEmbeddingCriterion(1)
local data = require 'Data'
local classes = data.Classes
----------------------------------------------------------------------
-- This matrix records the current confusion across classes
local confusion = optim.ConfusionMatrix(classes)
local AllowVarBatch = not opt.constBatchSize
----------------------------------------------------------------------
-- Output files configuration
os.execute('mkdir -p ' .. opt.save)
cmd:log(opt.save .. '/Log.txt', opt)
local netFilename = paths.concat(opt.save, 'Net')
local logFilename = paths.concat(opt.save,'ErrorRate.log')
local optStateFilename = paths.concat(opt.save,'optState')
local Log = optim.Logger(logFilename)
----------------------------------------------------------------------
local TensorType = 'torch.FloatTensor'
if paths.filep(opt.load) then
model = torch.load(opt.load)
print('==>Loaded model from: ' .. opt.load)
print(model)
end
if opt.type =='cuda' then
require 'cutorch'
cutorch.setDevice(opt.devid)
cutorch.setHeapTracking(true)
model:cuda()
GLRvec=GLRvec:cuda()
clipV=clipV:cuda()
loss = loss:cuda()
TensorType = 'torch.CudaTensor'
end
---Support for multiple GPUs - currently data parallel scheme
if opt.nGPU > 1 then
local net = model
model = nn.DataParallelTable(1)
for i = 1, opt.nGPU do
cutorch.setDevice(i)
model:add(net:clone():cuda(), i) -- Use the ith GPU
end
cutorch.setDevice(opt.devid)
end
-- Optimization configuration
local Weights,Gradients = model:getParameters()
----------------------------------------------------------------------
print '==> Network'
print(model)
print('==>' .. Weights:nElement() .. ' Parameters')
print '==> Loss'
print(loss)
------------------Optimization Configuration--------------------------
local optimState = {
learningRate = opt.LR,
momentum = opt.momentum,
weightDecay = opt.weightDecay,
learningRateDecay = opt.LRDecay,
GLRvec=GLRvec,
clipV=clipV
}
----------------------------------------------------------------------
local function SampleImages(images,labels)
if not opt.augment then
return images,labels
else
local sampled_imgs = images:clone()
for i=1,images:size(1) do
local sz = math.random(9) - 1
local hflip = math.random(2)==1
local startx = math.random(sz)
local starty = math.random(sz)
local img = images[i]:narrow(2,starty,32-sz):narrow(3,startx,32-sz)
if hflip then
img = image.hflip(img)
end
img = image.scale(img,32,32)
sampled_imgs[i]:copy(img)
end
return sampled_imgs,labels
end
end
------------------------------
local function Forward(Data, train)
local MiniBatch = DataProvider.Container{
Name = 'GPU_Batch',
MaxNumItems = opt.batchSize,
Source = Data,
ExtractFunction = SampleImages,
TensorType = TensorType
}
local yt = MiniBatch.Labels
local x = MiniBatch.Data
local SizeData = Data:size()
if not AllowVarBatch then SizeData = math.floor(SizeData/opt.batchSize)*opt.batchSize end
local NumSamples = 0
local NumBatches = 0
local lossVal = 0
while NumSamples < SizeData do
MiniBatch:getNextBatch()
local y, currLoss
NumSamples = NumSamples + x:size(1)
NumBatches = NumBatches + 1
if opt.nGPU > 1 then
model:syncParameters()
end
y = model:forward(x)
one_hot_yt=torch.zeros(yt:size(1),10)
one_hot_yt:scatter(2, yt:long():view(-1,1), 1)
one_hot_yt=one_hot_yt:mul(2):float():add(-1)
if opt.type == 'cuda' then
one_hot_yt=one_hot_yt:cuda()
end
currLoss = loss:forward(y,one_hot_yt)
if train then
function feval()
model:zeroGradParameters()
local dE_dy = loss:backward(y, one_hot_yt)
model:backward(x, dE_dy)
return currLoss, Gradients
end
--_G.optim[opt.optimization](feval, Weights, optimState) -- If you choose to use different optimization remember to clip the weights
adaMax_binary_clip_shift(feval, Weights, optimState)
end
lossVal = currLoss + lossVal
if type(y) == 'table' then --table results - always take first prediction
y = y[1]
end
confusion:batchAdd(y,one_hot_yt)
xlua.progress(NumSamples, SizeData)
if math.fmod(NumBatches,100)==0 then
collectgarbage()
end
end
return(lossVal/math.ceil(SizeData/opt.batchSize))
end
------------------------------
local function Train(Data)
model:training()
return Forward(Data, true)
end
local function Test(Data)
model:evaluate()
return Forward(Data, false)
end
------------------------------
local epoch = 1
print '\n==> Starting Training\n'
while epoch ~= opt.epoch do
data.TrainData:shuffleItems()
print('Epoch ' .. epoch)
--Train
confusion:zero()
local LossTrain = Train(data.TrainData)
if epoch%10==0 then
torch.save(netFilename, model)
end
confusion:updateValids()
local ErrTrain = (1-confusion.totalValid)
if #classes <= 10 then
print(confusion)
end
print('Training Error = ' .. ErrTrain)
print('Training Loss = ' .. LossTrain)
--validation
confusion:zero()
local LossValid = Test(data.ValidData)
confusion:updateValids()
local ErrValid = (1-confusion.totalValid)
if #classes <= 10 then
print(confusion)
end
print('Valid Error = ' .. ErrValid)
print('Valid Loss = ' .. LossValid)
--Test
confusion:zero()
local LossTest = Test(data.TestData)
confusion:updateValids()
local ErrTest = (1-confusion.totalValid)
if #classes <= 10 then
print(confusion)
end
print('Test Error = ' .. ErrTest)
print('Test Loss = ' .. LossTest)
Log:add{['Training Error']= ErrTrain, ['Valid Error'] = ErrValid, ['Test Error'] = ErrTest}
if opt.visualize == 1 then
Log:style{['Training Error'] = '-',['Validation Error'] = '-', ['Test Error'] = '-'}
Log:plot()
end
--optimState.learningRate=optimState.learningRate*opt.LRDecay
if epoch%50==0 then
optimState.learningRate=optimState.learningRate*0.5
else
optimState.learningRate=optimState.learningRate --*opt.LRDecay
end
print('-------------------LR-------------------')
print(optimState.learningRate)
epoch = epoch + 1
end