Last active
August 16, 2022 15:34
-
-
Save alexeygrigorev/26b011eb7edd7d0cad5251202dacefbf to your computer and use it in GitHub Desktop.
tf.make_tensor_proto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install grpcio-tools | |
wget https://github.com/tensorflow/tensorflow/archive/v1.9.0.zip -O tf-190.zip | |
unzip tf-190.zip && rm tf-190.zip | |
wget https://github.com/tensorflow/serving/archive/1.9.0.zip -O tf-serving-190.zip | |
unzip tf-serving-190.zip && rm tf-serving-190.zip | |
mv serving-1.9.0/tensorflow_serving tensorflow-1.9.0 | |
mkdir tfserving_proto | |
cd tensorflow-1.9.0 | |
TF_SERVING_PROTO=../tfserving_proto | |
python -m grpc.tools.protoc \ | |
./tensorflow/core/framework/*.proto \ | |
--python_out=${TF_SERVING_PROTO} \ | |
--grpc_python_out=${TF_SERVING_PROTO} \ | |
--proto_path=. | |
python -m grpc.tools.protoc \ | |
./tensorflow/core/example/*.proto \ | |
--python_out=${TF_SERVING_PROTO} \ | |
--grpc_python_out=${TF_SERVING_PROTO} \ | |
--proto_path=. | |
python -m grpc.tools.protoc \ | |
./tensorflow/core/protobuf/*.proto \ | |
--python_out=${TF_SERVING_PROTO} \ | |
--grpc_python_out=${TF_SERVING_PROTO} \ | |
--proto_path=. | |
python -m grpc.tools.protoc \ | |
./tensorflow_serving/apis/*.proto \ | |
--python_out=${TF_SERVING_PROTO} \ | |
--grpc_python_out=${TF_SERVING_PROTO} \ | |
--proto_path=. | |
# now move ${TF_SERVING_PROTO} to your project |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.core.framework import tensor_pb2, tensor_shape_pb2, types_pb2 | |
from tensorflow_serving.apis import predict_pb2, prediction_service_pb2 | |
from tensorflow_serving.apis import prediction_service_pb2_grpc | |
def dtypes_as_dtype(dtype): | |
if dtype == 'float32': | |
return types_pb2.DT_FLOAT | |
raise Exception('dtype %s is not supported' % dtype) | |
def make_tensor_proto(data): | |
# return tf.make_tensor_proto(data, shape=data.shape) | |
shape = data.shape | |
dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=i) for i in shape] | |
proto_shape= tensor_shape_pb2.TensorShapeProto(dim=dims) | |
proto_dtype = dtypes_as_dtype(data.dtype) | |
tensor_proto = tensor_pb2.TensorProto( | |
dtype=proto_dtype, | |
tensor_shape=proto_shape) | |
tensor_proto.tensor_content = data.tostring() | |
return tensor_proto | |
def beta_create_PredictionService_stub(channel): | |
return prediction_service_pb2_grpc.PredictionServiceStub(channel._channel) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is inspired by https://towardsdatascience.com/tensorflow-serving-client-make-it-slimmer-and-faster-b3e5f71208fb