-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathv5_Gesture_Recognition_Serving.py
executable file
·297 lines (227 loc) · 8.32 KB
/
v5_Gesture_Recognition_Serving.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
#!/usr/bin/env python3
"""Simple Flask example to serve Keras model."""
import glob
import json
import os
import sys
import signal
import logging
import numpy as np
import pandas as pd
from joblib import load
from pathlib import Path
from flask import Flask
from flask import abort
from flask import request
from flask_restplus import fields
from flask_restplus import Resource, Api, Namespace
from http import HTTPStatus
import tensorflow as tf
from sklearn.preprocessing.label import LabelEncoder
from tensorflow import keras
from demo_lib.demo import process_motion_rotation
from demo_lib.demo import clean_data
from demo_lib.demo import featurize
_HERE = Path(__file__).parent
_EXAMPLE_DATA_PATH = Path(_HERE, 'data/dance/floss/example.json')
_MODEL_DIR = Path(os.getenv("MODEL_DIR", _HERE / "models/v5"))
"""Path to model directory."""
_MODEL: keras.Model = None
"""Keras model."""
_ENCODER: LabelEncoder = None
"""Label encoder/decoder."""
app = Flask(__name__)
probe_ns = Namespace('probe', description="Health checks.")
model_ns = Namespace('model', description="Model namespace.")
example = json.loads(
Path(_EXAMPLE_DATA_PATH).read_text())
model_input = model_ns.model(u'ModelInput', {
'gesture': fields.String(
require=True,
description="String. Label of the dance move.",
example=example['gesture'],
),
'motion': fields.List(
fields.List(fields.Float),
required=True,
description="Array of floats. Model input motion data.",
example=example['motion']
),
'orientation': fields.List(
fields.List(fields.Float),
required=True,
description="Array of floats. Model input orientation data.",
example=example['orientation']
),
"playerId": fields.String(
required=False,
description="String. Player ID.",
example=example['playerId']
),
"type": fields.String(
required=False,
description="String. Type of data.",
example=example['type']
),
"uuid": fields.String(
required=False,
description="String. UUID",
example=example['uuid']
)
})
model_output = model_ns.model(u'ModelOutput', {
'candidate': fields.String(
required=True,
description="Best matching candidate."
),
'candidate_score': fields.Float(
required=True,
description="Best matching candidate's score."
),
'predictions': fields.Raw(
required=True,
description="Mapping candidate -> score for each predicted candidate."
),
})
prediction_input = model_ns.model(u'PredictionInput', {
'instances': fields.List(
fields.Nested(model_input),
required=True,
description="List of inputs to the model for prediction."
)
})
payload = model_ns.model('uPayload', {
'payload': fields.Nested(
model_output,
as_list=True,
required=True,
description="Array of model predictions for each input."
),
'total': fields.Integer(
required=False,
description="Total number of predictions.")
})
model_ns.add_model('model_input', model_input)
api = Api(title="Gestures model serving")
api.init_app(app)
api.add_namespace(probe_ns)
api.add_namespace(model_ns)
@probe_ns.route('/liveness')
class Liveness(Resource):
# noinspection PyMethodMayBeStatic
def get(self):
"""Heartbeat."""
return {'Status': "Running OK."}, HTTPStatus.OK
@probe_ns.route('/readiness')
class Readiness(Resource):
# noinspection PyMethodMayBeStatic
def get(self):
"""Readiness."""
if _MODEL is not None:
response = {'Status': "Ready."}, HTTPStatus.OK
else:
response = {'Status': "Model has not been loaded."}, \
HTTPStatus.SERVICE_UNAVAILABLE
return response
@model_ns.route('/predict')
class Model(Resource):
"""Model api resource."""
@model_ns.response(200, 'Success', model=payload)
@model_ns.response(400, 'Validation Error or invalid model input')
@model_ns.expect(prediction_input, validate=True)
@model_ns.marshal_list_with(payload)
def post(self):
"""Return predictions from the trained model."""
global _SESSION
model, encoder = _load_keras_model()
bins = json.loads(
Path(_MODEL_DIR / 'bins.json').read_text())
bins_partial = json.loads(
Path(_MODEL_DIR / 'bins_partial.json').read_text())
instances = request.get_json(force=True)['instances']
data = []
for sample in instances:
gesture = sample['gesture']
data_clean = list(
clean_data([process_motion_rotation(sample)]))
if data_clean:
_, feature_df_total = featurize(
data_clean, col_bins=bins[gesture])
_, feature_df_partial = featurize(
data_clean, col_bins=bins_partial[gesture])
data.append(np.vstack([feature_df_partial, feature_df_total]))
input_t: np.ndarray = np.array(data, dtype=np.float64)
if not np.any(input_t):
return "Error: Invalid model input.", HTTPStatus.BAD_REQUEST
tf.keras.backend.set_session(_SESSION)
scores: np.ndarray = model.predict(
input_t, max_queue_size=20, use_multiprocessing=True, workers=4)
probas: np.ndarray = np.exp(scores)
probas /= np.sum(probas, axis=1)[:, np.newaxis]
labels: np.ndarray = np.argmax(scores, axis=1)
candidates: list = encoder.inverse_transform(labels).tolist()
response = {
'payload': [
{
'candidate': candidates[i],
'candidate_score': float(sample[labels[i]]),
'predictions': dict(
zip(encoder.classes_, sample.tolist())),
} for i, sample in enumerate(probas)
],
'total': len(candidates)
}
return response, HTTPStatus.OK
def _load_keras_model():
"""Load Keras model."""
global _MODEL
global _ENCODER
global _SESSION
if all([_MODEL, _ENCODER]):
return _MODEL, _ENCODER
app.logger.info("Loading Keras model.")
# TODO: glob by pattern according to our model file naming
model_files = sorted(
# This serving is made for .h5 models
glob.iglob(str(_MODEL_DIR / '**/*.h5'), recursive=True), key=os.path.getctime, reverse=True)
if not model_files:
msg = f"Empty directory provided: {_MODEL_DIR}."
app.logger.error(msg)
# TODO: maybe BAD_REQUEST is not ideal here
abort(HTTPStatus.BAD_REQUEST, "Failed. Model not found.")
else:
latest = model_files[-1]
# set the global
_MODEL = keras.models.load_model(latest, custom_objects={
'log_softmax': tf.nn.log_softmax,
'softmax_cross_entropy_with_logits_v2_helper': tf.nn.softmax_cross_entropy_with_logits_v2
})
_ENCODER = load(_MODEL_DIR / 'encoder.joblib')
if isinstance(_MODEL, keras.Model):
app.logger.info(f"Model '{latest}' successfully loaded.")
else:
msg = f"Expected model of type: {keras.Model}, got {type(_MODEL)}"
app.logger.error(msg)
abort(HTTPStatus.BAD_REQUEST, "Failed. Model not loaded.")
if isinstance(_ENCODER, LabelEncoder):
app.logger.info(f"Encoder successfully loaded.")
else:
msg = f"Expected model of type: {LabelEncoder}, got {type(_ENCODER)}"
app.logger.error(msg)
abort(HTTPStatus.BAD_REQUEST, "Failed. Encoder not loaded.")
_SESSION = tf.keras.backend.get_session()
return _MODEL, _ENCODER
@app.before_first_request
def setup_logging():
levels = {"INFO": logging.INFO, "DEBUG": logging.DEBUG, "WARNING": logging.WARNING}
level = levels[os.environ.get('LOG_LEVEL', "WARNING")]
# In production mode, add log handler to sys.stdout.
#app.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
app.logger.setLevel(level)
def signal_term_handler(signal, frame):
app.logger.warn('got SIGTERM')
sys.exit(0)
_load_keras_model()
if __name__ == '__main__':
signal.signal(signal.SIGTERM, signal_term_handler)
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)), debug=False, threaded=False)