-
Notifications
You must be signed in to change notification settings - Fork 2
/
1_data.lua
195 lines (172 loc) · 5.57 KB
/
1_data.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
----------------------------------------------------------------------
-- It's a good idea to run this script with the interactive mode:
-- $ th -i 1_data.lua
-- this will give you a Torch interpreter at the end, that you
-- can use to analyze/visualize the data you've just loaded.
--
-- Clement Farabet
----------------------------------------------------------------------
require 'torch' -- torch
require 'nn' -- provides a normalization operator
----------------------------------------------------------------------
-- parse command line arguments
if not opt then
print '==> processing options'
cmd = torch.CmdLine()
cmd:text()
cmd:text('Gait Dataset Preprocessing')
cmd:text()
cmd:text('Options:')
cmd:option('-visualize', true, 'visualize input data and weights during training')
cmd:text()
opt = cmd:parse(arg or {})
end
----------------------------------------------------------------------
-- my utility functions
function string:split(sep)
local sep, fields = sep, {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(substr) fields[#fields + 1] = substr end)
return fields
end
----------------------------------------------------------------------
train_file = 'alignedData_train.csv'
test_file = 'alignedData_test.csv'
trsize = -1
tesize = -1
signal_len = -1
----------------------------------------------------------------------
print '==> loading dataset'
-- Count number of rows and columns in file
instance_id = -1
for line in io.lines(train_file) do
local l = line:split(',')
if instance_id ~= l[1] then
instance_id = l[1]
signal_len = 0
end
signal_len = signal_len + 1
end
trsize = instance_id + 1
-- Read data from CSV to tensor
data = torch.Tensor(trsize,2,3,signal_len) -- accel*3, alpha*3
labels = torch.Tensor(trsize)
i = 1
instance_id = -1
for line in io.lines(train_file) do
local l = line:split(',')
if instance_id ~= l[1] + 1 then
instance_id = l[1] + 1
labels[instance_id] = l[2]
i = 1
end
instance_id = l[1] + 1
local value_id = l[1] + 1
for key, val in ipairs(l) do
repeat
local col = key - 2
if col < 1 then
break
elseif col < 4 then
data[instance_id][1][col][i] = val
else
data[instance_id][2][col-3][i] = val
end
until true
end
i = i + 1
end
-- Note: the data, in X, is 4-d: the 1st dim indexes the samples, the 2nd
-- dim indexes the channels (accel or alpha), and the last two dims index the
-- direction(height) and timestamp(width) of the samples.
trainData = {
data = data,
labels = labels,
size = function() return trsize end
}
-- Finally we load the test data.
-- Count number of rows and columns in file
instance_id = -1
for line in io.lines(test_file) do
local l = line:split(',')
if instance_id ~= l[1] then
instance_id = l[1]
signal_len = 0
end
signal_len = signal_len + 1
end
tesize = instance_id + 1
-- Read data from CSV to tensor
data = torch.Tensor(tesize,2,3,signal_len) -- accel*3, alpha*3
labels = torch.Tensor(tesize)
i = 1
instance_id = -1
for line in io.lines(test_file) do
local l = line:split(',')
if instance_id ~= l[1] + 1 then
instance_id = l[1] + 1
labels[instance_id] = l[2]
i = 1
end
instance_id = l[1] + 1
local value_id = l[1] + 1
for key, val in ipairs(l) do
repeat
local col = key - 2
if col < 1 then
break
elseif col < 4 then
data[instance_id][1][col][i] = val
else
data[instance_id][2][col-3][i] = val
end
until true
end
i = i + 1
end
testData = {
data = data,
labels = labels,
size = function() return tesize end
}
----------------------------------------------------------------------
print '==> preprocessing data'
-- 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
-- + channels 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.
channels = {'accel','alpha'}
----------------------------------------------------------------------
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 itorch.image().
if opt.visualize then
if itorch then
first256Samples_y = trainData.data[{ {1,256},1 }]
first256Samples_u = trainData.data[{ {1,256},2 }]
first256Samples_v = trainData.data[{ {1,256},3 }]
itorch.image(first256Samples_y)
itorch.image(first256Samples_u)
itorch.image(first256Samples_v)
else
print("For visualization, run this script in an itorch notebook")
end
end