KerasΒΆ

About this page

This is an API reference for Keras in BentoML. Please refer to Keras guide for more information about how to use Keras in BentoML.

bentoml.keras.save_model(name: Tag | str, model: tf_ext.KerasModel, *, tf_signatures: 'tf_ext.ConcreteFunction' | None = None, tf_save_options: 'tf_ext.SaveOptions' | None = None, include_optimizer: bool = False, signatures: t.Dict[str, ModelSignature] | t.Dict[str, ModelSignatureDict] | None = None, labels: t.Optional[t.Dict[str, str]] = None, custom_objects: t.Optional[t.Dict[str, t.Any]] = None, external_modules: t.Optional[t.List[ModuleType]] = None, metadata: t.Optional[t.Dict[str, t.Any]] = None) bentoml.ModelΒΆ

Save a model instance to BentoML modelstore.

Parameters:
  • name – Name for given model instance. This should pass Python identifier check.

  • model – Instance of the Keras model to be saved to BentoML model store.

  • tf_signatures – Refer to Signatures explanation from Tensorflow documentation for more information.

  • tf_save_options – tf.saved_model.SaveOptions object that specifies options for saving.

  • signatures – Methods to expose for running inference on the target model. Signatures are used for creating Runner instances when serving model with bentoml.Service

  • labels – user-defined labels for managing models, e.g. team=nlp, stage=dev

  • custom_objects – Dictionary of Keras custom objects, if specified.

  • external_modules – user-defined additional python modules to be saved alongside the model or custom objects, e.g. a tokenizer module, preprocessor module, model configuration module

  • metadata – Custom metadata for given model.

Returns:

A BentoML model containing the saved Keras model instance.

Return type:

Model

Examples:

import bentoml
import tensorflow as tf
import tensorflow.keras as keras


def custom_activation(x):
    return tf.nn.tanh(x) ** 2


class CustomLayer(keras.layers.Layer):
    def __init__(self, units=32, **kwargs):
        super(CustomLayer, self).__init__(**kwargs)
        self.units = tf.Variable(units, name="units")

    def call(self, inputs, training=False):
        if training:
            return inputs * self.units
        else:
            return inputs

    def get_config(self):
        config = super(CustomLayer, self).get_config()
        config.update({"units": self.units.numpy()})
        return config


def KerasSequentialModel() -> keras.models.Model:
    net = keras.models.Sequential(
        (
            keras.layers.Dense(
                units=1,
                input_shape=(5,),
                use_bias=False,
                kernel_initializer=keras.initializers.Ones(),
            ),
        )
    )

    opt = keras.optimizers.Adam(0.002, 0.5)
    net.compile(optimizer=opt, loss="binary_crossentropy", metrics=["accuracy"])
    return net

model = KerasSequentialModel()

# `save` a given model and retrieve coresponding tag:
bento_model = bentoml.keras.save_model("keras_model", model)

# `save` a given model with custom objects definition:
custom_objects = {
    "CustomLayer": CustomLayer,
    "custom_activation": custom_activation,
},
custom_bento_model = bentoml.keras.save_model("custom_obj_keras", custom_objects=custom_objects)
bentoml.keras.load_model(bento_model: str | Tag | bentoml.Model, device_name: str = '/device:CPU:0') tf_ext.KerasModelΒΆ

Load a model from BentoML local modelstore with given name.

Parameters:
  • bento_model (str | Tag | Model) – Either the tag of the model to get from the store, or a BentoML ~bentoml.Model instance to load the model from.

  • device_name (str | None) – The device id to load the model on. The device id format should be compatible with tf.device

Returns:

an instance of users keras.Model from BentoML modelstore.

Return type:

keras.Model

Examples:

import bentoml

# load a model back into memory:
loaded = bentoml.keras.load_model("keras_model")
bentoml.keras.get(tag_like: str | Tag) ModelΒΆ

Get the BentoML model with the given tag.

Parameters:

tag_like – The tag of the model to retrieve from the model store.

Returns:

A BentoML Model with the matching tag.

Return type:

Model

Example:

import bentoml
# target model must be from the BentoML model store
model = bentoml.keras.get("keras_resnet50")