-
Notifications
You must be signed in to change notification settings - Fork 3
/
LeNet300_MNIST_Itraining_torch.py
281 lines (207 loc) · 8.36 KB
/
LeNet300_MNIST_Itraining_torch.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
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
import pickle
from tqdm import tqdm, trange
from LeNet300_swish_torch import LeNet300, init_weights
from get_mnist_data import mnist_dataset
print(f"torch version: {torch.__version__}")
# Check if there are multiple devices (i.e., GPU cards)-
print(f"Number of GPU(s) available = {torch.cuda.device_count()}")
if torch.cuda.is_available():
print(f"Current GPU: {torch.cuda.current_device()}")
print(f"Current GPU name: {torch.cuda.get_device_name(torch.cuda.current_device())}")
else:
print("PyTorch does not have access to GPU")
# Device configuration-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f'Available device is {device}\n\n')
path_files = "/home/amajumdar/Downloads/.data/"
batch_size = 512
train_dataset, test_dataset, train_loader, test_loader = mnist_dataset(
path_to_files = path_files, batch_size = batch_size
)
model = LeNet300(beta = 1.0)
model.apply(init_weights)
# Save randomly initialized parameters-
torch.save(model.state_dict(), "LeNet300_randomwts.pth")
def count_trainable_params(model):
# Count number of layer-wise parameters and total parameters-
tot_params = 0
for param in model.parameters():
layer_param = torch.count_nonzero(param)
tot_params += layer_param.item()
return tot_params
tot_params = count_trainable_params(model)
class CosineScheduler:
def __init__(
self, max_update,
base_lr = 0.01, final_lr = 0,
warmup_steps = 0, warmup_begin_lr = 0
):
self.base_lr_orig = base_lr
self.max_update = max_update
self.final_lr = final_lr
self.warmup_steps = warmup_steps
self.warmup_begin_lr = warmup_begin_lr
self.max_steps = self.max_update - self.warmup_steps
def get_warmup_lr(self, epoch):
increase = (self.base_lr_orig - self.warmup_begin_lr) \
* float(epoch) / float(self.warmup_steps)
return self.warmup_begin_lr + increase
def __call__(self, epoch):
if epoch < self.warmup_steps:
return self.get_warmup_lr(epoch)
if epoch <= self.max_update:
self.base_lr = self.final_lr + (
self.base_lr_orig - self.final_lr) * (1 + np.cos(
np.pi * (epoch - self.warmup_steps) / self.max_steps)) / 2
return self.base_lr
def train_one_epoch(
model, train_loader,
train_dataset, optimizer
):
'''
Function to perform one epoch of training by using 'train_loader'.
Returns loss and number of correct predictions for this epoch.
'''
running_loss = 0.0
running_corrects = 0.0
model.train()
with tqdm(train_loader, unit = 'batch') as tepoch:
for images, labels in tepoch:
tepoch.set_description(f"Training: ")
images = images.reshape(-1, 28 * 28)
images = images.to(device)
labels = labels.to(device)
# Get model predictions-
preds = model(images)
# Compute loss-
# output layer applies log-softmax (row-wise), hence, use
# NLL-loss instead of Cross-entropy cost function-
# loss = torch.nn.functional.nll_loss(preds, labels)
cost_fn = nn.CrossEntropyLoss()
loss = cost_fn(preds, labels)
# Empty accumulated gradients-
optimizer.zero_grad()
# Perform backprop-
loss.backward()
# Update parameters-
optimizer.step()
'''
# LR scheduler-
global step
optimizer.param_groups[0]['lr'] = custom_lr_scheduler.get_lr(step)
step += 1
'''
# Compute model's performance statistics-
running_loss += loss.item() * images.size(0)
_, predicted = torch.max(preds, 1)
running_corrects += torch.sum(predicted == labels.data)
tepoch.set_postfix(
loss = running_loss / len(train_dataset),
accuracy = (running_corrects.double().cpu().numpy() / len(train_dataset)) * 100
)
train_loss = running_loss / len(train_dataset)
train_acc = (running_corrects.double() / len(train_dataset)) * 100
return train_loss, train_acc.cpu().numpy()
def test_one_epoch(model, test_loader, test_dataset):
total = 0.0
correct = 0.0
running_loss_test = 0.0
with torch.no_grad():
with tqdm(test_loader, unit = 'batch') as tepoch:
for images, labels in tepoch:
tepoch.set_description(f"Testing: ")
images = images.reshape(-1, 28 * 28)
images = images.to(device)
labels = labels.to(device)
# Set model to evaluation mode-
model.eval()
# Predict using trained model-
outputs = model(images)
_, y_pred = torch.max(outputs, 1)
# Compute validation loss-
# J_test = torch.nn.functional.nll_loss(outputs, labels)
cost_fn = nn.CrossEntropyLoss()
J_test = loss = cost_fn(outputs, labels)
running_loss_test += J_test.item() * labels.size(0)
# Total number of labels-
total += labels.size(0)
# Total number of correct predictions-
correct += (y_pred == labels).sum()
tepoch.set_postfix(
test_loss = running_loss_test / len(test_dataset),
test_acc = 100 * (correct.cpu().numpy() / total)
)
# return (running_loss_val, correct, total)
test_loss = running_loss_test / len(test_dataset)
test_acc = (correct / total) * 100
return test_loss, test_acc.cpu().numpy()
def train_until_convergence(
model,
train_dataset, test_dataset,
train_loader, test_loader,
num_epochs = 50, warmup_epochs = 10,
best_test_acc = 90
):
# Python3 dict to contain training metrics-
train_history = {}
# Initialize parameters saving 'best' models-
# best_test_acc = 90
# num_epochs = 50
# Use SGD optimizer-
optimizer = torch.optim.SGD(
params = model.parameters(), lr = 0.0001,
momentum = 0.9, weight_decay = 5e-4
)
# Decay lr in cosine manner unitl 45th epoch-
scheduler = CosineScheduler(
max_update = 45, base_lr = 0.03,
final_lr = 0.001, warmup_steps = warmup_epochs,
warmup_begin_lr = 0.0001
)
for epoch in range(1, num_epochs + 1):
# Update LR scheduler-
for param_group in optimizer.param_groups:
param_group['lr'] = scheduler(epoch)
# Train and validate model for 1 epoch-
train_loss, train_acc = train_one_epoch(
model = model, train_loader = train_loader,
train_dataset = train_dataset,
optimizer = optimizer
)
test_loss, test_acc = test_one_epoch(
model = model, test_loader = test_loader,
test_dataset = test_dataset
)
curr_lr = optimizer.param_groups[0]['lr']
print(f"\nepoch: {epoch + 1} train loss = {train_loss:.4f}, "
f"train accuracy = {train_acc:.2f}%, test loss = {test_loss:.4f}"
f", test accuracy = {test_acc:.2f}% "
f"LR = {curr_lr:.4f}\n")
train_history[epoch + 1] = {
'loss': train_loss, 'acc': train_acc,
'test_loss': test_loss, 'test_acc': test_acc,
'lr': curr_lr,
}
# Save best weights achieved until now-
if (test_acc > best_test_acc):
# update 'best_val_loss' variable to lowest loss encountered so far-
best_test_acc = test_acc
print(f"Saving model with highest test acc = {test_acc:.3f}%\n")
# Save trained model with 'best' testing accuracy-
torch.save(model.state_dict(), "LeNet300_best_testacc_model.pth")
torch.save(optimizer.state_dict(), "LeNet300_best_optimizer.pth")
return train_history
train_history = train_until_convergence(
model = model,
train_dataset = train_dataset, test_dataset = test_dataset,
train_loader = train_loader, test_loader = test_loader,
num_epochs = 50, warmup_epochs = 10,
best_test_acc = 90
)
with open("LeNet300_train_history.pkl", "wb") as file:
pickle.dump(train_history, file)
del file