* rework lightning model for mnist

* sort imports

* add comment to pre-commit-config

* remove returns from test_step because it's bugged

* update experiment configs

* Update README.md
This commit is contained in:
Łukasz Zalewski authored and GitHub committed 2021-03-10 15:33:14 +01:00
1 parent 67d531ab67
commit b1e0bf9717
23 files changed
+480 -320

No files matched your search

+91 -56
View File
@@ -1,94 +1,129 @@
from pytorch_lightning.metrics.classification import Accuracy
from typing import Any, Dict, List, Sequence, Tuple, Union
import hydra
import pytorch_lightning as pl
import torch
import torch.nn.functional as F
import torch
from pytorch_lightning.metrics.classification import Accuracy
from torch.optim import Optimizer
# import custom architectures
from src.architectures.simple_dense_net import SimpleDenseNet
class LitModelMNIST(pl.LightningModule):
"""
This is example of LightningModule for MNIST classification.
Example of LightningModule for MNIST classification.
A LightningModule organizes your PyTorch code into 5 sections:
- Computations (init).
- Train loop (training_step)
- Validation loop (validation_step)
- Test loop (test_step)
- Optimizers (configure_optimizers)
Read the docs:
https://pytorch-lightning.readthedocs.io/en/latest/lightning_module.html
"""
def __init__(self, *args, **kwargs):
super().__init__()
# this line ensures params passed to LightningModule will be saved to ckpt
# it also allows to access params with 'self.hparams' attribute
self.save_hyperparameters()
self.accuracy = Accuracy()
self.architecture = SimpleDenseNet(hparams=self.hparams)
self.train_acc_hist = []
self.train_loss_hist = []
self.val_acc_hist = []
self.val_loss_hist = []
# loss function
self.criterion = torch.nn.CrossEntropyLoss()
def forward(self, x):
# use separate metric instance for train, val and test step
# to ensure a proper reduction over the epoch
self.train_accuracy = Accuracy()
self.val_accuracy = Accuracy()
self.test_accuracy = Accuracy()
self.metric_hist = {
"train/acc": [],
"val/acc": [],
"train/loss": [],
"val/loss": [],
}
def forward(self, x) -> torch.Tensor:
return self.architecture(x)
# logic for a single training step
def training_step(self, batch, batch_idx):
def step(self, batch) -> Dict[str, torch.Tensor]:
x, y = batch
logits = self.forward(x)
loss = F.nll_loss(logits, y)
# training metrics
loss = self.criterion(logits, y)
preds = torch.argmax(logits, dim=1)
acc = self.accuracy(preds, y)
return loss, preds, y
def training_step(self, batch: Any, batch_idx: int) -> Dict[str, torch.Tensor]:
loss, preds, targets = self.step(batch)
# log train metrics to your loggers!
acc = self.train_accuracy(preds, targets)
self.log("train/loss", loss, on_step=False, on_epoch=True, prog_bar=False)
self.log("train/acc", acc, on_step=False, on_epoch=True, prog_bar=True)
# we can return here anything and then read it in some callback or in training_epoch_end() below
return {"loss": loss, "preds": preds, "targets": y}
# we can return here dict with any tensors
# and then read it in some callback or in training_epoch_end() below
# remember to always return loss from training_step, or else backpropagation will fail!
return {"loss": loss, "preds": preds, "targets": targets}
# logic for a single validation step
def validation_step(self, batch, batch_idx):
x, y = batch
logits = self.forward(x)
loss = F.nll_loss(logits, y)
def validation_step(self, batch: Any, batch_idx: int) -> Dict[str, torch.Tensor]:
loss, preds, targets = self.step(batch)
# validation metrics
preds = torch.argmax(logits, dim=1)
acc = self.accuracy(preds, y)
# log val metrics to your loggers!
acc = self.val_accuracy(preds, targets)
self.log("val/loss", loss, on_step=False, on_epoch=True, prog_bar=False)
self.log("val/acc", acc, on_step=False, on_epoch=True, prog_bar=True)
# we can return here anything and then read it in some callback or in validation_epoch_end() below
return {"loss": loss, "preds": preds, "targets": y}
# we can return here dict with any tensors
# and then read it in some callback or in validation_epoch_end() below
return {"loss": loss, "preds": preds, "targets": targets}
# logic for a single testing step
def test_step(self, batch, batch_idx):
x, y = batch
logits = self.forward(x)
loss = F.nll_loss(logits, y)
def test_step(self, batch: Any, batch_idx: int) -> Dict[str, torch.Tensor]:
loss, preds, targets = self.step(batch)
# test metrics
preds = torch.argmax(logits, dim=1)
acc = self.accuracy(preds, y)
# log test metrics to your loggers!
acc = self.test_accuracy(preds, targets)
self.log("test/loss", loss, on_step=False, on_epoch=True)
self.log("test/acc", acc, on_step=False, on_epoch=True)
return loss
return {"loss": loss}
def training_epoch_end(self, outputs):
self.train_acc_hist.append(self.trainer.callback_metrics["train/acc"])
self.train_loss_hist.append(self.trainer.callback_metrics["train/loss"])
self.log("train/acc_best", max(self.train_acc_hist), prog_bar=False)
self.log("train/loss_best", min(self.train_loss_hist), prog_bar=False)
# [OPTIONAL METHOD]
def training_epoch_end(self, outputs: List[Any]) -> None:
# log best so far train acc and train loss
self.metric_hist["train/acc"].append(self.trainer.callback_metrics["train/acc"])
self.metric_hist["train/loss"].append(
self.trainer.callback_metrics["train/loss"]
)
self.log("train/acc_best", max(self.metric_hist["train/acc"]), prog_bar=False)
self.log("train/loss_best", min(self.metric_hist["train/loss"]), prog_bar=False)
def validation_epoch_end(self, outputs):
self.val_acc_hist.append(self.trainer.callback_metrics["val/acc"])
self.val_loss_hist.append(self.trainer.callback_metrics["val/loss"])
self.log("val/acc_best", max(self.val_acc_hist), prog_bar=False)
self.log("val/loss_best", min(self.val_loss_hist), prog_bar=False)
# [OPTIONAL METHOD]
def validation_epoch_end(self, outputs: List[Any]) -> None:
# log best so far val acc and val loss
self.metric_hist["val/acc"].append(self.trainer.callback_metrics["val/acc"])
self.metric_hist["val/loss"].append(self.trainer.callback_metrics["val/loss"])
self.log("val/acc_best", max(self.metric_hist["val/acc"]), prog_bar=False)
self.log("val/loss_best", min(self.metric_hist["val/loss"]), prog_bar=False)
def configure_optimizers(self):
if self.hparams.optimizer == "adam":
return torch.optim.Adam(
self.parameters(),
lr=self.hparams.lr,
weight_decay=self.hparams.weight_decay,
)
else:
raise Exception("Invalid optimizer name")
def configure_optimizers(
self,
) -> Union[Optimizer, Tuple[Sequence[Optimizer], Sequence[Any]]]:
"""Choose what optimizers and learning-rate schedulers to use in your optimization.
Normally you'd need one. But in the case of GANs or similar you might have multiple.
See examples here:
https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html#configure-optimizers
"""
optim = hydra.utils.instantiate(
self.hparams.optimizer, params=self.parameters()
)
return optim