forked from polarisZhao/PFLD-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
220 lines (186 loc) · 7.56 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
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
import argparse
import logging
from pathlib import Path
import time
import os
import numpy as np
import torch
from torch.utils import data
from torch.utils.data import DataLoader
import torchvision
from torchvision import datasets, transforms
import torchvision.utils as vutils
from tensorboardX import SummaryWriter
from dataset.datasets import WLFWDatasets
from models.pfld import PFLDInference, AuxiliaryNet
from pfld.loss import PFLDLoss
from pfld.utils import AverageMeter
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def print_args(args):
for arg in vars(args):
s = arg + ': ' + str(getattr(args, arg))
logging.info(s)
def save_checkpoint(state, filename='checkpoint.pth.tar'):
torch.save(state, filename)
logging.info('Save checkpoint to {0:}'.format(filename))
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected')
def train(train_loader, pfld_backbone, auxiliarynet, criterion, optimizer,
epoch):
losses = AverageMeter()
weighted_loss, loss = None, None
for img, landmark_gt, attribute_gt, euler_angle_gt in train_loader:
img = img.to(device)
attribute_gt = attribute_gt.to(device)
landmark_gt = landmark_gt.to(device)
euler_angle_gt = euler_angle_gt.to(device)
pfld_backbone = pfld_backbone.to(device)
auxiliarynet = auxiliarynet.to(device)
features, landmarks = pfld_backbone(img)
angle = auxiliarynet(features)
weighted_loss, loss = criterion(attribute_gt, landmark_gt,
euler_angle_gt, angle, landmarks,
args.train_batchsize)
optimizer.zero_grad()
weighted_loss.backward()
optimizer.step()
losses.update(loss.item())
return weighted_loss, loss
def validate(wlfw_val_dataloader, pfld_backbone, auxiliarynet, criterion):
pfld_backbone.eval()
auxiliarynet.eval()
losses = []
with torch.no_grad():
for img, landmark_gt, attribute_gt, euler_angle_gt in wlfw_val_dataloader:
img = img.to(device)
attribute_gt = attribute_gt.to(device)
landmark_gt = landmark_gt.to(device)
euler_angle_gt = euler_angle_gt.to(device)
pfld_backbone = pfld_backbone.to(device)
auxiliarynet = auxiliarynet.to(device)
_, landmark = pfld_backbone(img)
loss = torch.mean(torch.sum((landmark_gt - landmark)**2, axis=1))
losses.append(loss.cpu().numpy())
print("===> Evaluate:")
print('Eval set: Average loss: {:.4f} '.format(np.mean(losses)))
return np.mean(losses)
def main(args):
# Step 1: parse args config
logging.basicConfig(
format=
'[%(asctime)s] [p%(process)s] [%(pathname)s:%(lineno)d] [%(levelname)s] %(message)s',
level=logging.INFO,
handlers=[
logging.FileHandler(args.log_file, mode='w'),
logging.StreamHandler()
])
print_args(args)
# Step 2: model, criterion, optimizer, scheduler
pfld_backbone = PFLDInference().to(device)
auxiliarynet = AuxiliaryNet().to(device)
criterion = PFLDLoss()
optimizer = torch.optim.Adam([{
'params': pfld_backbone.parameters()
}, {
'params': auxiliarynet.parameters()
}],
lr=args.base_lr,
weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, mode='min', patience=args.lr_patience, verbose=True)
if args.resume:
checkpoint = torch.load(args.resume)
auxiliarynet.load_state_dict(checkpoint["auxiliarynet"])
pfld_backbone.load_state_dict(checkpoint["pfld_backbone"])
args.start_epoch = checkpoint["epoch"]
# step 3: data
# argumetion
transform = transforms.Compose([transforms.ToTensor()])
wlfwdataset = WLFWDatasets(args.dataroot, transform)
dataloader = DataLoader(wlfwdataset,
batch_size=args.train_batchsize,
shuffle=True,
num_workers=args.workers,
drop_last=False)
wlfw_val_dataset = WLFWDatasets(args.val_dataroot, transform)
wlfw_val_dataloader = DataLoader(wlfw_val_dataset,
batch_size=args.val_batchsize,
shuffle=False,
num_workers=args.workers)
# step 4: run
writer = SummaryWriter(args.tensorboard)
for epoch in range(args.start_epoch, args.end_epoch + 1):
weighted_train_loss, train_loss = train(dataloader, pfld_backbone,
auxiliarynet, criterion,
optimizer, epoch)
filename = os.path.join(str(args.snapshot),
"checkpoint_epoch_" + str(epoch) + '.pth.tar')
save_checkpoint(
{
'epoch': epoch,
'pfld_backbone': pfld_backbone.state_dict(),
'auxiliarynet': auxiliarynet.state_dict()
}, filename)
val_loss = validate(wlfw_val_dataloader, pfld_backbone, auxiliarynet,
criterion)
scheduler.step(val_loss)
writer.add_scalar('data/weighted_loss', weighted_train_loss, epoch)
writer.add_scalars('data/loss', {
'val loss': val_loss,
'train loss': train_loss
}, epoch)
writer.close()
def parse_args():
parser = argparse.ArgumentParser(description='pfld')
# general
parser.add_argument('-j', '--workers', default=0, type=int)
parser.add_argument('--devices_id', default='0', type=str) #TBD
parser.add_argument('--test_initial', default='false', type=str2bool) #TBD
# training
## -- optimizer
parser.add_argument('--base_lr', default=0.0001, type=int)
parser.add_argument('--weight-decay', '--wd', default=1e-6, type=float)
# -- lr
parser.add_argument("--lr_patience", default=40, type=int)
# -- epoch
parser.add_argument('--start_epoch', default=1, type=int)
parser.add_argument('--end_epoch', default=500, type=int)
# -- snapshot、tensorboard log and checkpoint
parser.add_argument('--snapshot',
default='./checkpoint/snapshot/',
type=str,
metavar='PATH')
parser.add_argument('--log_file',
default="./checkpoint/train.logs",
type=str)
parser.add_argument('--tensorboard',
default="./checkpoint/tensorboard",
type=str)
parser.add_argument(
'--resume',
default='',
type=str,
metavar='PATH')
# --dataset
parser.add_argument('--dataroot',
default='./data/train_data/list.txt',
type=str,
metavar='PATH')
parser.add_argument('--val_dataroot',
default='./data/test_data/list.txt',
type=str,
metavar='PATH')
parser.add_argument('--train_batchsize', default=256, type=int)
parser.add_argument('--val_batchsize', default=256, type=int)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
main(args)