-
Notifications
You must be signed in to change notification settings - Fork 5
/
1_data_cifar.lua
204 lines (168 loc) · 7.3 KB
/
1_data_cifar.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
----------------------------------------------------------------------
-- This script loads the CIFAR10 dataset
-- training data, and pre-process it to facilitate learning.
-- Clement Farabet, E. Culurciello
----------------------------------------------------------------------
require 'torch' -- torch
require 'image' -- to visualize the dataset
require 'nn' -- provides a normalization operator
----------------------------------------------------------------------
-- parse command line arguments
if not opt then
print '==> processing options'
cmd = torch.CmdLine()
cmd:text()
cmd:text('CIFAR Dataset Preprocessing')
cmd:text()
cmd:text('Options:')
-- cmd:option('-size', 'small', 'how many samples do we load: small | full | extra')
cmd:option('-visualize', true, 'visualize input data and weights during training')
cmd:text()
opt = cmd:parse(arg or {})
end
----------------------------------------------------------------------
print '==> downloading dataset'
tar = 'http://data.neuflow.org/data/cifar10.t7.tgz'
if not paths.dirp('../datasets/cifar-10-batches-t7') then
os.execute('wget ' .. tar)
os.execute('tar xvf ' .. paths.basename(tar))
end
----------------------------------------------------------------------
print '==> loading dataset'
-- dataset size:
trsize = 50000
tesize = 2000
trainData = {
data = torch.Tensor(trsize, 3*32*32),
labels = torch.Tensor(trsize),
size = function() return trsize end
}
for i = 0,4 do
subset = torch.load('../datasets/cifar-10-batches-t7/data_batch_' .. (i+1) .. '.t7', 'ascii')
trainData.data[{ {i*10000+1, (i+1)*10000} }] = subset.data:t()
trainData.labels[{ {i*10000+1, (i+1)*10000} }] = subset.labels
end
trainData.labels = trainData.labels + 1
subset = torch.load('../datasets/cifar-10-batches-t7/test_batch.t7', 'ascii')
testData = {
data = subset.data:t():double(),
labels = subset.labels[1]:double(),
size = function() return tesize end
}
testData.labels = testData.labels + 1
-- resize dataset (if using small version)
trsize = 20000 -- repeated here for smaller size train/test
tesize = 2000
trainData.data = trainData.data[{ {1,trsize} }]
trainData.labels = trainData.labels[{ {1,trsize} }]
testData.data = testData.data[{ {1,tesize} }]
testData.labels = testData.labels[{ {1,tesize} }]
-- reshape data
trainData.data = trainData.data:reshape(trsize,3,32,32)
testData.data = testData.data:reshape(tesize,3,32,32)
print('Training Data:')
print(trainData)
print()
print('Test Data:')
print(testData)
print()
----------------------------------------------------------------------
print '==> preprocessing data'
-- Preprocessing requires a floating point representation (the original
-- data is stored on bytes). Types can be easily converted in Torch,
-- in general by doing: dst = src:type('torch.TypeTensor'),
-- where Type=='Float','Double','Byte','Int',... Shortcuts are provided
-- for simplicity (float(),double(),cuda(),...):
trainData.data = trainData.data:float()
testData.data = testData.data:float()
-- We now preprocess the data. Preprocessing is crucial
-- when applying pretty much any kind of machine learning algorithm.
-- For natural images, we use several intuitive tricks:
-- + images are mapped into YUV space, to separate luminance information
-- from color information
-- + the luminance channel (Y) is locally normalized, using a contrastive
-- normalization operator: for each neighborhood, defined by a Gaussian
-- kernel, the mean is suppressed, and the standard deviation is normalized
-- to one.
-- + color channels are normalized globally, across the entire dataset;
-- as a result, each color component has 0-mean and 1-norm across the dataset.
-- Convert all images to YUV
-- EC: removed not bio-inspired!
--print '==> preprocessing data: colorspace RGB -> YUV'
--for i = 1,trsize do
-- trainData.data[i] = image.rgb2yuv(trainData.data[i])
--end
--for i = 1,tesize do
-- testData.data[i] = image.rgb2yuv(testData.data[i])
--end
-- Name channels for convenience
--channels = {'y','u','v'}
channels = {'r','g','b'}
-- Normalize each channel, and store mean/std
-- per channel. These values are important, as they are part of
-- the trainable parameters. At test time, test data will be normalized
-- using these values.
--print '==> preprocessing data: normalize each feature (channel) globally'
--mean = {}
--std = {}
--for i,channel in ipairs(channels) do
-- -- normalize each channel globally:
-- mean[i] = trainData.data[{ {},i,{},{} }]:mean()
-- std[i] = trainData.data[{ {},i,{},{} }]:std()
-- trainData.data[{ {},i,{},{} }]:add(-mean[i])
-- trainData.data[{ {},i,{},{} }]:div(std[i])
--end
--
---- Normalize test data, using the training means/stds
--for i,channel in ipairs(channels) do
-- -- normalize each channel globally:
-- testData.data[{ {},i,{},{} }]:add(-mean[i])
-- testData.data[{ {},i,{},{} }]:div(std[i])
--end
-- Local normalization
-- (note: the global normalization is useless, if this local normalization
-- is applied on all channels... the global normalization code is kept just
-- for the tutorial's purpose)
print '==> preprocessing data: normalize all three channels locally'
-- Define the normalization neighborhood:
--if not is then is = 7 end -- find is value from call-out script
--print("Normalizing kernel size is:", is)
neighborhood = image.gaussian1D(9)
-- Define our local normalization operator (It is an actual nn module,
-- which could be inserted into a trainable model):
normalization = nn.SpatialContrastiveNormalization(1, neighborhood, 1e-3):float()
-- Normalize all channels locally:
for c in ipairs(channels) do
for i = 1,trsize do
trainData.data[{ i,{c},{},{} }] = normalization:forward(trainData.data[{ i,{c},{},{} }])
end
for i = 1,tesize do
testData.data[{ i,{c},{},{} }] = normalization:forward(testData.data[{ i,{c},{},{} }])
end
end
----------------------------------------------------------------------
print '==> verify statistics'
-- It's always good practice to verify that data is properly
-- normalized.
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
----------------------------------------------------------------------
print '==> visualizing data'
-- Visualization is quite easy, using image.display(). Check out:
-- help(image.display), for more info about options.
if opt.visualize then
first256Samples_y = trainData.data[{ {1,256},1 }]
first256Samples_u = trainData.data[{ {1,256},2 }]
first256Samples_v = trainData.data[{ {1,256},3 }]
image.display{image=first256Samples_y, nrow=16, legend='Some training examples: ' ..channels[1].. ' channel'}
image.display{image=first256Samples_u, nrow=16, legend='Some training examples: ' ..channels[2].. ' channel'}
image.display{image=first256Samples_v, nrow=16, legend='Some training examples: ' ..channels[3].. ' channel'}
end