This repository has been archived by the owner on Jul 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
data_processor.lua
277 lines (225 loc) · 8.81 KB
/
data_processor.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require('torch')
require('image')
local boxSampling = require('boxsampling')
local classToNumber = require('data/classnumber')
local batchSize = 32
local allShapes = { {224, 224} ,
{224, 256}, {256, 224},
{224, 288} ,
{288, 224} }
torch.setdefaulttensortype('torch.FloatTensor')
local dataProcessor = {}
local img2caffe = function(img)
local mean_pixel = torch.Tensor({103.939, 116.779, 123.68})
local perm = torch.LongTensor{3, 2, 1}
img = img:index(1, perm):mul(256.0)
mean_pixel = mean_pixel:view(3, 1, 1):expandAs(img)
img:add(-1, mean_pixel)
return img
end
dataProcessor._init = function(modelInfo)
local self = dataProcessor
self.modelInfo = modelInfo
self.trainNumber = #self.trainSamples
self.trainPerm = torch.randperm(self.trainNumber)
self.trainPos = 1
self.verifyNumber = #self.verifySamples
self.verifyPerm = torch.randperm(self.verifyNumber)
self.verifyPos = 1
end
dataProcessor._buildTarget = function(targetWidth, targetHeight, labels, targetImg)
local self = dataProcessor
local _ = self.modelInfo.getSize(targetWidth, targetHeight)
local lastWidth = _[1]
local lastHeight = _[2]
local targets = {}
local masks = {}
local predBoxes = boxSampling(self.modelInfo, targetWidth, targetHeight, labels)
local pindex = 1 -- same order with boxSampling
for i = 1, #self.modelInfo.boxes do
local wid = lastWidth - (self.modelInfo.boxes[i][1] - 1)
local hei = lastHeight - (self.modelInfo.boxes[i][2] - 1)
local conf = torch.zeros(hei, wid)
local loc = torch.zeros(4, hei, wid)
local confMask = torch.zeros(21, hei, wid)
local locMask = torch.zeros(4, hei, wid)
for h = 1,hei do
for w = 1,wid do
local pbox = predBoxes[pindex]
pindex = pindex + 1
-- skkiped predition
if ( pbox.label == -1) then
-- set to backgroud
conf[h][w] = self.modelInfo.classNumber
end
-- negative predition
if ( pbox.label == 0) then
confMask[{{}, h, w}] = 1
-- background
conf[h][w] = self.modelInfo.classNumber
end
-- positive predition
if ( pbox.label > 0) then
confMask[{{}, h, w}] = 1
locMask[{{}, h, w}] = 1
-- object
conf[h][w] = classToNumber( labels[pbox.label].class )
-- location
loc[1][h][w] = (labels[pbox.label].xmin - pbox.xmin) / 16
loc[2][h][w] = (labels[pbox.label].ymin - pbox.ymin) / 16
loc[3][h][w] = (labels[pbox.label].xmax - pbox.xmax) / 16
loc[4][h][w] = (labels[pbox.label].ymax - pbox.ymax) / 16
--[[
targetImg = image.drawRect(targetImg, labels[pbox.label].xmin, labels[pbox.label].ymin, labels[pbox.label].xmax, labels[pbox.label].ymax);
targetImg = image.drawRect(targetImg, pbox.xmin+2, pbox.ymin+2, pbox.xmax-2, pbox.ymax-2, {color = {0, 255, 0}});
--]]
end
end
end
table.insert(targets, conf)
table.insert(targets, loc)
table.insert(masks, confMask)
table.insert(masks, locMask)
end
--[[
local randFile = './images/' .. math.random() .. '.jpg'
image.save(randFile, targetImg)
--]]
return targets, masks
end
dataProcessor.doSampling = function(isVerify)
local self = dataProcessor
local _ = math.floor(math.random() * 100) % 3 + 1
local targetWidth = allShapes[_][1]
local targetHeight = allShapes[_][2]
local _ = self.modelInfo.getSize(targetWidth, targetHeight)
local lastWidth = _[1]
local lastHeight = _[2]
local xinput = torch.Tensor(batchSize, 3, targetHeight, targetWidth)
local targets = {}
local masks = {}
for i = 1, #self.modelInfo.boxes do
local wid = lastWidth - (self.modelInfo.boxes[i][1] - 1)
local hei = lastHeight - (self.modelInfo.boxes[i][2] - 1)
local conf = torch.Tensor(batchSize, hei, wid)
local loc = torch.Tensor(batchSize, 4, hei, wid)
table.insert(targets, conf)
table.insert(targets, loc)
local confMask = torch.zeros(batchSize, 21, hei, wid)
local locMask = torch.zeros(batchSize, 4, hei, wid)
table.insert(masks, confMask)
table.insert(masks, locMask)
end
local i = 1
while ( i <= batchSize ) do
local ii = self.trainPerm[self.trainPos]
local info = self.trainSamples[ii]
if ( isVerify == true) then
ii = self.verifyPerm[self.verifyPos]
info = self.verifySamples[ii]
end
local targetImg, labels = self._processImage(info, targetWidth, targetHeight)
if ( targetImg ~= nil) then
xinput[i]:copy( targetImg);
local ts, ms = self._buildTarget(targetWidth, targetHeight, labels, targetImg)
for j = 1, #ts do
targets[j][i]:copy( ts[j] )
masks[j][i]:copy( ms[j] )
end
i = i + 1
end
if ( isVerify == true ) then
self.verifyPos = self.verifyPos + 1
if ( self.verifyPos > self.verifyNumber) then
self.verifyPos = 1
end
else
self.trainPos = self.trainPos + 1
if ( self.trainPos > self.trainNumber) then
self.trainPos = 1
end
end
end
collectgarbage();
return {xinput, targets, masks}
end
dataProcessor.doVerifySampling = function()
local self = dataProcessor
return self.doSampling(true)
end
-- image random sampling
dataProcessor._processImage = function(info, targetWidth, targetHeight)
local img = image.loadJPG('./data/' .. info['image']['file'])
local wid = img:size()[3]
local hei = img:size()[2]
-- scale and crop
local scale = targetWidth / wid
local cutWid, cutHei = targetWidth , math.floor(hei * scale)
local offsetx, offsety = 0, math.floor(math.random() * (cutHei - targetHeight) )
if ( cutHei < targetHeight) then
scale = targetHeight / hei
cutWid, cutHei = math.floor(wid * scale), targetHeight
offsety, offsetx = 0, math.floor(math.random() * (cutWid - targetWidth) )
end
if ( cutWid <= targetWidth ) then
cutWid = targetWidth
offsetx = 0
end
if ( cutHei <= targetHeight ) then
cutHei = targetHeight
cutsety = 0;
end
local labels = {}
local anns = info["annotation"]
for i = 1, #anns do
local bbox = {}
bbox.class = anns[i]['category_id']
bbox.xmin = math.floor(anns[i]['bbox'][1]*scale) - offsetx
bbox.ymin = math.floor(anns[i]['bbox'][2]*scale) - offsety
bbox.xmax = math.floor(anns[i]['bbox'][3]*scale) + bbox.xmin
bbox.ymax = math.floor(anns[i]['bbox'][4]*scale) + bbox.ymin
if ( bbox.xmax > 0 and bbox.ymax > 0 and bbox.xmin < targetWidth and bbox.ymin < targetHeight ) then
if ( bbox.xmin < 0) then
bbox.xmin = 0
end
if ( bbox.ymin < 0) then
bbox.ymin = 0
end
if ( bbox.xmax >= targetWidth ) then
bbox.xmax = targetWidth
end
if ( bbox.ymax >= targetHeight ) then
bbox.ymax = targetHeight
end
if ( (bbox.ymax - bbox.ymin) > 16 and (bbox.xmax - bbox.xmin) > 16 ) then
table.insert(labels, bbox)
end
end
end
if ( #labels == 0) then
return nil
end
local scaledImg = image.scale(img, cutWid, cutHei)
local targetImg = image.crop(scaledImg, offsetx, offsety, offsetx + targetWidth, offsety + targetHeight)
if ( math.random() > 0.5) then
targetImg = image.hflip(targetImg)
for i = 1, #labels do
local temp = targetWidth - labels[i].xmin
labels[i].xmin = targetWidth - labels[i].xmax
labels[i].xmax = temp
end
end
--[[
for i = 1, #labels do
local bbox = labels[i]
targetImg = image.drawRect(targetImg, bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax);
end
--local randFile = './images/' .. math.random() .. '.jpg'
--image.save(randFile, targetImg)
--]]
targetImg = img2caffe(targetImg)
return targetImg, labels
end
dataProcessor.trainSamples = infoDB[1]
dataProcessor.verifySamples = infoDB[2]
return dataProcessor