TensorFlow Hub is a way to share pretrained model components. See the TensorFlow Module Hub for a searchable listing of pre-trained models. This tutorial demonstrates:
layer_hub to load a mobilenet and transform it into a Keras layer. Any TensorFlow 2 compatible image classifier URL from tfhub.dev will work here.
We can then create our Keras model:
Download a single image to try the model on.
Using TF Hub it is simple to retrain the top layer of the model to recognize the classes in our dataset.
For this example you will use the TensorFlow flowers dataset:
The simplest way to load this data into our model is using
All of TensorFlow Hub’s image modules expect float inputs in the [0, 1] range. Use the image_data_generator’s rescale parameter to achieve this.
image_generator <- image_data_generator(rescale = 1/255, validation_split = 0.2) training_data <- flow_images_from_directory( directory = data_root, generator = image_generator, target_size = c(224, 224), subset = "training" ) validation_data <- flow_images_from_directory( directory = data_root, generator = image_generator, target_size = c(224, 224), subset = "validation" )
The resulting object is an iterator that returns
TensorFlow Hub also distributes models without the top classification layer. These can be used to easily do transfer learning.
Any Tensorflow 2 compatible image feature vector URL from tfhub.dev will work here.
Now we can create our classification model by attaching a classification head into the feature extractor layer. We define the following model:
We can now train our model in the same way we would train any other Keras model. We first use
compile to configure the training process:
We can then use the
fit function to fit our model.
You can then export your model with:
You can also reload the
model_from_saved_model function. Note that you need to pass the
custom_object with the definition of the KerasLayer since it/s not a default Keras layer.
We can verify that the predictions of both the trained model and the reloaded model are equal: