forked from hzhou3/omniring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_generate_angle.py
More file actions
160 lines (97 loc) · 3.78 KB
/
test_generate_angle.py
File metadata and controls
160 lines (97 loc) · 3.78 KB
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
# -*- 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.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, cfg=opt, sort=False)
# print(pred.shape, abs_all_finger.shape)
all_finger.append(abs_all_finger)
print("total inference time {}".format(time.time() - start_time))
all_finger = np.concatenate(all_finger)
print(all_finger.shape)
path = os.path.join(opt['output_dir'], 'angle')
if not os.path.exists(path):
os.makedirs(path)
np.savetxt(path + os.sep + 'angle.txt', all_finger, delimiter=',')
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(np.percentile(all_finger, 10), np.percentile(all_finger, 50), np.percentile(all_finger, 90))
#########################################
if __name__ == "__main__":
main()