-
Notifications
You must be signed in to change notification settings - Fork 2
/
ood_eval.py
148 lines (112 loc) · 5.27 KB
/
ood_eval.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
#!/usr/bin/env python
import argparse
import os
from datetime import datetime
import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
from tqdm import tqdm
from ash import get_score
from datasets.dataset_factory import build_dataset, get_num_classes
from models.model_factory import build_model
from utils.metrics import compute_in, compute_traditional_ood
from utils.utils import is_debug_session, load_config_yml, set_deterministic
def eval_id_dataset(model, transform, dataset_name, output_dir, batch_size, scoring_method, use_gpu, use_tqdm):
print(f'Processing {dataset_name} dataset.')
dataset = build_dataset(dataset_name, transform, train=False)
g, seed_worker = set_deterministic()
# setup dataset
kwargs = {}
if torch.cuda.is_available() and not is_debug_session():
kwargs = {'num_workers': 5, 'pin_memory': True, 'generator': g, 'worker_init_fn': seed_worker}
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False, **kwargs)
with torch.no_grad():
if use_tqdm:
progress_bar = tqdm(total=len(dataloader))
f1 = open(os.path.join(output_dir, "in_scores.txt"), 'w')
g1 = open(os.path.join(output_dir, "in_labels.txt"), 'w')
for i, samples in enumerate(dataloader):
images = samples[0]
labels = samples[1]
# Create non_blocking tensors for distributed training
if use_gpu:
images = images.cuda(non_blocking=True)
labels = labels.cuda(non_blocking=True)
logits = model(images)
outputs = F.softmax(logits, dim=1)
outputs = outputs.detach().cpu().numpy()
preds = np.argmax(outputs, axis=1)
confs = np.max(outputs, axis=1)
for k in range(preds.shape[0]):
g1.write("{} {} {}\n".format(labels[k], preds[k], confs[k]))
scores = get_score(logits, scoring_method)
for score in scores:
f1.write("{}\n".format(score))
if use_tqdm:
progress_bar.update()
f1.close()
g1.close()
if use_tqdm:
progress_bar.close()
def eval_ood_dataset(model, transform, dataset_name, output_dir, batch_size, scoring_method, use_gpu, use_tqdm):
print(f'Processing {dataset_name} dataset.')
dataset = build_dataset(dataset_name, transform, train=False)
g, seed_worker = set_deterministic()
kwargs = {}
if torch.cuda.is_available() and not is_debug_session():
kwargs = {'num_workers': 5, 'pin_memory': True, 'generator': g, 'worker_init_fn': seed_worker}
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False, **kwargs)
with torch.no_grad():
if use_tqdm:
progress_bar = tqdm(total=len(dataloader))
f1 = open(os.path.join(output_dir, f"{dataset_name}.txt"), 'w')
for i, samples in enumerate(dataloader):
images = samples[0]
# Create non_blocking tensors for distributed training
if use_gpu:
images = images.cuda(non_blocking=True)
logits = model(images)
scores = get_score(logits, scoring_method)
for score in scores:
f1.write("{}\n".format(score))
if use_tqdm:
progress_bar.update()
f1.close()
if use_tqdm:
progress_bar.close()
def ood_eval(config, use_gpu, use_tqdm):
num_classes = get_num_classes(config['id_dataset'])
base_dir = os.path.dirname(os.path.abspath(__file__))
now = str(datetime.now())
output_dir = os.path.join(base_dir, 'output', now)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# construct the model
model, transform = build_model(config['model_name'], num_classes=num_classes)
if config['train_restore_file']:
checkpoint = os.path.join(os.getenv('MODELS'), config['train_restore_file'])
checkpoint = torch.load(checkpoint, map_location='cpu')
model.load_state_dict(checkpoint)
else:
print('Warning: train_restore_file config not specified')
model.eval()
# apply ash
setattr(model, 'ash_method', config['method'])
if use_gpu:
model = model.cuda()
eval_id_dataset(model, transform, config['id_dataset'], output_dir, config['batch_size'], config['scoring_method'], use_gpu, use_tqdm)
for ood_dataset in config['ood_datasets']:
eval_ood_dataset(model, transform, ood_dataset, output_dir, config['batch_size'], config['scoring_method'], use_gpu, use_tqdm)
name = f"{config['method']} - {config['scoring_method']} - {config['id_dataset']}"
print(name)
compute_traditional_ood(output_dir, config['ood_datasets'], config['scoring_method'])
compute_in(output_dir, config['scoring_method'])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", required=True, type=str, help="Path to config YML")
parser.add_argument("--use-gpu", action="store_true", default=False, help="Enables GPU")
parser.add_argument("--use-tqdm", action="store_true", default=False, help="Enables progress bar")
args = parser.parse_args()
config = load_config_yml(args.config)
ood_eval(config, args.use_gpu, args.use_tqdm)