Skip to main content

Protobuf files from TensorFlow and TensorFlow-Serving

Project description

TensorFlow Protobuf

Imagine you want to talk to your model deployed with TF-Serving using gPRC and protobuf.

To convert your NumPy array, you'd need to use make_tensor_proto function:

import tensorflow as tf

def np_to_protobuf(data):
    return tf.make_tensor_proto(data, shape=data.shape)

Boom - you have a 2 GB dependency to deal with!

TensorFlow Protobuf

This project takes only the part you actually need for that: the protobuf files (compiled).

Installing it

pip install tensorflow-protobuf==2.3.0

Other available versions: 2.7.0

Using it

With it, your code will look like that:

from tensorflow.core.framework import tensor_pb2, tensor_shape_pb2, types_pb2


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):
    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 np_to_protobuf(data):
    if data.dtype != "float32":
        data = data.astype("float32")
    return make_tensor_proto(data)

A bit more verbose, but without the 2GB baggage.

See a full example here: example.py

Have fun!

Building and compiling it

To see how we extract and compile the protobuf files, check tf-serving-proto.sh.

To use it:

TF_VERSION="2.3.0" ./tf-serving-proto.sh

Publishing on PyPI

If you want to build it for other versions and publish it, do this:

export TF_VERSION=2.3.0
echo ${TF_VERSION} > .version

python -m venv env
source env/bin/activate

./tf-serving-proto.sh

pip install wheel twine

python setup.py sdist bdist_wheel

twine check dist/*
twine upload --repository-url https://test.pypi.org/legacy/ dist/*


rm -rf tensorflow/ tensorflow_serving/
rm -rf tensorflow_protobuf.egg-info/ build/ dist/ __pycache__/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensorflow-protobuf-2.7.0.tar.gz (135.5 kB view details)

Uploaded Source

Built Distribution

tensorflow_protobuf-2.7.0-py3-none-any.whl (229.0 kB view details)

Uploaded Python 3

File details

Details for the file tensorflow-protobuf-2.7.0.tar.gz.

File metadata

  • Download URL: tensorflow-protobuf-2.7.0.tar.gz
  • Upload date:
  • Size: 135.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for tensorflow-protobuf-2.7.0.tar.gz
Algorithm Hash digest
SHA256 89073a618c3439ac75051621158f2de5d401ed3a683a4d86a68882c35a9f51d3
MD5 97d4b0bbd90caa2e0160b33a1d9169f7
BLAKE2b-256 a53c3fdbf0c3309c048a77db15e45270801285cec3ccc6fdb5dca0638457ea3c

See more details on using hashes here.

File details

Details for the file tensorflow_protobuf-2.7.0-py3-none-any.whl.

File metadata

  • Download URL: tensorflow_protobuf-2.7.0-py3-none-any.whl
  • Upload date:
  • Size: 229.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for tensorflow_protobuf-2.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b4ddf9c468160e2fa97130d9a3d46b182dfdd6f3c2fcb95ad7ff9d549b2dd0d
MD5 0815a64a8d658f18af48a332bfd2d909
BLAKE2b-256 b67fce1421b12d7a1abe9bfc1849b08b6c1e051af4cda6bb83a8adf83a860caa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page