-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference.py
384 lines (317 loc) · 13.7 KB
/
inference.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import time
import torch
from torch import nn
from torch.utils.data import DataLoader
from dataset import SkelDataset, SkelSeqDataset, OrthoticDataset
from models.linear import LinearRegressor
from models.lstm import LSTMRegressor
from skel_viewer import plot_skeleton
num_gyro = 44
num_skel = 51
orthotic_width = 10
orthotic_height = 30
seq_len = 5000
class Inference:
def __init__(self, infer_type, model=None, model_type='lstm', model_dir='logs/', model_file='gp2s.pt', batch_size=512, n_layers=8, seq_len=32, loss='mse', gpu_ids=0):
self.infer_type = infer_type
self.model_type = model_type
self.model_dir = model_dir
self.model_file = model_file
self.batch_size = batch_size
self.n_layers = n_layers
self.seq_len = seq_len
self.loss = loss
# Device
self.device = torch.device('cuda:{}'.format(gpu_ids)) if torch.cuda.is_available() else torch.device('cpu')
self.num_workers = torch.get_num_threads()
# Loss
self.criterion = nn.MSELoss()
# Empty Dataset
self.dataset = None
self.extra_data = None
self.dataloader = None
# Load Model
if model is not None:
self.net = model.to(self.device)
else:
if self.model_type == 'linear':
self.net = LinearRegressor(num_gyro, num_skel)
elif self.model_type == 'lstm':
self.net = LSTMRegressor(num_gyro, num_skel, num_layers=self.n_layers)
self.net.load_state_dict(torch.load(os.path.join(model_dir, model_file)))
self.net = self.net.to(self.device)
self.net.eval()
def setAttr(self, batch_size=None, seq_len=None, gpu_ids=None, loss='None'):
if batch_size is not None:
self.batch_size = batch_size
if seq_len is not None:
self.seq_len = seq_len
if gpu_ids is not None:
self.gpu_ids = gpu_ids
self.device = torch.device('cuda:{}'.format(gpu_ids)) if torch.cuda.is_available() else torch.device('cpu')
def infer(self, gyro_data=None, skel_data=None, visualize=False):
if gyro_data is not None:
# repeat first data when first infer
if self.dataset is None:
if torch.is_tensor(gyro_data):
self.extra_data = torch.cat([gyro_data[:1]]*self.seq_len, dim=0)
else:
self.extra_data = np.concatenate([gyro_data[:1]]*self.seq_len, axis=0)
# concat from previous data
if torch.is_tensor(gyro_data):
gyro_data = torch.cat([torch.Tensor(self.extra_data), gyro_data], dim=0)
else:
gyro_data = np.concatenate([self.extra_data, gyro_data], axis=0)
self.extra_data = gyro_data[-self.seq_len:]
# Iniitalize dataset
if self.model_type == 'linear':
self.dataset = SkelDataset(train=False, data_x=gyro_data, data_y=skel_data)
elif self.model_type == 'lstm':
self.dataset = SkelSeqDataset(train=False, seq_len=self.seq_len, data_x=gyro_data, data_y=skel_data)
self.dataloader = DataLoader(dataset=self.dataset, batch_size=self.batch_size,
shuffle=False, num_workers=0)
# Inference
#self.net.eval()
with torch.no_grad():
test_loss = 0
test_len = 0
test_pred = torch.empty(0,num_skel)
for x, y in self.dataloader:
batch_size = len(y)
x, y = x.to(self.device), y.to(self.device)
if self.model_type == 'linear':
pred = self.net(x)
elif self.model_type == 'lstm':
hc = self.net.init_hidden_cell(batch_size)
pred, hc = self.net(x, hc)
# Calc loss
if skel_data is not None:
loss = self.criterion(pred, y)
test_loss += loss
test_len += len(x)
# Concat predictions
pred = pred.cpu()
#if model_type == 'lstm':
# pred = pred.reshape(pred.size(0)*pred.size(1), pred.size(2))
test_pred = torch.cat([test_pred, pred])
# Print loss when skeleton data exist
if skel_data is not None:
print('Test Loss: {}'.format(test_loss / test_len))
#print(test_pred.size())
test_pred = test_pred.numpy()
# plotting predicted skeleton
if visualize:
if self.infer_type == 'gp2s':
plot_skeleton(test_pred, is_csv=False)
elif self.infer_type == 'orthotics':
pass
return test_pred
def infer_csv(self, csv_file='skeleton_data/skeleton_walking.csv', pivot=0.8, visualize=False):
df = pd.read_csv(csv_file, index_col=0)
x = np.array(df.iloc[:,:44].values)
y = np.array(df.iloc[:,44:].values)
pivot = int(len(y) * pivot)
x, y = x[pivot:], y[pivot:]
test_pred = self.infer(gyro_data=x, skel_data=y, visualize=visualize)
return test_pred
# Predict skeleton from gyro data
# gyro_data is Numpy array(data_num x 44)
# (optional) skel_data is also Numpy array(data_num x 51, for Calculating Loss)
# Two options for Loading Model
# 1. model: get model from memory
# 2. model_dir, model_file: Load model from file path
# Return predicted skeleton: Numpy array(data_num x 51)
def gp2s(gyro_data=None, skel_data=None, model=None, seq_len=1, model_dir='logs/', model_type='lstm', model_file='gp2s.pt', batch_size=512, n_layers=6, gpu_ids=0, plot_skel=False):
if model_type == 'linear':
test_dataset = SkelDataset(train=False, data_x=gyro_data, data_y=skel_data)
elif model_type == 'lstm':
test_dataset = SkelSeqDataset(train=False, seq_len=seq_len, data_x=gyro_data, data_y=skel_data)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,
shuffle=False, num_workers=0)
# Device
device = torch.device('cuda:{}'.format(gpu_ids)) if torch.cuda.is_available() else torch.device('cpu')
# Calculate loss when skeleton data exist
if skel_data is not None:
criterion = nn.MSELoss()
# Load Model
if model is not None:
net = model.to(device)
else:
if model_type == 'linear':
net = LinearRegressor(num_gyro, num_skel)
elif model_type == 'lstm':
net = LSTMRegressor(num_gyro, num_skel, num_layers=n_layers)
net.load_state_dict(torch.load(os.path.join(model_dir, model_file)))
net = net.to(device)
# Inference
net.eval()
with torch.no_grad():
test_loss = 0
test_len = 0
test_pred = torch.empty(0,num_skel)
for x, y in test_loader:
batch_size = len(y)
x, y = x.to(device), y.to(device)
if model_type == 'linear':
pred = net(x)
elif model_type == 'lstm':
hc = net.init_hidden_cell(batch_size)
pred, hc = net(x, hc)
# Calc loss
if skel_data is not None:
loss = criterion(pred, y)
test_loss += loss
test_len += len(x)
# Concat predictions
pred = pred.cpu()
#if model_type == 'lstm':
# pred = pred.reshape(pred.size(0)*pred.size(1), pred.size(2))
test_pred = torch.cat([test_pred, pred])
# Print loss when skeleton data exist
if skel_data is not None:
print('Test Loss: {}'.format(test_loss / test_len))
print(test_pred.size())
test_pred = test_pred.numpy()
# plotting predicted skeleton
if plot_skel:
plot_skeleton(test_pred, is_csv=False)
return test_pred
# Predict orthotics from gyro data
# gyro_data is Numpy array(subject_num, seq_num(must 5000) x 44)
# 여러개의 csv를 읽을 땐 dataset.py의 read_orthotics_data()를 사용하시면 되고,
# 만약 한개의 csv만 사용한다면, numpy로 변환만 한 후 함수호출하셔도 됩니다.
# (optional) orthotic_left(right) is also Numpy array(data_num x 30 x 10, for Calculating Loss)
# model_type: lstm or linear (must be matched with .pt file)
# Two options for Loading Model
# 1. model: get model from memory
# 2. model_dir, model_file: Load model from file path
# Return predicted orthotics(left, right): each Numpy array(data_num x 30 x 10)
def orthotics(gyro_data=None, orthotic_left=None, orthotic_right=None, model_type='linear', model=None, model_dir='logs/', model_file='orthotics_fc.pt', batch_size=1, gpu_ids=0):
data_y = None
# concat left and right orthotics
if orthotic_left is not None and orthotic_right is not None:
data_y = torch.cat([orthotic_left, orthotic_right], dim=1)
# if data has no batch, unsqueeze data
gyro_data = torch.Tensor(gyro_data)
if len(gyro_data.size()) < 3:
gyro_num = len(gyro_data)
if gyro_num > seq_len:
gyro_data = gyro_data[:seq_len]
# repeat when data length < 5000
else:
gyro_data = torch.cat([gyro_data]*(seq_len//gyro_num + 1))[:seq_len]
gyro_data = torch.unsqueeze(gyro_data, dim=0)
test_dataset = OrthoticDataset(train=False, data_x=gyro_data, data_y=data_y)
test_loader = DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=0)
# Device
device = torch.device('cuda:{}'.format(gpu_ids)) if torch.cuda.is_available() else torch.device('cpu')
# Calculate loss when skeleton data exist
if data_y is not None:
criterion = nn.MSELoss()
# get model
if model is not None:
net = model.to(device)
# load model
else:
if model_type == 'lstm':
net = LSTMRegressor(num_gyro, orthotic_height * orthotic_width * 2)
elif model_type == 'linear':
net = LinearRegressor(num_gyro*seq_len, orthotic_height * orthotic_width * 2)
# load model file
net.load_state_dict(torch.load(os.path.join(model_dir, model_file)))
net = net.to(device)
# Inference
net.eval()
with torch.no_grad():
test_loss = 0
test_len = 0
test_pred = torch.empty(0,orthotic_height*orthotic_width*2)
for x, y in test_loader:
x, y = x.to(device), y.to(device)
if model_type == 'linear':
x= x.reshape(x.size(0), x.size(1)*x.size(2))
pred = net(x)
elif model_type == 'lstm':
hc = net.init_hidden_cell(batch_size)
pred, hc = net(x, hc)
# Calc loss
if data_y is not None:
loss = criterion(pred, y)
test_loss += loss
test_len += len(x)
# Concat predictions
test_pred = torch.cat([test_pred, pred.cpu()])
# Print loss when skeleton data exist
if data_y is not None:
print('Test Loss: {}'.format(test_loss / test_len))
# Smooth and Reshape to left and right orthotics
test_pred = ((test_pred - test_pred.min()) / test_pred.max() * 256).type(torch.int32)
test_pred[test_pred < 40] = 0
test_pred = test_pred.view(-1, orthotic_height*2, orthotic_width)
left = test_pred[:, :orthotic_height]
right = test_pred[:, orthotic_height:]
return left.numpy(), right.numpy()
def test_infer_orthotics(model_type='linear', model_file='orthotics.pt'):
data = OrthoticDataset(train=True,train_ratio=1.0)
left, right = orthotics(gyro_data=data.x, model_type=model_type, model_file=model_file, batch_size=5)
fig = plt.figure() # rows*cols 행렬의 i번째 subplot 생성
rows = len(data.x)
cols = 2
i = 1
for i in range(1, 11, 2):
# imshow left
ax = fig.add_subplot(rows, cols, i)
ax.imshow(left[i//2])
ax.set_xlabel(str(i//2))
ax.set_xticks([]), ax.set_yticks([])
# imshow right
ax = fig.add_subplot(rows, cols, i+1)
ax.imshow(right[i//2])
ax.set_xlabel(str(i//2))
ax.set_xticks([]), ax.set_yticks([])
plt.show()
if __name__=="__main__":
print('GP2Skel')
# Initialize inference class
gp2s = Inference(infer_type='gp2s', model_type='lstm', batch_size=1, n_layers=8, seq_len=32, model_dir='logs', model_file='gp2s_lstm_e500_n8_b8192_lr0_0001_seq32_str5_mse.pt')
for i in range(200):
#print('Inference #%d'%(i+1))
# prepare data
data = np.zeros((1, 44))
# inference
pred = gp2s.infer(gyro_data=data)
print('Shape of X:', data.shape)
print('Shape of Pred:', pred.shape)
#gp2s.infer_csv(csv_file='skeleton_data/skeleton_walking.csv', visualization=True)
print()
'''
# Initialize inference class
orth = Inference(infer_type='orthotics', model_type='lstm', batch_size=128, n_layers=8, seq_len=32, model_dir='logs', model_file='gp2s_lstm_e500_n8_b8192_lr0_0001_seq32_str5_mse.pt')
for i in range(5):
print('Inference #%d'%(i+1))
# prepare data
data = np.zeros((100, 44))
# inference
pred = gp2s(gyro_data=data)
print('Shape of X:', data.shape)
print('Shape of Pred:', pred.shape)
print()
'''
#print('Orthotics')
#test_infer_orthotics()
'''
data = np.ones((1, 5000, 44))
left, right = orthotics(gyro_data=data, model_type='linear', model_file='orthotics.pt')
print('Shape of X:', data.shape)
print('Left:')
print(left)
print('Right:')
print(right)
'''