-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathloadCamVid.lua
More file actions
231 lines (205 loc) · 8.87 KB
/
loadCamVid.lua
File metadata and controls
231 lines (205 loc) · 8.87 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
----------------------------------------------------------------------
-- Sample CamVid videos to get train/test images
-- and corresponding labels
--
-- Abhishek Chaurasia,
-- November 2016
----------------------------------------------------------------------
require 'image'
require 'xlua'
----------------------------------------------------------------------
local trsize = 367
local tesize = 233
local vasize = 101
local dirRoot = opt.datapath
local red = '\27[31m'
local green = '\27[32m'
local uline = '\27[4m'
local resetCol = '\27[0m'
----------------------------------------------------------------------
-- Classes in Alphabetical Order
local classes = {'Animal', 'Archway', 'Bicyclist', 'Bridge', 'Building', 'Car', 'CarLuggagePram', 'Child',
'ColumnPole', 'Fence', 'LaneMkgsDriv', 'LaneMkgsNonDriv', 'MiscText', 'MotorcycleScooter', 'OtherMoving', 'ParkingBlock',
'Pedestrian', 'Road', 'RoadShoulder', 'Sidewalk', 'SignSymbol', 'Sky', 'SUVPickupTruck', 'TrafficCone',
'TrafficLight', 'Train', 'Tree', 'TruckBus', 'Tunnel', 'VegetationMisc', 'Void', 'Wall'}
local conClasses = {'Animal', 'Archway', 'Bicyclist', 'Bridge', 'Building', 'Car', 'CarLuggagePram', 'Child',
'ColumnPole', 'Fence', 'LaneMkgsDriv', 'LaneMkgsNonDriv', 'MiscText', 'MotorcycleScooter', 'OtherMoving', 'ParkingBlock',
'Pedestrian', 'Road', 'RoadShoulder', 'Sidewalk', 'SignSymbol', 'Sky', 'SUVPickupTruck', 'TrafficCone',
'TrafficLight', 'Train', 'Tree', 'TruckBus', 'Tunnel', 'VegetationMisc', 'Void', 'Wall'}
----------------------------------------------------------------------
local classMap = {[25]={1}, -- Animal
[50]={3}, -- Archway
[11]={11}, -- Bicyclist
[9] ={3}, -- Bridge
[32]={3}, -- Building
[18]={9}, -- Car
[19]={10}, -- CartLuggagePram
[57]={10}, -- Child
[62]={8}, -- ColumnPole
[22]={4}, -- Fence
[35]={2}, -- LaneMkgsDiv
[49]={2}, -- LaneMkgsNonDiv
[41]={7}, -- MiscText
[51]={11}, -- MotorcycleScooter
[37]={9}, -- OtherMoving
[30]={6}, -- ParkingBlock
[20]={10}, -- Pedestrian
[38]={2}, -- Road
[43]={2}, -- RoadShoulder
[3] ={6}, -- Sidewalk
[58]={7}, -- SignSymbol
[42]={12}, -- Sky
[27]={9}, -- SUVPickupTruck
[1] ={8}, -- TrafficCone
[5] ={7}, -- TrafficLight
[54]={9}, -- Train
[40]={5}, -- Tree
[59]={9}, -- TruckBus
[17]={3}, -- Tunnel
[60]={5}, -- VegetationMisc
[0] ={1}, -- Void
[28]={4}} -- Wall
classes = {'Unlabeled', --1
'Road' , --2
'Building', --3
'Fence', --4
'Tree', --5
'Sidewalk', --6
'SignSymbol', --7
'ColumnPole', --8
'Car', --9
'Pedestrian', --10
'Bicyclist', --11
'Sky'} --12
conClasses = {
'Road' , --2
'Building', --3
'Fence', --4
'Tree', --5
'Sidewalk', --6
'SignSymbol', --7
'ColumnPole', --8
'Car', --9
'Pedestrian', --10
'Bicyclist', --11
'Sky'} --12
print '\n\27[31m\27[4mLoading CamVid dataset\27[0m\27[31m ...\27[0m'
print('# of classes: ' .. #classes ..', classes: ', classes)
--------------------------------------------------------------------------------
-- Initialize data structures:
--------------------------------------------------------------------------------
local trainData = {
data = torch.FloatTensor(trsize, opt.channels, opt.imHeight , opt.imWidth),
labels = torch.FloatTensor(trsize, opt.imHeight , opt.imWidth),
preverror = 1e10, -- a really huge number
size = function() return trsize end
}
local testData = {
data = torch.FloatTensor(tesize, opt.channels, opt.imHeight , opt.imWidth),
labels = torch.FloatTensor(tesize, opt.imHeight , opt.imWidth),
preverror = 1e10, -- a really huge number
size = function() return tesize end
}
local valData = {
data = torch.FloatTensor(vasize, opt.channels, opt.imHeight , opt.imWidth),
labels = torch.FloatTensor(vasize, opt.imHeight , opt.imWidth),
preverror = 1e10, -- a really huge number
size = function() return vasize end
}
local function rescaleRemapData(sourceData, destData)
local dataSize = destData:size()
for i = 1, dataSize do
xlua.progress(i, dataSize)
destData.data[i] = image.scale(sourceData.data[i], opt.imWidth, opt.imHeight)
tempLabel = sourceData.labels[i]
-- Remap labels based on new class mapping
tempLabel:apply(
function(x)
return classMap[x][1]
end)
destData.labels[i] = image.scale(tempLabel, opt.imWidth, opt.imHeight)
end
end
-----------------------------------------------------------------------------------
-- Main section
-----------------------------------------------------------------------------------
local loadedFromCache = false
local dirName = opt.imHeight .. '_' .. opt.imWidth
local cacheDir = paths.concat(opt.cachepath, dirName)
local camvidCachePath = paths.concat(cacheDir, 'trainData.t7')
if not paths.dirp(cacheDir) then paths.mkdir(cacheDir) end
if opt.cachepath ~= "none" and paths.filep(camvidCachePath) then
print('\27[32mData cache found at: \27[0m\27[4m' .. cacheDir .. '\27[0m')
trainData = torch.load(camvidCachePath)
camvidCachePath = paths.concat(cacheDir, 'testData.t7')
testData = torch.load(camvidCachePath)
loadedFromCache = true
collectgarbage()
else
-- Check if a compatible version of CamVid data is present or not
if not paths.filep(opt.cachepath .. '/trainTestVal.t7') then
paths.dofile('prepCamVid.lua')
collectgarbage()
end
print('Loading dataset as tensor from: ' .. uline .. opt.cachepath .. '/trainTestVal.t7' .. resetCol)
local dataCache = torch.load(opt.cachepath .. '/trainTestVal.t7')
local tempLabel = torch.Tensor(720, 960)
print(red .. 'Rescaling training set' .. resetCol)
rescaleRemapData(dataCache.trainData, trainData)
print(red .. 'Rescaling testing set' .. resetCol)
rescaleRemapData(dataCache.testData, testData)
print(red .. 'Rescaling validation set' .. resetCol)
rescaleRemapData(dataCache.valData, valData)
collectgarbage()
end
print(string.format("%s# of training data:%s %d", green, resetCol, trainData:size()))
print(string.format("%s# of validation data:%s %d", green, resetCol, valData:size()))
print(string.format("Frame res is: %dx%dx%d",
trainData.data:size(2), trainData.data:size(3),trainData.data:size(4)))
-----------------------------------------------------------------------------------
if opt.cachepath ~= "none" and not loadedFromCache then
print('==> Saving data to cache: ' .. cacheDir)
-- saving training data
camvidCachePath = paths.concat(cacheDir, 'trainData.t7')
torch.save(camvidCachePath, trainData)
-- saving testing data
camvidCachePath = paths.concat(cacheDir, 'testData.t7')
torch.save(camvidCachePath, testData)
-- saving validation data
camvidCachePath = paths.concat(cacheDir, 'valData.t7')
torch.save(camvidCachePath, valData)
collectgarbage()
end
-----------------------------------------------------------------------------------
print '==> Normalizing data'
-- It's always good practice to verify that data is properly
-- normalized.
local trainMean = torch.mean(trainData.data, 1)
for i = 1, trainData.data:size(1) do
trainData.data[i]:add(-trainMean)
end
for i = 1, valData.data:size(1) do
valData.data[i]:add(-trainMean)
end
for i = 1, testData.data:size(1) do
testData.data[i]:add(-trainMean)
end
torch.save(paths.concat(opt.cachepath, dirName, 'stat.t7'), trainMean)
-----------------------------------------------------------------------------------
local classes_td = {[1] = 'classes,targets\n'}
for _,cat in pairs(classes) do
table.insert(classes_td, cat .. ',1\n')
end
local file = io.open(paths.concat(opt.save, 'categories.txt'), 'w')
file:write(table.concat(classes_td))
file:close()
local histClasses = torch.histc(trainData.labels:double(), #classes, 1, #classes)
-- Exports
opt.dataClasses = classes
opt.dataconClasses = conClasses
opt.datahistClasses = histClasses
-- Chose if you want to send test data or validation data
return {
trainData = trainData,
testData = testData,
}