-
Notifications
You must be signed in to change notification settings - Fork 15
/
train.py
210 lines (181 loc) · 9 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
"""
Author: Amr Elsersy
email: [email protected]
-----------------------------------------------------------------------------------
Description: Training & Validation
"""
import numpy as np
import argparse, cv2
import logging
import time
import os
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.optim
import torch.utils.tensorboard as tensorboard
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
cudnn.enabled = True
from model.model import Mini_Xception
from dataset import create_train_dataloader, create_val_dataloader, create_test_dataloader
from utils import visualize_confusion_matrix
from sklearn.metrics import precision_score, recall_score, accuracy_score, confusion_matrix
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--epochs', type=int, default=300, help='num of training epochs')
parser.add_argument('--batch_size', type=int, default=15, help="training batch size")
parser.add_argument('--tensorboard', type=str, default='checkpoint/tensorboard', help='path log dir of tensorboard')
parser.add_argument('--logging', type=str, default='checkpoint/logging', help='path of logging')
parser.add_argument('--lr', type=float, default=0.001, help='learning rate')
parser.add_argument('--weight_decay', type=float, default=1e-6, help='optimizer weight decay')
parser.add_argument('--datapath', type=str, default='data', help='root path of dataset')
parser.add_argument('--pretrained', type=str,default='checkpoint/model_weights/weights_epoch_75.pth.tar',help='load checkpoint')
parser.add_argument('--resume', action='store_true', help='resume from pretrained path specified in prev arg')
parser.add_argument('--savepath', type=str, default='checkpoint/model_weights', help='save checkpoint path')
parser.add_argument('--savefreq', type=int, default=1, help="save weights each freq num of epochs")
parser.add_argument('--logdir', type=str, default='checkpoint/logging', help='logging')
parser.add_argument("--lr_patience", default=40, type=int)
parser.add_argument('--evaluate', action='store_true', help='evaluation only')
parser.add_argument('--mode', type=str, default='val', choices=['val','test', 'train'], help='dataset type for evaluation only')
args = parser.parse_args()
return args
# ======================================================================
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
args = parse_args()
# logging
logging.basicConfig(
format='[%(message)s',
level=logging.INFO,
handlers=[logging.FileHandler(args.logdir, mode='w'), logging.StreamHandler()])
# tensorboard
writer = tensorboard.SummaryWriter(args.tensorboard)
def main():
# ========= dataloaders ===========
train_dataloader = create_train_dataloader(root=args.datapath, batch_size=args.batch_size)
test_dataloader = create_val_dataloader(root=args.datapath, batch_size=args.batch_size)
# train_dataloader, test_dataloader = create_CK_dataloader(batch_size=args.batch_size)
start_epoch = 0
# ======== models & loss ==========
mini_xception = Mini_Xception()
loss = nn.CrossEntropyLoss()
# ========= load weights ===========
if args.resume or args.evaluate:
checkpoint = torch.load(args.pretrained, map_location=device)
mini_xception.load_state_dict(checkpoint['mini_xception'], strict=False)
start_epoch = checkpoint['epoch'] + 1
print(f'\tLoaded checkpoint from {args.pretrained}\n')
time.sleep(1)
else:
print("******************* Start training from scratch *******************\n")
time.sleep(2)
if args.evaluate:
if args.mode == 'test':
test_dataloader = create_test_dataloader(root=args.datapath, batch_size=args.batch_size)
elif args.mode == 'val':
test_dataloader = create_val_dataloader(root=args.datapath, batch_size=args.batch_size)
else:
test_dataloader = create_train_dataloader(root=args.datapath, batch_size=args.batch_size)
validate(mini_xception, loss, test_dataloader, 0)
return
# =========== optimizer ===========
# parameters = mini_xception.named_parameters()
# for name, p in parameters:
# print(p.requires_grad, name)
# return
optimizer = torch.optim.Adam(mini_xception.parameters(), lr=args.lr, weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', patience=args.lr_patience, verbose=True)
# ========================================================================
for epoch in range(start_epoch, args.epochs):
# =========== train / validate ===========
train_loss = train_one_epoch(mini_xception, loss, optimizer, train_dataloader, epoch)
val_loss, accuracy, percision, recall = validate(mini_xception, loss, test_dataloader, epoch)
scheduler.step(val_loss)
val_loss, accuracy, percision, recall = round(val_loss,3), round(accuracy,3), round(percision,3), round(recall,3)
logging.info(f"\ttraining epoch={epoch} .. train_loss={train_loss}")
logging.info(f"\tvalidation epoch={epoch} .. val_loss={val_loss}")
logging.info(f'\tAccuracy = {accuracy*100} % .. Percision = {percision*100} % .. Recall = {recall*100} % \n')
time.sleep(2)
# ============= tensorboard =============
writer.add_scalar('train_loss',train_loss, epoch)
writer.add_scalar('val_loss',val_loss, epoch)
writer.add_scalar('percision',percision, epoch)
writer.add_scalar('recall',recall, epoch)
writer.add_scalar('accuracy',accuracy, epoch)
# ============== save model =============
if epoch % args.savefreq == 0:
checkpoint_state = {
'mini_xception': mini_xception.state_dict(),
"epoch": epoch
}
savepath = os.path.join(args.savepath, f'weights_epoch_{epoch}.pth.tar')
torch.save(checkpoint_state, savepath)
print(f'\n\t*** Saved checkpoint in {savepath} ***\n')
time.sleep(2)
writer.close()
def train_one_epoch(model, criterion, optimizer, dataloader, epoch):
model.train()
model.to(device)
losses = []
for images, labels in tqdm(dataloader):
images = images.to(device) # (batch, 1, 48, 48)
labels = labels.to(device) # (batch,)
emotions = model(images)
# from (batch, 7, 1, 1) to (batch, 7)
emotions = torch.squeeze(emotions)
# print(emotions)
# print(labels,'\n')
loss = criterion(emotions, labels)
losses.append(loss.cpu().item())
print(f'training @ epoch {epoch} .. loss = {round(loss.item(),3)}')
optimizer.zero_grad()
loss.backward()
optimizer.step()
# images = images.squeeze().cpu().detach().numpy()
# cv2.imshow('f', images[0])
# cv2.waitKey(0)
return round(np.mean(losses).item(),3)
def validate(model, criterion, dataloader, epoch):
model.eval()
model.to(device)
losses = []
total_pred = []
total_labels = []
with torch.no_grad():
for images, labels in tqdm(dataloader):
mini_batch = images.shape[0]
images = images.to(device)
labels = labels.to(device)
emotions = model(images)
emotions = torch.squeeze(emotions)
emotions = emotions.reshape(mini_batch, -1)
loss = criterion(emotions, labels)
losses.append(loss.cpu().item())
# # ============== Evaluation ===============
# index of the max value of each sample (shape = (batch,))
_, indexes = torch.max(emotions, axis=1)
# print(indexes.shape, labels.shape)
total_pred.extend(indexes.cpu().detach().numpy())
total_labels.extend(labels.cpu().detach().numpy())
print(f'validation loss = {round(loss.item(),3)}')
val_loss = np.mean(losses).item()
percision = precision_score(total_labels, total_pred, average='macro')
recall = recall_score(total_labels, total_pred, average='macro')
accuracy = accuracy_score(total_labels, total_pred)
val_loss, accuracy, percision, recall = round(val_loss,3), round(accuracy,3), round(percision,3), round(recall,3)
print(f'Val loss = {val_loss} .. Accuracy = {accuracy} .. Percision = {percision} .. Recall = {recall}')
if args.evaluate:
conf_matrix = confusion_matrix(total_labels, total_pred, normalize='true')
print('Confusion Matrix\n', conf_matrix)
visualize_confusion_matrix(conf_matrix)
return val_loss, accuracy, percision, recall
if __name__ == "__main__":
main()
# total_labels = [0, 1, 2, 0]
# total_pred = [0, 2, 1, 2]
# avg = None
# avg = 'macro'
# percision = precision_score(total_labels, total_pred, average=avg)
# recall = recall_score(total_labels, total_pred, average=avg)
# accuracy = accuracy_score(total_labels, total_pred)
# print(percision, recall, accuracy)