-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathprepCamVid.lua
203 lines (180 loc) · 7.54 KB
/
prepCamVid.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
----------------------------------------------------------------------
-- Sample CamVid videos to get train/test/validation images
-- and corresponding labels
--
-- Abhishek Chaurasia,
-- November, 2016
----------------------------------------------------------------------
require 'image'
require 'xlua'
----------------------------------------------------------------------
local N = 701
local trsize = 367
local tesize = 233
local vasize = 101
local dirRoot = opt.datapath
local imHeight = 720
local imWidth = 960
local red = '\27[31m'
local green = '\27[32m'
local resetCol = '\27[0m'
----------------------------------------------------------------------
print '\n\27[31m\27[4mPreparing CamVid dataset for data loader\27[0m'
--------------------------------------------------------------------------------
-- Function to check if the given file is a valid video
local function validVideo(filename)
local ext = string.lower(paths.extname(filename))
local videoExt = {'avi', 'mp4', 'mxf'}
for i = 1, #videoExt do
if ext == videoExt[i] then
return true
end
end
print(red .. ext .. " extension is NOT supported!!!" .. resetCol)
return false
end
-- Function to read txt file and return image and ground truth path
function getPath(filepath)
print("Filenames and their role found in: " .. filepath)
local file = io.open(filepath, 'r')
local role = {}
local fileList = {}
local fline = file:read()
while fline ~= nil do
local col1, col2 = fline:match("([^,]+),([^,]+)")
table.insert(role, col1)
table.insert(fileList, col2)
fline = file:read()
end
return role, fileList
end
--------------------------------------------------------------------------------
-- Initialize class Frame which can be used to read videos/camera
local frame = assert(dofile('misc/framevideo.lua'))
local source = {}
-- switch input sources
source.w = 720
source.h = 960
source.fps = 30
local labelPrefixTable = {'0001TP_0', 'Seq05VD_f', '0006R0_f', '0016E5_'}
local labelStart = {6690, 0, 930, 390}
local labelOffset = { 30, 1, 931, 391}
local maxSampleFrames = { 124, 171, 101, 305}
--------------------------------------------------------------------------------
-- Initialize data structures:
--------------------------------------------------------------------------------
local trainData = {
data = torch.FloatTensor(trsize, 3, imHeight, imWidth),
labels = torch.FloatTensor(trsize, imHeight, imWidth),
size = function() return trsize end
}
local testData = {
data = torch.FloatTensor(tesize, 3, imHeight, imWidth),
labels = torch.FloatTensor(tesize, imHeight, imWidth),
size = function() return tesize end
}
local valData = {
data = torch.FloatTensor(vasize, 3, imHeight, imWidth),
labels = torch.FloatTensor(vasize, imHeight, imWidth),
size = function() return vasize end
}
local trc = 1
local tec = 1
local vac = 1
local totalCount = 1 -- Overall counter for whole dataset
--------------------------------------------------------------------------------
-- forward img and gather label and input frame for that label
-- Input : directory path containing videos, directory number
-- Output: tensors storing labels and their images
--------------------------------------------------------------------------------
local function forwardSeq(input, dirN, role, fileList)
-- source height and width gets updated by __init based on the input video
frame:init(input, source)
local nFrames = frame.nFrames() -- # of total frames in the video
local img = frame.forward(img)
local n = - labelOffset[dirN] -- Counter for frame index
local count = 1 -- Counter for how many frames have been added to one sequence
local labelPath
local labelPrefix = labelPrefixTable[dirN]
local label = torch.zeros(imHeight, imWidth)
local buggyLabel = dirRoot .. dirN .. '/label/Seq05VD_f02610_L.png'
while count <= maxSampleFrames[dirN] do
xlua.progress(count, maxSampleFrames[dirN])
--------------------------------------------------------------------------
-- Save representation alongwith corresponding label, only if label exists
--------------------------------------------------------------------------
local labelIdx = n + labelStart[dirN]
labelPath = dirRoot .. dirN .. '/label/' .. labelPrefix .. string.format('%05d_L.png', labelIdx)
if paths.filep(labelPath) then
count = count + 1
-- Load current label
local labelRGB = image.load(labelPath, 3, 'byte')/64
-- Convert RGB into grayscale
label = labelRGB[1] * 16 + labelRGB[2] * 4 + labelRGB[3]
if labelPath == buggyLabel then
local mask = label:eq(21)
label = label - 21 * mask
end
local verifyLabel = dirRoot .. dirN .. '/label/' .. fileList[totalCount]
if role[totalCount] == 'train' and labelPath == verifyLabel then
trainData.data[trc] = img[1]:clone()
trainData.labels[trc] = label:clone()
totalCount = totalCount + 1
trc = trc + 1
elseif role[totalCount] == 'test' and labelPath == verifyLabel then
testData.data[tec] = img[1]:clone()
testData.labels[tec] = label:clone()
totalCount = totalCount + 1
tec = tec + 1
elseif role[totalCount] == 'val' and labelPath == verifyLabel then
valData.data[vac] = img[1]:clone()
valData.labels[vac] = label:clone()
totalCount = totalCount + 1
vac = vac + 1
else
print('\27[31mLooking for \27[0m' .. verifyLabel .. '\27[31m in \27[0m' .. role[totalCount] .. 'set')
error('But something went wrong with:' .. labelPath)
end
end
img = frame.forward(img)
n = n + 1
collectgarbage()
end
collectgarbage()
end
-----------------------------------------------------------------------------------
-- Main section
-----------------------------------------------------------------------------------
local loadedFromCache = false
local cacheDir = opt.cachepath
local camvidCachePath = paths.concat(cacheDir, 'trainTestVal.t7')
if not paths.dirp(cacheDir) then paths.mkdir(cacheDir) end
--------------------------------------------------------------------------------
-- Acquire image and ground truth paths for training and testing set
assert(paths.dirp(dirRoot), 'No folder found at: ' .. dirRoot)
local role, fileList = getPath('./misc/dataDistributionCV.txt')
for dirN = 1, 4 do
local dirPath = dirRoot .. dirN .. '/input/'
for file in paths.iterfiles(dirPath) do
print(red .. "\nGetting input images and labels for: " .. resetCol .. file)
-- process each image
if validVideo(file) then
local vidPath = dirPath .. file
forwardSeq(vidPath, dirN, role, fileList)
print(green .. "Loaded input images and labels!!!" .. resetCol)
end
end
end
print()
print(string.format("%s# of training data :%s %d", green, resetCol, trainData:size()))
print(string.format("%s# of testing data :%s %d", green, resetCol, testData:size()))
print(string.format("%s# of validation data :%s %d", green, resetCol, valData:size()))
-----------------------------------------------------------------------------------
print('Saving compatible data for data-loader: ' .. camvidCachePath)
local dataCache = {
trainData = trainData,
testData = testData,
valData = valData,
}
torch.save(camvidCachePath, dataCache)
collectgarbage()