-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2torch.lua
More file actions
197 lines (172 loc) · 5.62 KB
/
csv2torch.lua
File metadata and controls
197 lines (172 loc) · 5.62 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
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
require 'dok'
csv = {}
function csv.open(...)
local _, filename = dok.unpack(
{...},
'csv.open',
[=[
Opens csv file <filename> reads and returns an anonymous function
which can be called to step line by line thorugh the file.
USAGE:
local csvg = csv.open(fname)
local firstline = csvg()
Returns nil at EOF.
]=],
{arg='filename', type='string',
help='filename of csv to open', req=true}
)
local f = assert(io.open(filename, 'r'))
return function()
local line = f:read("*line")
if line then
local ml = false
line = string.gsub(line, "'", "\\'") .. ',' -- ending comma
local row = {} -- table to collect fields
local f_start = 1
repeat
-- multiline field
if ml then
line = line .. string.gsub(f:read("*line"), "'", "\\'") .. ','
local i = f_start
repeat
-- find closing quote; chew accross escaped quotes
a, i, c = string.find(line, '(\\?)"', i+1)
until c ~= '\\' -- not an escaped quote?
if i then
local f = string.sub(line, f_start+1, i-1)
table.insert(row, (string.gsub(f, '\\"', '"')))
f_start = string.find(line, ',', i) + 1
ml = false
end
end
-- next field is quoted? (start with `"'?)
if string.find(line, '^"', f_start) then
local a, c
local i = f_start
repeat
-- find closing quote; chew accross escaped quotes
a, i, c = string.find(line, '(\\?)"', i+1)
until c ~= '\\' -- not an escaped quote?
if not i then
-- error('unmatched "')
line = string.gsub(line, '\\,$', '\n')
ml = true
else
local f = string.sub(line, f_start+1, i-1)
table.insert(row, (string.gsub(f, '\\"', '"')))
f_start = string.find(line, ',', i) + 1
end
else -- unquoted; find next comma
local nexti = string.find(line, ',', f_start)
table.insert(row, string.sub(line, f_start, nexti-1))
f_start = nexti + 1
end
until f_start > string.len(line)
return row
else
f:close()
end
end
end
function csv.make_reverse_table (...)
local _, row = dok.unpack(
{...},
'csv.make_reverse_table',
[=[
Simplifies access to a named column in the csv file by hashing column name to the column number
EXAMPLE:
t7> csvg = csv.open(fname)
t7> header = csvg()
t7> =header
{[1] = string : "bbx"
[2] = string : "bby"
[3] = string : "bbw"
[4] = string : "bbh"}
> rheader = csv.make_reverse_table(header)
t7> =rheader
{[bbx] = 1
[bby] = 2
[bbw] = 3
[bbh] = 4}
]=],
{arg='row', type='table',
help='parsed header of csv', req=true}
)
rv = {}
for i = 1,#row do
rv[row[i]] = i
end
return rv
end
function csv.open_with_header (...)
local _, filename = dok.unpack(
{...},
'csv.open_with_header',
[=[
Opens csv file <filename> reads and returns an anonymous function
which can be called to step line by line thorugh the file. The
anonymous function returns nil at EOF.
Parses the first header line to make access of named columns easier
EXAMPLE:
t7> csvg, header,rheader = csv.open_with_header(fname)
t7> =header
{[1] = string : "bbx"
[2] = string : "bby"
[3] = string : "bbw"
[4] = string : "bbh"}
t7> =rheader
{[bbx] = 1
[bby] = 2
[bbw] = 3
[bbh] = 4}
t7> line = csvg()
t7> =line[rheader["bbx"]]
34398
]=],
{arg='filename', type='string',
help='filename of csv to open', req=true}
)
local csvg = csv.open(filename)
local header = csvg()
local rheader = csv.make_reverse_table(header)
return csvg, header, rheader
end
require 'dp'
cmd = torch.CmdLine()
cmd:text('th csv2torch.lua --csvfile ~/data/FacialKeypoints/training.csv --th7file ~/data/FacialKeypoints/train.th7 --train')
cmd:option('--csvfile', '', 'location of the csv file')
cmd:option('--th7file', '', 'location of the th7 file')
cmd:option('--train', false, 'training set includes target keypoints')
cmd:text()
opt = cmd:parse(arg or {})
csvg, header, rheader = csv.open_with_header(opt.csvfile)
local data = {}
local line = csvg()
if opt.train then
while line do
local row = {}
local row = _.map(_.split(line[31], ' '), function(k,v)
return tonumber(v)
end)
for i=1,30 do
table.insert(row, i, tonumber(line[i]) or -1)
end
table.insert(data, torch.FloatTensor(row))
line = csvg()
end
else
while line do
local row = {}
local row = _.map(_.split(line[2], ' '), function(k,v)
return tonumber(v)
end)
table.insert(row, 1, tonumber(line[1]))
table.insert(data, torch.FloatTensor(row))
line = csvg()
end
end
local tensor = torch.FloatTensor(#data, data[1]:size(1))
for i,row in ipairs(data) do
tensor[i]:copy(row)
end
torch.save(opt.th7file, tensor)