This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
train_script.py
101 lines (81 loc) · 3.05 KB
/
train_script.py
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
import os
import torch
import torchvision.transforms as T
from lightning import LightningModule, LightningDataModule
from lightning.pytorch.cli import LightningCLI
from torch import nn
from torch.nn import functional as F
from torchvision.datasets import MNIST
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout(0.25)
self.dropout2 = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
return output
class ImageClassifier(LightningModule):
def __init__(self, model=None, lr=1.0, gamma=0.7, batch_size=32):
super().__init__()
self.save_hyperparameters(ignore="model")
self.model = model or Net()
checkpoint_path = os.path.join(os.path.dirname(__file__), "demo_weights.pt")
if os.path.exists(checkpoint_path):
self.load_state_dict(torch.load(checkpoint_path).state_dict())
@property
def example_input_array(self):
return torch.zeros((1, 1, 28, 28))
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, y = batch
logits = self.forward(x)
loss = F.nll_loss(logits, y.long())
self.log("train_loss", loss, on_step=True, on_epoch=True)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
logits = self.forward(x)
loss = F.nll_loss(logits, y.long())
self.log("val_loss", loss)
def configure_optimizers(self):
return torch.optim.Adadelta(self.model.parameters(), lr=self.hparams.lr)
class MNISTDataModule(LightningDataModule):
def __init__(self, batch_size=32):
super().__init__()
self.save_hyperparameters()
@property
def transform(self):
return T.Compose([T.ToTensor(), T.Normalize((0.1307,), (0.3081,))])
def prepare_data(self) -> None:
MNIST("./data", download=True)
def train_dataloader(self):
train_dataset = MNIST("./data", train=True, download=False, transform=self.transform)
return torch.utils.data.DataLoader(train_dataset, batch_size=self.hparams.batch_size)
def val_dataloader(self):
val_dataset = MNIST("./data", train=False, download=False, transform=self.transform)
return torch.utils.data.DataLoader(val_dataset, batch_size=self.hparams.batch_size)
if __name__ == "__main__":
cli = LightningCLI(
ImageClassifier,
MNISTDataModule,
seed_everything_default=42,
save_config_kwargs={"overwrite": True},
run=False,
)
cli.trainer.fit(cli.model, datamodule=cli.datamodule)