Skip to main content

Use any TensorFlow model in a single line of code

Project description

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.

Project details


Release history Release notifications | RSS feed

This version

0.4

Download files

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

Source Distribution

tfi-0.4.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

tfi-0.4-py2.py3-none-any.whl (9.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tfi-0.4.tar.gz.

File metadata

  • Download URL: tfi-0.4.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tfi-0.4.tar.gz
Algorithm Hash digest
SHA256 3ae3fa81fb64b6dbe4bd044b2d590986a159a1c0a387171ea3a9ffed7809eb08
MD5 47e081d737864595863f2ee6eff67492
BLAKE2b-256 64efeb268e7f73845e2dbb238b53de2ee54b105150d1da9439520647658771a5

See more details on using hashes here.

File details

Details for the file tfi-0.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for tfi-0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 177e789f45224e16c29fecab2a7c57fe1439262bd65aad50d216263074972864
MD5 67558006c23da8047968a3e6ec68d44a
BLAKE2b-256 6f038c273630fee9690c10fb330c7646db31854b44714cdaed298741805f55e6

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