2021-11-15 08:57:44 +00:00
|
|
|
"""GTLVQ example using the MNIST dataset."""
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import warnings
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
import prototorch as pt
|
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
2022-05-17 10:03:43 +00:00
|
|
|
from prototorch.models import (
|
|
|
|
ImageGTLVQ,
|
|
|
|
PruneLoserPrototypes,
|
|
|
|
VisImgComp,
|
|
|
|
)
|
|
|
|
from pytorch_lightning.callbacks import EarlyStopping
|
|
|
|
from pytorch_lightning.utilities.seed import seed_everything
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
|
|
|
from torch.utils.data import DataLoader
|
2021-11-15 08:50:33 +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-11-15 08:50:33 +00:00
|
|
|
if __name__ == "__main__":
|
2022-05-17 10:03:43 +00:00
|
|
|
# Reproducibility
|
|
|
|
seed_everything(seed=4)
|
|
|
|
|
2021-11-15 08:50:33 +00:00
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser = pl.Trainer.add_argparse_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# 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=0, batch_size=256)
|
|
|
|
test_loader = DataLoader(test_ds, num_workers=0, batch_size=256)
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
|
|
|
num_classes = 10
|
|
|
|
prototypes_per_class = 1
|
|
|
|
hparams = dict(
|
|
|
|
input_dim=28 * 28,
|
|
|
|
latent_dim=28,
|
|
|
|
distribution=(num_classes, prototypes_per_class),
|
|
|
|
proto_lr=0.01,
|
|
|
|
bb_lr=0.01,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = ImageGTLVQ(
|
2021-11-15 08:50:33 +00:00
|
|
|
hparams,
|
|
|
|
optimizer=torch.optim.Adam,
|
|
|
|
prototypes_initializer=pt.initializers.SMCI(train_ds),
|
2021-11-15 08:57:44 +00:00
|
|
|
#Use one batch of data for subspace initiator.
|
|
|
|
omega_initializer=pt.initializers.PCALinearTransformInitializer(
|
|
|
|
next(iter(train_loader))[0].reshape(256, 28 * 28)))
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisImgComp(
|
2021-11-15 08:50:33 +00:00
|
|
|
data=train_ds,
|
|
|
|
num_columns=10,
|
|
|
|
show=False,
|
|
|
|
tensorboard=True,
|
|
|
|
random_data=100,
|
|
|
|
add_embedding=True,
|
|
|
|
embedding_data=200,
|
|
|
|
flatten_data=False,
|
|
|
|
)
|
2022-05-17 10:03:43 +00:00
|
|
|
pruning = PruneLoserPrototypes(
|
2021-11-15 08:50:33 +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-11-15 08:50:33 +00:00
|
|
|
monitor="train_loss",
|
|
|
|
min_delta=0.001,
|
|
|
|
patience=15,
|
|
|
|
mode="min",
|
|
|
|
check_on_train_epoch_end=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Setup trainer
|
|
|
|
# using GPUs here is strongly recommended!
|
|
|
|
trainer = pl.Trainer.from_argparse_args(
|
|
|
|
args,
|
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
pruning,
|
2022-05-17 10:03:43 +00:00
|
|
|
es,
|
2021-11-15 08:50:33 +00:00
|
|
|
],
|
2022-05-17 10:03:43 +00:00
|
|
|
max_epochs=1000,
|
|
|
|
log_every_n_steps=1,
|
|
|
|
detect_anomaly=True,
|
2021-11-15 08:50:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|