-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn_classifier.py
170 lines (139 loc) · 5.29 KB
/
knn_classifier.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
import argparse
import os
import numpy as np
import torch
from lightning.pytorch import Trainer
from omegaconf import OmegaConf
from sklearn.neighbors import KNeighborsClassifier
from src.train import init_model
from src.utils.utils import load_yaml_config
from src.utils.init_datasets import init_datasets
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config",
type=str,
help="name of yaml config file",
default="best_models/morpho_mnist/baseline/config.yaml",
)
parser.add_argument(
"-ckpts",
"--ckpts_folder",
type=str,
help="path to ckpts folder",
default="best_models/morpho_mnist/baseline/checkpoints",
)
parser.add_argument(
"-k",
"--k",
type=int,
help="number of neighbors for kNN classifier",
default=30,
)
parser.add_argument(
"-a",
"--accelerator",
type=str,
help="compute device, either `cuda` or `cpu`",
default="cuda",
)
parser.add_argument(
"-d",
"--devices",
type=list,
help="If you use `cuda`, choose number of devices",
default=[0],
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
config = load_yaml_config(config_filename=args.config)
config = OmegaConf.create(config) # for dictionary dot notation
train_dataset, _, test_dataset_balanced = init_datasets(config)
print(f"len train dataset: {len(train_dataset)}")
train_loader = torch.utils.data.DataLoader(
train_dataset,
config.test_data.batch_size,
shuffle=False,
num_workers=config.test_data.num_workers,
prefetch_factor=config.test_data.prefetch_factor,
drop_last=False,
)
print(f"len balanced test dataset: {len(test_dataset_balanced)}")
test_loader_balanced = torch.utils.data.DataLoader(
test_dataset_balanced,
config.test_data.batch_size,
shuffle=False,
num_workers=config.test_data.num_workers,
prefetch_factor=config.test_data.prefetch_factor,
drop_last=False,
)
# Compute mean kNN classifier accuracy
k = args.k
model = init_model(config)
z1y1_accs = []
z1y2_accs = []
z2y1_accs = []
z2y2_accs = []
zy1_accs = []
zy2_accs = []
print(f"Method: {config.model.method}.")
model_ckpts = os.listdir(args.ckpts_folder)
knn_classifier = KNeighborsClassifier(n_neighbors=k)
for model_ckpt in model_ckpts:
model_ckpt = os.path.join(args.ckpts_folder, model_ckpt)
checkpoint = torch.load(model_ckpt, map_location=torch.device(args.accelerator))
model.load_state_dict(checkpoint["state_dict"])
model.eval()
trainer = Trainer(
devices=args.devices if args.accelerator == "cuda" else "auto",
accelerator=args.accelerator,
strategy="auto",
)
train_latents = torch.concat(
trainer.predict(model=model, dataloaders=train_loader)
)
test_latents = torch.concat(
trainer.predict(model=model, dataloaders=test_loader_balanced)
)
if config.model.method != "adv_cl":
z1_train = train_latents[:, : config.model.subspace_dims[0]].numpy()
z2_train = train_latents[:, config.model.subspace_dims[0] :].numpy()
y1_train = train_dataset._digit_labels
y2_train = train_dataset._pert_labels
z1_test = test_latents[:, : config.model.subspace_dims[0]].numpy()
z2_test = test_latents[:, config.model.subspace_dims[0] :].numpy()
y1_test = test_dataset_balanced._digit_labels
y2_test = test_dataset_balanced._pert_labels
knn_classifier.fit(z1_train, y1_train)
z1y1_accs.append(knn_classifier.score(z1_test, y1_test))
knn_classifier.fit(z1_train, y2_train)
z1y2_accs.append(knn_classifier.score(z1_test, y2_test))
knn_classifier.fit(z2_train, y1_train)
z2y1_accs.append(knn_classifier.score(z2_test, y1_test))
knn_classifier.fit(z2_train, y2_train)
z2y2_accs.append(knn_classifier.score(z2_test, y2_test))
else:
z_train = train_latents.numpy()
y1_train = train_dataset._digit_labels
y2_train = train_dataset._pert_labels
z_test = test_latents.numpy()
y1_test = test_dataset_balanced._digit_labels
y2_test = test_dataset_balanced._pert_labels
knn_classifier.fit(z_train, y1_train)
zy1_accs.append(knn_classifier.score(z_test, y1_test))
knn_classifier.fit(z_train, y2_train)
zy2_accs.append(knn_classifier.score(z_test, y2_test))
if config.model.method != "adv_cl":
print("Average accuracy scores on the balanced test set:")
print(
f"z1y1: {np.array(z1y1_accs).mean():0.3f}, z2y1: {np.array(z2y1_accs).mean():0.3f}"
)
print(
f"z1y2: {np.array(z1y2_accs).mean():0.3f}, z2y2: {np.array(z2y2_accs).mean():0.3f}"
)
else:
print("Average accuracy scores on the balanced test set:")
print(f"zy1: {np.array(zy1_accs).mean():0.3f}")
print(f"zy2: {np.array(zy2_accs).mean():0.3f}")