2021-05-12 14:36:22 +00:00
|
|
|
"""GMLVQ example using the MNIST dataset."""
|
|
|
|
|
2021-05-18 08:17:51 +00:00
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import warnings
|
2021-05-18 08:17:51 +00:00
|
|
|
|
2021-06-21 12:42:28 +00:00
|
|
|
import prototorch as pt
|
2021-05-12 14:36:22 +00:00
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
2023-06-20 15:30:21 +00:00
|
|
|
from lightning_fabric.utilities.seed import seed_everything
|
2022-05-17 10:03:43 +00:00
|
|
|
from prototorch.models import (
|
|
|
|
ImageGMLVQ,
|
|
|
|
PruneLoserPrototypes,
|
|
|
|
VisImgComp,
|
|
|
|
)
|
|
|
|
from pytorch_lightning.callbacks import EarlyStopping
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
|
|
|
from torch.utils.data import DataLoader
|
2021-05-12 14:36:22 +00:00
|
|
|
from torchvision import transforms
|
|
|
|
from torchvision.datasets import MNIST
|
|
|
|
|
2022-05-17 10:03:43 +00:00
|
|
|
warnings.filterwarnings("ignore", category=PossibleUserWarning)
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning)
|
|
|
|
|
2021-05-12 14:36:22 +00:00
|
|
|
if __name__ == "__main__":
|
2022-05-17 10:03:43 +00:00
|
|
|
# Reproducibility
|
|
|
|
seed_everything(seed=4)
|
2021-05-18 08:17:51 +00:00
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
2023-06-20 15:30:21 +00:00
|
|
|
parser.add_argument("--gpus", type=int, default=0)
|
|
|
|
parser.add_argument("--fast_dev_run", type=bool, default=False)
|
2021-05-18 08:17:51 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-05-12 14:36:22 +00:00
|
|
|
# Dataset
|
|
|
|
train_ds = MNIST(
|
|
|
|
"~/datasets",
|
|
|
|
train=True,
|
|
|
|
download=True,
|
|
|
|
transform=transforms.Compose([
|
|
|
|
transforms.ToTensor(),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
test_ds = MNIST(
|
|
|
|
"~/datasets",
|
|
|
|
train=False,
|
|
|
|
download=True,
|
|
|
|
transform=transforms.Compose([
|
|
|
|
transforms.ToTensor(),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Dataloaders
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, num_workers=4, batch_size=256)
|
|
|
|
test_loader = DataLoader(test_ds, num_workers=4, batch_size=256)
|
2021-05-12 14:36:22 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
2021-05-25 13:41:10 +00:00
|
|
|
num_classes = 10
|
2021-06-04 20:21:28 +00:00
|
|
|
prototypes_per_class = 10
|
2021-05-12 14:36:22 +00:00
|
|
|
hparams = dict(
|
|
|
|
input_dim=28 * 28,
|
|
|
|
latent_dim=28 * 28,
|
2021-05-25 13:41:10 +00:00
|
|
|
distribution=(num_classes, prototypes_per_class),
|
2021-05-18 08:17:51 +00:00
|
|
|
proto_lr=0.01,
|
|
|
|
bb_lr=0.01,
|
2021-05-12 14:36:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = ImageGMLVQ(
|
2021-05-12 14:36:22 +00:00
|
|
|
hparams,
|
|
|
|
optimizer=torch.optim.Adam,
|
2021-06-21 12:42:28 +00:00
|
|
|
prototypes_initializer=pt.initializers.SMCI(train_ds),
|
2021-05-12 14:36:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisImgComp(
|
2021-05-18 08:17:51 +00:00
|
|
|
data=train_ds,
|
2021-06-04 20:21:28 +00:00
|
|
|
num_columns=10,
|
2021-05-18 08:17:51 +00:00
|
|
|
show=False,
|
|
|
|
tensorboard=True,
|
2021-06-04 20:21:28 +00:00
|
|
|
random_data=100,
|
2021-05-20 14:07:16 +00:00
|
|
|
add_embedding=True,
|
2021-06-04 20:21:28 +00:00
|
|
|
embedding_data=200,
|
2021-05-20 14:07:16 +00:00
|
|
|
flatten_data=False,
|
2021-05-18 08:17:51 +00:00
|
|
|
)
|
2022-05-17 10:03:43 +00:00
|
|
|
pruning = PruneLoserPrototypes(
|
2021-06-04 20:21:28 +00:00
|
|
|
threshold=0.01,
|
|
|
|
idle_epochs=1,
|
|
|
|
prune_quota_per_epoch=10,
|
|
|
|
frequency=1,
|
|
|
|
verbose=True,
|
|
|
|
)
|
2022-05-17 10:03:43 +00:00
|
|
|
es = EarlyStopping(
|
2021-06-04 20:21:28 +00:00
|
|
|
monitor="train_loss",
|
|
|
|
min_delta=0.001,
|
|
|
|
patience=15,
|
|
|
|
mode="min",
|
|
|
|
check_on_train_epoch_end=True,
|
|
|
|
)
|
2021-05-12 14:36:22 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2023-06-20 15:30:21 +00:00
|
|
|
trainer = pl.Trainer(
|
|
|
|
accelerator="cuda" if args.gpus else "cpu",
|
|
|
|
devices=args.gpus if args.gpus else "auto",
|
|
|
|
fast_dev_run=args.fast_dev_run,
|
2021-06-04 20:21:28 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
pruning,
|
2022-05-17 10:03:43 +00:00
|
|
|
es,
|
2021-06-04 20:21:28 +00:00
|
|
|
],
|
2022-05-17 10:03:43 +00:00
|
|
|
max_epochs=1000,
|
|
|
|
log_every_n_steps=1,
|
|
|
|
detect_anomaly=True,
|
2021-05-12 14:36:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|