-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist.py
223 lines (166 loc) · 6.43 KB
/
mnist.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
import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
from timeit import default_timer as timer
# -- HYPERPARAMETERS --
BATCH_SIZE = 32
EPOCHS = 7
LEARNING_RATE = 0.1
HIDDEN_LAYER_SHAPE = 64
DEVICE = "cuda"
ISLINEAR = False
INPUT_SHAPE = 784 #28 * 28
HIDDEN_LAYER = 1 #not used
OUTPUT_SHAPE = 10
# ----------------------
# Best setup found
# B = 32 , E = 5 , HLS = 64 , LR = 0.1
# B = 16
def accuracy_fn(y_true, y_pred):
correct = torch.eq(y_true, y_pred).sum().item()
acc = (correct / len(y_pred)) * 100
return acc
def train_time(start: float,stop: float):
print(f"\nTrain time : {(stop - start):.3f} seconds")
def train_function(model, train_batch, loss_function, optimizer):
train_loss , train_acc = 0, 0
model.train()
print("Training...")
for batch, (img , label) in enumerate(train_batch):
img, label = img.to(DEVICE), label.to(DEVICE)
pred = model(img)
loss = loss_function(pred, label)
train_loss += loss
train_acc += accuracy_fn(y_true= label, y_pred = pred.argmax(dim = 1))
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss /= len(train_batch)
train_acc /= len(train_batch)
print("Training result:")
print(f"Train loss: {train_loss:.5f} | Train acc: {train_acc:.2f}%")
print("--------------------\n")
def test_function(model, test_batch, loss_function):
test_loss, test_acc = 0, 0
model.eval()
with torch.inference_mode():
print("Testing...")
for img, label in test_batch:
img, label = img.to(DEVICE), label.to(DEVICE)
pred = model(img)
test_loss += loss_function(pred, label)
test_acc += accuracy_fn(y_true = label, y_pred = pred.argmax(dim = 1))
test_loss /= len(test_batch)
test_acc /= len(test_batch)
print("Testing result:")
print(f"Test loss: {test_loss:.5f} | Test acc: {test_acc:.2f}%")
print("--------------------\n")
def final_model_evaluation(model,test_data):
model.eval()
with torch.inference_mode():
randindx = torch.randint(0,len(test_data), size = [1]).item()
img, label = test_data[randindx]
plt.imshow(img.squeeze(), cmap="gray")
plt.axis(False)
output = model(img.to(DEVICE))
prediction = output.argmax(dim = 1, keepdim = True).item()
print(f"The Model say... {prediction}!")
print(f"The label say... {label}!")
if( prediction == label):
print(f"The model is correct!")
else:
print(f"The model was wrong :( ")
print("\n")
train_data = torchvision.datasets.MNIST(
root = "dataset",
train = True,
download = True,
transform = torchvision.transforms.ToTensor(),
target_transform = None
)
test_data = torchvision.datasets.MNIST(
root = "dataset",
train = False,
download = True,
transform = torchvision.transforms.ToTensor(),
target_transform = None
)
train_batch = DataLoader(
dataset=train_data,
batch_size = BATCH_SIZE,
shuffle=True)
test_batch = DataLoader(
dataset=test_data,
batch_size = BATCH_SIZE,
shuffle=False)
#Linear model
class MNISTModelLinear(nn.Module):
def __init__(self,
input_layer: int,
hidden_layer:int,
output_layer: int):
super().__init__()
self.layer_stack = nn.Sequential(
nn.Flatten(),
nn.Linear(in_features=input_layer,
out_features=hidden_layer),
#
# nn.Linear(in_features = hidden_layer, out_features = hidden_layer),
#
nn.Linear(in_features=hidden_layer,
out_features=output_layer)
)
def forward(self, x):
return self.layer_stack(x)
#NONLinear model
class MNISTModelNONLinear(nn.Module):
def __init__(self,
input_layer: int,
hidden_layer:int,
output_layer: int):
super().__init__()
self.layer_stack = nn.Sequential(
nn.Flatten(),
nn.Linear(in_features=input_layer,out_features=hidden_layer),
nn.ReLU(),
#
# nn.Linear(in_features = hidden_layer, out_features = hidden_layer),
# nn.ReLU(),
#
nn.Linear(in_features=hidden_layer,out_features=output_layer),
nn.ReLU()
)
def forward(self, x):
return self.layer_stack(x)
print("\n Model infos:\n")
if ISLINEAR:
print(f"Linear Model | Device: {DEVICE} | ")
model = MNISTModelLinear(INPUT_SHAPE,
HIDDEN_LAYER_SHAPE,
OUTPUT_SHAPE
).to(DEVICE)
else:
print(f"NONLinear Model | Device: {DEVICE} | ")
model = MNISTModelNONLinear(INPUT_SHAPE,
HIDDEN_LAYER_SHAPE,
OUTPUT_SHAPE
).to(DEVICE)
print(f"Epochs: {EPOCHS} | Hidden Layer neurons: {HIDDEN_LAYER_SHAPE} | Batch size: {BATCH_SIZE} | LR: {LEARNING_RATE}\n\n")
loss_function = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(params = model.parameters(),
lr = LEARNING_RATE)
start_time = timer()
for epoch in range(EPOCHS):
print("================================")
print(f"\nEpoch: {epoch + 1}/{EPOCHS}\n--------------------")
train_function(model, train_batch, loss_function, optimizer)
test_function(model, test_batch, loss_function)
stop_time = timer()
train_time(start_time, stop_time)
print(f"RECAP:: Epochs: {EPOCHS} | Hidden Layer neurons: {HIDDEN_LAYER_SHAPE} | Batch size: {BATCH_SIZE} | LR: {LEARNING_RATE} \n\n")
final_model_evaluation(model, test_data)
test_function(model, test_batch, loss_function)
torch.save(model, 'MNIST_Model_CLEAN.pth')
# torch.save(model.state_dict(), 'MNIST_Model_WEIGHT_CLEAN.pth')