-
Notifications
You must be signed in to change notification settings - Fork 87
/
evaluate.py
executable file
·70 lines (52 loc) · 2.07 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
#!/usr/bin/env python
import pathlib
import numpy as np
import torch
import tqdm
from gaze_estimation import (GazeEstimationMethod, create_dataloader,
create_model)
from gaze_estimation.utils import compute_angle_error, load_config, save_config
def test(model, test_loader, config):
model.eval()
device = torch.device(config.device)
predictions = []
gts = []
with torch.no_grad():
for images, poses, gazes in tqdm.tqdm(test_loader):
images = images.to(device)
poses = poses.to(device)
gazes = gazes.to(device)
if config.mode == GazeEstimationMethod.MPIIGaze.name:
outputs = model(images, poses)
elif config.mode == GazeEstimationMethod.MPIIFaceGaze.name:
outputs = model(images)
else:
raise ValueError
predictions.append(outputs.cpu())
gts.append(gazes.cpu())
predictions = torch.cat(predictions)
gts = torch.cat(gts)
angle_error = float(compute_angle_error(predictions, gts).mean())
return predictions, gts, angle_error
def main():
config = load_config()
output_rootdir = pathlib.Path(config.test.output_dir)
checkpoint_name = pathlib.Path(config.test.checkpoint).stem
output_dir = output_rootdir / checkpoint_name
output_dir.mkdir(exist_ok=True, parents=True)
save_config(config, output_dir)
test_loader = create_dataloader(config, is_train=False)
model = create_model(config)
checkpoint = torch.load(config.test.checkpoint, map_location='cpu')
model.load_state_dict(checkpoint['model'])
predictions, gts, angle_error = test(model, test_loader, config)
print(f'The mean angle error (deg): {angle_error:.2f}')
output_path = output_dir / 'predictions.npy'
np.save(output_path, predictions.numpy())
output_path = output_dir / 'gts.npy'
np.save(output_path, gts.numpy())
output_path = output_dir / 'error.txt'
with open(output_path, 'w') as f:
f.write(f'{angle_error}')
if __name__ == '__main__':
main()