Skip to main content

TFI: Use any TensorFlow model in a single line of code

TFI provides a simple Python interface to any TensorFlow model. It does this by automatically generating a Python class on the fly.

Here’s an example of using TFI with a SavedModel based on Inception v1. This particular SavedModel has a single predict method and a SignatureDef that looks something like: predict(images float <1,224,224,3>) -> (categories string <1001>, scores float <1,1001>)

TFI in Action

>>> import tfi
>>> InceptionV1 = tfi.saved_model.as_class("./inception-v1.saved_model")

Passing in data

TFI can automatically adapt any data you provide to the shape expected by the graph. Let’s take a random photo of a dog I found on the internet…

dog
>>> model = InceptionV1()
>>> image = tfi.data.file("./dog-medium-landing-hero.jpg")
>>> result = model.predict(images=[image])
>>> categories, scores = result.categories, result.scores[0]

If we print the top 5 probabilities, we see:

>>> [(scores[i], categories[i].decode('utf-8')) for i in scores.argsort()[:-5:-1]]
[(0.80796158, 'bloodhound, sleuthhound'),
 (0.10305813, 'English foxhound'),
 (0.064740285, 'redbone'),
 (0.009166114, 'beagle')]

Not bad!

Image data

The tf.data.file function uses mimetypes to discover the right data decoder to use. If an input to a graph is an "image/*", TFI will automatically decode and resize the image to the proper size. In the example above, the JPEG image of a dog is automatically decoded and resized to 224x224.

Batches

If you look closely at the example code above, you’ll see that the images argument is actually an array. The class generated by TFI is smart enough to convert an array of images to an appropriately sized batch of Tensors.

Graphs with variables

Each instance of the class has separate variables from other instances. If a graph’s variables are mutated during a session in a useful way, you can continue to use those mutations by calling methods again on that same instance.

If you’d like to have multiple instances that do not interfere with one another, you can create a second instance and call methods on each of them separately.

TFI and SavedModels

TFI uses the information in a SavedModel’s SignatureDefs to generate methods on the resulting class. The keyword argument names for each method are also pulled from info in the SignatureDef.

The SavedModel used in the example was created using the tf.estimator.Estimator#export_savedmodel function.

Getting Started

TFI is on PyPI, install it with pip install tfi.

Future work

Adapting tfi.data functions to handle queues and datasets wouldn’t require much effort. If this is something you’d like me to do, please file an issue with your specific use case!

Extending tfi.data to support more formats is also quite straightforward. File an issue with a specific format you’d like to see. For bonus points, include the expected tensor dtype and shape. For double bonus points, include a way for me to test it in a real model.

It’s not very easy to create well-formed SavedModels today. If this is something you’d like TFI to do in the future… file an issue. ;)

Acknowledgements

If you’re curious, the photo used above was from a random Google image search.

PyPI packaging was way easier because of this fantastic guide.

Metadata

Release files for tfi 0.4

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

Source distribution (sdist)

Source distribution for tfi 0.4
File Size Uploaded Provenance Actions
tfi-0.4.tar.gz 7.5 kB Without provenance Details for tfi-0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tfi 0.4
File Interpreter ABI Platform Actions
tfi-0.4-py2.py3-none-any.whl Python 2, Python 3 none any Details for tfi-0.4-py2.py3-none-any.whl

Total release size: 17.4 kB

Release files / tfi-0.4.tar.gz

Download URL tfi-0.4.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3ae3fa81fb64b6dbe4bd044b2d590986a159a1c0a387171ea3a9ffed7809eb08
BLAKE2b-256 checksum
How to use checksums
64efeb268e7f73845e2dbb238b53de2ee54b105150d1da9439520647658771a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / tfi-0.4-py2.py3-none-any.whl

Download URL tfi-0.4-py2.py3-none-any.whl
Size 9.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
177e789f45224e16c29fecab2a7c57fe1439262bd65aad50d216263074972864
BLAKE2b-256 checksum
How to use checksums
6f038c273630fee9690c10fb330c7646db31854b44714cdaed298741805f55e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for tfi 0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4 This release

2 release files No provenance

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