-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem2.py
405 lines (317 loc) · 13.8 KB
/
problem2.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
# from torchmetrics.classification import PrecisionRecallCurve
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, precision_recall_curve, auc, average_precision_score, PrecisionRecallDisplay
from sklearn.preprocessing import label_binarize
# auc=Area under curve
import matplotlib.pylab as pylab
params = {'legend.fontsize': 'large',
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
pylab.rcParams.update(params)
# https://github.com/bkong999/COVNet/blob/master/main.py
# DONE: Use Git and GitHub
# DONE: Implement accuracy in the training
# DONE: Implement confusion matrix
# DONE: Implement validation loss and accuracy in the training
# DONE: Implement PR curves
# ! TODO: Endre tilbake størrelsen på testset når ferdig å programmere funksionalitetene
# DONE: Micro average over all classes and plot the average PR curve
# DONE: Implement Area under the PR curve
# DONE: Implement precision and recall (training-, test- and validation set?)
# ? TODO: Implement saving function of the results (training and validation accuracy and loss, confusion matrix, PR curves)
# ! Will have to do this manually
# ? TODO continuation: And the code to a file (epochs, training/validation split, neural network structure, loss function, optimizer)
# ! Will have to do this manually
# TODO: Forstå alle funksjonene jeg skal bruke for forbedre modellen
# DONE: Clean up funksjoner, få god oversikt over tallene og plotene
# DONE: Klare å endre på modellen
# TODO: Trene modellene og lagre resultatene
def validate(network, valloader, criterion):
val_running_loss = 0.0
# defining model state
network.eval()
print('validating...')
k = 0
with torch.no_grad(): # preventing gradient calculations since we will not be optimizing
# iterating through batches
for j, val_data in enumerate(valloader, 0):
val_inputs, val_labels = val_data[0].to(
device), val_data[1].to(device)
# --------------------------
# making classsifications and computing loss
# --------------------------
val_outputs = net(val_inputs)
val_loss = criterion(val_outputs, val_labels)
val_running_loss += val_loss.item()
k = j
print("validation loss: ", val_running_loss/k)
def accuracy(network, dataloader, train_val_test=0):
# setting model state
network.eval()
# instantiating counters
total_correct = 0
total_instances = 0
with torch.no_grad(): # preventing gradient calculations since we will not be optimizing
# iterating through batches
for data in dataloader:
images, labels = data[0].to(device), data[1].to(device)
# -------------------------------------------------------------------------
# making classifications and deriving indices of maximum value via argmax
# -------------------------------------------------------------------------
classifications = torch.argmax(network(images), dim=1)
# --------------------------------------------------
# comparing indicies of maximum values and labels
# --------------------------------------------------
correct_predictions = sum(classifications == labels).item()
total_correct += correct_predictions
total_instances += len(images)
if train_val_test == 1:
print("training accuracy: ", total_correct/total_instances)
elif train_val_test == 2:
print("validation accuracy: ", total_correct/total_instances)
elif train_val_test == 3:
print("test accuracy: ", total_correct/total_instances)
else:
print("[?] accuracy: ", total_correct/total_instances)
def precision_recall(y_pred, y_true):
precision_arr = [[], [], [], [], [], [], [], [], [], []]
recall_arr = [[], [], [], [], [], [], [], [], [], []]
for x in range(len(precision_arr)):
# En klasse er 1 og de andre er 0
y_pred_class = []
y_true_class = []
for y in range(len(y_true)):
if y_pred[y] == x:
y_pred_class.append(1)
else:
y_pred_class.append(0)
if y_true[y] == x:
y_true_class.append(1)
else:
y_true_class.append(0)
truePositives = 0
trueNegatives = 0
falsePositives = 0
falseNegatives = 0
for m in range(len(y_true)): # Se figur for å dobbeltsjekke
if y_pred_class[m] == y_true_class[m] and y_pred_class[m] == 1:
truePositives += 1
elif y_pred_class[m] == y_true_class[m] and y_pred_class[m] == 0:
trueNegatives += 1
elif y_pred_class[m] != y_true_class[m] and y_pred_class[m] == 1:
falsePositives += 1
elif y_pred_class[m] != y_true_class[m] and y_pred_class[m] == 0:
falseNegatives += 1
precision_class = truePositives / (truePositives+falsePositives)
recall_class = truePositives / (truePositives+falseNegatives)
precision_arr[x] = precision_class
recall_arr[x] = recall_class
return precision_arr, recall_arr
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))])
device = "cuda:0" if torch.cuda.is_available() else "cpu"
kwargs = {} if device == 'cpu' else {'num_workers': 1, 'pin_memory': True}
batch_size = 10 # Kan endre denne?
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform)
# kan endre på denne, hva som er fint og hjelper kommer an på kontekst ikke lett å si en enkel split (men fint å starte med 80/20)
train_set, val_set = torch.utils.data.random_split(trainset, [0.8, 0.2])
trainloader = torch.utils.data.DataLoader(train_set, batch_size=batch_size,
shuffle=True, **kwargs)
valloader = torch.utils.data.DataLoader(val_set, batch_size=batch_size,
shuffle=True, **kwargs)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
# testset_80p, testset_20p = torch.utils.data.random_split(
# trainset, [0.98, 0.02]) # ! Må endres!!!!
# testset_80p, testset_20p = torch.utils.data.random_split(
# trainset, [0.98, 0.02]) # ! Må endres!!!!
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=False, **kwargs)
classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
# https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html
class Net(nn.Module): # Kan endre de her
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 6) # prøve bigger square
self.pool_max2x = nn.MaxPool2d(2, 2)
self.pool_max3x = nn.MaxPool2d(3, 3) # Forsette med denne
# self.pool_mean = nn.AvgPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 4)
self.dropout_HL = nn.Dropout(0.3)
self.dropout_In = nn.Dropout(0.8)
self.fc1 = nn.Linear(16 * 3 * 3, 100)
self.fc2 = nn.Linear(100, 50)
self.fc3 = nn.Linear(50, 30)
self.fc4 = nn.Linear(30, 10)
def forward(self, x):
x = self.pool_max3x(F.relu(self.conv1(x)))
x = self.pool_max2x(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 3 * 3)
# print("view: ", x)
x = F.relu(self.fc1(x))
# print("fc1: ", x)
x = self.dropout_HL(x)
# print("fc1 drop: ", x)
x = F.relu(self.fc2(x))
# print("fc2: ", x)
x = self.dropout_HL(x)
x = F.relu(self.fc3(x))
x = self.dropout_HL(x)
x = self.fc4(x)
# print(x)
# print(self.dropout(x))
return x
# out = self.dropout(self.fc3(out))
net = Net()
net.to(device)
criterion = nn.CrossEntropyLoss() # Loss/distance funksjon
# optimizer = optim.SGD(net.parameters(), lr=0.001,
# momentum=0.9) # kan endre på denne
optimizer = optim.Adam(net.parameters(), lr=0.001)
print("Starting training")
for epoch in range(13): # loop over the dataset multiple times | Kan endre på denne?
print("starting epoch " + str(epoch+1) + " ...")
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
# inputs, labels = data
inputs, labels = data[0].to(device), data[1].to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
print('[%d] training running loss: %.3f' % (epoch + 1, running_loss / i))
accuracy(net, trainloader, 1)
validate(net, valloader, criterion)
accuracy(net, valloader, 2)
print('-----------------')
print('Finished Training')
print('-----------------')
correct = 0
total = 0
y_pred = np.array([])
y_true = np.array([])
y_probs = np.array([], dtype="float")
y_prob = []
with torch.no_grad():
for data in testloader:
images, labels = data[0].to(device), data[1].to(device)
y_true = np.append(y_true, labels)
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
y_pred = np.append(y_pred, predicted)
total += labels.size(0)
correct += (predicted == labels).sum().item()
probs = torch.nn.functional.softmax(outputs, dim=1)
y_prob.append(probs.numpy().tolist())
print('Test accuracy of the network: %d %%' % (100 * correct / total))
cf_matrix = confusion_matrix(y_true, y_pred)
df_cm = pd.DataFrame(cf_matrix / np.sum(cf_matrix, axis=1), index=[i for i in classes],
columns=[i for i in classes])
plt.figure(figsize=(12, 7))
sns.heatmap(df_cm, annot=True, annot_kws={"size": 12})
# plt.rcParams.update({'font.size': 18})
# plt.savefig('output.png')
plt.show()
params = {'legend.fontsize': 'large',
'axes.labelsize': 'large',
'axes.titlesize':'large',
'xtick.labelsize':'large',
'ytick.labelsize':'large'}
pylab.rcParams.update(params)
y_prob_samples = []
# Det er 10 probabilities i et sample
# Det er 10 samples i 1 batch
# Det er 100 samples til sammen
y_true = np.array([int(x) for x in y_true])
# one hot encode the test data true labels
y_true_binary = label_binarize(y_true, classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
precision_arr, recall_arr = precision_recall(y_pred, y_true)
print("Precision for each class: ", precision_arr)
print("Recall for each class: ", recall_arr)
for a in range(len(y_prob)):
for b in range(len(y_prob[0])):
y_prob_samples.append(y_prob[a][b])
# print(y_prob_samples)
y_prob_samples = np.array(y_prob_samples)
# Batch er 10, må dele opp slik at per sample og ikke per batch [[[...]]] ---> [[...]]
# Det er til sammen 1000 samples, og dette er også i y_prob selv om ser litt vanskelig ut
# Får riktig verdier, altså en verdi fra hver
# print(y_true_binary[:, 0])
# print()
# print(y_prob_samples[:, 0])
precision = dict()
recall = dict()
auc_precision_recall = []
average_precision = dict()
for i in range(len(classes)):
precision[i], recall[i], _ = precision_recall_curve(
y_true_binary[:, i], y_prob_samples[:, i])
average_precision[i] = average_precision_score(
y_true_binary[:, i], y_prob_samples[:, i])
# test_precision[i], test_recall[i], _ = precision_recall_curve(y_test_binary[:, i], y_test_score[:, i])
plt.plot(recall[i], precision[i], lw=2, label='class {}'.format(classes[i]))
auc_precision_recall.append(auc(recall[i], precision[i]))
print("AUPRC for each class: ", auc_precision_recall)
print("Average precision: ", average_precision)
plt.xlabel("Recall", fontsize=18)
# plt.rcParams.update({'font.size': 18})
# plt.rcParams.update({'axes.titlesize': 'large'})
# plt.rcParams.update({'axes.labelsize': 'large'})
plt.grid()
plt.ylabel("Precision", fontsize=18)
plt.legend(loc="best", fontsize=16)
plt.title("precision vs. recall curve", fontsize=22)
plt.show()
# A "micro-average": quantifying score on all classes jointly
precision["micro"], recall["micro"], _ = precision_recall_curve(
y_true_binary.ravel(), y_prob_samples.ravel()
)
average_precision["micro"] = average_precision_score(
y_true_binary, y_prob_samples, average="micro")
# auc_precision_recall.append(auc(recall[i], precision[i]))
display = PrecisionRecallDisplay(
recall=recall["micro"],
precision=precision["micro"],
average_precision=average_precision["micro"],
)
display.plot()
_ = display.ax_.set_title("Micro-averaged over all classes",fontsize=22)
display.ax_.set_xlabel('Recall', fontsize=18)
display.ax_ .set_ylabel('Precision', fontsize=18)
# plt.rcParams.update({'font.size': 18})
# plt.rcParams.update({'axes.titlesize': 'large'})
# plt.rcParams.update({'axes.labelsize': 'large'})
# mpl.rcParams['axes.titlesize'] = 2
plt.grid()
plt.show()
# Tar man AUCPR fra baseline eller fra x aksen?
display_x = display.line_.get_xdata()
display_y = display.line_.get_ydata()
display_auc = auc(display_x, display_y)
print("display_auc: ", display_auc)
# print(display.line_.get_xdata())
# print(display.line_.get_xydata())
# print(display.line_.get_ydata())
# print(display._y)
# print(display._xy)
print("hei")