forked from ikergarcia1996/Self-Driving-Car-in-Video-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_reorder.py
144 lines (123 loc) · 3.74 KB
/
eval_reorder.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
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
import os
import argparse
from model import Tedd1104ModelPLForImageReordering
from dataset_image_reordering import Tedd1104Dataset
import pytorch_lightning as pl
from typing import List, Union
from torch.utils.data import DataLoader
from tabulate import tabulate
def eval_model(
checkpoint_path: str,
test_dirs: List[str],
batch_size: int,
dataloader_num_workers: int = 16,
output_path: str = None,
):
"""
Evaluates a trained model on a set of test data.
:param str checkpoint_path: Path to the checkpoint file.
:param List[str] test_dirs: List of directories containing test data.
:param int batch_size: Batch size for the dataloader.
:param int dataloader_num_workers: Number of workers for the dataloader.
:param str output_path: Path to where the results should be saved.
"""
if not os.path.exists(os.path.dirname(output_path)):
os.makedirs(os.path.dirname(output_path))
print(f"Restoring model from {checkpoint_path}")
model = Tedd1104ModelPLForImageReordering.load_from_checkpoint(
checkpoint_path=checkpoint_path
)
trainer = pl.Trainer(
precision=16,
gpus=1,
# accelerator="ddp",
default_root_dir=os.path.join(
os.path.dirname(os.path.abspath(checkpoint_path)), "trainer_checkpoint"
),
)
results: List[List[Union[str, float]]] = []
for test_dir in test_dirs:
dataloader = DataLoader(
Tedd1104Dataset(
dataset_dir=test_dir,
hide_map_prob=0.0,
dropout_images_prob=[0.0, 0.0, 0.0, 0.0, 0.0],
),
batch_size=batch_size,
num_workers=dataloader_num_workers,
pin_memory=True,
shuffle=False,
)
print(f"Testing dataset: {os.path.basename(test_dir)}: ")
print()
out = trainer.test(
ckpt_path=checkpoint_path, model=model, dataloaders=[dataloader]
)[0]
results.append(
[
os.path.basename(test_dir),
round(out["Test/acc"] * 100, 1),
]
)
# print(out)
print(
tabulate(
results,
headers=[
"Accuracy",
],
)
)
if output_path:
with open(output_path, "w+", encoding="utf8") as output_file:
print(
tabulate(
results,
headers=[
"Accuracy",
],
tablefmt="tsv",
),
file=output_file,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Evaluate a trained model on the image reordering task."
)
parser.add_argument(
"--checkpoint_path",
type=str,
help="Path to the checkpoint file.",
)
parser.add_argument(
"--test_dirs",
type=str,
nargs="+",
help="List of directories containing test data.",
)
parser.add_argument(
"--batch_size",
type=int,
required=True,
help="Batch size for the dataloader.",
)
parser.add_argument(
"--dataloader_num_workers",
type=int,
default=min(os.cpu_count(), 16),
help="Number of workers for the dataloader.",
)
parser.add_argument(
"--output_path",
type=str,
default=None,
help="Path to where the results should be saved.",
)
args = parser.parse_args()
eval_model(
checkpoint_path=args.checkpoint_path,
test_dirs=args.test_dirs,
batch_size=args.batch_size,
dataloader_num_workers=args.dataloader_num_workers,
output_path=args.output_path,
)