-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinferer.py
250 lines (186 loc) · 7.47 KB
/
inferer.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
import torch
from models.linear import LinearRegressor
from models.lstm import LSTMRegressor
from inference import orthotics
import os
from torch import nn
import numpy as np
import pandas as pd
from collections import deque
from tqdm import tqdm
from threading import Thread
import cv2
num_gyro=44
num_skel=51
orthotic_width = 10
orthotic_height = 30
import time
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
self.flag = False
self.data_seq = torch.zeros((32, 44))
# 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):
if not self.flag:
self.flag = True
if not torch.is_tensor(gyro_data):
gyro_data = torch.Tensor(gyro_data)
self.data_seq = torch.cat([self.data_seq, gyro_data])[-self.seq_len:,:]
x = torch.unsqueeze(self.data_seq, 0)
with torch.no_grad():
batch_size = 1
x = x.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)
print("pred.shape = {}".format(pred.shape))
pred = pred.cpu()
self.flag = False
return pred.numpy()
else:
return None
class SkelInferer:
def __init__(self, gpu_ids=0, model_dir="logs/", model_file='gp2s_b1024_e300_lr0_0001_mse.pt', fake=False):
self.fake = fake
if self.fake:
self.start_time = time.time()
self.count = 0
df = pd.read_csv("skel_data/skeleton_data/skeleton_walking.csv")
self.skel = df.iloc[:,45:].values
self.total = self.skel.shape[0]
else:
if 'skel_data' in os.listdir():
model_dir = os.path.join('skel_data', model_dir)
print("load skel model")
device = torch.device('cuda:{}'.format(gpu_ids)) if torch.cuda.is_available() else torch.device('cpu')
net = LinearRegressor(num_gyro, num_skel)
net.load_state_dict(torch.load(os.path.join(model_dir, model_file)))
self.net = net.to(device)
self.net.eval()
self.device = device
print("skel model loaded")
def to_skel(self, gp):
if self.fake:
dt = time.time() - self.start_time
fn = int(dt * 30) % self.total
return self.skel[fn]
else:
x = torch.Tensor(gp)
x = x.to(self.device)
x = torch.unsqueeze(x, 0)
with torch.no_grad():
y = self.net(x)
s = y.cpu().numpy()
return s
def make_orthotics(data, base_dir="", model_file='orthotics_lstm_b1_e400_lr0_0002_mse.pt', save_dir="static/orthotics.csv"):
left, right = orthotics(gyro_data=data, model_type='lstm', model_file=model_file)
left, right = left[0], right[0]
mask = cv2.imread(os.path.join(base_dir, "mask.png"))
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
# cv2.imshow("mask", mask)
mid = np.zeros((orthotic_height, orthotic_height - orthotic_width * 2), dtype=np.int32)
print("l.s = {}, r.s = {}, m.s = {}".format(left.shape, right.shape, mid.shape))
pair = np.hstack([left, mid, right])
pair = pair.astype(np.uint8)
r_max = np.amax(pair, axis=1)
print(r_max)
print("p.s = {}".format(pair.shape))
for i in range(pair.shape[0]):
for j in range(pair.shape[1]):
if pair[i][j] < 50:
pair[i][j] = r_max[i]
# pair = pair.astype(np.uint8)
# cv2.imshow("pair", pair)
# print(pair)
# print("p.s = {}".format(pair.shape))
# pair = pair.astype(np.uint8)
pair = cv2.resize(pair, dsize=(300, 300), interpolation=cv2.INTER_AREA)
ks = 15
rks = ks * 2 + 1
pair = cv2.GaussianBlur(pair,(rks, rks),0)
pair = cv2.bitwise_and(pair, mask)
padding = np.zeros((330, 330))
padding[:]
pair = cv2.resize(pair, (100, 100))
# cv2.imshow("pair", pair)
# cv2.waitKey(0)
print("orthotics has been made!")
print("save as {}".format(save_dir))
df = pd.DataFrame(pair)
df.to_csv(save_dir)
return pair
class OrthoticsInferer:
def __init__(self, seq_len=5000):
print("load orthtics model")
self.save_dir = "static/orthotics.csv"
if 'skel_data' not in os.listdir():
self.save_dir = os.path.join("..", self.save_dir)
self.x_series = deque(maxlen=seq_len)
self.seq_len = seq_len
self.is_making = False
# self.pbar = tqdm(total=seq_len)
print("orthtics model loaded")
def make_orthotics(self):
self.is_making = True
gyro_data = np.array(self.x_series)
self.x_series.clear()
pair = make_orthotics(gyro_data,base_dir="skel_data", save_dir=self.save_dir)
self.is_making = False
return pair
# return left, right
def feed(self, foot):
# print("feed {}, is made = {}".format(foot, self.is_made))
self.x_series.append(foot)
print("feed skel_data {}/{}".format(len(self.x_series),self.seq_len))
if len(self.x_series) == self.seq_len and self.is_making:
self.make_orthotics()
if __name__ == "__main__":
df = pd.read_csv("sample_gyro_data.csv")
print("s.s = {}".format(df.shape))
s = df.iloc[:,1:45].values
print("s.s = {}".format(s.shape))
make_orthotics(s, save_dir="../static/orthotics.csv")
# pivot = s.shape[0] // 2
# inferer = OrthoticsInferer()
# for i in np.r_[:5000]:
# inferer.feed(s[i])
# pair = inferer.make_orthotics()
# cv2.imshow("pair", pair)
# cv2.waitKey(0)