-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize.py
More file actions
337 lines (270 loc) · 9.68 KB
/
visualize.py
File metadata and controls
337 lines (270 loc) · 9.68 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
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
"""Visualization utilities for Drifting Models.
Provides functions for:
- 2D toy data visualization
- Sample grid visualization
- Training progress plots
- Drifting field visualization
"""
from typing import List, Optional, Tuple
import numpy as np
import torch
def plot_2d_samples(
real_data: torch.Tensor,
generated_data: torch.Tensor,
title: str = "Drifting Model Samples",
save_path: Optional[str] = None,
figsize: Tuple[int, int] = (10, 5),
):
"""Plot 2D real and generated samples side by side.
Args:
real_data: Real data samples (N, 2)
generated_data: Generated samples (M, 2)
title: Plot title
save_path: Path to save figure (optional)
figsize: Figure size
"""
try:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=figsize)
# Real data
real_np = real_data.cpu().numpy() if isinstance(real_data, torch.Tensor) else real_data
axes[0].scatter(real_np[:, 0], real_np[:, 1], alpha=0.5, s=5)
axes[0].set_title("Real Data")
axes[0].set_aspect("equal")
axes[0].grid(True, alpha=0.3)
# Generated data
gen_np = generated_data.cpu().numpy() if isinstance(generated_data, torch.Tensor) else generated_data
axes[1].scatter(gen_np[:, 0], gen_np[:, 1], alpha=0.5, s=5)
axes[1].set_title("Generated Data")
axes[1].set_aspect("equal")
axes[1].grid(True, alpha=0.3)
plt.suptitle(title)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=150, bbox_inches="tight")
plt.close()
else:
plt.show()
except ImportError:
print("matplotlib not available for visualization")
def plot_training_progress(
real_data: torch.Tensor,
generated_history: List[torch.Tensor],
steps: List[int],
save_path: Optional[str] = None,
figsize: Tuple[int, int] = (15, 5),
):
"""Plot training progress showing generated samples at different steps.
Args:
real_data: Real data samples
generated_history: List of generated samples at different steps
steps: Step numbers corresponding to generated_history
save_path: Path to save figure
figsize: Figure size
"""
try:
import matplotlib.pyplot as plt
n_plots = len(generated_history) + 1
fig, axes = plt.subplots(1, n_plots, figsize=figsize)
# Real data
real_np = real_data.cpu().numpy() if isinstance(real_data, torch.Tensor) else real_data
axes[0].scatter(real_np[:, 0], real_np[:, 1], alpha=0.5, s=5)
axes[0].set_title("Real Data")
axes[0].set_aspect("equal")
# Generated at each step
for i, (gen, step) in enumerate(zip(generated_history, steps)):
gen_np = gen.cpu().numpy() if isinstance(gen, torch.Tensor) else gen
axes[i + 1].scatter(gen_np[:, 0], gen_np[:, 1], alpha=0.5, s=5)
axes[i + 1].set_title(f"Step {step}")
axes[i + 1].set_aspect("equal")
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=150, bbox_inches="tight")
plt.close()
else:
plt.show()
except ImportError:
print("matplotlib not available for visualization")
def plot_drifting_field(
samples: torch.Tensor,
drift_vectors: torch.Tensor,
real_data: Optional[torch.Tensor] = None,
save_path: Optional[str] = None,
figsize: Tuple[int, int] = (8, 8),
scale: float = 1.0,
):
"""Visualize the drifting field as a quiver plot.
Args:
samples: Sample positions (N, 2)
drift_vectors: Drift vectors at each sample (N, 2)
real_data: Real data samples for reference (optional)
save_path: Path to save figure
figsize: Figure size
scale: Arrow scale factor
"""
try:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=figsize)
samples_np = samples.cpu().numpy() if isinstance(samples, torch.Tensor) else samples
drift_np = drift_vectors.cpu().numpy() if isinstance(drift_vectors, torch.Tensor) else drift_vectors
# Plot real data if provided
if real_data is not None:
real_np = real_data.cpu().numpy() if isinstance(real_data, torch.Tensor) else real_data
ax.scatter(real_np[:, 0], real_np[:, 1], alpha=0.3, s=5, c="blue", label="Real")
# Plot samples
ax.scatter(samples_np[:, 0], samples_np[:, 1], alpha=0.5, s=10, c="red", label="Generated")
# Plot drift vectors
ax.quiver(
samples_np[:, 0], samples_np[:, 1],
drift_np[:, 0] * scale, drift_np[:, 1] * scale,
alpha=0.5, color="green", angles="xy", scale_units="xy", scale=1,
)
ax.set_title("Drifting Field Visualization")
ax.set_aspect("equal")
ax.legend()
ax.grid(True, alpha=0.3)
if save_path:
plt.savefig(save_path, dpi=150, bbox_inches="tight")
plt.close()
else:
plt.show()
except ImportError:
print("matplotlib not available for visualization")
def make_image_grid(
images: torch.Tensor,
nrow: int = 8,
normalize: bool = True,
value_range: Tuple[float, float] = (-1, 1),
) -> torch.Tensor:
"""Create a grid of images.
Args:
images: Images tensor (N, C, H, W)
nrow: Number of images per row
normalize: Whether to normalize to [0, 1]
value_range: Input value range for normalization
Returns:
Grid image (C, H, W)
"""
N, C, H, W = images.shape
ncol = (N + nrow - 1) // nrow
# Pad if necessary
if N < nrow * ncol:
padding = torch.zeros(nrow * ncol - N, C, H, W, device=images.device)
images = torch.cat([images, padding], dim=0)
# Reshape to grid
images = images.reshape(ncol, nrow, C, H, W)
images = images.permute(2, 0, 3, 1, 4) # (C, ncol, H, nrow, W)
images = images.reshape(C, ncol * H, nrow * W)
if normalize:
min_val, max_val = value_range
images = (images - min_val) / (max_val - min_val)
images = images.clamp(0, 1)
return images
def save_image_grid(
images: torch.Tensor,
path: str,
nrow: int = 8,
normalize: bool = True,
):
"""Save images as a grid.
Args:
images: Images tensor (N, C, H, W)
path: Output path
nrow: Number of images per row
normalize: Whether to normalize to [0, 1]
"""
try:
from torchvision.utils import save_image
save_image(images, path, nrow=nrow, normalize=normalize)
except ImportError:
# Fallback using PIL
try:
from PIL import Image
grid = make_image_grid(images, nrow, normalize)
grid_np = (grid.cpu().numpy() * 255).astype(np.uint8)
if grid_np.shape[0] == 1:
grid_np = grid_np[0]
else:
grid_np = grid_np.transpose(1, 2, 0)
Image.fromarray(grid_np).save(path)
except ImportError:
print("Neither torchvision nor PIL available for saving images")
def plot_loss_curves(
losses: dict,
save_path: Optional[str] = None,
figsize: Tuple[int, int] = (10, 6),
):
"""Plot training loss curves.
Args:
losses: Dictionary of loss name -> list of values
save_path: Path to save figure
figsize: Figure size
"""
try:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=figsize)
for name, values in losses.items():
ax.plot(values, label=name, alpha=0.8)
ax.set_xlabel("Step")
ax.set_ylabel("Loss")
ax.set_title("Training Loss")
ax.legend()
ax.grid(True, alpha=0.3)
ax.set_yscale("log")
if save_path:
plt.savefig(save_path, dpi=150, bbox_inches="tight")
plt.close()
else:
plt.show()
except ImportError:
print("matplotlib not available for visualization")
def visualize_cfg_interpolation(
model: torch.nn.Module,
noise: torch.Tensor,
class_label: int,
cfg_scales: List[float],
save_path: Optional[str] = None,
device: str = "cuda",
):
"""Visualize samples at different CFG scales.
Args:
model: Generator model
noise: Input noise (1, C, H, W)
class_label: Class label
cfg_scales: List of CFG scales to visualize
save_path: Path to save figure
device: Device
"""
try:
import matplotlib.pyplot as plt
samples = []
model.eval()
with torch.no_grad():
for cfg_scale in cfg_scales:
cfg_alpha = torch.full((1,), cfg_scale, device=device)
labels = torch.tensor([class_label], device=device)
sample = model(noise.to(device), labels, cfg_alpha)
samples.append(sample.cpu())
samples = torch.cat(samples, dim=0)
# Plot
n = len(cfg_scales)
fig, axes = plt.subplots(1, n, figsize=(3 * n, 3))
for i, (ax, scale) in enumerate(zip(axes, cfg_scales)):
img = samples[i].permute(1, 2, 0).numpy()
img = (img + 1) / 2 # Normalize to [0, 1]
img = img.clip(0, 1)
if img.shape[-1] == 1:
img = img[:, :, 0]
ax.imshow(img, cmap="gray")
else:
ax.imshow(img)
ax.set_title(f"CFG = {scale}")
ax.axis("off")
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=150, bbox_inches="tight")
plt.close()
else:
plt.show()
except ImportError:
print("matplotlib not available for visualization")