-
Notifications
You must be signed in to change notification settings - Fork 10
/
data.lua
146 lines (133 loc) · 3.29 KB
/
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
local data = {}
function data.readij(i,j)
return image.load('data/'..i..'/'..j..'.png')[1]
end
function data.thresh(img,h)
local h = h or 0.8
local y = img:clone()
y[y:lt(h)] = 0
y[y:gt(0)] = 1
return y
end
local function DFS(img,i,j,H,W)
local cnt = 0
if(i>=1 and i<=H and j>=1 and j<=W and img[i][j]==1) then
img[i][j] = 0
cnt = 1
cnt = cnt + DFS(img,i-1,j,H,W)
cnt = cnt + DFS(img,i,j-1,H,W)
cnt = cnt + DFS(img,i+1,j,H,W)
cnt = cnt + DFS(img,i,j+1,H,W)
end
return cnt
end
function data.clean(img,conn,thresh)
local img = data.thresh(img,thresh)
local H,W = img:size(1),img:size(2)
local conn = conn or 20
local z = img:clone()
local comp = 0
for i=1,H do
for j=1,W do
local cnt = DFS(img,i,j,H,W)
if(cnt>0 and cnt<conn) then
DFS(z,i,j,H,W)
end
if(cnt>=conn) then
comp = comp + 1
end
end
end
return z,comp
end
function data.components(cimg)
local H,W = cimg:size(1),cimg:size(2)
local z = cimg:clone()
local xy = {}
for j=1,W do
for i=1,H do
local zz = z:clone()
local cnt = DFS(z,i,j,H,W)
if(cnt>0) then
table.insert(xy,zz-z)
end
end
end
return xy
end
local function crop1(zzz)
local hsum = zzz:sum(2):squeeze()
local htop = nil
local hbot = nil
for i=1,hsum:nElement() do
if(hsum[i]~=0) then htop = htop or i end
if(htop and hsum[i]==0) then hbot = hbot or i end
end
return zzz[{{htop,hbot}}]
end
local function crop2(zzz)
local hsum = zzz:sum(1):squeeze()
local htop = nil
local hbot = nil
for i=1,hsum:nElement() do
if(hsum[i]~=0) then htop = htop or i end
if(htop and hsum[i]==0) then hbot = hbot or i end
end
return zzz[{{},{htop,hbot}}]
end
function data.crop(zzz)
return crop2(crop1(zzz))
end
function data.crop_comp(i,j)
local img = data.readij(i,j)
--itorch.image(img)
local z,c = data.clean(img)
--itorch.image(z)
local xy = data.components(z)
local ret = {}
for i=1,#xy do
if(data.crop(xy[i]):sum()>=20) then
table.insert(ret,-data.crop(xy[i]))
end
end
return ret
end
function data.preprocess(from,to)
for i=from or 1,to or 500 do
local cnt = 0
local tbl = {}
for j=1,99 do
local seq = data.crop_comp(i,j)
if(#seq==6) then
cnt = cnt+1
end
table.insert(tbl,seq)
end
torch.save('data/'..i..'/data.t7',tbl)
print(i,cnt)
end
end
function data.getY(file)
require 'csvigo';
local file = file or 'data/labels.txt'
local tbl = csvigo.load{path = 'data/labels.txt',header=false}.var_1
for i=1,#tbl do
tbl[i] = string.upper(tbl[i])
end
local N = #tbl
local d = 6
local Y = torch.zeros(N,d)
for i=1,N do
for j=1,d do
Y[i][j] = string.byte(tbl[i],j)-string.byte('A')+1
end
end
return Y
end
function data.loadY(file)
return torch.load(file or 'data/Y.t7')
end
function data.saveY(file)
return torch.load(file or 'data/Y.t7',data.getY())
end
return data