-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevaluateResultUniform32.lua
341 lines (274 loc) · 10.9 KB
/
evaluateResultUniform32.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
require 'torch'
require 'nn'
require 'cutorch'
require 'cunn'
require 'optim'
require 'cudnn'
dofile('utils/instanceNormalization.lua')
require 'lfs'
require 'image'
local hsp = require 'hsp'
function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
networkParameters, evalParams = dofile(arg[2])
cutorch.setDevice(arg[1])
local splitStr = evalParams.split
local objStr = ""
if evalParams.saveObj then
objStr = "_obj"
end
local metric = evalParams.metric
dofile('utils/hierarchicalShapenetDataLoader.lua')
hierarchical3DShapeNetDataSet:setResolution(evalParams.resolutionStr)
if networkParameters.useColor then
hierarchical3DShapeNetDataSet:setColor()
end
hierarchical3DShapeNetDataSet:setPath(evalParams.datasetFolder)
for c=1,#evalParams.classes do
if splitStr == "val" then
hierarchical3DShapeNetDataSet:addValDataFromClass(evalParams.classes[c])
elseif splitStr == "test" then
hierarchical3DShapeNetDataSet:addTestDataFromClass(evalParams.classes[c])
end
end
local peakPerformance = 0
local peakPerformanceIter = 0
local peakPerformanceThresh = 0
if metric == "CD" then
peakPerformance = 10 -- value which is bigger than biggest CD
end
local thresholds = evalParams.thresholds
local numLevels = networkParameters.numLevels
local AvGIoUAvgFile = assert(io.open(evalParams.outputFolder .. "/evaluation_" .. splitStr .. objStr .."_AvG" .. metric .. "_Avg_" .. evalParams.snapshotStartIter .. ".txt", "w"))
AvGIoUAvgFile:write("iter")
for t=1,#thresholds do
AvGIoUAvgFile:write(", " .. thresholds[t])
end
AvGIoUAvgFile:write("\n")
local AvGIoUFiles = {}
for c=1,#evalParams.classes do
AvGIoUFiles[evalParams.classes[c]] = assert(io.open(evalParams.outputFolder .. "/evaluation_" .. splitStr .. objStr .. "_AvG" .. metric .. "_" .. evalParams.classes[c] .. "_" .. evalParams.snapshotStartIter .. ".txt", "w"))
AvGIoUFiles[evalParams.classes[c]]:write("iter")
for t=1,#thresholds do
AvGIoUFiles[evalParams.classes[c]]:write(", " .. thresholds[t])
end
AvGIoUFiles[evalParams.classes[c]]:write("\n")
end
--generate obj folders if objs output is written
if (evalParams.saveObj == true) then
local objFolderExists = lfs.attributes(evalParams.outputFolder .. "/objs_" ..splitStr .. "/",'modification')
if (objFolderExists == nil) then
lfs.mkdir(evalParams.outputFolder .. "/objs_" .. splitStr .. "/",'modification')
end
for c=1,#evalParams.classes do
local classFolderExists = lfs.attributes(evalParams.outputFolder .. "/objs_" .. splitStr .. "/" .. evalParams.classes[c], 'modification')
if (classFolderExists == nil) then
lfs.mkdir(evalParams.outputFolder .. "/objs_" .. splitStr .. "/" .. evalParams.classes[c])
end
end
end
for iter=evalParams.snapshotStartIter,evalParams.snapshotEndIter,evalParams.snapshotInterval do
local totalIoU = {}
local numEvaluatedElements = {}
for c=1,#evalParams.classes do
totalIoU[evalParams.classes[c]] = {}
numEvaluatedElements[evalParams.classes[c]] = 0
for t=1,#thresholds do
totalIoU[evalParams.classes[c]][t] = 0
end
end
if not evalParams.loadCache then
net = torch.load(evalParams.snapshotPath .. "/net" .. iter .. ".t7")
net:evaluate()
-- open output file
local IoUFile = assert(io.open(evalParams.outputFolder .. "/evaluation_" .. splitStr .. objStr .. "_" .. metric .. "_" .. iter .. ".txt", "w"))
IoUFile:write("modelName")
for t=1,#thresholds do
IoUFile:write(", " .. thresholds[t])
end
IoUFile:write("\n")
local numEvalFiles = 0
if splitStr == "val" then
numEvalFiles = #hierarchical3DShapeNetDataSet.valDataFiles
else
numEvalFiles = #hierarchical3DShapeNetDataSet.testDataFiles
end
for j=1,numEvalFiles do
if ((j-1)%evalParams.subsampling == 0) then
local obs, blockIndices, blocks, modelName
if splitStr == "val" then
obs, blockIndices, blocks, modelName = hierarchical3DShapeNetDataSet:getNextValExample(false, false)
else
obs, blockIndices, blocks, modelName = hierarchical3DShapeNetDataSet:getNextTestExample(false, false)
end
class = split(modelName, "/")[1]
IoUFile:write(modelName .. " ")
if networkParameters.useColor then
input = obs:view(1,3,128,128):cuda()
else
input = obs:view(1,1,128,128):cuda()
end
for l=1,numLevels do
blocks[l] = blocks[l]:cuda()
end
print("Iteration " .. iter .. " running model " .. j .. " : " .. modelName)
-- run network
local result = net:forward(input)
numEvaluatedElements[class] = numEvaluatedElements[class] + 1
-- compile the ground truth
groundTruths = {}
for l=1,numLevels do
local bISize = blockIndices[l]:size()
groundTruths[l] = torch.Tensor(1,16*bISize[1],16*bISize[2],16*bISize[3]):cuda()
for bx=1,bISize[1] do
for by=1,bISize[2] do
for bz=1,bISize[3] do
local bI = blockIndices[l][bx][by][bz]
groundTruths[l][1][{{(bx-1)*16+1,bx*16},{(by-1)*16+1,by*16},{(bz-1)*16+1,bz*16}}]:copy(blocks[l][bI],1,2)
end
end
end
end
-- upsample the result to GT resolution
local GTRes = groundTruths[numLevels][1]:size()[1]
local upSampled
if (GTRes > 32) then
local upSampled1 = image.scale(result:double():view(32,32,32),256,256)
local upSampled1T = upSampled1:transpose(1,3)
local upSampled2T = image.scale(upSampled1T,256,256)
upSampled = upSampled2T:transpose(3,1)
upSampled = upSampled:cuda():view(1,1,256,256,256)
else
upSampled = result
end
-- compute Error Measures
local labelBoundary
local labelEDT
if metric == "CD" then
labelBoundary = groundTruths[numLevels]:clone():view(GTRes,GTRes,GTRes):double()
hsp.boundary(labelBoundary)
labelEDT = groundTruths[numLevels]:clone():view(GTRes,GTRes,GTRes):double()
hsp.boundaryEDT(labelEDT)
end
for t=1,#thresholds do
local threshold = thresholds[t]
local binarizedResult = upSampled:clone()
binarizedResult[torch.le(binarizedResult, threshold)] = 0
binarizedResult[torch.ge(binarizedResult, threshold)] = 1
if (metric == "CD") then
if torch.max(binarizedResult) < 1 then
local maxVal = torch.max(upSampled)
binarizedResult[torch.ge(upSampled,maxVal-1e-5)] = 1
end
end
-- compute IoU
local measure
if metric == "IoU" then
local union = torch.cmax(binarizedResult, groundTruths[numLevels])
local intersection = torch.cmin(binarizedResult, groundTruths[numLevels])
local IoU = torch.sum(intersection)/torch.sum(union)
measure = IoU
elseif metric == "CD" then
local outputVolBoundary = binarizedResult:clone():view(GTRes,GTRes,GTRes):double()
hsp.boundary(outputVolBoundary)
local outputEDT = binarizedResult:clone():view(GTRes,GTRes,GTRes):double()
hsp.boundaryEDT(outputEDT)
local numOutputVolBoundary = torch.sum(outputVolBoundary)
local numLabelBoundary = torch.sum(labelBoundary)
if (numOutputVolBoundary > 0 and numLabelBoundary > 0) then
local cd1 = torch.sum(torch.cmul(outputVolBoundary, labelEDT))/(GTRes*numOutputVolBoundary)
local cd2 = torch.sum(torch.cmul(labelBoundary, outputEDT))/(GTRes*numLabelBoundary)
--print("cd1 = " .. cd1 .. " cd2 = " .. cd2)
measure = (cd1 + cd2)/2
else
measure = math.sqrt(3)/2.0
end
end
IoUFile:write(measure .. " ")
totalIoU[class][t] = totalIoU[class][t] + measure
end
IoUFile:write("\n")
IoUFile:flush()
if (evalParams.saveObj == true) then
hsp.saveMeshAsObj(upSampled[{1,1,{},{},{}}]:double(),evalParams.marchingCubesThreshold, evalParams.outputFolder .. "/objs_" .. splitStr .. "/" .. modelName .. ".obj")
if networkParameters.useColor then
image.save(evalParams.outputFolder .. "/objs_" .. splitStr .."/" .. modelName .. "_input.png", obs)
else
obs = obs + 0.866
obs = obs:div(2*0.866)
image.save(evalParams.outputFolder .. "/objs_" .. splitStr .."/" .. modelName .. "_input.png", obs)
end
end
else
if splitStr == "val" then
hierarchical3DShapeNetDataSet:skipNextValExample()
else
hierarchical3DShapeNetDataSet:skipNextTestExample()
end
end
end
else
--loading the data from the valDataFiles, computing statistics
local IoUFile = assert(io.open(evalParams.outputFolder .. "/evaluation_" .. splitStr .. "_" .. metric .. "_" .. iter .. ".txt", "r"))
titleLine = true
for line in IoUFile:lines() do
if titleLine then
titleLine = false
else
-- reading data
elements = split(line, " ")
class = split(elements[1], "/")[1]
for t=1,#thresholds do
totalIoU[class][t] = totalIoU[class][t] + tonumber(elements[t+1])
end
numEvaluatedElements[class] = numEvaluatedElements[class] + 1
end
end
end
local IoUAvgs = {}
for t=1,#thresholds do
IoUAvgs[t] = 0
end
for c=1,#evalParams.classes do
AvGIoUFiles[evalParams.classes[c]]:write(iter)
for t=1,#thresholds do
local classIoU = totalIoU[evalParams.classes[c]][t] / numEvaluatedElements[evalParams.classes[c]]
AvGIoUFiles[evalParams.classes[c]]:write(" " .. classIoU)
IoUAvgs[t] = IoUAvgs[t] + classIoU
end
AvGIoUFiles[evalParams.classes[c]]:write("\n")
AvGIoUFiles[evalParams.classes[c]]:flush()
end
AvGIoUAvgFile:write(iter)
for t=1,#thresholds do
local IoUAvg = IoUAvgs[t]/#evalParams.classes
AvGIoUAvgFile:write(" " .. IoUAvg)
if metric == "IoU" then
if (IoUAvg > peakPerformance) then
peakPerformance = IoUAvg
peakPerformanceIter = iter
peakPerformanceThresh = thresholds[t]
end
elseif metric == "CD" then
if (IoUAvg < peakPerformance) then
peakPerformance = IoUAvg
peakPerformanceIter = iter
peakPerformanceThresh = thresholds[t]
end
end
end
AvGIoUAvgFile:write("\n")
AvGIoUAvgFile:flush()
end
print("peakPerformance = " .. peakPerformance)
print("peakPerformanceIter = " .. peakPerformanceIter)
print("peakPerformanceThresh = " .. peakPerformanceThresh)