-
Notifications
You must be signed in to change notification settings - Fork 5
/
train-cifar-CL1l-dist.lua
executable file
·257 lines (203 loc) · 8.46 KB
/
train-cifar-CL1l-dist.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
----------------------------------------------------------------------
-- Run k-means on CIFAR10 dataset - 1st layer generation/load and test
----------------------------------------------------------------------
import 'torch'
require 'image'
require 'unsup'
require 'eex'
cmd = torch.CmdLine()
cmd:text('Options')
cmd:option('-visualize', true, 'display kernels')
cmd:option('-seed', 1, 'initial random seed')
cmd:option('-threads', 8, 'threads')
cmd:option('-inputsize', 5, 'size of each input patches')
cmd:option('-nkernels', 16, 'number of kernels to learn')
cmd:option('-niter', 50, 'nb of k-means iterations')
cmd:option('-batchsize', 1000, 'batch size for k-means\' inner loop')
cmd:option('-nsamples', 1000*1000, 'nb of random training samples')
cmd:option('-initstd', 0.1, 'standard deviation to generate random initial templates')
cmd:option('-statinterval', 5000, 'interval for reporting stats/displaying stuff')
cmd:option('-savedataset', false, 'save modified dataset')
cmd:option('-classify', true, 'run classification train/test')
-- loss:
cmd:option('-loss', 'nll', 'type of loss function to minimize: nll | mse | margin')
-- training:
cmd:option('-save', 'results', 'subdirectory to save/log experiments in')
cmd:option('-plot', true, 'live plot')
cmd:option('-optimization', 'SGD', 'optimization method: SGD | ASGD | CG | LBFGS')
cmd:option('-learningRate', 1e-3, 'learning rate at t=0')
cmd:option('-batchSize', 1, 'mini-batch size (1 = pure stochastic)')
cmd:option('-weightDecay', 0, 'weight decay (SGD only)')
cmd:option('-momentum', 0, 'momentum (SGD only)')
cmd:option('-t0', 1, 'start averaging at t0 (ASGD only), in nb of epochs')
cmd:option('-maxIter', 2, 'maximum nb of iterations for CG and LBFGS')
cmd:text()
opt = cmd:parse(arg or {}) -- pass parameters to training files:
--if not qt then
-- opt.visualize = false
--end
torch.manualSeed(opt.seed)
torch.setnumthreads(opt.threads)
torch.setdefaulttensortype('torch.DoubleTensor')
is = opt.inputsize
nk = opt.nkernels
----------------------------------------------------------------------
-- loading and processing dataset:
dofile '1_data_cifar.lua'
----------------------------------------------------------------------
--[[print '==> extracting patches' -- only extract on Y channel (or R if RGB) -- all ok
data = torch.Tensor(opt.nsamples,is*is)
for i = 1,opt.nsamples do
img = math.random(1,trainData.data:size(1))
img2 = trainData.data[img] -- trainData1
z = math.random(1,trainData.data:size(2))
x = math.random(1,trainData.data:size(3)-is+1)
y = math.random(1,trainData.data:size(4)-is+1)
randompatch = img2[{ {z},{y,y+is-1},{x,x+is-1} }]
-- normalize patches to 0 mean and 1 std:
randompatch:add(-randompatch:mean())
--randompatch:div(randompatch:std())
data[i] = randompatch
end
-- show a few patches:
if opt.visualize then
f256S = data[{{1,256}}]:reshape(256,is,is)
image.display{image=f256S, nrow=16, nrow=16, padding=2, zoom=2, legend='Patches for 1st layer learning'}
end
--if not paths.filep('cifar10-1l.t7') then
print '==> running k-means'
function cb (kernels)
if opt.visualize then
win = image.display{image=kernels:reshape(nk,is,is), padding=2, symmetric=true,
zoom=2, win=win, nrow=math.floor(math.sqrt(nk)), legend='1st layer filters'}
end
end
-- kernels = unsup.kmeans(data, nk, opt.initstd,opt.niter, opt.batchsize,cb,true) --opt.niter
-- print('==> saving centroids to disk:')
-- torch.save('cifar10-1l.t7', kernels)]]
print('==> loading kernels from the disk:')
kernels = torch.load('cifar10-1l.t7')
--else
-- print '==> loading pre-trained k-means kernels'
-- kernels = torch.load('cifar10-1l.t7')
--end
for i=1,nk do
-- normalize kernels to 0 mean and 1 std:
-- kernels[i]:add(-kernels[i]:mean())
-- kernels[i]:div(kernels[i]:std())
-- clear nan kernels
if torch.sum(kernels[i]-kernels[i]) ~= 0 then
print('Found NaN kernels!')
kernels[i] = torch.zeros(kernels[1]:size())
end
-- give gaussian shape if needed:
-- sigma=0.25
-- fil = image.gaussian(is, sigma)
-- kernels[i] = kernels[i]:cmul(fil)
end
print '==> verify filters statistics'
print('filters max mean: ' .. kernels:mean(2):abs():max())
print('filters max standard deviation: ' .. kernels:std(2):abs():max())
----------------------------------------------------------------------
print "==> loading and initialize 1 layer CL model"
nk1=nk
opt.model = '1st-layer-dist'
dofile '2_model.lua'
l1net = model:clone()
-- initialize templates:
l1net.modules[1]:templates(kernels:reshape(nk, 1, is, is):expand(nk,3,is,is))
l1net.modules[1].bias = l1net.modules[1].bias *0
l1net.modules[3].weight = torch.ones(1)*(-1)
l1net.modules[7].weight = torch.ones(1)*(1/is)*(1/is)*(1/3)
torch.save('l1net.net', l1net)
l1net:forward(trainData.data[1]:double())
--print(l1net.modules[1].output)
--print('DDD')
print('1', l1net.modules[1], l1net.modules[1].output:min(), l1net.modules[1].output:max())
print('2', l1net.modules[2], l1net.modules[2].output:min(), l1net.modules[3].output:max())
print('3', l1net.modules[3], l1net.modules[3].output:min(), l1net.modules[3].output:max())
print('4', l1net.modules[4], l1net.modules[4].output:min(), l1net.modules[4].output:max())
print('5', l1net.modules[5], l1net.modules[5].output:min(), l1net.modules[5].output:max())
print('6', l1net.modules[6], l1net.modules[6].output:min(), l1net.modules[6].output:max())
print('7', l1net.modules[7], l1net.modules[7].output:min(), l1net.modules[7].output:max())
--tests:
--[[td_2 = image.lena()
out_2 = l1net:forward(td_2)
image.display(out_2)
]]
----------------------------------------------------------------------
print "==> processing dataset with CL network"
print(trsize)
print(l1netoutsize)
testData2 = {
data = torch.Tensor(tesize, nk*(l1netoutsize)^2),
labels = testData.labels:clone(),
size = function() return tesize end
}
trainData2 = {
data = torch.Tensor(trsize, nk*(l1netoutsize)^2),
labels = trainData.labels:clone(),
size = function() return trsize end
}
for t = 1,trainData2:size() do
trainData2.data[t] = l1net:forward(trainData.data[t]:double())
xlua.progress(t, trainData:size())
end
--trainData2.data = l1net:forward(trainData.data:double())
for t = 1,testData2:size() do
testData2.data[t] = l1net:forward(testData.data[t]:double())
xlua.progress(t, testData:size())
end
--testData2.data = l1net:forward(testData.data:double())
trainData2.data = trainData2.data:reshape(trsize, nk, l1netoutsize, l1netoutsize)
testData2.data = testData2.data:reshape(tesize, nk, l1netoutsize, l1netoutsize)
-- relocate pointers to new dataset:
--trainData1 = trainData -- save original dataset
--testData1 = testData
trainData = trainData2 -- relocate new dataset
testData = testData2
-- show a few outputs:
if opt.visualize then
f256S_y = trainData2.data[{ {1,256},1 }]
image.display{image=f256S_y, nrow=16, nrow=16, padding=2, zoom=2,
legend='Output 1st layer: first 256 examples, 1st feature'}
end
print '==> verify statistics'
channels = {'r','g','b'}
for i,channel in ipairs(channels) do
trainMean = trainData.data[{ {},i }]:mean()
trainStd = trainData.data[{ {},i }]:std()
testMean = testData.data[{ {},i }]:mean()
testStd = testData.data[{ {},i }]:std()
print('training data, '..channel..'-channel, mean: ' .. trainMean)
print('training data, '..channel..'-channel, standard deviation: ' .. trainStd)
print('test data, '..channel..'-channel, mean: ' .. testMean)
print('test data, '..channel..'-channel, standard deviation: ' .. testStd)
end
----------------------------------------------------------------------
-- save datasets:
if opt.savedataset then
trainData.data = trainData.data:float() -- float to save space if needed
testData.data = testData.data:float()
torch.save('trainData-cifar-CL1l-dist.t7', trainData)
torch.save('testData-cifar-CL1l-dist.t7', testData)
end
----------------------------------------------------------------------
-- classifier for train/test:
if opt.classify then
----------------------------------------------------------------------
print "==> creating classifier"
opt.model = '2mlp-classifier'
dofile '2_model.lua'
print "==> test network output:"
print(model:forward(trainData.data[1]:double()))
dofile '3_loss.lua'
dofile '4_train.lua'
dofile '5_test.lua'
----------------------------------------------------------------------
print "==> training classifier"
while true do
train()
test()
end
end