/hub-application

Reference implementations of popular deep learning models.

Primary LanguagePythonOtherNOASSERTION

Hub Applications

Overview

This shows how to use TensorFlow Hub and Keras to build maximally reusable ML.

This repo builds and shares a pretrained DenseNet121 for feature extraction (forked from DenseNet code in keras-applications).

Use cases

There are two high-level ways to use a Hub Application:

  • graph+weights: no need to clone any code, no need to use Keras. Just use hub.load and call the module.
  • code+weights: original Keras Applications method, use this if you're using Keras by cloning the repo and importing the python.

Try it!

Play around with the code:

Open In Colab

Run with TensorFlow Serving:

Run on Google Cloud

Usage: graph+weights

In this case we use TF-Hub and SavedModel directly - no need to clone any code or even be using Keras:

import tensorflow_hub as hub

module = hub.load('https://github.com/jharmsen/hub-application/releases/download/v1/densenet121_weights_tf_dim_ordering_tf_kernels_notop.tar.gz')

output = module(tf.random.normal(1, 32, 32, 3))

Pros

  • No model code needed
  • Can be used across TF ecosystem (e.g., Sonnet, other languages, etc...)
  • Can be easily used in Keras with hub.KerasLayer

Cons

  • Less flexibility without full model code

Usage: code+weights

In this case the Keras model code is cloned and produces a keras.Model whose weights are loaded from the SavedModel.

$ pip install git+https://github.com/jharmsen/hub-application.git
from hub_application import densenet
...
model = densenet.DenseNet121()

Pros

  • Full flexibility in modifying code
  • Produces a complete keras.Model

Cons

  • Only applicable if user is using python & Keras

Building a Hub Application

  1. Export your keras.Model with tf.saved_model.save to produce the SavedModel .tar.gz (example notebook)
  2. Upload SavedModel as a release binary file
  3. Add functionality in your keras.Model constructor to
    1. Download SavedModel with hub.resolve
    2. Load weights using keras.Model.load_weights

See an example constructor here.

Testing

python -m pytest tests/