.. TensorFlowModel: TensorFlowModel *************** Overview ======== 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. .. include:: _template/overview.rst The following steps take your trained ``TensorFlow`` model and deploy it into production with a few lines of code. **Create a TensorFlow Model** .. code-block:: python3 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) Initialize ========== 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 to ``None``. The default authentication is set using the ``ads.set_auth`` API. To override the default, use ``ads.common.auth.api_keys()`` or ``ads.common.auth.resource_principal()`` and create the appropriate authentication signer and the ``**kwargs`` required to instantiate the ``IdentityClient`` object. * ``estimator: Callable``: Any model object generated by the TensorFlow framework. * ``properties: (ModelProperties, optional)``: Defaults to ``None``. The ``ModelProperties`` object required to save and deploy a model. .. include:: _template/initialize.rst Summary Status ============== .. include:: _template/summary_status.rst .. figure:: figures/summary_status.png :align: center Model Deployment ================ Prepare ------- 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 the ``X_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 the ``model_file_name`` attribute. By default, the model is stored in an h5 file. You can use the ``as_onnx`` parameter to save it in the ONNX format. * ``output_schema.json``: A JSON file that defines the nature of the dependent variable in the ``y_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 the ``load_model()`` and ``predict()`` functions. The ``load_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. .. include:: _template/prepare.rst Verify ------ .. include:: _template/verify.rst * ``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. Save ---- .. include:: _template/save.rst Deploy ------ .. include:: _template/deploy.rst Predict ------- .. include:: _template/predict.rst * ``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. Load ==== 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. .. include:: _template/loading_model_artifact.rst .. code-block:: python3 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" ) .. include:: _template/loading_model_catalog.rst .. code-block:: python3 from ads.model.framework.tensorflow_model import TensorFlowModel model = TensorFlowModel.from_model_catalog(model_id="", model_file_name="model.tf", artifact_dir=tempfile.mkdtemp()) .. include:: _template/loading_model_deployment.rst .. code-block:: python3 from ads.model.generic_model import TensorFlowModel model = TensorFlowModel.from_model_deployment( model_deployment_id="", model_file_name="model.pkl", artifact_dir=tempfile.mkdtemp()) Delete a Deployment =================== .. include:: _template/delete_deployment.rst Example ======== .. code-block:: python3 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) tensorflow_model.delete()