-
Notifications
You must be signed in to change notification settings - Fork 72
/
train.py
executable file
·168 lines (149 loc) · 5.32 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
#coding=utf-8
import torch
from torch.autograd import Variable
from torch.backends import cudnn
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import numpy as np
import pprint
from data_loader import KFDataset
from models import KFSGNet
config = dict()
config['lr'] = 0.000001
config['momentum'] = 0.9
config['weight_decay'] = 1e-4
config['epoch_num'] = 400
config['batch_size'] = 72
config['sigma'] = 5.
config['debug_vis'] = False # 是否可视化heatmaps
config['fname'] = 'data/test.csv'
# config['fname'] = 'data/training.csv'
# config['is_test'] = False
config['is_test'] = True
config['save_freq'] = 10
config['checkout'] = 'data/weight/kd_epoch_909_model.ckpt'
config['start_epoch'] = 850
config['eval_freq'] = 5
config['debug'] = False
config['lookup'] = 'data/IdLookupTable.csv'
config['featurename2id'] = {
'left_eye_center_x':0,
'left_eye_center_y':1,
'right_eye_center_x':2,
'right_eye_center_y':3,
'left_eye_inner_corner_x':4,
'left_eye_inner_corner_y':5,
'left_eye_outer_corner_x':6,
'left_eye_outer_corner_y':7,
'right_eye_inner_corner_x':8,
'right_eye_inner_corner_y':9,
'right_eye_outer_corner_x':10,
'right_eye_outer_corner_y':11,
'left_eyebrow_inner_end_x':12,
'left_eyebrow_inner_end_y':13,
'left_eyebrow_outer_end_x':14,
'left_eyebrow_outer_end_y':15,
'right_eyebrow_inner_end_x':16,
'right_eyebrow_inner_end_y':17,
'right_eyebrow_outer_end_x':18,
'right_eyebrow_outer_end_y':19,
'nose_tip_x':20,
'nose_tip_y':21,
'mouth_left_corner_x':22,
'mouth_left_corner_y':23,
'mouth_right_corner_x':24,
'mouth_right_corner_y':25,
'mouth_center_top_lip_x':26,
'mouth_center_top_lip_y':27,
'mouth_center_bottom_lip_x':28,
'mouth_center_bottom_lip_y':29,
}
def get_peak_points(heatmaps):
"""
:param heatmaps: numpy array (N,15,96,96)
:return:numpy array (N,15,2)
"""
N,C,H,W = heatmaps.shape
all_peak_points = []
for i in range(N):
peak_points = []
for j in range(C):
yy,xx = np.where(heatmaps[i,j] == heatmaps[i,j].max())
y = yy[0]
x = xx[0]
peak_points.append([x,y])
all_peak_points.append(peak_points)
all_peak_points = np.array(all_peak_points)
return all_peak_points
def get_mse(pred_points,gts,indices_valid=None):
"""
:param pred_points: numpy (N,15,2)
:param gts: numpy (N,15,2)
:return:
"""
pred_points = pred_points[indices_valid[0],indices_valid[1],:]
gts = gts[indices_valid[0],indices_valid[1],:]
pred_points = Variable(torch.from_numpy(pred_points).float(),requires_grad=False)
gts = Variable(torch.from_numpy(gts).float(),requires_grad=False)
criterion = nn.MSELoss()
loss = criterion(pred_points,gts)
return loss
def calculate_mask(heatmaps_target):
"""
:param heatmaps_target: Variable (N,15,96,96)
:return: Variable (N,15,96,96)
"""
N,C,_,_ = heatmaps_targets.size()
N_idx = []
C_idx = []
for n in range(N):
for c in range(C):
max_v = heatmaps_targets[n,c,:,:].max().data[0]
if max_v != 0.0:
N_idx.append(n)
C_idx.append(c)
mask = Variable(torch.zeros(heatmaps_targets.size()))
mask[N_idx,C_idx,:,:] = 1.
mask = mask.float().cuda()
return mask,[N_idx,C_idx]
if __name__ == '__main__':
pprint.pprint(config)
torch.manual_seed(0)
cudnn.benchmark = True
net = KFSGNet()
net.float().cuda()
net.train()
criterion = nn.MSELoss()
# optimizer = optim.SGD(net.parameters(), lr=config['lr'], momentum=config['momentum'] , weight_decay=config['weight_decay'])
optimizer = optim.Adam(net.parameters(),lr=config['lr'])
trainDataset = KFDataset(config)
trainDataset.load()
trainDataLoader = DataLoader(trainDataset,config['batch_size'],True)
sample_num = len(trainDataset)
if (config['checkout'] != ''):
net.load_state_dict(torch.load(config['checkout']))
for epoch in range(config['start_epoch'],config['epoch_num']+config['start_epoch']):
running_loss = 0.0
for i, (inputs, heatmaps_targets, gts) in enumerate(trainDataLoader):
inputs = Variable(inputs).cuda()
heatmaps_targets = Variable(heatmaps_targets).cuda()
mask,indices_valid = calculate_mask(heatmaps_targets)
optimizer.zero_grad()
outputs = net(inputs)
outputs = outputs * mask
heatmaps_targets = heatmaps_targets * mask
loss = criterion(outputs, heatmaps_targets)
loss.backward()
optimizer.step()
# 统计最大值与最小值
v_max = torch.max(outputs)
v_min = torch.min(outputs)
# 评估
all_peak_points = get_peak_points(heatmaps_targets.cpu().data.numpy())
loss_coor = get_mse(all_peak_points, gts.numpy(),indices_valid)
print('[ Epoch {:005d} -> {:005d} / {} ] loss : {:15} loss_coor : {:15} max : {:10} min : {}'.format(
epoch, i * config['batch_size'],
sample_num, loss.data[0],loss_coor.data[0],v_max.data[0],v_min.data[0]))
if (epoch+1) % config['save_freq'] == 0 or epoch == config['epoch_num'] - 1:
torch.save(net.state_dict(),'kd_epoch_{}_model.ckpt'.format(epoch))