Skip to content

Commit

Permalink
Merge pull request #317 from cliveseldon/grpc_api
Browse files Browse the repository at this point in the history
Fix grpc tensor convert for python2
  • Loading branch information
ukclivecox authored Nov 29, 2018
2 parents f40cdfe + ae9e3cc commit d17650f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions wrappers/python/microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import time
import logging
import sys
import multiprocessing as mp
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -117,13 +118,15 @@ def array_to_rest_datadef(array,names,original_datadef):
def grpc_datadef_to_array(datadef):
data_type = datadef.WhichOneof("data_oneof")
if data_type == "tensor":
sz = np.prod(datadef.tensor.shape) # get number of float64 entries
c = datadef.tensor.SerializeToString() # get bytes
# create array from packed entries which are at end of bytes - assumes same endianness
features = np.frombuffer(memoryview(c[-(sz*8):]), dtype=np.float64, count=sz, offset=0)
features = features.reshape(datadef.tensor.shape)
# Previous method which is slower
# features = np.array(datadef.tensor.values).reshape(datadef.tensor.shape)
if (sys.version_info >= (3, 0)):
sz = np.prod(datadef.tensor.shape) # get number of float64 entries
c = datadef.tensor.SerializeToString() # get bytes
# create array from packed entries which are at end of bytes - assumes same endianness
features = np.frombuffer(memoryview(c[-(sz*8):]), dtype=np.float64, count=sz, offset=0)
features = features.reshape(datadef.tensor.shape)
else:
# Python 2 version which is slower
features = np.array(datadef.tensor.values).reshape(datadef.tensor.shape)
elif data_type == "ndarray":
features = np.array(datadef.ndarray)
else:
Expand Down

0 comments on commit d17650f

Please sign in to comment.