-
Notifications
You must be signed in to change notification settings - Fork 86
/
train.py
244 lines (197 loc) · 8.89 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
import argparse
import better_exceptions
from pathlib import Path
from collections import OrderedDict
from tqdm import tqdm
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
from torch.optim.lr_scheduler import StepLR
import torch.utils.data
from torch.utils.data import DataLoader
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
import pretrainedmodels
import pretrainedmodels.utils
from model import get_model
from dataset import FaceDataset
from defaults import _C as cfg
def get_args():
model_names = sorted(name for name in pretrainedmodels.__dict__
if not name.startswith("__")
and name.islower()
and callable(pretrainedmodels.__dict__[name]))
parser = argparse.ArgumentParser(description=f"available models: {model_names}",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--data_dir", type=str, required=True, help="Data root directory")
parser.add_argument("--resume", type=str, default=None, help="Resume from checkpoint if any")
parser.add_argument("--checkpoint", type=str, default="checkpoint", help="Checkpoint directory")
parser.add_argument("--tensorboard", type=str, default=None, help="Tensorboard log directory")
parser.add_argument('--multi_gpu', action="store_true", help="Use multi GPUs (data parallel)")
parser.add_argument("opts", default=[], nargs=argparse.REMAINDER,
help="Modify config options using the command-line")
args = parser.parse_args()
return args
class AverageMeter(object):
def __init__(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val
self.count += n
self.avg = self.sum / self.count
def train(train_loader, model, criterion, optimizer, epoch, device):
model.train()
loss_monitor = AverageMeter()
accuracy_monitor = AverageMeter()
with tqdm(train_loader) as _tqdm:
for x, y in _tqdm:
x = x.to(device)
y = y.to(device)
# compute output
outputs = model(x)
# calc loss
loss = criterion(outputs, y)
cur_loss = loss.item()
# calc accuracy
_, predicted = outputs.max(1)
correct_num = predicted.eq(y).sum().item()
# measure accuracy and record loss
sample_num = x.size(0)
loss_monitor.update(cur_loss, sample_num)
accuracy_monitor.update(correct_num, sample_num)
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
_tqdm.set_postfix(OrderedDict(stage="train", epoch=epoch, loss=loss_monitor.avg),
acc=accuracy_monitor.avg, correct=correct_num, sample_num=sample_num)
return loss_monitor.avg, accuracy_monitor.avg
def validate(validate_loader, model, criterion, epoch, device):
model.eval()
loss_monitor = AverageMeter()
accuracy_monitor = AverageMeter()
preds = []
gt = []
with torch.no_grad():
with tqdm(validate_loader) as _tqdm:
for i, (x, y) in enumerate(_tqdm):
x = x.to(device)
y = y.to(device)
# compute output
outputs = model(x)
preds.append(F.softmax(outputs, dim=-1).cpu().numpy())
gt.append(y.cpu().numpy())
# valid for validation, not used for test
if criterion is not None:
# calc loss
loss = criterion(outputs, y)
cur_loss = loss.item()
# calc accuracy
_, predicted = outputs.max(1)
correct_num = predicted.eq(y).sum().item()
# measure accuracy and record loss
sample_num = x.size(0)
loss_monitor.update(cur_loss, sample_num)
accuracy_monitor.update(correct_num, sample_num)
_tqdm.set_postfix(OrderedDict(stage="val", epoch=epoch, loss=loss_monitor.avg),
acc=accuracy_monitor.avg, correct=correct_num, sample_num=sample_num)
preds = np.concatenate(preds, axis=0)
gt = np.concatenate(gt, axis=0)
ages = np.arange(0, 101)
ave_preds = (preds * ages).sum(axis=-1)
diff = ave_preds - gt
mae = np.abs(diff).mean()
return loss_monitor.avg, accuracy_monitor.avg, mae
def main():
args = get_args()
if args.opts:
cfg.merge_from_list(args.opts)
cfg.freeze()
start_epoch = 0
checkpoint_dir = Path(args.checkpoint)
checkpoint_dir.mkdir(parents=True, exist_ok=True)
# create model
print("=> creating model '{}'".format(cfg.MODEL.ARCH))
model = get_model(model_name=cfg.MODEL.ARCH)
if cfg.TRAIN.OPT == "sgd":
optimizer = torch.optim.SGD(model.parameters(), lr=cfg.TRAIN.LR,
momentum=cfg.TRAIN.MOMENTUM,
weight_decay=cfg.TRAIN.WEIGHT_DECAY)
else:
optimizer = torch.optim.Adam(model.parameters(), lr=cfg.TRAIN.LR)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)
# optionally resume from a checkpoint
resume_path = args.resume
if resume_path:
if Path(resume_path).is_file():
print("=> loading checkpoint '{}'".format(resume_path))
checkpoint = torch.load(resume_path, map_location="cpu")
start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(resume_path, checkpoint['epoch']))
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
else:
print("=> no checkpoint found at '{}'".format(resume_path))
if args.multi_gpu:
model = nn.DataParallel(model)
if device == "cuda":
cudnn.benchmark = True
criterion = nn.CrossEntropyLoss().to(device)
train_dataset = FaceDataset(args.data_dir, "train", img_size=cfg.MODEL.IMG_SIZE, augment=True,
age_stddev=cfg.TRAIN.AGE_STDDEV)
train_loader = DataLoader(train_dataset, batch_size=cfg.TRAIN.BATCH_SIZE, shuffle=True,
num_workers=cfg.TRAIN.WORKERS, drop_last=True)
val_dataset = FaceDataset(args.data_dir, "valid", img_size=cfg.MODEL.IMG_SIZE, augment=False)
val_loader = DataLoader(val_dataset, batch_size=cfg.TEST.BATCH_SIZE, shuffle=False,
num_workers=cfg.TRAIN.WORKERS, drop_last=False)
scheduler = StepLR(optimizer, step_size=cfg.TRAIN.LR_DECAY_STEP, gamma=cfg.TRAIN.LR_DECAY_RATE,
last_epoch=start_epoch - 1)
best_val_mae = 10000.0
train_writer = None
if args.tensorboard is not None:
opts_prefix = "_".join(args.opts)
train_writer = SummaryWriter(log_dir=args.tensorboard + "/" + opts_prefix + "_train")
val_writer = SummaryWriter(log_dir=args.tensorboard + "/" + opts_prefix + "_val")
for epoch in range(start_epoch, cfg.TRAIN.EPOCHS):
# train
train_loss, train_acc = train(train_loader, model, criterion, optimizer, epoch, device)
# validate
val_loss, val_acc, val_mae = validate(val_loader, model, criterion, epoch, device)
if args.tensorboard is not None:
train_writer.add_scalar("loss", train_loss, epoch)
train_writer.add_scalar("acc", train_acc, epoch)
val_writer.add_scalar("loss", val_loss, epoch)
val_writer.add_scalar("acc", val_acc, epoch)
val_writer.add_scalar("mae", val_mae, epoch)
# checkpoint
if val_mae < best_val_mae:
print(f"=> [epoch {epoch:03d}] best val mae was improved from {best_val_mae:.3f} to {val_mae:.3f}")
model_state_dict = model.module.state_dict() if args.multi_gpu else model.state_dict()
torch.save(
{
'epoch': epoch + 1,
'arch': cfg.MODEL.ARCH,
'state_dict': model_state_dict,
'optimizer_state_dict': optimizer.state_dict()
},
str(checkpoint_dir.joinpath("epoch{:03d}_{:.5f}_{:.4f}.pth".format(epoch, val_loss, val_mae)))
)
best_val_mae = val_mae
else:
print(f"=> [epoch {epoch:03d}] best val mae was not improved from {best_val_mae:.3f} ({val_mae:.3f})")
# adjust learning rate
scheduler.step()
print("=> training finished")
print(f"additional opts: {args.opts}")
print(f"best val mae: {best_val_mae:.3f}")
if __name__ == '__main__':
main()