-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_cdf.py
executable file
·221 lines (133 loc) · 5.5 KB
/
test_cdf.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
# -*- coding: utf-8 -*-
"""
Transfer Learning Project
author: Hao Zhou
"""
import torch
from tqdm import tqdm
import numpy as np
import os, random, time
# from signTrans.Constants import (
# # UNK_TOKEN,
# PAD_TOKEN,
# SOS_TOKEN,
# EOS_TOKEN,
# )
from signTrans.Models import Transformer, Transformer2d
from signTrans.utils import (
load_cfg,
cal_cdf,
)
from signTrans.batch import Batch
from signTrans.iter import make_iter
from model_loader import load_model
def main():
opt = load_cfg()
# For reproducibility
if opt['seed'] is not None:
torch.manual_seed(opt['seed'])
torch.backends.cudnn.benchmark = False
# torch.set_deterministic(True)
np.random.seed(opt['seed'])
random.seed(opt['seed'])
if not opt['output_dir']:
print('No experiment result will be saved.')
raise
if not os.path.exists(opt['output_dir']):
print("=== Please specify where model weights are ===")
return
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
opt['src_vocab_size'] = opt['dim_in'] * opt['seqlen']
opt['trg_vocab_size'] = opt['dim_out'] * opt['seqlen']
if opt['model_type'] == '2d':
from video2imu_datasets_2d import load_train, load_test
opt['src_is_text'] = False
opt['src_vocab_size'] = opt['dim_in'] * opt['seqlen']
if opt['model_type'] == 'Transformer_test':
from video2imu_datasets_test import load_train, load_test
opt['src_is_text'] = False
opt['src_vocab_size'] = opt['dim_in'] * opt['seqlen']
batch_size = opt['batch_size']
# if opt['batch_size'] != 1:
# batch_size = 1
_, _, testset, _, _ = load_test(opt)
# test_iter = make_iter(dataset = testset, batch_size=batch_size)
test_iter = make_iter(dataset = testset, batch_size=batch_size, train=False)
# import gc
# gc.collect()
# torch.cuda.empty_cache()
model = load_model(opt, device)
if torch.cuda.is_available():
opt['cuda'] = True
else:
opt['cuda'] = False
all_finger = []
model.eval()
start_time = time.time()
with torch.no_grad():
for batch in tqdm(test_iter, mininterval=2, desc=' - (Test)', leave=False):
batch = Batch(
batch=batch,
cuda=opt['cuda'],
)
if opt['model_type'] == 'multiseq':
src_seq_long = batch.src_long
src_seq_short = batch.src_short
trg_seq = batch.trg
pred = model(src_seq_long, src_seq_short, trg_seq)
else:
src_seq = batch.src
trg_seq = batch.trg
pred = model(src_seq, trg_seq)
abs_all_finger = cal_cdf(pred, trg_seq, opt)
all_finger.append(abs_all_finger)
print("total inference time {}".format(time.time() - start_time))
all_finger = np.concatenate(all_finger, axis=0)
print(all_finger.shape)
# all_finger = all_finger.reshape(all_finger.shape[0] * opt['seqlen'], opt['dim_out'])
# print(all_finger.shape)
###########################################
## get distance in mm
if mm == '_mm':
all_finger[:,5] = (all_finger[:, 5] + all_finger[:, 6]) / 2
all_finger[:,6] = 0
all_finger = np.deg2rad(all_finger)
all_finger = all_finger[:,0:6]
print(all_finger.shape)
all_finger[:, 0] *= 31.57
all_finger[:, 1] *= 39.78
all_finger[:, 2] *= 44.63
all_finger[:, 3] *= 41.37
all_finger[:, 4] *= 32.74
all_finger[:, 5] *= 46.22
print(all_finger.shape)
##########################################
idx = all_finger.argsort(axis=0)
all_finger = all_finger[idx, np.arange(idx.shape[1])]
# cut = int(all_finger.shape[0] * opt['cut_ratio'])
# print(all_finger.shape[0], cut, opt['cut_ratio'])
# all_finger = all_finger[:cut, :]
##########################################
# # average over each row
all_finger = np.mean(all_finger, axis=1)
print(all_finger.shape)
print(np.percentile(all_finger, 10), np.percentile(all_finger, 50), np.percentile(all_finger, 90))
print(float('%.2f' % np.percentile(all_finger, 50)), ' & ', \
float('%.2f' % np.percentile(all_finger, 90)), ' & ', \
float('%.2f' % np.mean(all_finger)), ' & ', \
float('%.2f' % np.std(all_finger)), ' & ', \
float('%.2f' % np.median(np.absolute(all_finger - np.median(all_finger)))), ' & ', \
float('%.2f' % np.mean(np.absolute(all_finger - np.mean(all_finger)))))
#########################################
if opt['output_dir'] == 'results':
fname = 'cdf.txt'
else:
fname = opt['output_dir'] + '.txt'
path = os.path.join(opt['output_dir'], 'cdf')
if not os.path.exists(path):
os.makedirs(path)
np.savetxt(path + os.sep + fname, all_finger, delimiter=',')
if __name__ == "__main__":
# mm = '_mm'
mm = ''
main()