Skip to main content

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!

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

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

PyPI

The package is available on PyPI

pip install tensorflow-protobuf==2.3.0

Publishing on PyPI

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



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

Metadata

Release files for tensorflow-protobuf 2.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tensorflow-protobuf 2.3.0
File Size Uploaded
tensorflow-protobuf-2.3.0.tar.gz 121.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tensorflow-protobuf 2.3.0
File Interpreter ABI Platform
tensorflow_protobuf-2.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 326.0 kB

Release files / tensorflow-protobuf-2.3.0.tar.gz

Download URL tensorflow-protobuf-2.3.0.tar.gz
Size 121.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8d5e50870468d60e964736804d95f8a2d8813b6213cd6070733f2d6fdd6d4cd1
BLAKE2b-256 checksum
How to use checksums
4788450f0156b04aa99b238e73e191827fc1d2fb24e4042b6d58c5d7bb18e799
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / tensorflow_protobuf-2.3.0-py3-none-any.whl

Download URL tensorflow_protobuf-2.3.0-py3-none-any.whl
Size 204.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73feedd6d6427854a02bce269e6cfb19e93b08e978e445c056bbded7b66ad56a
BLAKE2b-256 checksum
How to use checksums
40f83794ddd73fa87c508ce49f8e8aa378d0cf06edd7cd300bfc2ddf79a33a15
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

2.11.0

2 release files

2.7.0

2 release files

This release

2.3.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page