-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
96 lines (79 loc) · 2.51 KB
/
Copy pathgraph.py
File metadata and controls
96 lines (79 loc) · 2.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
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
import pickle
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report
import seaborn as sns
import os
# -----------------------------
# 1. Load dataset
# -----------------------------
data_dict = pickle.load(open('./data.pickle', 'rb'))
data = np.asarray(data_dict['data'])
labels = np.asarray(data_dict['labels'])
# Count images per class
unique, counts = np.unique(labels, return_counts=True)
# -----------------------------
# 2. Dataset Distribution Graph
# -----------------------------
plt.figure(figsize=(10, 5))
plt.bar(unique, counts)
plt.xlabel("Class")
plt.ylabel("No. of Images")
plt.title("Dataset Distribution (Images per Class)")
plt.xticks(unique)
plt.tight_layout()
plt.show()
# -----------------------------
# 3. Load Model
# -----------------------------
model_dict = pickle.load(open('model.p', 'rb'))
model = model_dict['model']
# -----------------------------
# 4. Split data for evaluation
# -----------------------------
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(
data, labels, test_size=0.2, shuffle=True, stratify=labels
)
y_pred = model.predict(x_test)
# -----------------------------
# 5. Confusion Matrix
# -----------------------------
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(12, 8))
sns.heatmap(cm, annot=True, cmap="Blues", fmt='g')
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()
# -----------------------------
# 6. Precision, Recall, F1 Bar Graphs
# -----------------------------
report = classification_report(y_test, y_pred, output_dict=True)
classes = [str(i) for i in range(len(unique))]
precision = [report[c]['precision'] for c in classes]
recall = [report[c]['recall'] for c in classes]
f1 = [report[c]['f1-score'] for c in classes]
x = np.arange(len(classes))
width = 0.25
plt.figure(figsize=(14, 6))
plt.bar(x - width, precision, width, label='Precision')
plt.bar(x, recall, width, label='Recall')
plt.bar(x + width, f1, width, label='F1-Score')
plt.xlabel("Classes")
plt.ylabel("Score")
plt.title("Precision, Recall, F1-score per Class")
plt.xticks(x, classes)
plt.legend()
plt.tight_layout()
plt.show()
# -----------------------------
# 7. Overall Accuracy Graph
# -----------------------------
accuracy = np.mean(y_pred == y_test)
plt.figure(figsize=(5, 5))
plt.bar(['Accuracy'], [accuracy])
plt.ylim(0, 1)
plt.title("Model Accuracy")
plt.text(0, accuracy/2, f"{accuracy*100:.2f}%", ha='center', fontsize=12)
plt.show()