add template
This commit is contained in:
1 parent
b76bb9a271
commit
4be05dd4f9
5 files changed
+277
No files matched your search
@@ -0,0 +1,13 @@
|
||||
from pytorch_lightning.callbacks import Callback
|
||||
|
||||
|
||||
class ExampleCallback(Callback):
|
||||
|
||||
def on_init_start(self, trainer):
|
||||
print('Starting to init trainer!')
|
||||
|
||||
def on_init_end(self, trainer):
|
||||
print('Trainer is init now')
|
||||
|
||||
def on_train_end(self, trainer, pl_module):
|
||||
print('Do something when training ends')
|
||||
@@ -0,0 +1,45 @@
|
||||
# pytorch
|
||||
from torch.utils.data import DataLoader, random_split
|
||||
|
||||
# pytorch lightning
|
||||
import pytorch_lightning as pl
|
||||
|
||||
# torchvision
|
||||
from torchvision.datasets import MNIST
|
||||
from torchvision import transforms
|
||||
|
||||
|
||||
class MNISTDataModule(pl.LightningDataModule):
|
||||
|
||||
def __init__(self, data_dir='data/mnist', batch_size=256):
|
||||
super().__init__()
|
||||
self.data_dir = data_dir
|
||||
self.batch_size = batch_size
|
||||
self.transform = transforms.ToTensor()
|
||||
|
||||
self.data_train = None
|
||||
self.data_val = None
|
||||
self.data_test = None
|
||||
|
||||
def prepare_data(self):
|
||||
# download data
|
||||
MNIST(self.data_dir, train=True, download=True)
|
||||
MNIST(self.data_dir, train=False, download=True)
|
||||
|
||||
def setup(self, stage=None):
|
||||
# we set up only relevant datasets when stage is specified (automatically set by Pytorch-Lightning)
|
||||
if stage == 'fit' or stage is None:
|
||||
mnist_train = MNIST(self.data_dir, train=True, transform=self.transform)
|
||||
self.data_train, self.data_val = random_split(mnist_train, [55000, 5000])
|
||||
|
||||
if stage == 'test' or stage is None:
|
||||
self.data_test = MNIST(self.data_dir, train=False, transform=self.transform)
|
||||
|
||||
def train_dataloader(self):
|
||||
return DataLoader(self.data_train, batch_size=self.batch_size)
|
||||
|
||||
def val_dataloader(self):
|
||||
return DataLoader(self.data_val, batch_size=self.batch_size)
|
||||
|
||||
def test_dataloader(self):
|
||||
return DataLoader(self.data_test, batch_size=self.batch_size)
|
||||
@@ -0,0 +1,68 @@
|
||||
# pytorch
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
# pytorch lightning
|
||||
import pytorch_lightning as pl
|
||||
from pytorch_lightning.metrics.functional import accuracy, precision, recall, f1_score, fbeta_score
|
||||
|
||||
# custom models
|
||||
from utils.models import ModelMNISTv1, ModelMNISTv2
|
||||
|
||||
|
||||
class LitModel(pl.LightningModule):
|
||||
|
||||
def __init__(self, config):
|
||||
super().__init__()
|
||||
|
||||
self.save_hyperparameters(config)
|
||||
self.model = ModelMNISTv1(config=self.hparams.model)
|
||||
# self.model = ModelMNISTv2(config=self.hparams.model)
|
||||
|
||||
def forward(self, data):
|
||||
return self.model(data)
|
||||
|
||||
# logic for a single training step
|
||||
def training_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
logits = self.model(x)
|
||||
loss = F.nll_loss(logits, y)
|
||||
|
||||
# training metrics
|
||||
preds = torch.argmax(logits, dim=1)
|
||||
acc = accuracy(preds, y)
|
||||
self.log('train_loss', loss, on_epoch=True, logger=True)
|
||||
self.log('train_acc', acc, on_epoch=True, logger=True)
|
||||
|
||||
return loss
|
||||
|
||||
# logic for a single validation step
|
||||
def validation_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
logits = self.model(x)
|
||||
loss = F.nll_loss(logits, y)
|
||||
|
||||
# validation metrics
|
||||
preds = torch.argmax(logits, dim=1)
|
||||
acc = accuracy(preds, y)
|
||||
self.log('val_loss', loss, prog_bar=True, logger=True)
|
||||
self.log('val_acc', acc, prog_bar=True, logger=True)
|
||||
|
||||
return loss
|
||||
|
||||
# logic for a single testing step
|
||||
def test_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
logits = self.model(x)
|
||||
loss = F.nll_loss(logits, y)
|
||||
|
||||
# test metrics
|
||||
preds = torch.argmax(logits, dim=1)
|
||||
acc = accuracy(preds, y)
|
||||
self.log('test_loss', loss, prog_bar=True, logger=True)
|
||||
self.log('test_acc', acc, prog_bar=True, logger=True)
|
||||
|
||||
return loss
|
||||
|
||||
def configure_optimizers(self):
|
||||
return torch.optim.Adam(self.parameters(), lr=self.hparams.learning_rate)
|
||||
@@ -0,0 +1,53 @@
|
||||
from torch import nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
|
||||
class ModelMNISTv1(nn.Module):
|
||||
|
||||
def __init__(self, config):
|
||||
super().__init__()
|
||||
|
||||
# mnist images are (1, 28, 28) (channels, width, height)
|
||||
self.layer_1 = nn.Linear(28 * 28, 128)
|
||||
self.layer_2 = nn.Linear(128, 256)
|
||||
self.layer_3 = nn.Linear(256, 10)
|
||||
|
||||
def forward(self, x):
|
||||
batch_size, channels, width, height = x.size()
|
||||
|
||||
# (b, 1, 28, 28) -> (b, 1*28*28)
|
||||
x = x.view(batch_size, -1)
|
||||
x = self.layer_1(x)
|
||||
x = F.relu(x)
|
||||
x = self.layer_2(x)
|
||||
x = F.relu(x)
|
||||
x = self.layer_3(x)
|
||||
|
||||
return F.log_softmax(x, dim=1)
|
||||
|
||||
|
||||
class ModelMNISTv2(nn.Module):
|
||||
|
||||
def __init__(self, config):
|
||||
super().__init__()
|
||||
|
||||
# mnist images are (1, 28, 28) (channels, width, height)
|
||||
self.layer_1 = nn.Linear(28 * 28, 256)
|
||||
self.layer_2 = nn.Linear(256, 128)
|
||||
self.layer_3 = nn.Linear(128, 64)
|
||||
self.layer_4 = nn.Linear(64, 10)
|
||||
|
||||
def forward(self, x):
|
||||
batch_size, channels, width, height = x.size()
|
||||
|
||||
# (b, 1, 28, 28) -> (b, 1*28*28)
|
||||
x = x.view(batch_size, -1)
|
||||
x = self.layer_1(x)
|
||||
x = F.relu(x)
|
||||
x = self.layer_2(x)
|
||||
x = F.relu(x)
|
||||
x = self.layer_3(x)
|
||||
x = F.relu(x)
|
||||
x = self.layer_4(x)
|
||||
|
||||
return F.log_softmax(x, dim=1)
|
||||
Reference in new issue
Block a user