-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathch13_part3_lightning.py
285 lines (163 loc) · 6.88 KB
/
ch13_part3_lightning.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# coding: utf-8
from pkg_resources import parse_version
import sys
from python_environment_check import check_packages
import pytorch_lightning as pl
import torch
import torch.nn as nn
from torchmetrics import __version__ as torchmetrics_version
from torchmetrics import Accuracy
from torch.utils.data import DataLoader
from torch.utils.data import random_split
from torchvision.datasets import MNIST
from torchvision import transforms
from pytorch_lightning.callbacks import ModelCheckpoint
# # Machine Learning with PyTorch and Scikit-Learn
# # -- Code Examples
# ## Package version checks
# Add folder to path in order to load from the check_packages.py script:
sys.path.insert(0, '..')
# Check recommended package versions:
d = {
'torch': '1.8',
'torchvision': '0.9.0',
'tensorboard': '2.7.0',
'pytorch_lightning': '1.5.0',
'torchmetrics': '0.6.2'
}
check_packages(d)
# # Chapter 13: Going Deeper -- the Mechanics of PyTorch (Part 3/3)
# **Outline**
#
# - [Higher-level PyTorch APIs: a short introduction to PyTorch Lightning](#Higher-level-PyTorch-APIs-a-short-introduction-to-PyTorch-Lightning)
# - [Setting up the PyTorch Lightning model](#Setting-up-the-PyTorch-Lightning-model)
# - [Setting up the data loaders for Lightning](#Setting-up-the-data-loaders-for-Lightning)
# - [Training the model using the PyTorch Lightning Trainer class](#Training-the-model-using-the-PyTorch-Lightning-Trainer-class)
# - [Evaluating the model using TensorBoard](#Evaluating-the-model-using-TensorBoard)
# - [Summary](#Summary)
# ## Higher-level PyTorch APIs: a short introduction to PyTorch Lightning
# ### Setting up the PyTorch Lightning model
# ## Higher-level PyTorch APIs: a short introduction to PyTorch Lightning
# ### Setting up the PyTorch Lightning model
class MultiLayerPerceptron(pl.LightningModule):
def __init__(self, image_shape=(1, 28, 28), hidden_units=(32, 16)):
super().__init__()
# new PL attributes:
if parse_version(torchmetrics_version) > parse_version(0.8):
self.train_acc = Accuracy(task="multiclass", num_classes=10)
self.valid_acc = Accuracy(task="multiclass", num_classes=10)
self.test_acc = Accuracy(task="multiclass", num_classes=10)
else:
self.train_acc = Accuracy()
self.valid_acc = Accuracy()
self.test_acc = Accuracy()
# Model similar to previous section:
input_size = image_shape[0] * image_shape[1] * image_shape[2]
all_layers = [nn.Flatten()]
for hidden_unit in hidden_units:
layer = nn.Linear(input_size, hidden_unit)
all_layers.append(layer)
all_layers.append(nn.ReLU())
input_size = hidden_unit
all_layers.append(nn.Linear(hidden_units[-1], 10))
self.model = nn.Sequential(*all_layers)
def forward(self, x):
x = self.model(x)
return x
def training_step(self, batch, batch_idx):
x, y = batch
logits = self(x)
loss = nn.functional.cross_entropy(logits, y)
preds = torch.argmax(logits, dim=1)
self.train_acc.update(preds, y)
self.log("train_loss", loss, prog_bar=True)
return loss
def training_epoch_end(self, outs):
self.log("train_acc", self.train_acc.compute())
self.train_acc.reset()
def validation_step(self, batch, batch_idx):
x, y = batch
logits = self(x)
loss = nn.functional.cross_entropy(logits, y)
preds = torch.argmax(logits, dim=1)
self.valid_acc.update(preds, y)
self.log("valid_loss", loss, prog_bar=True)
return loss
def validation_epoch_end(self, outs):
self.log("valid_acc", self.valid_acc.compute(), prog_bar=True)
self.valid_acc.reset()
def test_step(self, batch, batch_idx):
x, y = batch
logits = self(x)
loss = nn.functional.cross_entropy(logits, y)
preds = torch.argmax(logits, dim=1)
self.test_acc.update(preds, y)
self.log("test_loss", loss, prog_bar=True)
self.log("test_acc", self.test_acc.compute(), prog_bar=True)
return loss
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=0.001)
return optimizer
# ### Setting up the data loaders
class MnistDataModule(pl.LightningDataModule):
def __init__(self, data_path='./'):
super().__init__()
self.data_path = data_path
self.transform = transforms.Compose([transforms.ToTensor()])
def prepare_data(self):
MNIST(root=self.data_path, download=True)
def setup(self, stage=None):
# stage is either 'fit', 'validate', 'test', or 'predict'
# here note relevant
mnist_all = MNIST(
root=self.data_path,
train=True,
transform=self.transform,
download=False
)
self.train, self.val = random_split(
mnist_all, [55000, 5000], generator=torch.Generator().manual_seed(1)
)
self.test = MNIST(
root=self.data_path,
train=False,
transform=self.transform,
download=False
)
def train_dataloader(self):
return DataLoader(self.train, batch_size=64, num_workers=4)
def val_dataloader(self):
return DataLoader(self.val, batch_size=64, num_workers=4)
def test_dataloader(self):
return DataLoader(self.test, batch_size=64, num_workers=4)
torch.manual_seed(1)
mnist_dm = MnistDataModule()
# ### Training the model using the PyTorch Lightning Trainer class
mnistclassifier = MultiLayerPerceptron()
callbacks = [ModelCheckpoint(save_top_k=1, mode='max', monitor="valid_acc")] # save top 1 model
if torch.cuda.is_available(): # if you have GPUs
trainer = pl.Trainer(max_epochs=10, callbacks=callbacks, gpus=1)
else:
trainer = pl.Trainer(max_epochs=10, callbacks=callbacks)
trainer.fit(model=mnistclassifier, datamodule=mnist_dm)
# ### Evaluating the model using TensorBoard
trainer.test(model=mnistclassifier, datamodule=mnist_dm, ckpt_path='best')
# Start tensorboard
path = 'lightning_logs/version_0/checkpoints/epoch=8-step=7739.ckpt'
if torch.cuda.is_available(): # if you have GPUs
trainer = pl.Trainer(
max_epochs=15, callbacks=callbacks, resume_from_checkpoint=path, gpus=1
)
else:
trainer = pl.Trainer(
max_epochs=15, callbacks=callbacks, resume_from_checkpoint=path
)
trainer.fit(model=mnistclassifier, datamodule=mnist_dm)
trainer.test(model=mnistclassifier, datamodule=mnist_dm)
trainer.test(model=mnistclassifier, datamodule=mnist_dm, ckpt_path='best')
path = "lightning_logs/version_0/checkpoints/epoch=13-step=12039.ckpt"
model = MultiLayerPerceptron.load_from_checkpoint(path)
# ## Summary
# ---
#
# Readers may ignore the next cell.