-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
163 lines (133 loc) · 5.19 KB
/
Copy pathevaluation.py
File metadata and controls
163 lines (133 loc) · 5.19 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
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
import numpy as np
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
import torch
class Evaluator:
def __init__(self, class_names=None):
"""
Initialize the evaluator
Args:
class_names: List of class names
"""
self.class_names = class_names if class_names is not None else ["Class 0", "Class 1", "Class 2"]
def calculate_metrics(self, true_labels, predictions):
"""
Calculate performance metrics
Args:
true_labels: Ground truth labels
predictions: Predicted labels
Returns:
Dictionary of metrics
"""
# Calculate metrics
accuracy = accuracy_score(true_labels, predictions)
precision, recall, f1, _ = precision_recall_fscore_support(
true_labels, predictions, average='weighted', zero_division=0
)
# Per-class metrics
per_class_precision, per_class_recall, per_class_f1, _ = precision_recall_fscore_support(
true_labels, predictions, average=None, zero_division=0
)
metrics = {
'accuracy': accuracy,
'precision': precision,
'recall': recall,
'f1': f1,
'per_class_precision': per_class_precision,
'per_class_recall': per_class_recall,
'per_class_f1': per_class_f1
}
return metrics
def print_metrics(self, metrics):
"""
Print calculated metrics
Args:
metrics: Dictionary of metrics
"""
print(f"Overall Metrics:")
print(f" Accuracy: {metrics['accuracy']:.4f}")
print(f" Precision: {metrics['precision']:.4f}")
print(f" Recall: {metrics['recall']:.4f}")
print(f" F1 Score: {metrics['f1']:.4f}")
print("\nPer-Class Metrics:")
for i, class_name in enumerate(self.class_names):
print(f" {class_name}:")
print(f" Precision: {metrics['per_class_precision'][i]:.4f}")
print(f" Recall: {metrics['per_class_recall'][i]:.4f}")
print(f" F1 Score: {metrics['per_class_f1'][i]:.4f}")
def plot_confusion_matrix(self, true_labels, predictions):
"""
Plot confusion matrix
Args:
true_labels: Ground truth labels
predictions: Predicted labels
Returns:
Matplotlib figure
"""
cm = confusion_matrix(true_labels, predictions)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=self.class_names,
yticklabels=self.class_names)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
return plt.gcf()
def plot_training_history(self, history):
"""
Plot training history
Args:
history: Dictionary containing training history
Returns:
Matplotlib figure
"""
plt.figure(figsize=(12, 5))
# Plot training & validation accuracy
plt.subplot(1, 2, 1)
plt.plot(history['train_acc'], label='Train')
if 'val_acc' in history:
plt.plot(history['val_acc'], label='Validation')
plt.title('Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
# Plot training & validation loss
plt.subplot(1, 2, 2)
plt.plot(history['train_loss'], label='Train')
if 'val_loss' in history:
plt.plot(history['val_loss'], label='Validation')
plt.title('Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.tight_layout()
return plt.gcf()
def evaluate_classifier(self, classifier, features, labels, batch_size=32):
"""
Evaluate a trained classifier
Args:
classifier: Trained classifier object
features: Test features
labels: Test labels
batch_size: Batch size for evaluation
Returns:
Dictionary of metrics
"""
# Convert numpy arrays to PyTorch tensors
features_tensor = torch.FloatTensor(features)
labels_tensor = torch.LongTensor(labels)
# Create dataset and data loader
dataset = torch.utils.data.TensorDataset(features_tensor, labels_tensor)
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=False
)
# Get loss and accuracy using the classifier's evaluate method
loss, accuracy = classifier.evaluate(dataloader)
# Get predictions for detailed metrics
predictions, _ = classifier.predict(features)
# Calculate detailed metrics
metrics = self.calculate_metrics(labels, predictions)
metrics['loss'] = loss
return metrics