-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
387 lines (286 loc) · 15.3 KB
/
testing.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# -*- coding: utf-8 -*-
"""
The file contains implementations of the functions used to test a CNN model using different options.
test_cnn - Function used to test a Convolutional Neural Network.
test_laplace - Function used to test a Bayesian Neural Network (Laplace Approximation).
test_temperature - Function used to test a Convolutional Neural Network with temperature scaling.
test_bnn - Function used to test a Bayesian Neural Network (Bayes by Backprop).
"""
# Library Imports
import laplace
import pandas as pd
from torch.cuda import amp
from torch.nn import functional as F
from torch.utils.data import DataLoader
# Own Modules
from utils import *
from dataset import get_datasets
from temperature import get_temperature
from model import Classifier, BayesByBackpropClassifier
__author__ = ["Jacob Carse", "Andres Alvarez Olmo"]
__copyright__ = "Copyright 2022, Calibration"
__credits__ = ["Jacob Carse", "Andres Alvarez Olmo"]
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer = ["Jacob Carse", "Andres Alvarez Olmo"]
__email__ = ["j.carse@dundee.ac.uk", "alvarezolmoandres@gmail.com"]
__status__ = "Development"
def test_cnn(arguments: Namespace, device: torch.device) -> None:
"""
Function for testing the Convolutional Neural Network.
:param arguments: ArgumentParser Namespace object with arguments used for training.
:param device: PyTorch device that will be used for training.
"""
# Loads the testing data.
_, _, test_data = get_datasets(arguments)
# Creates the testing data loader using the dataset objects.
testing_data_loader = DataLoader(test_data, batch_size=arguments.batch_size * 2,
shuffle=False, num_workers=arguments.data_workers,
pin_memory=False, drop_last=False)
log(arguments, "Loaded Datasets\n")
# Initialises the classifier model.
classifier = Classifier(arguments.efficient_net, test_data.num_class, pretrained=False)
# Loads the trained model.
classifier.load_state_dict(torch.load(os.path.join(arguments.model_dir, f"{arguments.experiment}_best.pt")))
# Sets the classifier to evaluation mode.
classifier.eval()
# Moves the classifier to the selected device.
classifier.to(device)
# Defines the list of data to be used to collect results.
batch_count = 0
data_frame = [[] for _ in range(test_data.num_class + 2)]
# Loops through the testing data batches with no gradient calculations.
with torch.no_grad():
for images, labels, file_names in testing_data_loader:
# Adds to the current batch count.
batch_count += 1
# Moves the images to the selected device also moves the labels to the cpu.
images = images.to(device)
labels = labels.cpu().numpy()
# Performs forward propagation using 16 bit precision.
if arguments.precision == 16 and device != torch.device("cpu"):
with amp.autocast():
logits = classifier(images)
# Performs forward propagation using 32 bit precision.
else:
logits = classifier(images)
# Gets the predictive probabilities from the neural network.
predictions = F.softmax(logits, dim=1).cpu().numpy()
# Adds all information to the dataframe.
data_frame[0] += list(file_names)
data_frame[1] += labels.tolist()
for i in range(test_data.num_class):
data_frame[2 + i] += predictions[:, i].tolist()
# If the number of batches have been reached end testing.
if batch_count == arguments.batches_per_epoch:
break
# Creates the output directory for the output files.
os.makedirs(arguments.output_dir, exist_ok=True)
# Creates the DataFrame from the output predictions.
data_frame = pd.DataFrame(data_frame).transpose()
# Outputs the output DataFrame to a csv file.
data_frame.to_csv(os.path.join(arguments.output_dir, f"{arguments.experiment}_point.csv"))
def test_laplace(arguments: Namespace, device: torch.device) -> None:
"""
Function for testing the Laplace Convolutional Neural Network.
:param arguments: ArgumentParser Namespace object with arguments used for training.
:param device: PyTorch device that will be used for training.
"""
# Loads the training and testing data.
train_data, _, test_data = get_datasets(arguments)
# Creates the training data loader using the dataset objects.
training_data_loader = DataLoader(train_data, batch_size=int(4 if arguments.dataset.lower() == "isic" else 16),
shuffle=True, num_workers=arguments.data_workers,
pin_memory=False, drop_last=False)
# Creates the testing data loader using the dataset objects.
testing_data_loader = DataLoader(test_data, batch_size=arguments.batch_size * 2,
shuffle=False, num_workers=arguments.data_workers,
pin_memory=False, drop_last=False)
log(arguments, "Loaded Datasets\n")
# Initialises the classifier model.
classifier = Classifier(arguments.efficient_net, test_data.num_class, pretrained=False)
# Loads the trained model.
classifier.load_state_dict(torch.load(os.path.join(arguments.model_dir, f"{arguments.experiment}_best.pt")))
# Sets the classifier to evaluation mode.
classifier.eval()
# Moves the classifier to the selected device.
classifier.to(device)
# Sets up the Laplace Approximation model using the trained model.
la = laplace.Laplace(classifier, "classification", subset_of_weights="last_layer", hessian_structure="full")
log(arguments, "Fitting Laplace Approximation to training data")
# Fits Laplace Approximation using 16 bit precision.
if arguments.precision == 16 and device != torch.device("cpu"):
with amp.autocast():
# Fits the Laplace Approximation to the training data.
la.fit(training_data_loader)
log(arguments, "Optimising Prior Precision")
# Optimises the prior precision using Marginal-likelihood.
la.optimize_prior_precision(method='marglik')
# Fits Laplace Approximation using 32 bit precision.
else:
# Fits the Laplace Approximation to the training data.
la.fit(training_data_loader)
log(arguments, "Optimising Prior Precision")
# Optimises the prior precision using Marginal-likelihood.
la.optimize_prior_precision(method='marglik')
# Defines the list of data to be used to collect results.
batch_count = 0
data_frame = [[] for _ in range(test_data.num_class + 2)]
# Loops through the testing data batches with no gradient calculations.
with torch.no_grad():
for images, labels, file_names in testing_data_loader:
# Adds to the current batch count.
batch_count += 1
# Moves the images to the selected device also moves the labels to the cpu.
images = images.to(device)
labels = labels.cpu().numpy()
# Gets the predictive samples from the laplace model.
predictions = la.predictive_samples(images, n_samples=arguments.testing_samples)
predictions = torch.swapaxes(predictions, 0, 1)
# Moves the predictions to the CPU.
predictions = predictions.cpu().numpy()
# Averages the Bayesian predictions.
predictions = np.mean(predictions, axis=1)
# Adds all information to the dataframe.
data_frame[0] += list(file_names)
data_frame[1] += labels.tolist()
for i in range(test_data.num_class):
data_frame[2 + i] += predictions[:, i].tolist()
# If the number of batches have been reached end testing.
if batch_count == arguments.batches_per_epoch:
break
# Creates the output directory for the output files.
os.makedirs(arguments.output_dir, exist_ok=True)
# Creates the DataFrame from the output predictions.
data_frame = pd.DataFrame(data_frame).transpose()
# Outputs the output DataFrame to a csv file.
data_frame.to_csv(os.path.join(arguments.output_dir, f"{arguments.experiment}_laplace.csv"))
def test_temperature(arguments: Namespace, device: torch.device, temperature: str = "cross_entropy") -> None:
"""
Function for testing the Convolutional Neural Network.
:param arguments: ArgumentParser Namespace object with arguments used for training.
:param device: PyTorch device that will be used for training.
:param temperature: String for the mode used to optimise the temperature.
"""
# Loads the validation and testing data.
_, val_data, test_data = get_datasets(arguments)
# Creates the validation data loader using the dataset objects.
val_data_loader = DataLoader(val_data, batch_size=arguments.batch_size * 2,
shuffle=False, num_workers=arguments.data_workers,
pin_memory=False, drop_last=False)
# Creates the testing data loader using the dataset objects.
testing_data_loader = DataLoader(test_data, batch_size=arguments.batch_size * 2,
shuffle=False, num_workers=arguments.data_workers,
pin_memory=False, drop_last=False)
log(arguments, "Loaded Datasets\n")
# Initialises the classifier model.
classifier = Classifier(arguments.efficient_net, test_data.num_class, pretrained=False)
# Loads the trained model.
classifier.load_state_dict(torch.load(os.path.join(arguments.model_dir, f"{arguments.experiment}_best.pt")))
# Sets the classifier to evaluation mode.
classifier.eval()
# Moves the classifier to the selected device.
classifier.to(device)
# Gets the temperature using a specified mode.
temp_value = get_temperature(arguments, classifier, val_data_loader, device, temperature)
# Defines the list of data to be used to collect results.
batch_count = 0
data_frame = [[] for _ in range(test_data.num_class + 2)]
# Loops through the testing data batches with no gradient calculations.
with torch.no_grad():
for images, labels, file_names in testing_data_loader:
# Adds to the current batch count.
batch_count += 1
# Moves the images to the selected device also moves the labels to the cpu.
images = images.to(device)
labels = labels.cpu().numpy()
# Performs forward propagation using 16 bit precision.
if arguments.precision == 16 and device != torch.device("cpu"):
with amp.autocast():
logits = classifier(images)
# Performs forward propagation using 32 bit precision.
else:
logits = classifier(images)
# Scales the logits using the provided temperature.
logits = torch.div(logits, temp_value)
# Gets the predictive probabilities from the neural network.
predictions = F.softmax(logits, dim=1).cpu().numpy()
# Adds all information to the dataframe.
data_frame[0] += list(file_names)
data_frame[1] += labels.tolist()
for i in range(test_data.num_class):
data_frame[2 + i] += predictions[:, i].tolist()
# If the number of batches have been reached end testing.
if batch_count == arguments.batches_per_epoch:
break
# Creates the output directory for the output files.
os.makedirs(arguments.output_dir, exist_ok=True)
# Creates the DataFrame from the output predictions.
data_frame = pd.DataFrame(data_frame).transpose()
# Outputs the output DataFrame to a csv file.
data_frame.to_csv(os.path.join(arguments.output_dir, f"{arguments.experiment}_{temperature}.csv"))
def test_bnn(arguments, device) -> None:
"""
Function for testing the Bayesian Neural Network.
:param arguments: ArgumentParser Namespace object with arguments used for training.
:param device: PyTorch device that will be used for training.
"""
# Loads the testing data.
_, _, test_data = get_datasets(arguments)
# Creates the testing data loader using the dataset objects.
testing_data_loader = DataLoader(test_data, batch_size=arguments.batch_size * 2,
shuffle=False, num_workers=arguments.data_workers,
pin_memory=False, drop_last=False)
log(arguments, "Loaded Datasets\n")
# Initialises the classifier model.
classifier = BayesByBackpropClassifier(arguments.efficient_net, test_data.num_class,
pretrained=False, device=device)
# Loads the trained model.
classifier.load_state_dict(torch.load(os.path.join(arguments.model_dir, f"{arguments.experiment}_best.pt")))
# Sets the classifier to evaluation mode.
classifier.eval()
# Moves the classifier to the selected device.
classifier.to(device)
# Defines the list of data to be used to collect results.
batch_count = 0
data_frame = [[] for _ in range(test_data.num_class + 2)]
# Loops through the testing data batches with no gradient calculations.
with torch.no_grad():
for images, labels, file_names in testing_data_loader:
# Adds to the current batch count.
batch_count += 1
# Moves the images to the selected device also moves the labels to the cpu.
images = images.to(device)
labels = labels.cpu().numpy()
# Declares a list for Monte-Carlo Samples.
predictions = []
# Performs Monte-Carlo Sampling using 16 bit precision.
if arguments.precision == 16 and device != torch.device("cpu"):
with amp.autocast():
for _ in range(arguments.testing_samples):
output = classifier(images)
predictions.append(output)
# Performs Monte-Carlo Sampling using 32 bit precision.
else:
for _ in range(arguments.testing_samples):
output = classifier(images)
predictions.append(output)
# Stacks the Monte-Carlo samples into a single tensor.
predictions = torch.stack(predictions)
predictions = np.moveaxis(F.softmax(predictions, dim=2).cpu().numpy(), 0, 1)
# Finds the mean of predictive samples.
predictions = np.mean(predictions, axis=1)
# Adds all information to the dataframe.
data_frame[0] += list(file_names)
data_frame[1] += labels.tolist()
for i in range(test_data.num_class):
data_frame[2 + i] += predictions[:, i].tolist()
# If the number of batches have been reached end testing.
if batch_count == arguments.batches_per_epoch:
break
# Creates the output directory for the output files.
os.makedirs(arguments.output_dir, exist_ok=True)
# Creates the DataFrame from the output predictions.
data_frame = pd.DataFrame(data_frame).transpose()
# Outputs the output DataFrame to a csv file.
data_frame.to_csv(os.path.join(arguments.output_dir, f"{arguments.experiment}_bbb.csv"))