-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_utils.py
226 lines (186 loc) · 6.72 KB
/
plot_utils.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
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
from collections import Counter
from math import ceil
from pathlib import Path
from typing import Callable
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import torch
from torch.utils.data import Dataset, Subset
from constants import IMAGENET_CLASSES_DICT
from utils import (
calculate_cosine_similarity,
calculate_distances,
name_mapping_fn,
unnormalize,
)
def plot_experiment_results(
target_class: int,
counters: dict[float, Counter],
latent_vectors: dict[int, torch.Tensor],
model_name: str,
class_dataset: Dataset | Subset = None,
save_path: str = None,
) -> None:
"""Plot the predictions, distances and similarities of the model for the target class.
Args:
target_class: The target class to compare the predictions to
counters: The counters of the predictions for different alpha values
latent_vectors: The latent vectors of the dataset
model_name: The name of the model
class_dataset: The dataset of the classes
save_path: The path to save the plot
"""
# Calculate distances and similarities
target_vector = latent_vectors[target_class]
distances = calculate_distances(latent_vectors, target_vector)
similarities = calculate_cosine_similarity(latent_vectors, target_vector)
# Plot the predictions and distances
_, axs = plt.subplots(len(counters), 3, figsize=(12, 20))
for i, (alpha, counter) in enumerate(counters.items()):
plot_predictions(
axs[i, 0], counter, name_mapping_fn=name_mapping_fn, title=f"Alpha={alpha}"
)
plot_distances(
axs[i, 1],
counter,
similarities,
name_mapping_fn=name_mapping_fn,
title="Cosine similarity",
)
plot_distances(
axs[i, 2],
counter,
distances,
name_mapping_fn=name_mapping_fn,
title="L2 norm",
)
plt.tight_layout()
# Save the plot if a save path is provided
if save_path:
save_path = Path(save_path)
save_path.mkdir(parents=True, exist_ok=True)
plt.savefig(f"{save_path}/{model_name}.png")
plt.show()
# Visualize the images if a dataset is provided
if class_dataset:
plot_images(class_dataset, len(class_dataset), save_path=save_path)
def plot_images(
dataset: Dataset | Subset,
number_of_images: int = 10,
save_path: str = None,
) -> None:
"""Plot the images in the dataset.
Args:
dataset: The dataset to visualize
number_of_images: The number of images to visualize
save_path: The path to save the images
"""
number_of_plots = ceil(number_of_images / 10)
for plot_index in range(number_of_plots):
start_index = plot_index * 10
end_index = min((plot_index + 1) * 10, number_of_images)
# Get the images and their labels for the current plot
images_and_labels = list(
zip(*[dataset[i] for i in range(start_index, end_index)])
)
images, labels = images_and_labels[0], images_and_labels[1]
# Create a grid of subplots
fig, axs = plt.subplots(2, 5, figsize=(15, 6))
# For each image and its corresponding label
for i, (img, _) in enumerate(zip(images, labels)):
img = unnormalize(img)
# Plot the image in the corresponding subplot
ax = axs[i // 5, i % 5]
ax.imshow(img)
ax.axis("off")
if save_path:
Path(save_path).mkdir(parents=True, exist_ok=True)
fig.savefig(f"{save_path}/images_{plot_index}.png")
fig.show()
def plot_predictions(
subplot: plt.Axes,
counter: Counter,
name_mapping_fn: Callable = None,
title: str = None,
) -> None:
"""Plot the predictions.
Args:
subplot: The subplot to plot the predictions on
counter: The counter of the predictions
name_mapping_fn: The function to map the labels to names
title: The title of the plot
"""
labels, values = zip(*reversed(counter.items()))
if name_mapping_fn:
labels = [name_mapping_fn(label) for label in labels]
subplot.barh(range(len(labels)), values)
subplot.set_yticks(range(len(labels)))
subplot.set_yticklabels(labels)
if title:
subplot.set_title(title)
def plot_distances(
subplot: plt.Axes,
counter: Counter,
distances_to_target: dict[int, float],
name_mapping_fn: Callable = None,
title: str = None,
draw_names: bool = False,
) -> None:
"""Plot the distances to the target class.
Args:
subplot: The subplot to plot the distances on
counter: The counter of the predictions
distances_to_target: The distances to the target class
name_mapping_fn: The function to map the labels to names
title: The title of the plot
draw_names: Whether to draw the names of the classes
"""
for label in counter:
counter[label] = distances_to_target.get(label, 0)
labels, values = zip(*reversed(counter.items()))
if name_mapping_fn:
labels = [name_mapping_fn(label) for label in labels]
subplot.barh(labels, values)
subplot.set_yticks(range(len(labels)))
subplot.set_yticklabels(labels if draw_names else [])
subplot.set_xlim(0, 1)
if title:
subplot.set_title(title)
def plot_classes_tsne(
class_latent_vectors: dict[int, torch.Tensor],
save_path: str = None,
fig_size: tuple[int, int] = (10, 10),
dimension: int = 2,
label_dict: dict[int, str] = IMAGENET_CLASSES_DICT,
) -> None:
"""Plot the classes in a t-SNE plot.
Args:
class_latent_vectors (dict[int, Tensor]): The latent vectors of the classes
save_path (Optional[str]): The path to save the plot
fig_size (tuple[int, int]): The size of the figure
dimension (int): The dimension of the t-SNE plot
"""
# Get the number of the latent vectors for each class
class_latent_vec_count = {
key: value.shape[0] for key, value in class_latent_vectors.items()
}
# Concatenate all the latent vectors
X = torch.cat([value for value in class_latent_vectors.values()])
# Perform t-SNE
X_embedded = TSNE(n_components=dimension).fit_transform(X)
# Plot the results
fig, ax = plt.subplots(figsize=fig_size)
start = 0
for i, (key, count) in enumerate(class_latent_vec_count.items()):
end = start + count
ax.scatter(
X_embedded[start:end, 0],
X_embedded[start:end, 1],
label=label_dict[key].split(",")[0],
)
start = end
ax.legend()
if save_path:
Path(save_path).mkdir(parents=True, exist_ok=True)
fig.savefig(f"{save_path}/tsne.png")
plt.show()