-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
334 lines (244 loc) · 13.8 KB
/
train.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""This script trains and validates different models"""
# import the necessary packages
from models import lenet_mnist, lenet_cifar10, resnet_cifar10, resnet_mnist, vgg_cifar10, vgg_mnist, resnet_dropout
from datasets import load_mnist_dataset, load_cifar10_dataset
from tqdm.notebook import tqdm
import argparse
from pathlib import Path
from torchmetrics import Accuracy
import torch.nn as nn
import torch
import torchplot as plt
parser = argparse.ArgumentParser()
parser.add_argument('--model', default='lenet_mnist',
help="model to train and validate. One of [lenet_mnist, lenet_cifar10, ...]")
parser.add_argument('--batchsz', default=32,
help="batch size for training and validation")
parser.add_argument('--lr', default=0.01,
help="the learning rate")
parser.add_argument('--epochs', default=50,
help="no of training iterations")
parser.add_argument('--optim', default="sgd",
help="the optimizer to use for training")
parser.add_argument('--wd', default=0.0, help="weight decay")
def train_and_validate(model, model_name, train_dataloader, val_dataloader, loss_fn, optimizer, epochs, device, accuracy):
"""Train and validate a model on the training data and evaluate it on the test data.
Args:
model (nn.Module): The model to train and validate.
train_loader (DataLoader): The training data loader.
val_loader (DataLoader): The validation data loader.
loss_fn (Loss): The loss function.
optimizer (Optimizer): The optimizer.
epochs (int): The number of epochs to train the model.
device (torch.device): The device to use for training and validation.
Returns:
list of the training loss, validation loss, training accuracy, and validation accuracy.
"""
train_loss_list = []
val_loss_list = []
train_acc_list = []
val_acc_list = []
for epoch in tqdm(range(epochs)):
# Training loop
train_loss, train_acc = 0.0, 0.0
for X, y in train_dataloader:
X, y = X.to(device), y.to(device)
model.train()
if model_name == 'lenet_cifar10' or model_name == 'resnet_mnist':
y_pred, probas = model(X)
else: y_pred = model(X)
loss = loss_fn(y_pred, y)
train_loss += loss.item()
acc = accuracy(y_pred, y)
train_acc += acc
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss /= len(train_dataloader)
train_acc /= len(train_dataloader)
if epoch % 5 == 0:
train_loss_list.append(train_loss)
train_acc_list.append(train_acc)
# Validation loop
val_loss, val_acc = 0.0, 0.0
model.eval()
with torch.inference_mode():
for X, y in val_dataloader:
X, y = X.to(device), y.to(device)
if model_name == 'lenet_cifar10' or model_name == 'resnet_mnist':
y_pred, probas = model(X)
else: y_pred = model(X)
loss = loss_fn(y_pred, y)
val_loss += loss.item()
acc = accuracy(y_pred, y)
val_acc += acc
val_loss /= len(val_dataloader)
val_acc /= len(val_dataloader)
if epoch % 5 == 0:
val_loss_list.append(val_loss)
val_acc_list.append(val_acc)
print(f" Model: {model_name} | Epoch: {epoch}| Train loss: {train_loss: .5f}| Train acc: {train_acc: .5f}| Val loss: {val_loss: .5f}| Val acc: {val_acc: .5f}")
# save the trained model
MODEL_PATH = Path("saved_models")
MODEL_PATH.mkdir(parents=True, exist_ok=True)
MODEL_NAME = f"{model_name}.pth"
MODEL_SAVE_PATH = MODEL_PATH / MODEL_NAME
print(f"Saving the model: {MODEL_SAVE_PATH}") # saving the model
torch.save(obj=model.state_dict(), f=MODEL_SAVE_PATH)
return train_loss_list, val_loss_list, train_acc_list, val_acc_list
def plot_loss_and_accuracy(train_loss_list, val_loss_list, train_acc_list, val_acc_list, model_name):
"""Plot the training, validation loss and accuracy on the same plot.
Args:
train_loss_list (list): The training loss.
val_loss_list (list): The validation loss.
train_acc_list (list): The training accuracy.
val_acc_list (list): The validation accuracy.
"""
epochs = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
dest = Path("figures")
dest.mkdir(parents=True, exist_ok=True)
dest1 = dest / f"{model_name}_loss_and_accuracy.png"
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(epochs, train_loss_list, label="Train loss")
plt.plot(epochs, val_loss_list, label="Val loss")
plt.title("training loss and validation loss vs epochs")
plt.xlabel("Epochs")
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(epochs, train_acc_list, label="Train acc")
plt.plot(epochs, val_acc_list, label="Val acc")
plt.title("training accuracy and validation accuracy vs epochs")
plt.xlabel("Epochs")
plt.legend()
plt.savefig(dest1)
return plt.show()
if __name__ == '__main__':
# general initializations and setup
args = parser.parse_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu' # device-agnostic setup
accuracy = Accuracy(task="multiclass", num_classes=10)
accuracy = accuracy.to(device)
loss_fn = nn.CrossEntropyLoss()
if args.model == "lenet_mnist":
# load the training and validation data
train_dataloader, val_dataloader, _ = load_mnist_dataset("lenet_mnist", int(args.batchsz))
# initialize the model, loss function, optimizer and evaluation metrics
model = lenet_mnist.LeNet5()
optimizer = torch.optim.SGD(model.parameters(), float(args.lr))
# train the model
model_lenet5v1 = model.to(device)
# call the train and validate function
train_llt, val_llt, train_acl, val_acl = train_and_validate(model_lenet5v1, args.model, train_dataloader,
val_dataloader, loss_fn, optimizer, int(args.epochs), device, accuracy)
# plot the training, validation loss and accuracy on the same plot
plot_loss_and_accuracy(train_llt, val_llt, train_acl, val_acl, args.model)
if args.model == "lenet_cifar10":
# load the training and validation data
train_dataloader, val_dataloader, _ = load_cifar10_dataset("lenet_cifar10", int(args.batchsz))
# initialize the model, loss function, optimizer and evaluation metrics
model = lenet_cifar10.LeNet5(num_classes=10)
if args.optim == "sgd":
optimizer = torch.optim.SGD(model.parameters(), float(args.lr), momentum=0.9)
if args.optim == "adam":
optimizer = torch.optim.Adam(model.parameters(), float(args.lr))
# train the model
model_lenet5v1 = model.to(device)
# call the train and validate function
train_llt, val_llt, train_acl, val_acl = train_and_validate(model_lenet5v1, args.model, train_dataloader,
val_dataloader, loss_fn, optimizer, int(args.epochs), device, accuracy)
# plot the training, validation loss and accuracy on the same plot
plot_loss_and_accuracy(train_llt, val_llt, train_acl, val_acl, args.model)
if args.model == "resnet_cifar10":
# load the training and validation data
train_dataloader, val_dataloader, _ = load_cifar10_dataset("resnet_cifar10", int(args.batchsz))
# initialize the model, loss function, optimizer and evaluation metrics
model = resnet_cifar10.ResNet18()
if args.optim == "sgd":
optimizer = torch.optim.SGD(model.parameters(), float(args.lr), momentum=0.9)
if args.optim == "adam":
optimizer = torch.optim.Adam(model.parameters(), float(args.lr))
# train the model
model_resnet18v1 = model.to(device)
# call the train and validate function
train_llt, val_llt, train_acl, val_acl = train_and_validate(model_resnet18v1, args.model, train_dataloader,
val_dataloader, loss_fn, optimizer, int(args.epochs), device, accuracy)
# plot the training, validation loss and accuracy on the same plot
plot_loss_and_accuracy(train_llt, val_llt, train_acl, val_acl, args.model)
if args.model == "resnet_mnist":
# load the training and validation data
train_dataloader, val_dataloader, _ = load_mnist_dataset("resnet_mnist", int(args.batchsz))
# initialize the model, loss function, optimizer and evaluation metrics
model = resnet_mnist.ResNet18(num_classes=10)
if args.optim == "sgd":
optimizer = torch.optim.SGD(model.parameters(), float(args.lr), momentum=0.9)
if args.optim == "adam":
optimizer = torch.optim.Adam(model.parameters(), float(args.lr))
# train the model
model_resnet18v1 = model.to(device)
# call the train and validate function
train_llt, val_llt, train_acl, val_acl = train_and_validate(model_resnet18v1, args.model, train_dataloader,
val_dataloader, loss_fn, optimizer, int(args.epochs), device, accuracy)
# plot the training, validation loss and accuracy on the same plot
plot_loss_and_accuracy(train_llt, val_llt, train_acl, val_acl, args.model)
if args.model == "vgg_cifar10":
# load the training and validation data
train_dataloader, val_dataloader, _ = load_cifar10_dataset("vgg_cifar10", int(args.batchsz))
# initialize the model, loss function, optimizer and evaluation metrics
model = vgg_cifar10.VGG16(num_classes=10)
if args.optim == "sgd":
optimizer = torch.optim.SGD(model.parameters(), float(args.lr), momentum=0.9)
if args.optim == "adam":
optimizer = torch.optim.Adam(model.parameters(), float(args.lr))
# train the model
model_vggv1 = model.to(device)
# call the train and validate function
train_llt, val_llt, train_acl, val_acl = train_and_validate(model_vggv1, args.model, train_dataloader,
val_dataloader, loss_fn, optimizer, int(args.epochs), device, accuracy)
# plot the training, validation loss and accuracy on the same plot
plot_loss_and_accuracy(train_llt, val_llt, train_acl, val_acl, args.model)
if args.model == "vgg_mnist":
# load the training and validation data
train_dataloader, val_dataloader, _ = load_mnist_dataset("vgg_mnist", int(args.batchsz))
# initialize the model, loss function, optimizer and evaluation metrics
model = vgg_mnist.VGG16(num_classes=10)
if args.optim == "sgd":
optimizer = torch.optim.SGD(model.parameters(), float(args.lr), momentum=0.9)
if args.optim == "adam":
optimizer = torch.optim.Adam(model.parameters(), float(args.lr))
# train the model
model_vggv1 = model.to(device)
# call the train and validate function
train_llt, val_llt, train_acl, val_acl = train_and_validate(model_vggv1, args.model, train_dataloader,
val_dataloader, loss_fn, optimizer, int(args.epochs), device, accuracy)
# plot the training, validation loss and accuracy on the same plot
plot_loss_and_accuracy(train_llt, val_llt, train_acl, val_acl, args.model)
if args.model == "resnet_dropout":
# load the training and validation data
train_dataloader, val_dataloader, _ = load_cifar10_dataset("resnet_dropout", int(args.batchsz))
# initialize the model, loss function, optimizer and evaluation metrics
model = resnet_dropout.ResNet18()
if args.optim == "sgd":
optimizer = torch.optim.SGD(model.parameters(), float(args.lr), momentum=0.9)
if args.optim == "adam":
optimizer = torch.optim.Adam(model.parameters(), float(args.lr))
# train the model
model_resnet18v1 = model.to(device)
# call the train and validate function
train_llt, val_llt, train_acl, val_acl = train_and_validate(model_resnet18v1, args.model, train_dataloader,
val_dataloader, loss_fn, optimizer, int(args.epochs), device, accuracy)
# plot the training, validation loss and accuracy on the same plot
plot_loss_and_accuracy(train_llt, val_llt, train_acl, val_acl, args.model)
if args.model == "resnet_cifar10_wd":
# load the training and validation data
train_dataloader, val_dataloader, _ = load_cifar10_dataset("resnet_cifar10", int(args.batchsz))
# initialize the model, loss function, optimizer and evaluation metrics
model = resnet_cifar10.ResNet18()
optimizer = torch.optim.Adam(model.parameters(), float(args.lr), weight_decay=float(args.wd))
# train the model
model_resnet18v1 = model.to(device)
# call the train and validate function
train_llt, val_llt, train_acl, val_acl = train_and_validate(model_resnet18v1, args.model, train_dataloader,
val_dataloader, loss_fn, optimizer, int(args.epochs), device, accuracy)
# plot the training, validation loss and accuracy on the same plot
plot_loss_and_accuracy(train_llt, val_llt, train_acl, val_acl, args.model)