-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
200 lines (179 loc) · 7.31 KB
/
train.py
File metadata and controls
200 lines (179 loc) · 7.31 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
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
import torch
import argparse
import torch.nn as nn
import torch.optim as optim
import time
# from torchmetrics.functional import precision_recall
from tqdm.auto import tqdm
from Dataprocess import get_datasets, get_data_loaders
from model_builder import build_model
from utilities import save_model, save_plots
# construct the argument parser
parser = argparse.ArgumentParser()
parser.add_argument(
'-e', '--epochs', type=int, default=40,
help='Number of epochs to train our network for'
)
parser.add_argument(
'-pt', '--pretrained', action='store_true',
help='Whether to use pretrained weights or not'
)
parser.add_argument(
'-lr', '--learning-rate', type=float,
dest='learning_rate', default=0.001,
help='Learning rate for training the model'
)
args = vars(parser.parse_args())
# Training function.
def train(model, trainloader, optimizer, criterion):
model.train()
print('Training')
train_running_loss = 0.0
train_running_correct = 0
counter = 0
for i, data in tqdm(enumerate(trainloader), total=len(trainloader)):
counter += 1
image, labels = data
image = image.to(device)
labels = labels.to(device)
optimizer.zero_grad()
# Forward pass.
outputs = model(image)
# Calculate the loss.
loss = criterion(outputs, labels)
train_running_loss += loss.item()
# Calculate the accuracy.
_, preds = torch.max(outputs.data, 1)
train_running_correct += (preds == labels).sum().item()
# Backpropagation
loss.backward()
# Update the weights.
optimizer.step()
# Loss and accuracy for the complete epoch.
epoch_loss = train_running_loss / counter
epoch_acc = 100. * (train_running_correct / len(trainloader.dataset))
return epoch_loss, epoch_acc
# Validation function.
def validate(model, testloader, criterion):
model.eval()
print('Validation')
valid_running_loss = 0.0
valid_running_correct = 0
counter = 0
with torch.no_grad():
for i, data in tqdm(enumerate(testloader), total=len(testloader)):
counter += 1
image, labels = data
image = image.to(device)
labels = labels.to(device)
# Forward pass.
outputs = model(image)
# Calculate the loss.
loss = criterion(outputs, labels)
valid_running_loss += loss.item()
# Calculate the accuracy.
_, preds = torch.max(outputs.data, 1)
valid_running_correct += (preds == labels).sum().item()
# t_pred= torch.tensor(preds)
# t_label= torch.tensor(labels)
# precision_recall(t_pred, t_label, average='macro',num_classes=5)
# Loss and accuracy for the complete epoch.
# prec = ((preds == labels) / preds).sum().item()
# prec= float(prec)
epoch_loss = valid_running_loss / counter
epoch_acc = 100. * (valid_running_correct / len(testloader.dataset))
return epoch_loss, epoch_acc # , precision_recall
# Validation function.
def test(model, testloader, criterion):
model.eval()
print('testing')
test_running_loss = 0.0
test_running_correct = 0
counter = 0
with torch.no_grad():
for i, data in tqdm(enumerate(testloader), total=len(testloader)):
counter += 1
image, labels = data
image = image.to(device)
labels = labels.to(device)
# Forward pass.
outputs = model(image)
# Calculate the loss.
loss = criterion(outputs, labels)
test_running_loss += loss.item()
# Calculate the accuracy.
_, preds = torch.max(outputs.data, 1)
test_running_correct += (preds == labels).sum().item()
# t_pred= torch.tensor(preds)
# t_label= torch.tensor(labels)
# precision_recall(t_pred, t_label, average='macro',num_classes=5)
# Loss and accuracy for the complete epoch.
# prec = ((preds == labels) / preds).sum().item()
# prec= float(prec)
epoch_loss = test_running_loss / counter
epoch_acc = 100. * (test_running_correct / len(testloader.dataset))
return epoch_loss, epoch_acc # , precision_recall
if __name__ == '__main__':
# Load the training and validation datasets.
dataset_train, dataset_valid, dataset_test, dataset_classes = get_datasets(args['pretrained']
)
print(f"[INFO]: Number of training images: {len(dataset_train)}")
print(f"[INFO]: Number of validation images: {len(dataset_valid)}")
print(f"[INFO]: Number of testing images: {len(dataset_test)}")
print(f"[INFO]: Class names: {dataset_classes}\n")
# Load the training and validation data loaders.
train_loader, valid_loader, test_loader = get_data_loaders(dataset_train, dataset_valid, dataset_test)
# Learning_parameters.
lr = args['learning_rate']
epochs = args['epochs']
device = ('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Computation device: {device}")
print(f"Learning rate: {lr}")
print(f"Epochs to train for: {epochs}\n")
model = build_model(
pretrained=args['pretrained'],
fine_tune=True,
num_classes=len(dataset_classes)
).to(device)
# Total parameters and trainable parameters.
total_params = sum(p.numel() for p in model.parameters())
print(f"{total_params:,} total parameters.")
total_trainable_params = sum(
p.numel() for p in model.parameters() if p.requires_grad)
print(f"{total_trainable_params:,} training parameters.")
# Optimizer.
optimizer = optim.Adam(model.parameters(), lr=lr)
# Loss function.
criterion = nn.CrossEntropyLoss()
# Lists to keep track of losses and accuracies.
train_loss, valid_loss, test_loss = [], [], []
train_acc, valid_acc, test_acc = [], [], []
# Start the training.
for epoch in range(epochs):
print(f"[INFO]: Epoch {epoch + 1} of {epochs}")
train_epoch_loss, train_epoch_acc = train(model, train_loader,
optimizer, criterion)
valid_epoch_loss, valid_epoch_acc = validate(model, valid_loader,
criterion)
test_epoch_loss, test_epoch_acc = test(model, test_loader,
criterion)
# , precision_recall
train_loss.append(train_epoch_loss)
valid_loss.append(valid_epoch_loss)
test_loss.append(test_epoch_loss)
train_acc.append(train_epoch_acc)
valid_acc.append(valid_epoch_acc)
test_acc.append(test_epoch_acc)
# pre, device= preds
print(f"Training loss: {train_epoch_loss:.3f}, training acc: {train_epoch_acc:.3f}")
print(f"Validation loss: {valid_epoch_loss:.3f}, validation acc: {valid_epoch_acc:.3f}")
print(f"testing loss: {test_epoch_loss:.3f}, testing acc: {test_epoch_acc:.3f}")
# print(preds)
# print(labels)
print('-' * 50)
time.sleep(5)
# Save the trained model weights.
save_model(epochs, model, optimizer, criterion, args['pretrained'])
# Save the loss and accuracy plots.
save_plots(train_acc, valid_acc, test_acc, train_loss, valid_loss, test_loss, args['pretrained'])
print('TRAINING COMPLETE')