-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_fashionmnist_wandb.py
More file actions
482 lines (396 loc) · 14.9 KB
/
train_fashionmnist_wandb.py
File metadata and controls
482 lines (396 loc) · 14.9 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# Reference: https://chatgpt.com/c/69b72e10-7f8c-832e-9845-6a532587cb13
# ============================================================
# FashionMNIST training script with Weights & Biases (W&B) logging.
#
# W&B key concepts used in this script:
# wandb.init() – start a new experiment run and upload config
# wandb.config – read hyperparameters synced from init()
# wandb.log() – stream metrics (loss, accuracy, …) to the dashboard
# wandb.watch() – auto-log model gradients and parameter histograms
# wandb.Image/Table – log rich media (images, structured tables)
# wandb.plot.* – log built-in interactive charts (e.g. confusion matrix)
# wandb.run.summary – store final / best scalar values for a run
# wandb.save() – upload files (checkpoints) as run artifacts
# wandb.finish() – mark the run as complete and flush any remaining data
# ============================================================
import os
import random
import numpy as np
import torch
import wandb
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
CLASS_NAMES = [
"T-shirt/top",
"Trouser",
"Pullover",
"Dress",
"Coat",
"Sandal",
"Shirt",
"Sneaker",
"Bag",
"Ankle boot",
]
# ============================================================
# CONFIG
# ============================================================
def get_config():
# This dict is passed directly to wandb.init(config=...), which uploads
# every key-value pair so you can compare runs in the W&B dashboard.
config = {
"project_name": "fashionmnist-pytorch-demo",
"group_name": "model_comparison",
"model_name": "cnn", # options: "mlp" or "cnn"
"data_dir": "data",
"checkpoint_dir": "checkpoints",
"batch_size": 128,
"learning_rate": 1e-3, # options: 1e-2, 1e-3, 1e-4
"epochs": 10,
"hidden_dim_1": 256,
"hidden_dim_2": 128,
"cnn_channels_1": 32,
"cnn_channels_2": 64,
"num_classes": 10,
"seed": 42,
}
if config["model_name"] == "mlp":
config["run_name"] = (
f"mlp-lr{config['learning_rate']}-"
f"bs{config['batch_size']}-"
f"h1{config['hidden_dim_1']}-"
f"h2{config['hidden_dim_2']}-"
f"seed{config['seed']}"
)
elif config["model_name"] == "cnn":
config["run_name"] = (
f"cnn-lr{config['learning_rate']}-"
f"bs{config['batch_size']}-"
f"c1{config['cnn_channels_1']}-"
f"c2{config['cnn_channels_2']}-"
f"seed{config['seed']}"
)
else:
raise ValueError(f"Unsupported model_name: {config['model_name']}")
return config
# ============================================================
# SETUP
# ============================================================
def seed_everything(seed: int = 42):
"""
Set random seeds for reproducibility.
"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def get_device():
"""
Choose device in priority order:
CUDA > MPS > CPU
"""
if torch.cuda.is_available():
device = torch.device("cuda")
elif torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
return device
# ============================================================
# DATA
# ============================================================
def build_dataloaders(config):
transform = transforms.ToTensor()
train_dataset = datasets.FashionMNIST(
root=config["data_dir"],
train=True,
download=True,
transform=transform,
)
test_dataset = datasets.FashionMNIST(
root=config["data_dir"],
train=False,
download=True,
transform=transform,
)
train_loader = DataLoader(
train_dataset,
batch_size=config["batch_size"],
shuffle=True,
)
test_loader = DataLoader(
test_dataset,
batch_size=config["batch_size"],
shuffle=False,
)
return train_loader, test_loader
# ============================================================
# MODEL
# ============================================================
class SimpleCNN(nn.Module):
def __init__(self, num_classes=10, c1=32, c2=64):
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(1, c1, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2), # 28x28 -> 14x14
nn.Conv2d(c1, c2, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2), # 14x14 -> 7x7
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(c2 * 7 * 7, 128),
nn.ReLU(),
nn.Linear(128, num_classes),
)
def forward(self, x):
x = self.features(x)
x = self.classifier(x)
return x
def build_model(config):
if config["model_name"] == "mlp":
model = nn.Sequential(
nn.Flatten(),
nn.Linear(28 * 28, config["hidden_dim_1"]),
nn.ReLU(),
nn.Linear(config["hidden_dim_1"], config["hidden_dim_2"]),
nn.ReLU(),
nn.Linear(config["hidden_dim_2"], config["num_classes"]),
)
elif config["model_name"] == "cnn":
model = SimpleCNN(
num_classes=config["num_classes"],
c1=config["cnn_channels_1"],
c2=config["cnn_channels_2"],
)
else:
raise ValueError(f"Unsupported model_name: {config['model_name']}")
return model
# ============================================================
# TRAIN
# ============================================================
def train_one_epoch(model, dataloader, loss_fn, optimizer, device, epoch):
model.train()
total_loss = 0.0
total_samples = 0
for batch_idx, (images, labels) in enumerate(dataloader):
images = images.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = loss_fn(outputs, labels)
loss.backward()
optimizer.step()
batch_size = images.size(0)
total_loss += loss.item() * batch_size
total_samples += batch_size
if batch_idx % 100 == 0:
# wandb.log() streams a dict of metrics to the W&B dashboard.
# Keys with a "/" prefix (e.g. "train/") create sub-sections in the UI.
# Each call auto-increments an internal step counter used on the x-axis.
wandb.log(
{
"train/batch_loss": loss.item(),
"train/epoch": epoch,
"train/batch_idx": batch_idx,
}
)
return total_loss / total_samples
# ============================================================
# EVAL
# ============================================================
@torch.no_grad()
def evaluate(model, dataloader, loss_fn, device):
model.eval()
total_loss = 0.0
total_correct = 0
total_samples = 0
for images, labels in dataloader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
loss = loss_fn(outputs, labels)
batch_size = images.size(0)
total_loss += loss.item() * batch_size
total_samples += batch_size
predictions = outputs.argmax(dim=1)
total_correct += (predictions == labels).sum().item()
avg_loss = total_loss / total_samples
accuracy = total_correct / total_samples
return avg_loss, accuracy
@torch.no_grad()
def log_vision_predictions(model, dataloader, device, max_samples=24, max_misclassified=24):
model.eval()
all_true = []
all_pred = []
sample_rows = []
misclassified_rows = []
for images, labels in dataloader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
predictions = outputs.argmax(dim=1)
all_true.extend(labels.cpu().tolist())
all_pred.extend(predictions.cpu().tolist())
images_cpu = images.cpu()
labels_cpu = labels.cpu()
predictions_cpu = predictions.cpu()
for i in range(images_cpu.size(0)):
true_idx = labels_cpu[i].item()
pred_idx = predictions_cpu[i].item()
# wandb.Image() wraps a tensor/PIL image for rich media logging.
# The caption is overlaid in the W&B media browser.
image_for_wandb = wandb.Image(
images_cpu[i],
caption=f"true={CLASS_NAMES[true_idx]}, pred={CLASS_NAMES[pred_idx]}"
)
if len(sample_rows) < max_samples:
sample_rows.append([
image_for_wandb,
CLASS_NAMES[true_idx],
CLASS_NAMES[pred_idx],
true_idx == pred_idx,
])
if true_idx != pred_idx and len(misclassified_rows) < max_misclassified:
misclassified_rows.append([
image_for_wandb,
CLASS_NAMES[true_idx],
CLASS_NAMES[pred_idx],
])
if len(sample_rows) >= max_samples and len(misclassified_rows) >= max_misclassified:
break
# wandb.Table() creates an interactive table in the W&B UI.
# You can filter, sort, and view embedded media (images) directly in the table.
sample_table = wandb.Table(
columns=["image", "true_label", "pred_label", "correct"],
data=sample_rows,
)
misclassified_table = wandb.Table(
columns=["image", "true_label", "pred_label"],
data=misclassified_rows,
)
# wandb.plot.confusion_matrix() is a built-in chart — no custom code needed.
# It renders an interactive heatmap in the W&B dashboard.
confusion = wandb.plot.confusion_matrix(
preds=all_pred,
y_true=all_true,
class_names=CLASS_NAMES,
title="Test Confusion Matrix",
)
# Log all media in one call so they share the same step in the timeline.
wandb.log({
"eval/sample_predictions": sample_table,
"eval/misclassified_examples": misclassified_table,
"eval/confusion_matrix": confusion,
})
def main():
# ----------------------------
# config / setup
# ----------------------------
config = get_config()
seed_everything(config["seed"])
device = get_device()
print(f"Using device: {device}")
print(f"Group name: {config['group_name']}")
print(f"Run name: {config['run_name']}")
# wandb.init() creates a new run inside the given project.
# project – groups all runs of this experiment in the W&B UI.
# name – human-readable label for this specific run.
# group – lets you visually cluster related runs (e.g. mlp vs cnn).
# config – uploads the hyperparameter dict; accessible as wandb.config.
wandb.init(
project=config["project_name"],
name=config["run_name"],
group=config["group_name"],
config=config,
)
# Add any values not known at init time (here: which device was selected).
# allow_val_change=True is required when updating a key that already exists.
wandb.config.update({"device": str(device)}, allow_val_change=True)
checkpoint_dir = os.path.join(config["checkpoint_dir"], config["model_name"])
os.makedirs(checkpoint_dir, exist_ok=True)
# ----------------------------
# data
# ----------------------------
train_loader, test_loader = build_dataloaders(config)
# ----------------------------
# model
# ----------------------------
model = build_model(config).to(device)
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=config["learning_rate"])
# wandb.watch() hooks into PyTorch to automatically record:
# log="all" – both gradients AND parameter tensors each step.
# log_freq=100 – only record every 100 optimizer steps (reduces overhead).
# Remove this call if you only care about scalar metrics.
wandb.watch(model, loss_fn, log="all", log_freq=100)
# ----------------------------
# train / eval loop
# ----------------------------
best_accuracy = 0.0
for epoch in range(1, config["epochs"] + 1):
train_loss = train_one_epoch(
model=model,
dataloader=train_loader,
loss_fn=loss_fn,
optimizer=optimizer,
device=device,
epoch=epoch,
)
val_loss, val_accuracy = evaluate(
model=model,
dataloader=test_loader,
loss_fn=loss_fn,
device=device,
)
print(
f"Epoch [{epoch}/{config['epochs']}] "
f"train_loss={train_loss:.4f} "
f"val_loss={val_loss:.4f} "
f"val_accuracy={val_accuracy:.4f}"
)
# Log epoch-level metrics alongside the step-level batch metrics above.
# Using the same wandb.log() call keeps all metrics on a shared x-axis.
wandb.log(
{
"epoch": epoch,
"train/epoch_loss": train_loss,
"val/loss": val_loss,
"val/accuracy": val_accuracy,
}
)
if val_accuracy > best_accuracy:
best_accuracy = val_accuracy
best_model_path = os.path.join(checkpoint_dir, "best_model.pt")
torch.save(model.state_dict(), best_model_path)
# wandb.run.summary stores single "final" values for a run.
# Unlike wandb.log(), summary values are not time-series — they appear
# in the runs table so you can sort/filter all runs by best metric.
wandb.run.summary["best_val_accuracy"] = best_accuracy
print(f"Saved best model to: {best_model_path}")
# ----------------------------
# final save
# ----------------------------
final_model_path = os.path.join(checkpoint_dir, "final_model.pt")
torch.save(model.state_dict(), final_model_path)
print(f"Saved final model to: {final_model_path}")
log_vision_predictions(
model=model,
dataloader=test_loader,
device=device,
max_samples=24,
max_misclassified=24,
)
# wandb.save() uploads matched files as artifacts attached to this run.
# They will be available for download from the W&B run page.
wandb.save(os.path.join(checkpoint_dir, "*.pt"))
# wandb.finish() finalises the run: flushes remaining logs, marks it
# as "finished" in the dashboard, and closes the background sync thread.
# Always call this at the end of a script (or use wandb.init() as a context manager).
wandb.finish()
if __name__ == "__main__":
main()