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
).
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.
Play around with the code:
Run with TensorFlow Serving:
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
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
- Export your
keras.Model
withtf.saved_model.save
to produce the SavedModel .tar.gz (example notebook) - Upload SavedModel as a release binary file
- Add functionality in your
keras.Model
constructor to- Download SavedModel with
hub.resolve
- Load weights using
keras.Model.load_weights
- Download SavedModel with
See an example constructor here.
python -m pytest tests/