-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathevaluate.py
More file actions
executable file
·72 lines (54 loc) · 2.25 KB
/
Copy pathevaluate.py
File metadata and controls
executable file
·72 lines (54 loc) · 2.25 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
#!/usr/bin/env python
import pathlib
from typing import cast
import numpy as np
import torch
import tqdm
from omegaconf import DictConfig
from torch.utils.data import DataLoader
from gaze_estimation import GazeEstimationMethod, create_dataloader, create_model
from gaze_estimation.utils import compute_angle_error, load_config, save_config
def test(
model: torch.nn.Module, test_loader: DataLoader, config: DictConfig
) -> tuple[torch.Tensor, torch.Tensor, float]:
model.eval()
device = torch.device(config.device)
predictions = []
gts = []
with torch.no_grad():
for batch in tqdm.tqdm(test_loader):
images, poses, gazes = (tensor.to(device) for tensor in batch)
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())
prediction_tensor = torch.cat(predictions)
gt_tensor = torch.cat(gts)
angle_error = float(compute_angle_error(prediction_tensor, gt_tensor).mean())
return prediction_tensor, gt_tensor, angle_error
def main() -> None:
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 = cast("DataLoader", 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}") # noqa: T201
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 output_path.open("w") as f:
f.write(f"{angle_error}")
if __name__ == "__main__":
main()