-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataset_video.lua
More file actions
392 lines (368 loc) · 14.1 KB
/
dataset_video.lua
File metadata and controls
392 lines (368 loc) · 14.1 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
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
require 'torch'
torch.setdefaulttensortype('torch.FloatTensor')
local ffi = require 'ffi'
local class = require('pl.class')
local dir = require 'pl.dir'
local tablex = require 'pl.tablex'
local argcheck = require 'argcheck'
require 'sys'
require 'xlua'
require 'image'
local dataset = torch.class('dataLoader')
local initcheck = argcheck{
pack=true,
help=[[
A dataset class for images in a flat folder structure (folder-name is class-name).
Optimized for extremely large datasets (upwards of 14 million images).
Tested only on Linux (as it uses command-line linux utilities to scale up)
]],
{check=function(paths)
local out = true;
for k,v in ipairs(paths) do
if type(v) ~= 'string' then
print('paths can only be of string input');
-- out = false
end
end
return out
end,
name="paths",
type="table",
help="Multiple paths of directories with images"},
{name="sampleSize",
type="table",
help="a consistent sample size to resize the images"},
{name="split",
type="number",
help="Percentage of split to go to Training"
},
{name="samplingMode",
type="string",
help="Sampling mode: random | balanced ",
default = "balanced"},
{name="verbose",
type="boolean",
help="Verbose mode during initialization",
default = false},
{name="loadSize",
type="table",
help="a size to load the images to, initially",
opt = true},
{name="forceClasses",
type="table",
help="If you want this loader to map certain classes to certain indices, "
.. "pass a classes table that has {classname : classindex} pairs."
.. " For example: {3 : 'dog', 5 : 'cat'}"
.. "This function is very useful when you want two loaders to have the same "
.. "class indices (trainLoader/testLoader for example)",
opt = true},
{name="sampleHookTrain",
type="function",
help="applied to sample during training(ex: for lighting jitter). "
.. "It takes the image path as input",
opt = true},
{name="sampleHookTest",
type="function",
help="applied to sample during testing",
opt = true},
}
function dataset:__init(...)
-- argcheck
local args = initcheck(...)
print(args)
for k,v in pairs(args) do self[k] = v end
if not self.loadSize then self.loadSize = self.sampleSize; end
if not self.sampleHookTrain then self.sampleHookTrain = self.defaultSampleHook end
if not self.sampleHookTest then self.sampleHookTest = self.defaultSampleHook end
-- first pass
-- find class names
self.classes = {}
local tnumL=0
local tnumC=0
local tmaxPathLength=0
local file = io.open(self.paths[1])
if file then
for line in file:lines() do
local imgPath, numFrame, classId = unpack(line:split(" "))
tmaxPathLength = math.max(tmaxPathLength,string.len(imgPath))
--[[
classId = tonumber(classId)+1
if tnumC < classId then
tnumC=tnumC+1
table.insert(self.classes, string.format('%d',classId))
end
--]]
-- for billiard
classId = tonumber(classId)+1
if self.classes[classId]==nil then
tnumC=tnumC+1
table.insert(self.classes, classId)
end
tnumL=tnumL+1
end
end
-- find the image path names
tmaxPathLength = tmaxPathLength+1 -- so that names won't connect
self.imagePath = torch.CharTensor(tnumL,tmaxPathLength):fill(0) -- path to each image in dataset
self.imageClass = torch.Tensor(tnumL) -- class index of each image (class index in self.classes
self.imageLen = torch.Tensor(tnumL) -- class index of each image (class index in self.classes)
self.classCount = torch.Tensor(tnumC):fill(0) -- class index of each image (class index in self.classes)
local s_data = self.imagePath:data()
-- caffe train/test list: imgPath #frames class label
local file = io.open(self.paths[1])
if file then
local cc=1
for line in file:lines() do
local imgPath, numFrame, classId = unpack(line:split(" ")) --unpack turns a table like the one given (if you use the recommended version) into a bunch of separate variables
classId = tonumber(classId)+1 -- prototxt: 0
ffi.copy(s_data, imgPath)
s_data = s_data + tmaxPathLength
self.imageLen[cc] = tonumber(numFrame);
self.imageClass[cc] = classId;
self.classCount[classId]=self.classCount[classId]+1;
cc=cc+1
end
end
self.classList = {} -- index of imageList to each image of a particular class
self.classListSample = self.classList -- the main list used when sampling data
self.numSamples = self.imagePath:size(1)
if self.verbose then print(self.numSamples .. ' samples found.') end
--==========================================================================
print('Updating classList and imageClass appropriately')
for i=1,tnumC do
self.classList[i] = torch.Tensor(self.classCount[i])
end
local tclassCount = torch.Tensor(#self.classes):fill(1)
for i=1,tnumL do
local cid = self.imageClass[i]
self.classList[cid][tclassCount[cid]] = i
tclassCount[cid]=tclassCount[cid]+1
end
--==========================================================================
if self.split == 100 then
self.testIndicesSize = 0
else
print('Splitting training and test sets to a ratio of '
.. self.split .. '/' .. (100-self.split))
self.classListTrain = {}
self.classListTest = {}
self.classListSample = self.classListTrain
local totalTestSamples = 0
-- split the classList into classListTrain and classListTest
for i=1,#self.classes do
local list = self.classList[i]
if list==nil then
self.classListTrain[i] = torch.LongTensor()
self.classListTest[i] = torch.LongTensor()
else
local count = self.classList[i]:size(1)
local splitidx = math.floor((count * self.split / 100) + 0.5) -- +round
local perm = torch.randperm(count)
self.classListTrain[i] = torch.LongTensor(splitidx)
for j=1,splitidx do
self.classListTrain[i][j] = list[perm[j]]
end
if splitidx == count then -- all samples were allocated to train set
self.classListTest[i] = torch.LongTensor()
else
self.classListTest[i] = torch.LongTensor(count-splitidx)
totalTestSamples = totalTestSamples + self.classListTest[i]:size(1)
local idx = 1
for j=splitidx+1,count do
self.classListTest[i][idx] = list[perm[j]]
idx = idx + 1
end
end
end
end
-- Now combine classListTest into a single tensor
self.testIndices = torch.LongTensor(totalTestSamples)
self.testIndicesSize = totalTestSamples
local tdata = self.testIndices:data()
local tidx = 0
for i=1,#self.classes do
local list = self.classListTest[i]
if list:dim() ~= 0 then
local ldata = list:data()
for j=0,list:size(1)-1 do
tdata[tidx] = ldata[j]
tidx = tidx + 1
end
end
end
end
end
-- size(), size(class)
function dataset:size(class, list)
list = list or self.classList
if not class then
return self.numSamples
elseif type(class) == 'string' then
return list[self.classIndices[class]]:size(1)
elseif type(class) == 'number' then
return list[class]:size(1)
end
end
-- getByClass
function dataset:getByClass(class,batchId)
if self.classListSample[class] == nil then
print('class '..class..' is empty !')
return nil
else
local index = math.ceil(torch.uniform() * self.classListSample[class]:nElement())
-- assert(index)
-- print('ss: '..class..' '..index)
--print(self.classListSample..' '..#self.imagePath)
local imgpath = ffi.string(torch.data(self.imagePath[self.classListSample[class][index]]))
local vnF =self.imageLen[self.classListSample[class][index]]
-- number of frames: start from 1
local imId = math.ceil(torch.uniform() * (vnF-opt.vnF+1))
if opt.TcropType[1]==2 then -- middle crop
imId = math.ceil(0.5*(vnF-opt.vnF+1))
elseif opt.TcropType[1]==3 then -- use len as temporal crop, vnF=1, start_id (ucf101)
imId = vnF+math.ceil(torch.uniform() * opt.TcropType[2]-1)
end
-- if not paths.filep(imgpath..'/flow_x_0001.jpg') then local dbg = require('debugger');dbg();end
-- if imId<=0 then local dbg = require('debugger');dbg();end
return self:sampleHookTrain(imgpath,imId,opt.vnF,batchId,vnF)
end
end
-- converts a table of samples (and corresponding labels) to a clean tensor
local function tableToOutput(self, dataTable, scalarTable)
local data, scalarLabels, labels
local quantity = #scalarTable
-- assert(dataTable[1]:dim() == 3)
local nAug=1;
if opt.testOpt==1 then nAug=10;end -- 10 aug
scalarLabels = torch.LongTensor(nAug*quantity):fill(-1111)
local step = 1;
local numRow=0;
local p0=0;
if string.match(torch.type(dataTable[1]),"Tensor") then -- just images
if string.sub(opt.netType,1,2)=="sm" then -- batchSize vary
for i =1, quantity do;numRow = numRow+dataTable[i]:size(1);end
if dataTable[1]:dim() == 2 then
data = torch.Tensor(numRow,dataTable[1]:size(2))
end
for i=1,#dataTable do
data:narrow(1,1+p0,step):copy(dataTable[i])
p0 = p0+step
scalarLabels:narrow(1,1+(i-1)*nAug,nAug):fill(scalarTable[i]+opt.labelOffset) --BCE
dataTable[i]=nil -- save memory
scalarTable[i]=nil
end
else
if opt.retrainOpt=='test' then
step = dataTable[1]:size(1)
end
if dataTable[1]:dim() == 4 then -- siamese
data = torch.Tensor(quantity*step,
dataTable[1]:size(2), dataTable[1]:size(3), dataTable[1]:size(4))
elseif dataTable[1]:dim() == 3 then
data = torch.Tensor(quantity*step,
self.sampleSize[1], self.sampleSize[2], self.sampleSize[3])
elseif dataTable[1]:dim() == 2 then
data = torch.Tensor(quantity*step,
dataTable[1]:size(1), dataTable[1]:size(2))
else
data = torch.Tensor(quantity*step,dataTable[1]:size(1))
end
for i=1,#dataTable do
data:narrow(1,(i-1)*step+1,step):copy(dataTable[i])
scalarLabels:narrow(1,1+(i-1)*nAug,nAug):fill(scalarTable[i]+opt.labelOffset) --BCE
dataTable[i]=nil -- save memory
scalarTable[i]=nil
end
end
-- local dbg = require('debugger');dbg()
else --image+bbox
local numB = 0
for i=1,#dataTable do numB=numB+dataTable[i][2]:size(1) end
data = {torch.Tensor(quantity,dataTable[1][1]:size(1), opt.sampleSize[1], opt.sampleSize[2]),
torch.Tensor(numB,5)}
if #dataTable==1 then
data[1]:copy(dataTable[1][1])
data[2]:copy(dataTable[1][2])
scalarLabels[1]=scalarTable[1]+opt.labelOffset
else
numB=1
local numB2
for i=1,#dataTable do
data[1][i]:copy(dataTable[i][1])
numB2 = dataTable[i][2]:size(1)
data[2]:narrow(1,numB,numB2):copy(dataTable[i][2])
numB=numB+numB2
scalarLabels[i] = scalarTable[i]+opt.labelOffset
dataTable[i]=nil
scalarTable[i]=nil
end
end
dataTable=nil
scalarTable=nil
end
collectgarbage();collectgarbage();
return data, scalarLabels
end
-- sampler, samples from the training set.
function dataset:sample(quantity)
assert(quantity>0)
local dataTable = {}
local scalarTable = {}
if opt.batchRand==0 then -- purely random
for i=1,quantity do
local out =nil
local classId
while out==nil do
classId = torch.random(1, #self.classes)
out = self:getByClass(classId,i)
end
table.insert(dataTable, out)
table.insert(scalarTable, classId)
end
elseif opt.batchRand==1 then -- proportional to class size
local classId=torch.multinomial(self.classCount,quantity,true);
for i=1,quantity do
local out = self:getByClass(classId[i],i)
table.insert(dataTable, out)
table.insert(scalarTable, classId[i])
end
end
local data, scalarLabels = tableToOutput(self, dataTable, scalarTable)
return data, scalarLabels
end
function dataset:get(i1, i2)
local indices = torch.range(i1, i2);
local quantity = i2 - i1 + 1;
assert(quantity > 0)
-- now that indices has been initialized, get the samples
local dataTable = {}
local scalarTable = {}
local vid
for i=1,quantity do
-- load the sample
local index = indices[i]
local imgpath = ffi.string(torch.data(self.imagePath[index]))
local vnF =self.imageLen[index]
-- e.g 0-9/10
if opt.TcropType[1]==1 then
-- default: test range
vid = 1+math.ceil( opt.testRange[2]*(vnF-opt.vnF)/(opt.testRange[1]-1) )
if string.match(imgpath,"bflowb") then
-- same frame in the backward order: end -> start of the sample
vid = (vnF+1)-(vid+opt.vnF-1);
end
else -- vnF:start_id
vid = vnF+opt.TcropType[2]
end
-- print(i,imgpath,vid,opt.vnF,vnF)
local out = self:sampleHookTest(imgpath,vid,opt.vnF,i,vnF)
table.insert(dataTable, out)
-- local dbg = require('debugger');dbg()
table.insert(scalarTable, self.imageClass[index])
end
-- local dbg = require('debugger');dbg()
local data, scalarLabels = tableToOutput(self, dataTable, scalarTable)
-- local dbg = require('debugger');dbg()
return data, scalarLabels
end
return dataset