-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
189 lines (144 loc) · 6.93 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import random
import torch
import argparse
import numpy as np
from tabulate import tabulate
from misc.utils_python import mkdir, import_yaml_config, save_dict, load_dict
from model_engines.factory import create_model_engine
from model_engines.interface import verify_model_outputs
from ood_detectors.factory import create_ood_detector
from eval_assets import save_performance
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model_name', '-m', type=str,
default='resnet50-react',
choices=[
'resnet50-supcon',
'resnet50-react',
'regnet-y-16gf-swag-e2e-v1',
'vit-b16-swag-e2e-v1',
'mobilenet-v2'
],
help='The name of model')
parser.add_argument('--seed', type=int,
default=0,
help='Seed number')
parser.add_argument('--gpu_idx', '-g', type=int,
default=0,
help='gpu idx')
parser.add_argument('--num_workers', '-nw', type=int,
default=8,
help='number of workers')
parser.add_argument('--train_data_name', '-td', type=str,
# default='imagenet1k',
default='cifar10',
choices=['imagenet1k'],
help='The data name for the in-distribution')
parser.add_argument('--id_data_name', '-id', type=str,
# default='imagenet1k',
default='cifar10',
choices=['imagenet1k',
'imagenet1k-v2-a',
'imagenet1k-v2-b',
'imagenet1k-v2-c'],
help='The data name for the in-distribution')
parser.add_argument('--ood_data_name', '-ood', type=str,
# default='inaturalist',
default='svhn',
choices=['inaturalist', 'sun', 'places', 'textures', 'openimage-o']
)
parser.add_argument("--ood_detectors", type=str, nargs='+',
# default=['energy', 'nnguide', 'msp', 'maxlogit', 'vim', 'ssd', 'mahalanobis', 'knn'],
default=['energy', 'nnguide'],
help="List of OOD detectors")
parser.add_argument('--batch_size', '-bs', type=int,
default=32,
help='Batch size for inference')
parser.add_argument('--data_root_path', type=str,
default='/home/jay/datasets',
help='Data root path')
parser.add_argument('--save_root_path', type=str,
default='./saved_model_outputs')
args = parser.parse_args()
args.device = torch.device('cuda:%d' % (args.gpu_idx) if torch.cuda.is_available() else 'cpu')
args = import_yaml_config(args, f'./configs/model/{args.model_name}.yaml')
args.log_dir_path = f"./logs/seed-{args.seed}/{args.model_name}/{args.train_data_name}/{args.id_data_name}"
args.train_save_dir_path = f"{args.save_root_path}/seed-{args.seed}/{args.model_name}/{args.train_data_name}"
args.id_save_dir_path = f"{args.save_root_path}/seed-{args.seed}/{args.model_name}/{args.id_data_name}"
args.ood_save_dir_path = f"{args.save_root_path}/seed-{args.seed}/{args.model_name}/{args.ood_data_name}"
args.detector_save_dir_path = f"{args.save_root_path}/seed-{args.seed}/{args.model_name}/{args.train_data_name}/detectors"
mkdir(args.log_dir_path)
mkdir(args.train_save_dir_path)
mkdir(args.id_save_dir_path)
mkdir(args.ood_save_dir_path)
mkdir(args.detector_save_dir_path)
print(tabulate(list(vars(args).items()), headers=['arguments', 'values']))
return args
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
def main():
args = get_args()
set_seed(args.seed)
scores_set = {}
accs = {}
for oodd_name in args.ood_detectors:
args = import_yaml_config(args, f"./configs/detector/{oodd_name}.yaml")
scores_set[oodd_name], labels, accs[oodd_name] = evaluate(args, oodd_name)
save_performance(scores_set, labels, accs, f"{args.log_dir_path}/ood-{args.ood_data_name}.csv")
def evaluate(args, ood_detector_name: str):
'''
Executing model engine
'''
print(f"[{args.model_name} / {ood_detector_name}]: running model...")
model_engine = create_model_engine(args.model_name)
model_engine.set_model(args)
model_engine.set_dataloaders()
save_dir_paths = {}
save_dir_paths['train'] = args.train_save_dir_path
save_dir_paths['id'] = args.id_save_dir_path
save_dir_paths['ood'] = args.ood_save_dir_path
model_outputs = {}
labels = {}
try:
for fold in ['train', 'id', 'ood']:
model_outputs[fold] = torch.load(f"{save_dir_paths[fold]}/model_outputs_{fold}.pt")
except:
model_engine.train_model()
model_outputs = {}
model_outputs['train'], model_outputs['id'], model_outputs['ood'] = model_engine.get_model_outputs()
for fold in ['train', 'id', 'ood']:
assert verify_model_outputs(model_outputs[fold])
torch.save(model_outputs[fold], f"{save_dir_paths[fold]}/model_outputs_{fold}.pt")
labels = {}
labels['id'] = model_outputs['id']['labels']
labels['ood'] = model_outputs['ood']['labels']
'''
Executing ood detector
'''
print(f"[{args.model_name} / {ood_detector_name}]: running detector...")
saved_detector_path = f"{args.detector_save_dir_path}/{ood_detector_name}.pkl"
try:
ood_detector = load_dict(saved_detector_path)["detector"]
except:
ood_detector = create_ood_detector(ood_detector_name)
ood_detector.setup(args, model_outputs['train'])
print(f"[{args.model_name} / {ood_detector_name}]: saving detector...")
save_dict({"detector": ood_detector}, saved_detector_path)
print(f"[{args.model_name} / {ood_detector_name}]: detector saved!")
'''
Evaluating metrics
'''
print(f"[{args.model_name} / {ood_detector_name}]: evaluating metrics...")
id_scores = ood_detector.infer(model_outputs['id'])
ood_scores = ood_detector.infer(model_outputs['ood'])
scores = torch.cat([id_scores, ood_scores], dim=0).numpy()
id_logits = model_outputs['id']['logits']
detection_labels = torch.cat([torch.ones_like(labels['id']), torch.zeros_like(labels['ood'])], dim=0).numpy()
preds_id = torch.max(id_logits, dim=-1)[1]
acc = (preds_id == labels['id']).float().mean().numpy()
return scores, detection_labels, acc
if __name__ == '__main__':
main()