-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevaluate.py
95 lines (76 loc) · 2.67 KB
/
evaluate.py
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
import time
import torch
import numpy as np
import torch.nn as nn
import torch.nn.parallel
import torch.nn.functional
from utils import *
from models.net import SARPN
from options import get_args
from collections import OrderedDict
from dataloader import nyudv2_dataloader
args = get_args('test')
# lode nyud v2 test set
TestImgLoader = nyudv2_dataloader.getTestingData_NYUDV2(args.batch_size, args.testlist_path, args.root_path)
# model
model = SARPN(args)
model = nn.DataParallel(model)
model.cuda()
# load test model
if args.loadckpt is not None:
print("loading model {}".format(args.loadckpt))
state_dict = torch.load(args.loadckpt)
model.load_state_dict(state_dict)
else:
print("You have not loaded any models.")
def test():
model.eval()
totalNumber = 0
Ae = 0
Pe = 0
Re = 0
Fe = 0
errorSum = {'MSE': 0, 'RMSE': 0, 'ABS_REL': 0, 'LG10': 0,
'MAE': 0, 'DELTA1': 0, 'DELTA2': 0, 'DELTA3': 0}
for batch_idx, sample in enumerate(TestImgLoader):
print("Processing the {}th image!".format(batch_idx))
image, depth = sample['image'], sample['depth']
depth = depth.cuda()
image = image.cuda()
image = torch.autograd.Variable(image)
depth = torch.autograd.Variable(depth)
start = time.time()
pred_depth = model(image)
end = time.time()
running_time = end - start
output = torch.nn.functional.interpolate(pred_depth[-1], size=[depth.size(2), depth.size(3)], mode='bilinear', align_corners=True)
depth_edge = edge_detection(depth)
output_edge = edge_detection(output)
batchSize = depth.size(0)
totalNumber = totalNumber + batchSize
errors = evaluateError(output, depth)
errorSum = addErrors(errorSum, errors, batchSize)
averageError = averageErrors(errorSum, totalNumber)
edge1_valid = (depth_edge > args.threshold)
edge2_valid = (output_edge > args.threshold)
nvalid = np.sum(torch.eq(edge1_valid, edge2_valid).float().data.cpu().numpy())
A = nvalid / (depth.size(2)*depth.size(3))
nvalid2 = np.sum(((edge1_valid + edge2_valid) ==2).float().data.cpu().numpy())
P = nvalid2 / (np.sum(edge2_valid.data.cpu().numpy()))
R = nvalid2 / (np.sum(edge1_valid.data.cpu().numpy()))
F = (2 * P * R) / (P + R)
Ae += A
Pe += P
Re += R
Fe += F
Av = Ae / totalNumber
Pv = Pe / totalNumber
Rv = Re / totalNumber
Fv = Fe / totalNumber
print('PV', Pv)
print('RV', Rv)
print('FV', Fv)
averageError['RMSE'] = np.sqrt(averageError['MSE'])
print(averageError)
if __name__ == '__main__':
test()