-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDataLoader.lua
82 lines (61 loc) · 1.99 KB
/
DataLoader.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
require 'torch'
require 'hdf5'
local DataLoader = {}
DataLoader.__index = DataLoader
function DataLoader.create(batch_size)
-- split_fractions is e.g. {0.9, 0.05, 0.05}
local self = {}
setmetatable(self, DataLoader)
self.batch_size = batch_size
self.splits={
train={},
val={},
test={}
}
for split, _ in pairs(self.splits) do
self.splits[split].batch_idx = 1
local myFile = hdf5.open(split..'.h5','r')
local dim = myFile:read('data'):dataspaceSize()
myFile:close()
print("the size of ".. split.." is ",dim)
self.splits[split].sample_size = dim[1]
print("the example size of "..split.." is ",self.splits[split].sample_size)
end
print(string.format('data have read once for the size.'))
collectgarbage()
return self
end
function DataLoader:nextBatch(split)
assert(split == 'train' or split == 'test' or split == 'val')
local dataset=self.splits[split]
--batch has done in preprocssing
local start=dataset.batch_idx
local start_end=start+self.batch_size-1
-- set the deadline
if(start_end>=dataset.sample_size) then
self.splits[split].batch_idx=1
return nil
else
local myFile=hdf5.open(split..'.h5','r')
local myFileData={data=myFile:read('data'):partial({start,start_end},{1,20},{1,1},{1,24},{1,113}),label=myFile:read('label'):partial({start,start_end})}
myFile:close()
self.splits[split].batch_idx=start_end+1
activityData={}
activityLabel={}
local batch={
data=myFileData.data,
label=myFileData.label
-- label=torch.cat(activityLabel,1),
}
setmetatable(batch,
{__index = function(t, k)
return {t.data[k], t.label[k]}
end}
);
function batch:size()
return self.label:size(1)
end
return batch
end
end
return DataLoader