-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist.py
More file actions
153 lines (131 loc) · 4.81 KB
/
mnist.py
File metadata and controls
153 lines (131 loc) · 4.81 KB
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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets
import torchvision.transforms as T
import torch.nn.functional as F
from torchabc import TorchABC
from functools import cached_property, partial
class MNISTClassifier(TorchABC):
"""A simple convolutional neural network for classifying MNIST digits."""
@cached_property
def dataloaders(self):
train_dataset = datasets.MNIST(
'./data',
train=True,
download=True,
transform=partial(self.preprocess, hparams=self.hparams, flag='train')
)
val_dataset = datasets.MNIST(
'./data',
train=False,
download=True,
transform=partial(self.preprocess, hparams=self.hparams, flag='val')
)
return {
'train': DataLoader(
dataset=train_dataset,
shuffle=True,
batch_size=self.hparams['dataloaders']['batch_size'],
num_workers=self.hparams['dataloaders']['num_workers'],
collate_fn=self.collate
),
'val': DataLoader(
dataset=val_dataset,
shuffle=False,
batch_size=self.hparams['dataloaders']['batch_size'],
collate_fn=self.collate
)
}
@staticmethod
def preprocess(sample, hparams, flag=''):
transform = T.Compose([
T.ToTensor(),
T.Normalize((0.1307,), (0.3081,)),
T.RandomPerspective(
# perform data augmentation when training
p=hparams['preprocess']['p'] if flag == 'train' else 0,
distortion_scale=hparams['preprocess']['distortion']
)
])
return transform(sample)
@staticmethod
def collate(samples):
return torch.utils.data.default_collate(samples)
@cached_property
def network(self):
class SimpleCNN(nn.Module):
def __init__(self, hparams):
super().__init__()
h1, h2, h3 = hparams['network']['hidden_dims']
self.cnn = nn.Sequential(
nn.Conv2d(1, h1, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(h1, h2, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Flatten(),
nn.Linear(h2 * 7 * 7, h3),
nn.ReLU(),
nn.Linear(h3, 10)
)
def forward(self, x):
return self.cnn(x)
return SimpleCNN(self.hparams)
@cached_property
def optimizer(self):
return torch.optim.Adam(self.network.parameters(), lr=self.hparams['optimizer']['lr'])
@staticmethod
def loss(outputs, targets, hparams):
return {
"loss": F.cross_entropy(outputs, targets),
"y_pred": torch.argmax(outputs, dim=1),
"y_true": targets,
}
@staticmethod
def metrics(batches, hparams):
loss = torch.stack([batch['loss'] for batch in batches])
y_pred = torch.cat([batch['y_pred'] for batch in batches])
y_true = torch.cat([batch['y_true'] for batch in batches])
return {
"loss": loss.mean().item(),
"accuracy": (y_pred == y_true).float().mean().item(),
}
def checkpoint(self, epoch, metrics, out):
if epoch == 1 or metrics["accuracy"] > self.best_accuracy:
self.best_accuracy = metrics["accuracy"]
self.save(out)
@staticmethod
def postprocess(outputs, hparams):
return torch.argmax(outputs, dim=1).cpu().numpy().tolist()
if __name__ == "__main__":
# set up hyperparameters
hparams = {
'dataloaders': {
'batch_size': 64,
'num_workers': 2,
},
'preprocess': {
'p': 0.5,
'distortion': 0.1,
},
'network': {
'hidden_dims': (32, 64, 128),
},
'optimizer': {
'lr': 0.001,
}
}
# initialize model
model = MNISTClassifier(hparams=hparams)
# training and validation with checkpoint selection
model.train(epochs=3, on='train', val='val', out="mnist.pth")
# load selected checkpoint
model.load("mnist.pth")
# read raw data
model.dataloaders['val'].dataset.transform = None # disable transform
img, cls = model.dataloaders['val'].dataset[0] # read PIL image and label
# inference from raw data
pred = model([img]) # predict from PIL image
print(f"Prediction {pred[0]} | Target {cls}") # print results