forked from MadPlayer/numpy-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_numpy_server.py
More file actions
71 lines (54 loc) · 1.64 KB
/
test_numpy_server.py
File metadata and controls
71 lines (54 loc) · 1.64 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
from flask import Flask, request
import pickle
import numpy as np
import json
from build.include.packet_pb2 import tensor, batched_tensor
app = Flask(__name__)
def parse_tensor_from_string(data):
t = tensor()
t.ParseFromString(data)
array = np.array(t.data)
array.shape = tuple(t.shape)
return array
def serialize_numpy_to_tensor(numpy_array):
t = tensor()
t.shape[:] = list(numpy_array.shape)
t.data.extend(numpy_array.flatten("A"))
return t.SerializeToString()
def parse_tensors_from_string(data):
t = batched_tensor()
t.ParseFromString(data)
batch = []
for tensor in t.tensors:
array = np.array(tensor.data)
array.shape = tuple(tensor.shape)
batch.append(array)
return np.array(batch)
def serialize_numpy_to_tensors(numpy_array):
bt = batched_tensor()
for tensor in numpy_array:
t = tensor()
t.shape = list(tensor.shape)
t.data.extend(numpy_array.flatten("A"))
bt.tensors.append(t)
return t.SerializeToString()
@app.route('/')
def home():
return "hello, flask"
@app.route("/numpy/inference", methods=["POST"])
def processing_tensor():
msg = request.data
received = parse_tensor_from_string(msg)
print(received.shape)
print(received)
numpy_array = np.array([i for i in range(27)])
numpy_array.shape = (3, 1, 9)
return serialize_numpy_to_tensor(numpy_array)
@app.route("/numpy/inference/batched", methods=["POST"])
def processing_tensors():
arrays = parse_tensor_from_string(request.data)
for arr in arrays:
print(arr)
return "done"
if __name__ == '__main__':
app.run(debug=True)