Repository navigation
Expand file tree
/
Copy pathevaluate.py
More file actions
51 lines (40 loc) · 1.51 KB
/
Copy pathevaluate.py
File metadata and controls
51 lines (40 loc) · 1.51 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
import torch
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt
from data import dataloaders, class_names
from config import device
def evaluate_model(model, phase='test'):
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
print(classification_report(all_labels, all_preds, target_names=class_names))
cm = confusion_matrix(all_labels, all_preds)
plot_confusion_matrix(cm, class_names)
return cm
def plot_confusion_matrix(cm, class_names, save_path='docs/confusion_matrix.png'):
fig, ax = plt.subplots(figsize=(5, 5))
im = ax.imshow(cm, cmap='Blues')
ax.set_xticks(range(len(class_names)))
ax.set_yticks(range(len(class_names)))
ax.set_xticklabels(class_names)
ax.set_yticklabels(class_names)
ax.set_xlabel('Predicted')
ax.set_ylabel('Actual')
ax.set_title('Confusion Matrix')
for i in range(len(class_names)):
for j in range(len(class_names)):
ax.text(j, i, cm[i, j], ha='center', va='center',
color='white' if cm[i, j] > cm.max()/2 else 'black')
plt.colorbar(im)
plt.tight_layout()
plt.savefig(save_path)
plt.show()