-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.lua
More file actions
156 lines (143 loc) · 4.02 KB
/
data.lua
File metadata and controls
156 lines (143 loc) · 4.02 KB
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
math.randomseed(os.time())
----------------------------------------------------------------------
-- function readfbank(filename, lines)
-- local file = io.open(filename, 'r')
-- if not(file) then
-- print("Error "..filename.." does not exist")
-- return 0
-- end
-- for line in file:lines() do
-- lines[#lines+1] = line
-- end
-- file:close()
-- return lines
-- end
-- function parsefbank(lines, data, labels)
-- for i = 1,#lines do
-- tb = {}
-- for k, v in string.gmatch(lines[i], "%S*") do
-- if 0~=string.len(k) then
-- tb[#tb+1] = tonumber(k)
-- end
-- end
-- labels[i] = tb[1]
-- -- r = math.random()
-- -- if (r<0.3) then
-- -- labels[i] = 2
-- -- sum = sum+1
-- -- end
-- for h = 1, height do
-- for m = 1, nfeats do
-- for w = 1, width do
-- data[i][m][h][w] = tb[1+w+(m-1)*width+(h-1)*nfeats*width]
-- end
-- end
-- end
-- end
-- end
function readLabel(filename)
filelabel = {}
nlabels = 0
if (filename~='') then
local fin = io.open(filename,'r')
for line in fin:lines() do
local l = line:split(' ')
filelabel[l[1]] = tonumber(l[2])
nlabels = nlabels+1
end
end
end
-- function readDataFeat(featfile)
-- print('==> reading feature from '..opt.featfile)
-- local lines = {}
-- local curlinenum = 0
-- while (curlinenum<opt.maxrows) do
-- curlinenum = curlinenum+1
-- local line = featfile:read()
-- if (line~=nil) then
-- lines[#lines+1] = line
-- else
-- break
-- end
-- end
-- local tdata = torch.Tensor(#lines, ninputs)
-- local tlabels = torch.Tensor(#lines,1)
-- parsefbank(lines, tdata, tlabels)
-- -- print(trlabels)
-- local Data = {
-- data = tdata,
-- labels = tlabels,
-- size = function() return #lines end
-- }
-- return Data
-- end
-- function readDataScp1(listfile,filenum)
-- local lines = {}
-- local curlinenum = 0
-- while (curlinenum<filenum) do
-- curlinenum = curlinenum+1
-- local line = listfile:read()
-- if (line~=nil) then
-- local fbankfilename = line
-- print("Reading feature from "..fbankfilename)
-- readfbank(fbankfilename, lines)
-- else
-- break
-- end
-- end
-- local tdata = torch.Tensor(#lines, nfeats, height, width)
-- local tlabels = torch.Tensor(#lines,1)
-- parsefbank(lines, tdata, tlabels)
-- -- print(trlabels)
-- local Data = {
-- data = tdata,
-- labels = tlabels,
-- size = function() return #lines end
-- }
-- return Data
-- end
function readDataScp2(listfile,filenum,means,variances)
local feats = {}
local labels = {}
local curlinenum = 0
while (curlinenum<filenum) do
curlinenum = curlinenum+1
local line = listfile:read()
if (line~=nil) then
filename = paths.basename(line)
local chunk = filename:split('_')
print("Reading feature from "..line)
assert(io.open(line),"File " .. line .. " does not exist!")
local feat = loadhtk(line, frameExt)
for i=1,feat:size(1) do
feats[#feats+1] = feat[i]
if (nlabels~=0) then
labels[#feats] = filelabel[chunk[1]]
else
labels[#feats] = 1
end
end
else
break
end
end
if (#feats==0) then
return
end
-- print(labels)
local tdata = torch.Tensor(#feats, feats[1]:size(1))
local tlabels = torch.Tensor(labels)
for i=1,#feats do
tdata[i] = feats[i]
-- normlize if globalnorm exists, T norm is defined as (data-means)/variance
if (opt.globalnorm~='') then
tdata[i]:map2(means, variances, function(data,mean,variance) return (data+mean)*variance end)
end
end
local Data = {
data = tdata,
labels = tlabels,
size = function() return #feats end
}
return Data
end