-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpm_train.py
197 lines (148 loc) · 7.99 KB
/
cpm_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
"""
"""
# from data_loader.uci_hand_data import UCIHandPoseDataset as Mydata
from dataloaders.cmu_hand_data import CMUHand as Mydata
from network.cpm import CPM
from network.cpm_mobilenet import CPM_MobileNet
from src.util import *
import scipy.misc
import os
from collections import OrderedDict
import torch
import torch.optim as optim
import torch.nn as nn
import configparser
from torch.autograd import Variable
from torch.utils.data import DataLoader
from modules.load_state import load_state, load_from_mobilenet
from modules.get_parameters import get_parameters_conv, get_parameters_bn, get_parameters_conv_depthwise
# multi-GPU
# *********************** hyper parameter ***********************
config = configparser.ConfigParser()
config.read('conf.text')
train_data_dir = config.get('data', 'train_data_dir')
train_label_dir = config.get('data', 'train_label_dir')
train_large_data_dir = config.get('data', 'train_large_data_dir')
train_large_label_dir = config.get('data', 'train_large_label_dir')
train_synth_data_dir = config.get('data', 'train_synth_data_dir')
train_synth_label_dir = config.get('data', 'train_synth_label_dir')
save_dir = config.get('data', 'save_dir')
learning_rate = config.getfloat('training', 'learning_rate')
batch_size = config.getint('training', 'batch_size')
epochs = config.getint('training', 'epochs')
begin_epoch = config.getint('training', 'begin_epoch')
device_ids = config.get('training', 'device_ids')
device_ids = [int(x) for x in device_ids.split(',')]
cuda = torch.cuda.is_available()
if not os.path.exists(save_dir):
os.mkdir(save_dir)
# *********************** Build dataset ***********************
# train_data = Mydata(data_dir=train_data_dir, label_dir=train_label_dir)
train_data = Mydata(data_dir=[train_data_dir,train_synth_data_dir, train_large_data_dir], label_dir=[train_label_dir, train_synth_label_dir, train_large_label_dir])
print('Train dataset total number of images sequence is ----' + str(len(train_data)))
# Data Loader
train_dataset = DataLoader(train_data, batch_size=batch_size, shuffle=True)
# *********************** Build model ***********************
# net = CPM(out_c=21)
n_refine_stages = 3
net = CPM_MobileNet(n_refine_stages)
# base_lr = 4e-7# 4e-5
# optimizer = optim.Adam([
# {'params': get_parameters_conv(net.model, 'weight')},
# {'params': get_parameters_conv_depthwise(net.model, 'weight'), 'weight_decay': 0},
# {'params': get_parameters_bn(net.model, 'weight'), 'weight_decay': 0},
# {'params': get_parameters_bn(net.model, 'bias'), 'lr': base_lr * 2, 'weight_decay': 0},
# {'params': get_parameters_conv(net.cpm, 'weight'), 'lr': base_lr},
# {'params': get_parameters_conv(net.cpm, 'bias'), 'lr': base_lr * 2, 'weight_decay': 0},
# {'params': get_parameters_conv_depthwise(net.cpm, 'weight'), 'weight_decay': 0},
# {'params': get_parameters_conv(net.initial_stage, 'weight'), 'lr': base_lr},
# {'params': get_parameters_conv(net.initial_stage, 'bias'), 'lr': base_lr * 2, 'weight_decay': 0},
# {'params': get_parameters_bn(net.initial_stage, 'weight'), 'weight_decay': 0},
# {'params': get_parameters_bn(net.initial_stage, 'bias'), 'lr': base_lr * 2, 'weight_decay': 0},
# {'params': get_parameters_conv(net.refinement_stages, 'weight'), 'lr': base_lr * 4},
# {'params': get_parameters_conv(net.refinement_stages, 'bias'), 'lr': base_lr * 8, 'weight_decay': 0},
# {'params': get_parameters_bn(net.refinement_stages, 'weight'), 'weight_decay': 0},
# {'params': get_parameters_bn(net.refinement_stages, 'bias'), 'lr': base_lr * 2, 'weight_decay': 0},
# ], lr=base_lr, weight_decay=5e-4)
optimizer = optim.Adam(params=net.parameters(), lr=learning_rate, betas=(0.5, 0.999))
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, patience=100, threshold=1e-2, eps=1e-16, verbose=True)
# cuda = False
if cuda:
print("Detected gpus.")
net = net.cuda(device_ids[0])
net = nn.DataParallel(net, device_ids=device_ids)
pretrained = False
if begin_epoch > 0:
save_path = os.path.join(save_dir, 'cpm_r' + str(n_refine_stages) + '_model_epoch' + str(begin_epoch) + '.pth')
state_dict = torch.load(save_path, lambda storage, loc: storage)
net.module.load_state_dict(state_dict)
# if cuda:
# net.load_state_dict(state_dict)
# else:
# # trained with DataParallel but test on cpu
# single_state_dict = OrderedDict()
# for k, v in state_dict.items():
# name = k.replace("module.", "") # remove `module.`
# single_state_dict[name] = v
# # load params
# net.load_state_dict(single_state_dict)
elif pretrained:
pretrained_model_path = "ckpt/mobilenet_sgd_68.848.pth.tar"
pretrained_checkpoint = torch.load(pretrained_model_path, lambda storage, loc: storage)
load_from_mobilenet(net, pretrained_checkpoint)
def train():
# *********************** initialize optimizer ***********************
# optimizer = optim.Adam(params=net.parameters(), lr=learning_rate, betas=(0.5, 0.999))
criterion = nn.MSELoss(reduction='mean') # loss function MSE average
net.train()
for epoch in range(begin_epoch, epochs + 1):
print('epoch....................' + str(epoch))
for step, (image, label_map, label_mask, imgs) in enumerate(train_dataset):
image = image.cuda(device_ids[0]) if cuda else image # 4D Tensor
# Batch_size * 3 * width(368) * height(368)
# 4D Tensor to 5D Tensor
label_map = torch.stack([label_map]*(n_refine_stages + 1), dim=1)
# Batch_size * 21 * 45 * 45
# Batch_size * 6 * 21 * 45 * 45
label_map = label_map.cuda(device_ids[0]) if cuda else label_map
# Batch_size * 21
label_mask = label_mask.cuda(device_ids[0]) if cuda else label_mask
# Batch_size * 1 * 21 * 1 * 1
label_mask.unsqueeze_(1).unsqueeze_(-1).unsqueeze_(-1)
# last batch could be smaller than the actual batch size
fillings = torch.zeros(label_map.size()).cuda(device_ids[0]) if cuda else torch.zeros(label_map.size())
# only visible keypoints contribute to loss
masked_label_map = torch.where(label_mask, label_map, fillings)
# center_map = Variable(center_map.cuda() if cuda else center_map) # 4D Tensor
# Batch_size * width(368) * height(368)
optimizer.zero_grad()
# pred_6 = net(image, center_map) # 5D tensor: batch size * stages * 21 * 45 * 45
pred_multi_stage = net(image)
masked_label_pred = torch.where(label_mask, pred_multi_stage, fillings)
# ******************** calculate loss of each joints ********************
# loss = criterion(pred_multi_stage, label_map)
loss = criterion(masked_label_pred, masked_label_map)
# backward
loss.backward()
optimizer.step()
if step % 10 == 0:
print('--step .....' + str(step))
print('--loss ' + str(float(loss.data.item())))
# if epoch % 100 == 0:
# save_images(label_map[:, -1, :, :, :].cpu(), pred_6[:, -1, :, :, :].cpu(), step, epoch, imgs)
# break
if step == 0 and epoch % 20 == 0:
save_images(image.cpu(), label_map[:, -1, :, :, :].cpu(), pred_multi_stage[:, -1, :, :, :].cpu(),
step, epoch, imgs)
# break
scheduler.step(loss, epoch)
if epoch % 20 == 0:
if isinstance(net, torch.nn.DataParallel):
torch.save(net.module.state_dict(),
os.path.join(save_dir, 'cpm_r' + str(n_refine_stages) + '_model_epoch{:d}.pth'.format(epoch)))
else:
torch.save(net.state_dict(),
os.path.join(save_dir, 'cpm_r' + str(n_refine_stages) + '_model_epoch{:d}.pth'.format(epoch)))
print('train done!')
if __name__ == '__main__':
train()