-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
31 lines (25 loc) · 996 Bytes
/
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
# evaluate.py
import torch
import torch.nn.functional as F
from sklearn.metrics import accuracy_score, precision_recall_fscore_support, confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
from train import model, testloader
def evaluate_model():
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for inputs, labels in testloader:
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
all_preds.extend(predicted.numpy())
all_labels.extend(labels.numpy())
acc = accuracy_score(all_labels, all_preds)
precision, recall, f1, _ = precision_recall_fscore_support(all_labels, all_preds, average='weighted')
cm = confusion_matrix(all_labels, all_preds)
print(f"Accuracy: {acc}")
print(f"Precision: {precision}, Recall: {recall}, F1-Score: {f1}")
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.show()
evaluate_model()