-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
347 lines (272 loc) · 11.4 KB
/
evaluate.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""This module implements the evaluation of the model on OOD detection """
import argparse
from typing import Union
import cv2
import matplotlib.pyplot as plt
import numpy as np
import scipy
import seaborn as sns
import torch
from sklearn import metrics
from torchvision import transforms
from survae.data.loaders.image import (CIFAR10, CIFAR100, FMNIST, MNIST,
OMNIGLOT, SVHN, CelebA)
from survae.data.loaders.toy import Constant, Noise
from survae.flows.flow import Flow
from survae.utils import loglik_bpd
from utils import load_model
plt.style.use('seaborn')
plt.rcParams["figure.figsize"] = (6, 6)
def get_dataloaders(trainset: Union[FMNIST, CIFAR10],
datasets: list, batch_size: int = 128) -> dict:
"""Creates a dictionary of dataloaders for the `datasets`.
Args:
- trainset (CIFAR10 | FMNIST) : the dataset on which the model was
trained on
- datasets (list) : list of datasets to evaluate the model on
- batch_size (int) : number of samples per batch
Returns:
- loaders (dict) : A dictionary of dataloaders.
"""
# Initialize dictionary
loaders = {}
# Get the test set of the dataset that the model was trained on
_, trainset_test = trainset.get_data_loaders(batch_size)
# Add loader for the Constant dataset
loaders["Constant"] = Constant(length=len(trainset_test))
# Loop through datasets and add their loaders to the dictionary
for dl in datasets:
_, test_dataloader = dl.get_data_loaders(batch_size)
loaders[dl.__class__.__name__] = test_dataloader
# Add loader for the Noise dataset
loaders["Noise"] = Noise(length=len(trainset_test))
loaders[trainset.__class__.__name__] = trainset_test
return loaders
def auroc(score: list, datasets: list, plot: bool = False,
filename: str = "aucroc_plot.pdf") -> list:
"""Calculate AUROC values for each dataset.
This function calculates the AUROC values for each dataset in `datasets`.
The last element in the `datasets` list corresponds to the test set of the
dataset that the model was trained on. We calculate the AUROC values by
creating pairs between the aforementioned test set and all the others in
the list.
Args:
- score (list): list of np.arrays containing scores calculated for each
of the `datasets`.
- datasets (list): list of datasets to evaluate the model on
- plot (bool): if true, the function generates and saves the ROC curve
- filename (str): if `plot` is set to true, the ROC curve is set using
the `filename` (Default: auroc_plot.pdf).
"""
# Set colors for the datasets in the plot
colors = sns.color_palette('Set3', 10)
aucrocs = []
# label_1 corresponds to the test set of the dataset that
# the model was trained on.
label_1 = np.ones(score[-1].shape[0])
# Loop through the rest datasets and calculate AUROC values
for i in range(len(score) - 1):
combined = np.concatenate((score[-1], score[i]))
label_2 = np.zeros(score[i].shape[0])
label = np.concatenate((label_1, label_2))
fpr, tpr, _ = metrics.roc_curve(label, combined, pos_label=1)
auc = metrics.auc(fpr, tpr)
aucrocs.append(auc)
if plot:
plt.plot(fpr, tpr, color=colors[i])
if plot:
plt.plot([0, 1], [0, 1], color='gray', linestyle='--')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.title(f'{datasets[-1]} AUROC')
plt.legend(datasets[:-1])
plt.tight_layout()
plt.savefig(filename)
plt.close()
return aucrocs
def complexity(x: torch.Tensor, compression: str = "png"):
"""Calculate the complexity of input `x`.
The complexities of the images in `x`, in bits per dimension, are
calculated using the formula described in https://arxiv.org/abs/1909.11480.
In particular, each image is encoded using the specified `compression` and
the complexity is calculated as follows:
L(x) = len(encoded(image)) * 8 / dimensionality(image)
Args:
- x (Tensor): tensor of images
- compression (str): compression to be used (Default: png)
"""
complexities = []
for img in x:
img = img.permute(1, 2, 0)
img = img.detach().cpu().numpy()
img *= 255
img = img.astype(np.uint8)
if compression == 'jp2':
img_encoded = cv2.imencode('.jp2', img)
elif compression == 'png':
img_encoded = cv2.imencode(
'.png', img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
# For 8 bit images
complexities.append(
len(img_encoded[1]) * 8 / (img.shape[0]*img.shape[1]*img.shape[2]))
return np.mean(complexities)
def correlation(model: Flow, loaders: dict, batch_size: int,
compression: str = "png", device: str = "cpu"):
"""Calculate the correlation between complexity and likelihood.
This function calculates the correlation between the complexity of 200
images from the test sets in `loaders` and their corresponding likelihood
values in bits per dimension calculated from our trained model. It plots
the corresponding Figure 4 of https://arxiv.org/abs/1909.11480.
Args:
- model (Flow) : tensor of images
- loaders (dict) : dictionary with loaders for the datasets we evaluate
- batch_size (int) : number of samples per batch in `loaders`
- compression (str) : compression to be used (Default: png)
- device (str) : device to be used (Default: cpu)
"""
# Set colors for the datasets in the plot
colors = sns.color_palette('Set3', 10)
# Number of batches needed for 200 images
if batch_size < 200:
batches = 200 // batch_size
final_batch = 200 % batch_size
else:
batches = 1
tot_comp = []
tot_lls = []
for i, dataloader in enumerate(loaders.values()):
print(f"Loader {i}/{len(loaders)-1}", end="\r")
data = torch.Tensor()
# Get 200 test images
if batches == 0:
data = next(iter(dataloader))[0:200]
else:
for _ in range(batches):
data = torch.cat([data, next(iter(dataloader))], axis=0)
data = torch.cat([data, next(iter(dataloader))[:final_batch]])
likelihoods = []
complexities = []
for x in data:
# Change dimensions (1, :)
x = x.unsqueeze(0)
# Calculate and append likelihood
likelihoods.append(-loglik_bpd(
model, x.to(
device, dtype=torch.float)).detach().cpu().numpy())
# Calculate and append complexity
complexities.append(complexity(x, compression=compression))
tot_lls.append(likelihoods)
tot_comp.append(complexities)
# Find max likelihood value calculated
max_val = np.amax(tot_lls)
# Normalize likelihoods in [0,1]
for i in range(len(tot_lls)):
for j in range(len(tot_lls[i])):
tot_lls[i][j] = np.exp(tot_lls[i][j] - max_val)
plt.scatter(tot_lls[i], tot_comp[i], color=colors[i])
# Calculate correlation
flat_lls = [value for ll in tot_lls for value in ll]
flat_comp = [value for comp in tot_comp for value in comp]
correlation = scipy.stats.pearsonr(flat_comp, flat_lls)
formatted_correlation = "{:.3f}".format(correlation[0])
plt.ylabel('L(x)')
plt.xlabel('p(x|M)')
plt.legend(
list(loaders.keys()),
loc='best', ncol=2)
plt.tight_layout()
plt.gcf().text(
0.01, 0.01, f"Correlation: {formatted_correlation}", fontsize=14)
plt.savefig("Figure4.png")
plt.close()
def main(args):
# Define input dimensions for the model
input_dim = (3, 32, 32)
# Transforms for datasets with 1x28x28 images
baw_transforms = [
transforms.Grayscale(num_output_channels=3),
transforms.Resize((32, 32))]
# Transforms for omniglot
omniglot_transforms = [
transforms.ToPILImage(),
transforms.Grayscale(num_output_channels=3),
transforms.Resize((32, 32))
]
# Datasets to be evaluated
data = [
MNIST(pil_transforms=baw_transforms),
OMNIGLOT(pil_transforms=omniglot_transforms),
CIFAR100(),
SVHN(),
CelebA(pil_transforms=[transforms.Resize((32, 32))])
]
if args.dataset == "FMNIST":
trainset = FMNIST(pil_transforms=baw_transforms)
data.append(CIFAR10())
else:
trainset = CIFAR10()
data.append(FMNIST(pil_transforms=baw_transforms))
# Load model from checkpoint
model = load_model(
input_dim=input_dim,
latent_dim=args.latent,
checkpoint=args.checkpoint
)
# Get data loaders
loaders = get_dataloaders(trainset, data, args.batch_size)
# If --correlation flag was used, create and save correlation plot
if args.correlation:
correlation(model, loaders, args.batch_size,
compression="png", device=args.device)
score = []
for i, dataloader in enumerate(loaders.values()):
print('')
losses = []
for j, x in enumerate(dataloader):
# Comput Negative Log-Likeligood in bits per dimension
nll = -loglik_bpd(model, x.to(args.device,
dtype=torch.float))
# Convert image for compression
img = x[0].permute(1, 2, 0)
img = img.detach().cpu().numpy()
img *= 255
img = img.astype(np.uint8)
# If compression i specified, calculate S-score
if args.compression is not None:
c = complexity(x, compression=args.compression)
losses.append(nll.detach().cpu().numpy() - c)
else:
losses.append(nll.detach().cpu().numpy())
print(
(f'Dataset {i+1}/{len(loaders)} '
f'Batch {j+1}/{len(dataloader)}'),
end='\r')
score.append(np.array(losses))
auroc_score = auroc(score, list(loaders.keys()), plot=True,
filename="auroc_score.pdf")
score_type = "S-score" if args.compression is not None else "Likelihood"
print('\n')
print("-"*30)
print(f"AUROC values for {score_type}")
print("-"*30)
for i, score in enumerate(auroc_score):
print(f"{list(loaders.keys())[i]}: {score}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--dataset', required=True, choices=["FMNIST", "CIFAR10"],
help='Name of the dataset that the model was trained on.')
parser.add_argument('--batch_size', default=128, type=int,
help='Number of samples per minibatch')
parser.add_argument('--latent', default=10, type=int,
choices=[10, 50, 75],
help='Dimension of latent space')
parser.add_argument('--checkpoint', required=True,
help='Path to checkpoint')
parser.add_argument('--compression', default=None,
help='Compression type', choices=["jp2", "png"])
parser.add_argument('--correlation', action='store_true')
parser.add_argument('--device', default="cpu", choices=["cpu", "cuda"],
help='Device to use for training')
args = parser.parse_args()
main(args)