File size: 11,141 Bytes
ea8bfa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
CLARA Visualization Module

Functions for visualizing results, confusion matrices, and attention weights.
"""

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import torch
from sklearn.metrics import confusion_matrix
from typing import List, Dict, Optional
import os


# Set style
plt.style.use('seaborn-v0_8-darkgrid')
sns.set_palette("husl")


def plot_confusion_matrix(
    y_true: np.ndarray,
    y_pred: np.ndarray,
    class_names: List[str],
    save_path: Optional[str] = None,
    figsize: tuple = (8, 6),
    cmap: str = 'Blues'
):
    """
    Plot confusion matrix
    
    Args:
        y_true: True labels
        y_pred: Predicted labels
        class_names: List of class names
        save_path: Path to save figure
        figsize: Figure size
        cmap: Colormap
    """
    cm = confusion_matrix(y_true, y_pred)
    cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    
    fig, ax = plt.subplots(figsize=figsize)
    sns.heatmap(
        cm_normalized,
        annot=True,
        fmt='.2%',
        cmap=cmap,
        xticklabels=class_names,
        yticklabels=class_names,
        ax=ax,
        cbar_kws={'label': 'Normalized Frequency'}
    )
    
    ax.set_xlabel('Predicted Label', fontsize=12)
    ax.set_ylabel('True Label', fontsize=12)
    ax.set_title('Confusion Matrix', fontsize=14, fontweight='bold')
    
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
        print(f"✅ Confusion matrix saved to: {save_path}")
    
    plt.show()


def plot_training_curves(
    history: Dict[str, List[float]],
    save_path: Optional[str] = None,
    figsize: tuple = (15, 5)
):
    """
    Plot training curves (loss, accuracy, F1)
    
    Args:
        history: Training history dictionary
        save_path: Path to save figure
        figsize: Figure size
    """
    fig, axes = plt.subplots(1, 3, figsize=figsize)
    
    epochs = range(1, len(history['train_loss']) + 1)
    
    # Loss
    axes[0].plot(epochs, history['train_loss'], 'o-', label='Train', linewidth=2)
    axes[0].plot(epochs, history['val_loss'], 's-', label='Validation', linewidth=2)
    axes[0].set_xlabel('Epoch', fontsize=11)
    axes[0].set_ylabel('Loss', fontsize=11)
    axes[0].set_title('Training Loss', fontsize=12, fontweight='bold')
    axes[0].legend()
    axes[0].grid(True, alpha=0.3)
    
    # Accuracy
    axes[1].plot(epochs, history['train_acc'], 'o-', label='Train', linewidth=2)
    axes[1].plot(epochs, history['val_acc'], 's-', label='Validation', linewidth=2)
    axes[1].set_xlabel('Epoch', fontsize=11)
    axes[1].set_ylabel('Accuracy', fontsize=11)
    axes[1].set_title('Training Accuracy', fontsize=12, fontweight='bold')
    axes[1].legend()
    axes[1].grid(True, alpha=0.3)
    
    # F1 Score
    axes[2].plot(epochs, history['train_f1'], 'o-', label='Train', linewidth=2)
    axes[2].plot(epochs, history['val_f1'], 's-', label='Validation', linewidth=2)
    axes[2].set_xlabel('Epoch', fontsize=11)
    axes[2].set_ylabel('F1 Score', fontsize=11)
    axes[2].set_title('Training F1 Score', fontsize=12, fontweight='bold')
    axes[2].legend()
    axes[2].grid(True, alpha=0.3)
    
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
        print(f"✅ Training curves saved to: {save_path}")
    
    plt.show()


def plot_per_class_metrics(
    metrics: Dict[str, np.ndarray],
    class_names: List[str],
    save_path: Optional[str] = None,
    figsize: tuple = (10, 6)
):
    """
    Plot per-class precision, recall, and F1 scores
    
    Args:
        metrics: Dictionary with 'precision', 'recall', 'f1' arrays
        class_names: List of class names
        save_path: Path to save figure
        figsize: Figure size
    """
    x = np.arange(len(class_names))
    width = 0.25
    
    fig, ax = plt.subplots(figsize=figsize)
    
    ax.bar(x - width, metrics['precision'], width, label='Precision', alpha=0.8)
    ax.bar(x, metrics['recall'], width, label='Recall', alpha=0.8)
    ax.bar(x + width, metrics['f1'], width, label='F1 Score', alpha=0.8)
    
    ax.set_xlabel('Class', fontsize=12)
    ax.set_ylabel('Score', fontsize=12)
    ax.set_title('Per-Class Performance Metrics', fontsize=14, fontweight='bold')
    ax.set_xticks(x)
    ax.set_xticklabels(class_names)
    ax.legend()
    ax.grid(True, axis='y', alpha=0.3)
    ax.set_ylim([0, 1.0])
    
    # Add value labels on bars
    for container in ax.containers:
        ax.bar_label(container, fmt='%.2f', padding=3)
    
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
        print(f"✅ Per-class metrics saved to: {save_path}")
    
    plt.show()


def plot_performance_comparison(
    results: Dict[str, Dict[str, float]],
    metrics: List[str] = ['accuracy', 'f1'],
    save_path: Optional[str] = None,
    figsize: tuple = (10, 6)
):
    """
    Plot performance comparison between models
    
    Args:
        results: Dictionary mapping model names to metrics
        metrics: List of metrics to plot
        save_path: Path to save figure
        figsize: Figure size
    """
    models = list(results.keys())
    x = np.arange(len(metrics))
    width = 0.8 / len(models)
    
    fig, ax = plt.subplots(figsize=figsize)
    
    for i, model_name in enumerate(models):
        values = [results[model_name][metric] for metric in metrics]
        ax.bar(x + i * width, values, width, label=model_name, alpha=0.8)
    
    ax.set_xlabel('Metric', fontsize=12)
    ax.set_ylabel('Score', fontsize=12)
    ax.set_title('Model Performance Comparison', fontsize=14, fontweight='bold')
    ax.set_xticks(x + width * (len(models) - 1) / 2)
    ax.set_xticklabels([m.replace('_', ' ').title() for m in metrics])
    ax.legend()
    ax.grid(True, axis='y', alpha=0.3)
    ax.set_ylim([0, 1.0])
    
    # Add value labels
    for container in ax.containers:
        ax.bar_label(container, fmt='%.3f', padding=3)
    
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
        print(f"✅ Performance comparison saved to: {save_path}")
    
    plt.show()


def plot_ablation_study(
    ablation_results: Dict[str, float],
    baseline_name: str = 'Full CLARA',
    metric_name: str = 'Weighted F1 (%)',
    save_path: Optional[str] = None,
    figsize: tuple = (10, 6)
):
    """
    Plot ablation study results
    
    Args:
        ablation_results: Dictionary mapping variant names to scores
        baseline_name: Name of the baseline (full model)
        metric_name: Name of the metric
        save_path: Path to save figure
        figsize: Figure size
    """
    variants = list(ablation_results.keys())
    scores = list(ablation_results.values())
    baseline_score = ablation_results[baseline_name]
    
    # Calculate differences from baseline
    differences = [score - baseline_score for score in scores]
    colors = ['green' if d >= 0 else 'red' for d in differences]
    
    fig, ax = plt.subplots(figsize=figsize)
    
    bars = ax.barh(variants, scores, color=colors, alpha=0.7)
    ax.axvline(baseline_score, color='black', linestyle='--', linewidth=2, label='Baseline')
    
    ax.set_xlabel(metric_name, fontsize=12)
    ax.set_title('Ablation Study Results', fontsize=14, fontweight='bold')
    ax.legend()
    ax.grid(True, axis='x', alpha=0.3)
    
    # Add value labels
    for i, (score, diff) in enumerate(zip(scores, differences)):
        label = f"{score:.2f}"
        if diff != 0:
            label += f" ({diff:+.2f})"
        ax.text(score + 0.5, i, label, va='center')
    
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
        print(f"✅ Ablation study saved to: {save_path}")
    
    plt.show()


def visualize_predictions(
    images: List,
    texts: List[str],
    true_labels: List[str],
    pred_labels: List[str],
    confidences: List[float],
    save_path: Optional[str] = None,
    figsize: tuple = (15, 10),
    max_samples: int = 6
):
    """
    Visualize sample predictions
    
    Args:
        images: List of PIL images
        texts: List of text captions
        true_labels: List of true labels
        pred_labels: List of predicted labels
        confidences: List of confidence scores
        save_path: Path to save figure
        figsize: Figure size
        max_samples: Maximum number of samples to show
    """
    n_samples = min(len(images), max_samples)
    cols = 3
    rows = (n_samples + cols - 1) // cols
    
    fig, axes = plt.subplots(rows, cols, figsize=figsize)
    axes = axes.flatten() if n_samples > 1 else [axes]
    
    for i in range(n_samples):
        ax = axes[i]
        ax.imshow(images[i])
        ax.axis('off')
        
        # Title with prediction info
        color = 'green' if true_labels[i] == pred_labels[i] else 'red'
        title = f"True: {true_labels[i]}\nPred: {pred_labels[i]} ({confidences[i]:.2%})"
        ax.set_title(title, fontsize=10, color=color, fontweight='bold')
        
        # Add text caption below image
        ax.text(
            0.5, -0.1, f'"{texts[i]}"',
            transform=ax.transAxes,
            ha='center',
            fontsize=8,
            style='italic',
            wrap=True
        )
    
    # Hide extra subplots
    for i in range(n_samples, len(axes)):
        axes[i].axis('off')
    
    plt.suptitle('Sample Predictions', fontsize=16, fontweight='bold', y=1.02)
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
        print(f"✅ Predictions visualization saved to: {save_path}")
    
    plt.show()


def save_all_figures(
    history: Dict,
    results: Dict,
    y_true: np.ndarray,
    y_pred: np.ndarray,
    class_names: List[str],
    output_dir: str
):
    """
    Save all visualization figures
    
    Args:
        history: Training history
        results: Evaluation results
        y_true: True labels
        y_pred: Predicted labels
        class_names: List of class names
        output_dir: Directory to save figures
    """
    os.makedirs(output_dir, exist_ok=True)
    
    # Training curves
    plot_training_curves(
        history,
        save_path=os.path.join(output_dir, 'training_curves.png')
    )
    
    # Confusion matrix
    plot_confusion_matrix(
        y_true, y_pred, class_names,
        save_path=os.path.join(output_dir, 'confusion_matrix.png')
    )
    
    # Per-class metrics
    if 'precision_per_class' in results and 'recall_per_class' in results and 'f1_per_class' in results:
        plot_per_class_metrics(
            {
                'precision': np.array(results['precision_per_class']),
                'recall': np.array(results['recall_per_class']),
                'f1': np.array(results['f1_per_class'])
            },
            class_names,
            save_path=os.path.join(output_dir, 'per_class_metrics.png')
        )
    
    print(f"\n✅ All figures saved to: {output_dir}")