-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.lua
86 lines (71 loc) · 2.52 KB
/
test.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
testLogger = optim.Logger(paths.concat(opt.save, 'test.log'))
local batchNumber
local top1_center, loss
local timer = torch.Timer()
function test()
print('==> doing epoch on validation data:')
print("==> online epoch # " .. epoch)
batchNumber = 0
cutorch.synchronize()
timer:reset()
-- set the dropouts to evaluate mode
model:evaluate()
top1_center = 0
loss = 0
for i=1,nTest/opt.batchSize do -- nTest is set in 1_data.lua
local indexStart = (i-1) * opt.batchSize + 1
local indexEnd = (indexStart + opt.batchSize - 1)
donkeys:addjob(
-- work to be done by donkey thread
function()
local inputs, labels = testLoader:get(indexStart, indexEnd)
return inputs, labels
end,
-- callback that is run in the main thread once the work is done
testBatch
)
end
donkeys:synchronize()
cutorch.synchronize()
top1_center = top1_center * 100 / nTest
loss = loss / (nTest/opt.batchSize) -- because loss is calculated per batch
testLogger:add{
['% top1 accuracy (test set) (center crop)'] = top1_center,
['avg loss (test set)'] = loss
}
print(string.format('Epoch: [%d][TESTING SUMMARY] Total Time(s): %.2f \t'
.. 'average loss (per batch): %.2f \t '
.. 'accuracy [Center](%%):\t top-1 %.2f\t ',
epoch, timer:time().real, loss, top1_center))
print('\n')
end -- of test()
-----------------------------------------------------------------------------
local inputs = torch.CudaTensor()
local labels = torch.CudaTensor()
os.execute('rm t.txt')
function testBatch(inputsCPU, labelsCPU)
cutorch.synchronize()
collectgarbage()
batchNumber = batchNumber + opt.batchSize
local inputs1=inputsCPU[1]:cuda()
local inputs2=inputsCPU[2]:cuda()
local inputs3=inputsCPU[3]:cuda()
inputs={inputs1,inputs2,inputs3}
labels:resize(labelsCPU:size()):copy(labelsCPU)
local outputs = model:forward(inputs)
local err = criterion:forward(outputs, labels)
--cutorch.synchronize()
local pred = outputs:float()
loss = loss + err
local _, pred_sorted = pred:sort(2, true)
file1=io.open("t.txt","aw")
for i=1,pred:size(1) do
local g = labelsCPU[i]
file1:write(g .. "******" .. pred_sorted[i][1])
file1:write("\n")
if pred_sorted[i][1] == g then top1_center = top1_center + 1 end
end
if batchNumber % 1024 == 0 then
print(('Epoch: Testing [%d][%d/%d]'):format(epoch, batchNumber, nTest))
end
end