-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_ood.py
163 lines (137 loc) · 4.64 KB
/
evaluate_ood.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import argparse
from modeling.ood_segmentation import OoDSegmentationModel
from easydict import EasyDict as edict
from tools import overwrite_config
from pytorch_lightning import Trainer
from datamodules import SemanticSegmentationDataModule
def evaluate_single_ckpt(ckpt, args, opts):
"""
opts is a list of even size containing the hyperparameters to overwrite
the model config. even value indices are the keys, and odd value indices
are the values.
"""
print(f"Evaluating {ckpt}")
segmentor_ckpt = None
if args.segmentor_ckpt is not None:
segmentor_ckpt = args.segmentor_ckpt
model = OoDSegmentationModel.load_from_checkpoint(
ckpt, segmentor_ckpt=segmentor_ckpt)
model.save_hyperparameters(overwrite_config(model.hparams, opts))
datamodule = SemanticSegmentationDataModule(model.hparams)
devices = 1
if args.devices is not None:
devices = [int(d) for d in args.devices.split(",")]
output = Trainer(precision='32', devices=devices).test(
model, datamodule=datamodule)
return edict(output[0])
def write_results(results, args):
"""
Expected hierarchy of the results dictionary
- model name
- dataset name
- metric name
"""
if args.out_path is not None:
# if args.out_path doesn't exist create it
if not os.path.exists(args.out_path):
os.makedirs(args.out_path, exist_ok=True)
model_folder = '/'.join(args.ckpt.split("/")[:-1])
model_name = args.ckpt.split("/")[-1].split(".")[0]
out_path = os.path.join(model_folder, model_name + "_results")
# in the first row of the output, leave the first column empty for the checkpoint name
# and print the metrics in the order given in the argument
with open(f"{out_path}.txt", "w") as f:
f.write("\t")
dataset_list = list(results[list(results.keys())[0]].keys())
for dataset in dataset_list:
for metric in args.metrics_order.split(","):
f.write(f"{dataset}_{metric}\t")
f.write("\n")
for ckpt, ckpt_results in results.items():
f.write(f"{ckpt}\t")
for dataset, dataset_results in ckpt_results.items():
for metric in args.metrics_order.split(","):
f.write(f"{100 * dataset_results[metric]:.4f}\t")
f.write("\n")
def main(args):
if args.multiple_datasets:
datasets = args.dataset.split(",")
else:
datasets = [args.dataset]
results = edict()
ckpt_name = args.ckpt.split("/")[-1]
results[ckpt_name] = edict()
opts = ["DATA.DATASETS_FOLDER", args.datasets_folder]
if args.segmentor_ckpt is not None:
opts.extend(["MODEL.SEGMENTOR_CKPT", args.segmentor_ckpt])
for dataset in datasets:
results[ckpt_name][dataset] = evaluate_single_ckpt(
args.ckpt,
args,
opts + ["DATA.EVAL_DATASET", dataset]
)
write_results(results, args)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Evaluate OoD Metrics")
parser.add_argument(
"--ckpt",
type=str,
help="Path to the model checkpoint"
)
parser.add_argument(
"--segmentor-ckpt",
type=str,
default=None,
help="Path to the segmentation model checkpoint, if none then use the checkpoint stored in the model config"
)
parser.add_argument(
"--dataset",
type=str,
default="road_anomaly",
help="Dataset to evaluate"
)
parser.add_argument(
"--multiple-datasets",
action="store_true",
help="Evaluate multiple datasets, names given separated by commas in --dataset argument"
)
parser.add_argument(
"--batch-size",
type=int,
default=1,
)
parser.add_argument(
"--num-workers",
type=int,
default=4,
)
parser.add_argument(
"--devices",
type=str,
default=None,
)
parser.add_argument(
"--out-path",
type=str,
default=None,
help="Path to save the results as a text file"
)
parser.add_argument(
"--metrics-order",
type=str,
default="llr_AUPR,llr_AUROC,llr_FPR95",
)
parser.add_argument(
"--datasets-folder",
type=str,
default=None,
help="Path to the evaluation datasets folder"
)
parser.add_argument(
"--store-anomaly-scores",
action="store_true",
help="Store anomaly scores in the output"
)
args = parser.parse_args()
main(args)