forked from thohemp/6DRepNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
199 lines (160 loc) · 6.4 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
import sys
import os
import argparse
import time
import numpy as np
import torch
from torchvision import transforms
import torch.backends.cudnn as cudnn
from model import SixDRepNet, SixDRepNet2
import datasets
from loss import GeodesicLoss
import torch.utils.model_zoo as model_zoo
import torchvision
def parse_args():
"""Parse input arguments."""
parser = argparse.ArgumentParser(
description='Head pose estimation using the 6DRepNet.')
parser.add_argument(
'--gpu', dest='gpu_id', help='GPU device id to use [0]',
default=0, type=int)
parser.add_argument(
'--num_epochs', dest='num_epochs',
help='Maximum number of training epochs.',
default=30, type=int)
parser.add_argument(
'--batch_size', dest='batch_size', help='Batch size.',
default=64, type=int)
parser.add_argument(
'--lr', dest='lr', help='Base learning rate.',
default=0.00001, type=float)
parser.add_argument(
'--dataset', dest='dataset', help='Dataset type.',
default='Pose_300W_LP', type=str) #Pose_300W_LP
parser.add_argument(
'--data_dir', dest='data_dir', help='Directory path for data.',
default='/datasets/300W_LP', type=str)#BIWI_70_30_train.npz
parser.add_argument(
'--filename_list', dest='filename_list',
help='Path to text file containing relative paths for every example.',
default='datasets/300W_LP/files.txt', type=str) #BIWI_70_30_train.npz #300W_LP/files.txt
parser.add_argument(
'--output_string', dest='output_string',
help='String appended to output snapshots.', default='', type=str)
parser.add_argument(
'--snapshot', dest='snapshot', help='Path of model snapshot.',
default='', type=str)
args = parser.parse_args()
return args
def get_ignored_params(model):
b = [model.layer0]
#b = [model.conv1, model.bn1, model.fc_finetune]
for i in range(len(b)):
for module_name, module in b[i].named_modules():
if 'bn' in module_name:
module.eval()
for name, param in module.named_parameters():
yield param
def get_non_ignored_params(model):
b = [model.layer1, model.layer2, model.layer3, model.layer4]
for i in range(len(b)):
for module_name, module in b[i].named_modules():
if 'bn' in module_name:
module.eval()
for name, param in module.named_parameters():
yield param
def get_fc_params(model):
b = [model.linear_reg]
for i in range(len(b)):
for module_name, module in b[i].named_modules():
for name, param in module.named_parameters():
yield param
def load_filtered_state_dict(model, snapshot):
# By user apaszke from discuss.pytorch.org
model_dict = model.state_dict()
snapshot = {k: v for k, v in snapshot.items() if k in model_dict}
model_dict.update(snapshot)
model.load_state_dict(model_dict)
if __name__ == '__main__':
args = parse_args()
cudnn.enabled = True
num_epochs = args.num_epochs
batch_size = args.batch_size
gpu = args.gpu_id
if not os.path.exists('output/snapshots'):
os.makedirs('output/snapshots')
summary_name = '{}_{}_bs{}'.format(
'SixDRepNet', int(time.time()), args.batch_size)
if not os.path.exists('output/snapshots/{}'.format(summary_name)):
os.makedirs('output/snapshots/{}'.format(summary_name))
model = SixDRepNet(backbone_name='RepVGG-B1g2',
backbone_file='RepVGG-B1g2-train.pth',
deploy=False,
pretrained=True)
if not args.snapshot == '':
saved_state_dict = torch.load(args.snapshot)
model.load_state_dict(saved_state_dict['model_state_dict'])
print('Loading data.')
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
transformations = transforms.Compose([transforms.Resize(240),
transforms.RandomCrop(224),
transforms.ToTensor(),
normalize])
pose_dataset = datasets.getDataset(
args.dataset, args.data_dir, args.filename_list, transformations)
train_loader = torch.utils.data.DataLoader(
dataset=pose_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=4)
model.cuda(gpu)
crit = GeodesicLoss().cuda(gpu) #torch.nn.MSELoss().cuda(gpu)
optimizer = torch.optim.Adam([
{'params': get_ignored_params(model), 'lr': 0},
{'params': get_non_ignored_params(model), 'lr': args.lr},
{'params': get_fc_params(model), 'lr': args.lr * 10}
], lr=args.lr)
if not args.snapshot == '':
optimizer.load_state_dict(saved_state_dict['optimizer_state_dict'])
#milestones = np.arange(num_epochs)
milestones = [10, 20]
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=milestones, gamma=0.5)
print('Starting training.')
for epoch in range(num_epochs):
loss_sum = .0
iter = 0
for i, (images, gt_mat, _, _) in enumerate(train_loader):
iter += 1
images = torch.Tensor(images).cuda(gpu)
# Forward pass
pred_mat = model(images)
# Calc loss
loss = crit(gt_mat.cuda(gpu), pred_mat)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_sum += loss.item()
if (i+1) % 100 == 0:
print('Epoch [%d/%d], Iter [%d/%d] Loss: '
'%.6f' % (
epoch+1,
num_epochs,
i+1,
len(pose_dataset)//batch_size,
loss.item(),
)
)
scheduler.step()
# Save models at numbered epochs.
if epoch % 1 == 0 and epoch < num_epochs:
print('Taking snapshot...',
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
}, 'output/snapshots/' + summary_name + '/' + args.output_string +
'_epoch_' + str(epoch+1) + '.tar')
)