The TensorFlowModel
class in ADS is designed to allow you to rapidly get a TensorFlow model into production. The .prepare()
method creates the model artifacts that are needed to deploy a functioning model without you having to configure it or write code. However, you can customize the required score.py
file.
The following steps take your trained TensorFlow
model and deploy it into production with a few lines of code.
Create a TensorFlow Model
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential(
[
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10),
])
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=1)
Instantiate a TensorFlowModel()
object with a TensorFlow model. Each instance accepts the following parameters:
artifact_dir: str
: Artifact directory to store the files needed for deployment.auth: (Dict, optional)
: Defaults toNone
. The default authentication is set using theads.set_auth
API. To override the default, useads.common.auth.api_keys()
orads.common.auth.resource_principal()
and create the appropriate authentication signer and the**kwargs
required to instantiate theIdentityClient
object.estimator: Callable
: Any model object generated by the TensorFlow framework.properties: (ModelProperties, optional)
: Defaults toNone
. TheModelProperties
object required to save and deploy a model.
The prepare step is performed by the .prepare()
method. It creates several customized files used to run the model after it is deployed. These files include:
input_schema.json
: A JSON file that defines the nature of the features of theX_sample
data. It includes metadata such as the data type, name, constraints, summary statistics, feature type, and more.model.h5
: This is the default filename of the serialized model. You can change it with themodel_file_name
attribute. By default, the model is stored in an h5 file. You can use theas_onnx
parameter to save it in the ONNX format.output_schema.json
: A JSON file that defines the nature of the dependent variable in they_sample
data. It includes metadata such as the data type, name, constraints, summary statistics, feature type, and more.runtime.yaml
: This file contains information that is needed to set up the runtime environment on the deployment server. It has information about which conda environment was used to train the model, and what environment should be used to deploy the model. The file also specifies what version of Python should be used.score.py
: This script contains theload_model()
andpredict()
functions. Theload_model()
function understands the format the model file was saved in, and loads it into memory. The.predict()
method is used to make inferences in a deployed model. There are also hooks that allow you to perform operations before and after inference. You are able to modify this script to fit your specific needs.
data: Any
: Data used to test if deployment works in local environment.
In TensorFlowModel
, data serialization is supported for JSON serializable objects. Plus, there is support for a dictionary, string, list, np.ndarray
, and tf.python.framework.ops.EagerTensor
. Not all these objects are JSON serializable, however, support to automatically serializes and deserialized is provided.
data: Any
: JSON serializable data used for making inferences.
In TensorFlowModel
, data serialization is supported for JSON serializable objects. Plus, there is support for a dictionary, string, list, np.ndarray
, and tf.python.framework.ops.EagerTensor
. Not all these objects are JSON serializable, however, support to automatically serializes and deserialized is provided.
You can restore serialization models from model artifacts, from model deployments or from models in the model catalog. This section provides details on how to restore serialization models.
from ads.model.framework.tensorflow_model import TensorFlowModel
model = TensorFlowModel.from_model_artifact(
uri="/folder_to_your/artifact.zip",
model_file_name="model.joblib",
artifact_dir="/folder_store_artifact"
)
from ads.model.framework.tensorflow_model import TensorFlowModel
model = TensorFlowModel.from_model_catalog(model_id="<model_id>",
model_file_name="model.tf",
artifact_dir=tempfile.mkdtemp())
from ads.model.generic_model import TensorFlowModel
model = TensorFlowModel.from_model_deployment(
model_deployment_id="<model_deployment_id>",
model_file_name="model.pkl",
artifact_dir=tempfile.mkdtemp())
import tempfile
import tensorflow as tf
from ads.catalog.model import ModelCatalog
from ads.common.model_metadata import UseCaseType
from ads.model.framework.tensorflow_model import TensorFlowModel
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential(
[
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10),
])
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=1)
# Deploy the model, test it and clean up.
artifact_dir = tempfile.mkdtemp()
tensorflow_model = TensorFlowModel(estimator=model, artifact_dir= artifact_dir)
tensorflow_model.prepare(
inference_conda_env="generalml_p37_cpu_v1",
training_conda_env="generalml_p37_cpu_v1",
use_case_type=UseCaseType.MULTINOMIAL_CLASSIFICATION,
X_sample=x_test,
y_sample=y_test,
)
tensorflow_model.verify(x_test[:1])
model_id = tensorflow_model.save()
tensorflow_model_deployment = model.deploy()
tensorflow_model.predict(x_test[:1])
tensorflow_model.delete_deployment(wait_for_completion=True)
ModelCatalog(compartment_id=os.environ['NB_SESSION_COMPARTMENT_OCID']).delete_model(model_id)