-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
176 lines (144 loc) · 5.49 KB
/
training.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from argparse import ArgumentParser
import pytorch_lightning as pl
import timm
import torch
import torch.nn as nn
import torchmetrics
import torchvision.transforms as T
from sklearn.model_selection import train_test_split
from torchvision.datasets import MNIST
class Classifier(pl.LightningModule):
"""General class for the model."""
def __init__(
self, learning_rate: float = 0.001, model_name="mobilenetv3_rw", num_classes=10
):
"""Initialize the model."""
super().__init__()
self.model_name = model_name
self.learning_rate = learning_rate
self.model = timm.create_model(
model_name, pretrained=True, num_classes=num_classes, in_chans=1
)
self.loss = nn.CrossEntropyLoss()
self.train_acc = torchmetrics.Accuracy()
self.val_acc = torchmetrics.Accuracy()
self.test_acc = torchmetrics.Accuracy()
self.test_f1 = torchmetrics.F1(num_classes, average="weighted")
self.test_cm = torchmetrics.ConfusionMatrix(num_classes)
@staticmethod
def add_argparse_args(parent_parser):
"""Argument parser for model."""
parser = parent_parser.add_argument_group("Classifier")
parser.add_argument("--learning_rate", type=float, default=0.0005)
parser.add_argument("--model_name",type=str,default="mobilenetv3_rw")
return parent_parser
def forward(self, x):
"""Forward pass."""
return self.model(x)
def _step(self, batch, name):
"""General step."""
inputs, targets = batch
outputs = self.model(inputs)
loss = self.loss(outputs, targets)
_, preds = torch.max(outputs, 1)
if name == "train":
self.log("train_loss", loss)
self.train_acc(preds, targets)
self.log(
"train_acc", self.train_acc, prog_bar=True, on_step=True, on_epoch=False
)
elif name == "val":
self.val_acc(preds, targets)
self.log(
"val_loss",
loss,
on_step=False,
on_epoch=True,
)
self.log(
"val_acc",
self.val_acc,
prog_bar=True,
on_step=False,
on_epoch=True,
)
else:
raise ValueError(f"Invalid step name given: {name}")
return loss
def training_step(self, batch, batch_idx):
"""Training step."""
return self._step(batch, "train")
def validation_step(self, batch, batch_idx):
"""Validation step."""
return self._step(batch, "val")
def test_step(self, batch, batch_idx):
"""Test step."""
inputs, targets = batch
outputs = self.model(inputs)
loss = self.loss(outputs, targets)
_, preds = torch.max(outputs, 1)
self.test_acc(preds, targets)
self.test_f1(preds, targets)
self.test_cm(preds, targets)
return loss
def test_epoch_end(self, outputs):
"""Metrics calculation at th end of the training."""
self.log("test_acc", self.test_acc.compute(), sync_dist=True)
self.log("test_f1", self.test_f1.compute(), sync_dist=True)
print("Confusion matrix:")
print(self.test_cm.compute().cpu().numpy())
def configure_optimizers(self):
"""Optimizer settings."""
optimizer = torch.optim.AdamW(self.parameters(), lr=self.learning_rate)
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=[100, 150], gamma=0.1
)
return [optimizer], [scheduler]
class MNISTDataModule(pl.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
)
val_dataset, _ = train_test_split(val_dataset, test_size=0.5)
return torch.utils.data.DataLoader(
val_dataset, batch_size=self.hparams.batch_size
)
def test_dataloader(self):
test_dataset = MNIST(
"./data", train=False, download=False, transform=self.transform
)
_, test_dataset = train_test_split(test_dataset, test_size=0.5)
return torch.utils.data.DataLoader(
test_dataset, batch_size=self.hparams.batch_size
)
if __name__ == "__main__":
parser = ArgumentParser()
parser = pl.Trainer.add_argparse_args(parser)
parser = Classifier.add_argparse_args(parser)
args = parser.parse_args()
checkpoint_callback = pl.callbacks.ModelCheckpoint(
dirpath="lightning_logs",
filename=f"MNIST_classifier_{args.model_name}" + "{epoch}-{val_loss:.2f}",
monitor="val_acc",
mode="max",
)
dm = MNISTDataModule()
model = Classifier(learning_rate=args.learning_rate,model_name=args.model_name)
trainer = pl.Trainer.from_argparse_args(args, callbacks=[checkpoint_callback])
trainer.fit(model, dm)
trainer.test(model, dm)