-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevaluate.py
More file actions
106 lines (84 loc) · 3.82 KB
/
Copy pathevaluate.py
File metadata and controls
106 lines (84 loc) · 3.82 KB
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
# -*- coding: utf-8 -*-
"""Evaluate.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1e1OPp4X5jViLwOsEtLJpxnE8R9iySR7x
"""
import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision
def show_misclassified_images(model, device, dataset, classes,number = 25):
misclassified_images = []
for images, labels in dataset:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
for i in range(len(labels)):
if(len(misclassified_images)<number and predicted[i]!=labels[i]):
misclassified_images.append([images[i],predicted[i],labels[i]])
if(len(misclassified_images)>number):
break
return misclassified_images
# --------------------------------------Plot Misclassified Images-------------------------------------------------
def plot_misclassified_images(misclassified_images, classes, Figsize = (20,20),number = 25) :
fig = plt.figure(figsize = Figsize)
for i in range(25):
sub = fig.add_subplot(5, 5, i+1)
img = misclassified_images[i][0].cpu()
img = img *0.29 + 0.6
npimg = img.numpy()
plt.imshow(np.transpose(npimg,(1, 2, 0)),interpolation='none')
sub.set_title("P={}\nA={}".format(str(classes[misclassified_images[i][1].data.cpu().numpy()]),str(classes[misclassified_images[i][2].data.cpu().numpy()])), fontsize = 10)
sub.axis('off')
plt.tight_layout()
# ----------------------------Evaluate Accuracy---------------------------------------------------------------------
def evaluate_accuracy(model, device, test_loader):
correct = 0
total = 0
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %.2f %%' % (
100 * correct / total))
# --------------------------------Evaluate classWise Accuracy------------------------------------------------------
def evaluate_classwise_accuracy(model, device, classes, test_loader):
class_correct = list(0. for i in range(len(classes)))
class_total = list(0. for i in range(len(classes)))
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
c = (predicted == labels).squeeze().to('cpu').flatten()
for i in range(len(c)):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
for i in range(len(classes)):
print('Accuracy of %5s : %2d%%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
# ---------------------------------Plot Accuracy/Loss Curves----------------------------------------------------
import matplotlib.pyplot as plt
"""
args:
points should be of type list of tuples or lists
Ex : [{[x: xpoints, y: ypoints, label: title, xlabel: "" , ylabel: " "}),([....points],label)]
"""
def plot_curve(curves,title,Figsize = (7,7)):
fig = plt.figure(figsize=Figsize)
ax = plt.subplot()
for curve in curves:
if("x" not in curve):
ax.plot(curve["y"], label=curve.get("label", "label"))
else:
ax.plot(curve["x"],curve["y"], label=curve.get("label","label"))
plt.xlabel(curve.get("xlabel","x-axis"))
plt.ylabel(curve.get("ylabel","y-axis"))
plt.title(title)
ax.legend()
plt.show()