-
Notifications
You must be signed in to change notification settings - Fork 10
/
main_baseline_human.py
executable file
·277 lines (234 loc) · 11.2 KB
/
main_baseline_human.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import argparse
import os
import time
import torch
import torch.utils.data
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
from tensorboardX import SummaryWriter
from torch.optim.lr_scheduler import StepLR
from model.dataset import ShadowPairedDataset
from model.model import *
parser = argparse.ArgumentParser(description='deepShadowTeleop')
parser.add_argument('--tag', type=str, default='default')
parser.add_argument('--epoch', type=int, default=200)
parser.add_argument('--mode', choices=['train', 'test'], required=True)
parser.add_argument('--batch-size', type=int, default=1)
parser.add_argument('--cuda', action='store_true')
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--load-model', type=str, default='')
parser.add_argument('--load-epoch', type=int, default=-1)
parser.add_argument('--model-path', type=str, default='./assets/learned_models',
help='pre-trained model path')
parser.add_argument('--data-path', type=str, default='./data', help='data path')
parser.add_argument('--log-interval', type=int, default=10)
parser.add_argument('--save-interval', type=int, default=1)
args = parser.parse_args()
args.cuda = args.cuda if torch.cuda.is_available else False
if args.cuda:
torch.cuda.manual_seed(1)
logger = SummaryWriter(os.path.join('./assets/log/', args.tag))
np.random.seed(int(time.time()))
def worker_init_fn(pid):
np.random.seed(torch.initial_seed() % (2**31-1))
def my_collate(batch):
batch = list(filter(lambda x:x is not None, batch))
return torch.utils.data.dataloader.default_collate(batch)
input_viewpoint=[0,1,2,3,4,5,6,7,8]
input_size=100
embedding_size=128
joint_size=22
thresh_acc=[0.2, 0.25, 0.3]
joint_upper_range = torch.tensor([0.349, 1.571, 1.571, 1.571, 0.785, 0.349, 1.571, 1.571,
1.571, 0.349, 1.571, 1.571, 1.571, 0.349, 1.571, 1.571,
1.571, 1.047, 1.222, 0.209, 0.524, 1.571])
joint_lower_range = torch.tensor([-0.349, 0, 0, 0, 0, -0.349, 0, 0, 0, -0.349, 0, 0, 0,
-0.349, 0, 0, 0, -1.047, 0, -0.209, -0.524, 0])
train_loader = torch.utils.data.DataLoader(
ShadowPairedDataset(
path=args.data_path,
input_size=input_size,
input_viewpoint=input_viewpoint,
is_train=True,
),
batch_size=args.batch_size,
num_workers=32,
pin_memory=True,
shuffle=True,
worker_init_fn=worker_init_fn,
collate_fn=my_collate,
)
test_loader = torch.utils.data.DataLoader(
ShadowPairedDataset(
path=args.data_path,
input_size=input_size,
input_viewpoint=input_viewpoint,
is_train=False,
with_name=True,
),
batch_size=args.batch_size,
num_workers=32,
pin_memory=True,
shuffle=True,
worker_init_fn=worker_init_fn,
collate_fn=my_collate,
)
is_resume = 0
if args.load_model and args.load_epoch != -1:
is_resume = 1
if is_resume or args.mode == 'test':
model = torch.load(args.load_model, map_location='cuda:{}'.format(args.gpu))
model.device_ids = [args.gpu]
print('load model {}'.format(args.load_model))
else:
# model = TeachingTeleModel(input_size=input_size, embedding_size=embedding_size, joint_size=joint_size)
# model = NaiveTeleModel(input_size=input_size, embedding_size=embedding_size, joint_size=joint_size)
model = NaiveRENModel(input_size=input_size, embedding_size=embedding_size, joint_size=joint_size)
if args.cuda:
if args.gpu != -1:
torch.cuda.set_device(args.gpu)
model = model.cuda()
else:
device_id = [1,2]
torch.cuda.set_device(device_id[0])
model = nn.DataParallel(model, device_ids=device_id).cuda()
joint_upper_range = joint_upper_range.cuda()
joint_lower_range = joint_lower_range.cuda()
optimizer = optim.Adam(model.parameters(), lr=args.lr)
scheduler = StepLR(optimizer, step_size=50, gamma=0.5)
def train(model, loader, epoch):
scheduler.step()
model.train()
torch.set_grad_enabled(True)
correct_human = [0,0,0]
train_error_human = 0
for batch_idx, (shadow, human, target) in enumerate(loader):
if args.cuda:
shadow, human, target = shadow.cuda(), human.cuda(), target.cuda()
# human part
optimizer.zero_grad()
embedding_human, joint_human = model(human, is_human=True)
joint_human = joint_human * (joint_upper_range - joint_lower_range) + joint_lower_range
loss_human_reg = F.mse_loss(joint_human, target)
loss_human_cons = constraints_loss(joint_human)/target.shape[0]
loss_human = loss_human_reg + loss_human_cons
loss_human.backward()
optimizer.step()
loss = loss_human
# compute acc
res_human = [np.sum(np.sum(abs(joint_human.cpu().data.numpy() - target.cpu().data.numpy()) < thresh,
axis=-1) == joint_size) for thresh in thresh_acc]
correct_human = [c + r for c, r in zip(correct_human, res_human)]
# compute average angle error
train_error_human += F.l1_loss(joint_human, target, size_average=False)/joint_size
if batch_idx % args.log_interval == 0:
if isinstance(loss_human_cons, float):
loss_human_cons = torch.zeros(1)
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tLoss_reg_human: {:.6f}\t'
'Loss_cons_human: {:.6f}\t{}'.format(
epoch, batch_idx * args.batch_size, len(loader.dataset),
100. * batch_idx * args.batch_size / len(loader.dataset),
loss.item(), loss_human_reg.item(), loss_human_cons.item(), args.tag))
logger.add_scalar('train_loss', loss.item(),
batch_idx + epoch * len(loader))
logger.add_scalar('train_loss_human_reg', loss_human_reg.item(),
batch_idx + epoch * len(loader))
logger.add_scalar('train_loss_human_cons', loss_human_cons.item(),
batch_idx + epoch * len(loader))
train_error_human /= len(loader.dataset)
acc_human = [float(c) / float(len(loader.dataset)) for c in correct_human]
return acc_human, train_error_human
def test(model, loader):
model.eval()
torch.set_grad_enabled(False)
test_loss_human_reg = 0
test_loss_human_cons = 0
correct_human = [0,0,0]
test_error_human = 0
res = []
for shadow, human, target, name in loader:
if args.cuda:
shadow, human, target = shadow.cuda(), human.cuda(), target.cuda()
# human part
embedding_human, joint_human = model(human, is_human=True)
joint_human = joint_human * (joint_upper_range - joint_lower_range) + joint_lower_range
test_loss_human_reg += F.mse_loss(joint_human, target, size_average=False).item()
cons = constraints_loss(joint_human)
if not isinstance(cons, float):
test_loss_human_cons += cons
# compute acc
res_human = [np.sum(np.sum(abs(joint_human.cpu().data.numpy() - target.cpu().data.numpy()) < thresh,
axis=-1) == joint_size) for thresh in thresh_acc]
correct_human = [c + r for c, r in zip(correct_human, res_human)]
# compute average angle error
test_error_human += F.l1_loss(joint_human, target, size_average=False)/joint_size
res.append((name, joint_human))
test_loss_human_reg /= len(loader.dataset)
test_loss_human_cons /= len(loader.dataset)
test_loss = test_loss_human_reg + test_loss_human_cons
test_error_human /= len(loader.dataset)
acc_human = [float(c)/float(len(loader.dataset)) for c in correct_human]
# f = open('input.csv', 'w')
# for batch in res:
# for name, joint in zip(batch[0], batch[1]):
# buf = [name, '0.0', '0.0'] + [str(i) for i in joint.cpu().data.numpy()]
# f.write(','.join(buf) + '\n')
return acc_human, test_error_human, test_loss, test_loss_human_reg, test_loss_human_cons
def constraints_loss(joint_angle):
F4 = [joint_angle[:, 0], joint_angle[:, 5], joint_angle[:, 9], joint_angle[:, 13]]
F1_3 = [joint_angle[:, 1], joint_angle[:, 6], joint_angle[:, 10], joint_angle[:, 14],
joint_angle[:, 2], joint_angle[:, 7], joint_angle[:, 11], joint_angle[:, 15],
joint_angle[:, 3], joint_angle[:, 8], joint_angle[:, 12], joint_angle[:, 16],
joint_angle[:, 21]]
loss_cons = 0.0
for pos in F1_3:
for f in pos:
loss_cons = loss_cons + max(0 - f, 0) + max(f - 1.57, 0)
for pos in F4:
for f in pos:
loss_cons = loss_cons + max(-0.349 - f, 0) + max(f - 0.349, 0)
for f in joint_angle[:, 4]:
loss_cons = loss_cons + max(0 - f, 0) + max(f - 0.785, 0)
for f in joint_angle[:, 17]:
loss_cons = loss_cons + max(-1.047 - f, 0) + max(f - 1.047, 0)
for f in joint_angle[:, 18]:
loss_cons = loss_cons + max(0 - f, 0) + max(f - 1.222, 0)
for f in joint_angle[:, 19]:
loss_cons = loss_cons + max(-0.209 - f, 0) + max(f - 0.209, 0)
for f in joint_angle[:, 20]:
loss_cons = loss_cons + max(-0.524 - f, 0) + max(f - 0.524, 0)
return loss_cons
def main():
if args.mode == 'train':
for epoch in range(is_resume*args.load_epoch, args.epoch):
acc_train_human, train_error_human = train(model, train_loader, epoch)
print('Train done, acc_human={}, train_error_human={}'.format(acc_train_human, train_error_human))
acc_test_human, test_error_human, loss, loss_human_reg, loss_human_cons = test(model, test_loader)
print('Test done, acc_human={}, error_human={}, loss={}, loss_human_reg={}, '\
'loss_human_cons={}'.format(acc_test_human, test_error_human, loss, loss_human_reg,
loss_human_cons))
logger.add_scalar('train_acc_human0.2', acc_train_human[0], epoch)
logger.add_scalar('train_acc_human0.25', acc_train_human[1], epoch)
logger.add_scalar('train_acc_human0.3', acc_train_human[2], epoch)
logger.add_scalar('test_acc_human0.2', acc_test_human[0], epoch)
logger.add_scalar('test_acc_human0.25', acc_test_human[1], epoch)
logger.add_scalar('test_acc_human0.3', acc_test_human[2], epoch)
logger.add_scalar('test_error_human', test_error_human, epoch)
logger.add_scalar('test_loss', loss, epoch)
logger.add_scalar('test_loss_human_reg', loss_human_reg, epoch)
logger.add_scalar('test_loss_human_cons', loss_human_cons, epoch)
if epoch % args.save_interval == 0:
path = os.path.join(args.model_path, args.tag + '_{}.model'.format(epoch))
torch.save(model, path)
print('Save model @ {}'.format(path))
else:
print('testing...')
acc_test_human, test_error_human, loss, loss_human_reg, loss_human_cons = test(model, test_loader)
print('Test done, acc_human={}, error_human={}, loss={}, loss_human_reg={}, ' \
'loss_human_cons={}'.format(acc_test_human, test_error_human, loss, loss_human_reg,
loss_human_cons))
if __name__ == "__main__":
main()