-
Notifications
You must be signed in to change notification settings - Fork 40
/
DataSetPascal.lua
421 lines (363 loc) · 10.9 KB
/
DataSetPascal.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
local matio = require 'matio'
local argcheck = require 'argcheck'
local xml = require 'xml'
matio.use_lua_strings = true
local DataSetPascal = torch.class('nnf.DataSetPascal')
local function lines_from(file)
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
if not paths.filep(file) then return {} end
local lines = {}
for line in io.lines(file) do
table.insert(lines,line)
end
return lines
end
--
local env = require 'argcheck.env' -- retrieve argcheck environement
-- this is the default type function
-- which can be overrided by the user
function env.istype(obj, typename)
local t = torch.type(obj)
if t:find('torch.*Tensor') then
return 'torch.Tensor' == typename
end
return torch.type(obj) == typename
end
local initcheck = argcheck{
pack=true,
noordered=true,
help=[[
A dataset class for object detection in Pascal-like datasets.
]],
{name="image_set",
type="string",
help="ImageSet name"},
{name="datadir",
type="string",
help="Path to dataset (follows standard Pascal folder structure)",
check=function(datadir)
return paths.dirp(datadir)
end},
{name="classes",
type="table",
help="Classes to be considered",
default = {'aeroplane','bicycle','bird','boat','bottle','bus','car',
'cat','chair','cow','diningtable','dog','horse','motorbike',
'person','pottedplant','sheep','sofa','train','tvmonitor'},
check=function(classes)
local out = true;
for k,v in ipairs(classes) do
if type(v) ~= 'string' then
print('classes can only be of string input');
out = false
end
end
return out
end},
{name="imgsetpath",
type="string",
help="Path to the ImageSet file",
opt = true},
{name="with_hard_samples",
type="boolean",
help="Use difficult samples in the proposals",
opt = true},
{name="year",
type="number",
help="Year of the dataset (for Pascal)",
opt = true},
{name="roidbdir",
type="string",
help="Path to the folder with the bounding boxes",
opt = true},
{name="annopath",
type="string",
help="Path to the annotations",
opt = true},
{name="imgpath",
type="string",
help="Path to the images",
opt = true},
{name="roidbfile",
type="string",
help="Mat file with the bounding boxes",
opt = true},
{name="dataset_name",
type="string",
help="Name of the dataset",
opt = true}--[[,
{name="image",
type="torch.Tensor",
help="Dataset of one single image",
opt = true}]]
}
function DataSetPascal:__init(...)
local args = initcheck(...)
print(args)
for k,v in pairs(args) do self[k] = v end
local image_set = self.image_set
if not self.year then
self.year = 2007
end
local year = self.year
if not self.dataset_name then
self.dataset_name = 'VOC'..year
end
if not self.annopath then
self.annopath = paths.concat(self.datadir,self.dataset_name,'Annotations','%s.xml')
end
if not self.imgpath then
self.imgpath = paths.concat(self.datadir,self.dataset_name,'JPEGImages','%s.jpg')
end
if not self.imgsetpath then
self.imgsetpath = paths.concat(self.datadir,self.dataset_name,'ImageSets','Main','%s.txt')
end
if not self.roidbfile and self.roidbdir then
self.roidbfile = paths.concat(self.roidbdir,'voc_'..year..'_'..image_set..'.mat')
end
self.num_classes = #self.classes
self.class_to_id = {}
for i,v in ipairs(self.classes) do
self.class_to_id[v] = i
end
self.img_ids = lines_from(string.format(self.imgsetpath,image_set))
self.num_imgs = #self.img_ids
--
if self.image then
self.img_ids = {}
end
--[[
self.sizes = {}
print('Getting Image Sizes')
for i=1,#self.img_ids do
xlua.progress(i,#self.img_ids)
local imp = string.format(self.imgpath,self.img_ids[i])
table.insert(self.sizes,{image.getJPGsize(imp)})
if i%100 == 0 then
collectgarbage()
end
end
self.sizes = torch.IntTensor(self.sizes)
]]
end
function DataSetPascal:size()
return #self.img_ids
end
function DataSetPascal:getImage(i)
return image.load(string.format(self.imgpath,self.img_ids[i]))
end
local function parsePascalAnnotation(ann,ind,parent)
local res = {}
for i,j in ipairs(ann) do
if #j == 1 then
res[j.xml] = j[1]
else
local sub = parsePascalAnnotation(j,i,j.xml)
if not res[j.xml] then
res[j.xml] = sub
elseif #res[j.xml] == 0 then
res[j.xml] = {res[j.xml]}
table.insert(res[j.xml],sub)
else
table.insert(res[j.xml],sub)
end
end
end
return res
end
function DataSetPascal:getAnnotation(i)
local ann = xml.loadpath(string.format(self.annopath,self.img_ids[i]))
local parsed = parsePascalAnnotation(ann,1,{})
if parsed.object and #parsed.object == 0 then
parsed.object = {parsed.object}
end
return parsed
end
function DataSetPascal:__tostring__()
local str = torch.type(self)
str = str .. '\n Dataset Name: ' .. self.dataset_name
str = str .. '\n ImageSet: '.. self.image_set
str = str .. '\n Number of images: '.. self:size()
str = str .. '\n Classes:'
for k,v in ipairs(self.classes) do
str = str .. '\n '..v
end
return str
end
function DataSetPascal:loadROIDB()
if self.roidb then
return
end
local roidbfile = self.roidbfile
assert(roidbfile and paths.filep(roidbfile),'Need to specify the bounding boxes file')
local dt = matio.load(roidbfile)
self.roidb = {}
local img2roidb = {}
-- compat: change coordinate order from [y1 x1 y2 x2] to [x1 y1 x2 y2]
for i=1,#dt.images do
img2roidb[dt.images[i] ] = i
end
for i=1,self:size() do
if dt.boxes[img2roidb[self.img_ids[i] ] ]:size(2) ~= 4 then
table.insert(self.roidb,torch.IntTensor(0,4))
else
table.insert(self.roidb, dt.boxes[img2roidb[self.img_ids[i] ] ]:index(2,torch.LongTensor{2,1,4,3}):int())
end
end
end
function DataSetPascal:getROIBoxes(i)
if not self.roidb then
self:loadROIDB()
end
return self.roidb[i]--self.roidb[self.img2roidb[self.img_ids[i] ] ]
end
local function boxoverlap(a,b)
local b = b.xmin and {b.xmin,b.ymin,b.xmax,b.ymax} or b
local x1 = a:select(2,1):clone()
x1[x1:lt(b[1])] = b[1]
local y1 = a:select(2,2):clone()
y1[y1:lt(b[2])] = b[2]
local x2 = a:select(2,3):clone()
x2[x2:gt(b[3])] = b[3]
local y2 = a:select(2,4):clone()
y2[y2:gt(b[4])] = b[4]
local w = x2-x1+1;
local h = y2-y1+1;
local inter = torch.cmul(w,h):float()
local aarea = torch.cmul((a:select(2,3)-a:select(2,1)+1) ,
(a:select(2,4)-a:select(2,2)+1)):float()
local barea = (b[3]-b[1]+1) * (b[4]-b[2]+1);
-- intersection over union overlap
local o = torch.cdiv(inter , (aarea+barea-inter))
-- set invalid entries to 0 overlap
o[w:lt(0)] = 0
o[h:lt(0)] = 0
return o
end
function DataSetPascal:getGTBoxes(i)
local anno = self:getAnnotation(i)
local valid_objects = {}
local gt_boxes = torch.IntTensor()
local gt_classes = {}
if self.with_hard_samples then -- inversed with respect to RCNN code
for idx,obj in ipairs(anno.object) do
if self.class_to_id[obj.name] then -- to allow a subset of the classes
table.insert(valid_objects,idx)
end
end
else
for idx,obj in ipairs(anno.object) do
if obj.difficult == '0' and self.class_to_id[obj.name] then
table.insert(valid_objects,idx)
end
end
end
gt_boxes:resize(#valid_objects,4)
for idx0,idx in ipairs(valid_objects) do
gt_boxes[idx0][1] = anno.object[idx].bndbox.xmin
gt_boxes[idx0][2] = anno.object[idx].bndbox.ymin
gt_boxes[idx0][3] = anno.object[idx].bndbox.xmax
gt_boxes[idx0][4] = anno.object[idx].bndbox.ymax
table.insert(gt_classes,self.class_to_id[anno.object[idx].name])
end
return gt_boxes,gt_classes,valid_objects,anno
end
function DataSetPascal:attachProposals(i)
if not self.roidb then
self:loadROIDB()
end
local boxes = self:getROIBoxes(i)
local gt_boxes,gt_classes,valid_objects,anno = self:getGTBoxes(i)
local all_boxes
if anno.object then
if #valid_objects > 0 and boxes:dim() > 0 then
all_boxes = torch.cat(gt_boxes,boxes,1)
elseif boxes:dim() == 0 then
all_boxes = gt_boxes
else
all_boxes = boxes
end
else
gt_boxes = torch.IntTensor(0,4)
all_boxes = boxes
end
local num_boxes = boxes:dim() > 0 and boxes:size(1) or 0
local num_gt_boxes = #gt_classes
local rec = {}
if num_gt_boxes > 0 and num_boxes > 0 then
rec.gt = torch.cat(torch.ByteTensor(num_gt_boxes):fill(1),
torch.ByteTensor(num_boxes):fill(0) )
elseif num_boxes > 0 then
rec.gt = torch.ByteTensor(num_boxes):fill(0)
elseif num_gt_boxes > 0 then
rec.gt = torch.ByteTensor(num_gt_boxes):fill(1)
else
rec.gt = torch.ByteTensor(0)
end
rec.overlap_class = torch.FloatTensor(num_boxes+num_gt_boxes,self.num_classes):fill(0)
rec.overlap = torch.FloatTensor(num_boxes+num_gt_boxes,num_gt_boxes):fill(0)
for idx=1,num_gt_boxes do
local o = boxoverlap(all_boxes,gt_boxes[idx])
local tmp = rec.overlap_class[{{},gt_classes[idx]}] -- pointer copy
tmp[tmp:lt(o)] = o[tmp:lt(o)]
rec.overlap[{{},idx}] = boxoverlap(all_boxes,gt_boxes[idx])
end
-- get max class overlap
--rec.overlap,rec.label = rec.overlap:max(2)
--rec.overlap = torch.squeeze(rec.overlap,2)
--rec.label = torch.squeeze(rec.label,2)
--rec.label[rec.overlap:eq(0)] = 0
if num_gt_boxes > 0 then
rec.overlap,rec.correspondance = rec.overlap:max(2)
rec.overlap = torch.squeeze(rec.overlap,2)
rec.correspondance = torch.squeeze(rec.correspondance,2)
rec.correspondance[rec.overlap:eq(0)] = 0
else
rec.overlap = torch.FloatTensor(num_boxes+num_gt_boxes):fill(0)
rec.correspondance = torch.LongTensor(num_boxes+num_gt_boxes):fill(0)
end
rec.label = torch.IntTensor(num_boxes+num_gt_boxes):fill(0)
for idx=1,(num_boxes+num_gt_boxes) do
local corr = rec.correspondance[idx]
if corr > 0 then
rec.label[idx] = self.class_to_id[anno.object[valid_objects[corr] ].name]
end
end
rec.boxes = all_boxes
if num_gt_boxes > 0 and num_boxes > 0 then
rec.class = torch.cat(torch.CharTensor(gt_classes),
torch.CharTensor(num_boxes):fill(0))
elseif num_boxes > 0 then
rec.class = torch.CharTensor(num_boxes):fill(0)
elseif num_gt_boxes > 0 then
rec.class = torch.CharTensor(gt_classes)
else
rec.class = torch.CharTensor(0)
end
if self.save_objs then
rec.objects = {}
for _,idx in pairs(valid_objects) do
table.insert(rec.objects,anno.object[idx])
end
else
rec.correspondance = nil
end
function rec:size()
return (num_boxes+num_gt_boxes)
end
return rec
end
function DataSetPascal:createROIs()
if self.rois then
return
end
self.rois = {}
for i=1,self.num_imgs do
xlua.progress(i,self.num_imgs)
table.insert(self.rois,self:attachProposals(i))
if i%500 == 0 then
collectgarbage()
end
end
end