To get my hands on MLflow, I started off by reading the excellent tutorial at MLflow.org to acquaint myself with the steps of packaging and serving a ML model to a REST API endpoint. The linear regression model and steps described in the tutorial works right out of the box flawlessly. Kudos to the documentation!
To gain a little more exposure, I then tried to deploy the deep learning model described in this databricks blog. Unlike the linear regression model, the DL model uses the keras library and the API input is tensor rather than dataframe.
In this blog post, I will share the steps needed and the modification necessary to the original DL code in order to deploy the model.
For reference, my setup is:
- Ubuntu 20.04
- Python 3.8
- Anaconda3
- Keras 2.6
The original DL code which uses Keras 2.7 was throwing errors so I had to downgrade to version 2.6 and consequently modified all instances of “keras” to “tf.keras” in the code. The complete modified code is:
import kerasfrom keras.layers import Dense, Flatten, Dropoutimport numpy as npimport mlflowimport mlflow.kerasfrom mlflow.models.signature import infer_signatureimport tensorflow as tffrom urllib.parse import urlparse
# Let's prepare the training data!(train_X, train_Y), (test_X, test_Y) = tf.keras.datasets.mnist.load_data()trainX, testX = train_X / 255.0, test_X / 255.0trainY = tf.keras.utils.to_categorical(train_Y)testY = tf.keras.utils.to_categorical(test_Y)
# Let's define the model!model = tf.keras.models.Sequential( [ Flatten(), Dense(128, activation="relu", name="layer1"), Dropout(0.2), Dense(10, activation='softmax') ])opt = tf.keras.optimizers.SGD(lr=0.01, momentum=0.9)model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
# Let's fit the model!model.fit(trainX, trainY, epochs=2, batch_size=32, validation_data=(testX, testY))
# Create a model signature using the tensor input to store in the MLflow model registrysignature = infer_signature(testX, model.predict(testX))
# Let's check out how it looksprint(signature)
# inputs:# [Tensor('float64', (-1, 28, 28))]# outputs:# [Tensor('float32', (-1, 10))]
# Create an input example to store in the MLflow model registryinput_example = np.expand_dims(trainX[0], axis=0)
# Let's log the model in the MLflow model registryregistered_model_name = "tensor-blog-post"
# Model registry does not work with file storetracking_url_type_store = urlparse(mlflow.get_tracking_uri()).schemeif tracking_url_type_store != "file": # Register the model # There are other ways to use the Model Registry, which depends on the use case, # please refer to the doc for more information: # https://mlflow.org/docs/latest/model-registry.html#api-workflow mlflow.keras.log_model(model, "model", signature=signature, input_example=input_example,
registered_model_name=registered_model_name)else: mlflow.keras.log_model(model, "model", signature=signature, input_example=input_example)
The steps to deploy the model is described in the MLflow tutorial so I won’t repeat it here. Basically, what is needed to serve the model is the run ID of the model. The run ID is generated each time you run the code. You can find the run ID by launching the MLflow UI:
mlflow ui --host 0.0.0.0
No comments:
Post a Comment