File size: 7,292 Bytes
99ca773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Visualization utilities for architectural style classification.

"""

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import torch
from typing import Dict, List, Optional, Tuple

def plot_attention_weights(attention_weights: torch.Tensor, 

                          image: torch.Tensor,

                          title: str = "Attention Weights",

                          save_path: Optional[str] = None) -> None:
    """

    Plot attention weights overlaid on the image.

    

    Args:

        attention_weights: Attention weights tensor [H, W]

        image: Input image tensor [C, H, W]

        title: Plot title

        save_path: Path to save the plot

    """
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
    
    # Plot original image
    img_np = image.permute(1, 2, 0).numpy()
    img_np = (img_np - img_np.min()) / (img_np.max() - img_np.min())
    ax1.imshow(img_np)
    ax1.set_title("Original Image")
    ax1.axis('off')
    
    # Plot attention weights
    attention_np = attention_weights.numpy()
    im = ax2.imshow(attention_np, cmap='hot', alpha=0.7)
    ax2.imshow(img_np, alpha=0.3)
    ax2.set_title(title)
    ax2.axis('off')
    
    plt.colorbar(im, ax=ax2)
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
    
    plt.show()

def plot_training_curves(train_losses: List[float], 

                        val_losses: List[float],

                        train_accuracies: Optional[List[float]] = None,

                        val_accuracies: Optional[List[float]] = None,

                        save_path: Optional[str] = None) -> None:
    """

    Plot training and validation curves.

    

    Args:

        train_losses: Training losses per epoch

        val_losses: Validation losses per epoch

        train_accuracies: Training accuracies per epoch

        val_accuracies: Validation accuracies per epoch

        save_path: Path to save the plot

    """
    epochs = range(1, len(train_losses) + 1)
    
    if train_accuracies and val_accuracies:
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
        
        # Plot losses
        ax1.plot(epochs, train_losses, 'b-', label='Training Loss')
        ax1.plot(epochs, val_losses, 'r-', label='Validation Loss')
        ax1.set_title('Training and Validation Loss')
        ax1.set_xlabel('Epoch')
        ax1.set_ylabel('Loss')
        ax1.legend()
        ax1.grid(True)
        
        # Plot accuracies
        ax2.plot(epochs, train_accuracies, 'b-', label='Training Accuracy')
        ax2.plot(epochs, val_accuracies, 'r-', label='Validation Accuracy')
        ax2.set_title('Training and Validation Accuracy')
        ax2.set_xlabel('Epoch')
        ax2.set_ylabel('Accuracy')
        ax2.legend()
        ax2.grid(True)
        
    else:
        fig, ax = plt.subplots(1, 1, figsize=(10, 6))
        ax.plot(epochs, train_losses, 'b-', label='Training Loss')
        ax.plot(epochs, val_losses, 'r-', label='Validation Loss')
        ax.set_title('Training and Validation Loss')
        ax.set_xlabel('Epoch')
        ax.set_ylabel('Loss')
        ax.legend()
        ax.grid(True)
    
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
    
    plt.show()

def plot_confusion_matrix(confusion_matrix: np.ndarray,

                         class_names: List[str],

                         title: str = "Confusion Matrix",

                         save_path: Optional[str] = None) -> None:
    """

    Plot confusion matrix.

    

    Args:

        confusion_matrix: Confusion matrix array

        class_names: List of class names

        title: Plot title

        save_path: Path to save the plot

    """
    plt.figure(figsize=(10, 8))
    sns.heatmap(confusion_matrix, annot=True, fmt='d', cmap='Blues',
                xticklabels=class_names, yticklabels=class_names)
    plt.title(title)
    plt.xlabel('Predicted')
    plt.ylabel('Actual')
    plt.xticks(rotation=45)
    plt.yticks(rotation=0)
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
    
    plt.show()

def plot_class_distribution(class_counts: Dict[str, int],

                           title: str = "Class Distribution",

                           save_path: Optional[str] = None) -> None:
    """

    Plot class distribution.

    

    Args:

        class_counts: Dictionary mapping class names to counts

        title: Plot title

        save_path: Path to save the plot

    """
    classes = list(class_counts.keys())
    counts = list(class_counts.values())
    
    plt.figure(figsize=(12, 6))
    bars = plt.bar(classes, counts)
    plt.title(title)
    plt.xlabel('Class')
    plt.ylabel('Count')
    plt.xticks(rotation=45)
    
    # Add count labels on bars
    for bar, count in zip(bars, counts):
        plt.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.01,
                str(count), ha='center', va='bottom')
    
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
    
    plt.show()

def plot_feature_maps(feature_maps: torch.Tensor,

                     title: str = "Feature Maps",

                     num_maps: int = 16,

                     save_path: Optional[str] = None) -> None:
    """

    Plot feature maps from a convolutional layer.

    

    Args:

        feature_maps: Feature maps tensor [B, C, H, W]

        title: Plot title

        num_maps: Number of feature maps to display

        save_path: Path to save the plot

    """
    # Take first batch and select first num_maps channels
    maps = feature_maps[0, :num_maps].detach().cpu()
    
    # Calculate grid size
    grid_size = int(np.ceil(np.sqrt(num_maps)))
    
    fig, axes = plt.subplots(grid_size, grid_size, figsize=(12, 12))
    axes = axes.flatten()
    
    for i in range(num_maps):
        if i < maps.shape[0]:
            im = axes[i].imshow(maps[i], cmap='viridis')
            axes[i].set_title(f'Feature Map {i+1}')
            axes[i].axis('off')
        else:
            axes[i].axis('off')
    
    plt.suptitle(title)
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
    
    plt.show()

def plot_learning_rate_schedule(learning_rates: List[float],

                               steps: List[int],

                               title: str = "Learning Rate Schedule",

                               save_path: Optional[str] = None) -> None:
    """

    Plot learning rate schedule.

    

    Args:

        learning_rates: List of learning rates

        steps: List of step numbers

        title: Plot title

        save_path: Path to save the plot

    """
    plt.figure(figsize=(10, 6))
    plt.plot(steps, learning_rates, 'b-', linewidth=2)
    plt.title(title)
    plt.xlabel('Step')
    plt.ylabel('Learning Rate')
    plt.yscale('log')
    plt.grid(True)
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
    
    plt.show()