This notebook illustrates a simple client-server interaction to perform inference on a TensorFlow model using the Python SDK for Konduit Serving.
This tutorial is split into three parts:
Freezing models
Configuration
Running the server
This tutorial is tested on TensorFlow 1.14, 1.15 and 2.00.
from konduit import ParallelInferenceConfig, ServingConfig, ModelConfigType, TensorFlowConfigfrom konduit import TensorDataTypesConfig, ModelStep, InferenceConfigurationfrom konduit.server import Serverfrom konduit.client import Clientimport tensorflow as tfif tf.__version__[0]=="1":from tensorflow import keraselif tf.__version__[0]=="2":import tensorflow.compat.v1 as tffrom tensorflow.compat.v1 import keraselse:print("No valid TensorFlow version detected")from keras.layers import Flatten, Dense, Dropout, Lambdafrom keras.models import Sequentialfrom keras.datasets import mnistfromPILimport Imageimport numpy as npimport imageioimport osimport matplotlib.pyplot as plt import pandas as pd
Creating frozen models (Tensorflow 1.x)
In TensorFlow 1.x, "frozen" models can be exported in the TensorFlow Graph format. For deployment, we only need information about the graph and checkpoint variables. Freezing a model allows you to discard information that is not required for deploying your model.
TensorFlow 2.0 introduces the SavedModel format as the universal format for saving models. Even though the deployable protobuff (PB) files have the same file extension as frozen TensorFlow Graph files, SavedModel protobuff files are not currently supported in Konduit Serving. A workaround for TensorFlow 2.0 is to adapt the code from this tutorial for your use case to create TensorFlow Graph protobuffs, or save your models as Keras HDF5 files and serve as Keras models (refer to the Keras tutorial for details).
The following code is adapted from tf-import-examples in the deeplearning4j-examples repository.
Konduit Serving works by defining a series of steps. These include operations such as
Pre- or post-processing steps
One or more machine learning models
Transforming the output in a way that can be understood by humans
If deploying your model does not require pre- nor post-processing, only one step - a machine learning model - is required. This configuration is defined using a single ModelStep.
Before running this notebook, run the build_jar.py script or the konduit init command. Refer to the Building from source page for details.
Configure the step
Define the TensorFlow configuration as a TensorFlowConfig object.
tensor_data_types_config: The TensorFlowConfig object requires a dictionary input_data_types. Its keys should represent column names, and the values should represent data types as strings, e.g. "INT32". See here for a list of supported data types.
model_config_type: This argument requires a ModelConfigType object. Specify model_type as TENSORFLOW, and model_loading_path to point to the location of TensorFlow weights saved in the PB file format.
Now that we have a TensorFlowConfig defined, we can define a ModelStep. The following parameters are specified:
model_config: pass the TensorFlowConfig object here
parallel_inference_config: specify the number of workers to run in parallel. Here, we specify workers=1.
input_names: names for the input data
output_names: names for the output data
In the YAML configuration file, we define a single tensorflow_step with
type: TENSORFLOW
model_loading_path pointing to the location of the weights
input_names and output_names: names of the input and output nodes. Define this as a list.
input_data_types: maps each of the inputs to a corresponding data type. Values should represent data types as strings, e.g. INT32. See here for a list of supported data types.
Konduit Serving requires input and output names to be specified. In TensorFlow, you can find the names of your input and output nodes by printing model.inputs[0].op.name and model.outputs[0].op.name respectively. For more details, please refer to this StackOverflow answer.
Configure the server
Specify the following:
http_port: select a random port.
input_data_format, output_data_format: Specify input and output data formats as strings.
The ServingConfig has to be passed to Server in addition to the steps as a Python list. In this case, there is a single step: tf_step.
By default, Server() looks for the Konduit Serving JAR konduit.jar in the directory the script is run in. To change this default, use the jar_path argument.
The following parameters should be specified to serving:
http_port: specify an integer as port number
input_data_format, output_data_format: Input and output data formats
Accepted input and output data formats are as follows:
Input: JSON, ARROW, IMAGE, ND4J (not yet implemented) and NUMPY.
Output: NUMPY, JSON, ND4J (not yet implemented) and ARROW.
Start the server
Start the server:
Configure the client
To configure the client, create a Client object by specifying the port number:
The Client's attributes will be obtained from the Server.
Add the following to your YAML configuration file:
In Python, use the client_from_file function to load the client configuration:
Inference
NDARRAY inputs to ModelSteps must be specified with a preceding batchSize dimension. For batches with a single observation, this can be done by using numpy.expand_dims() to add an additional dimension to your array.
We obtain test images from the test set defined by keras.datasets.
png
png
png
Batch prediction
To predict in batches, the data_input dictionary has to be specified differently for client images in NDARRAY format. To input a batch of observations, ensure that your inputs are in the NCHW format: number of observations, channels (optional if single channel), height and width.
An example is as follows:
We compare the predicted probabilities and the corresponding labels:
0
1
2
3
4
5
6
7
8
9
0
0.0
0.000
0.001
0.001
0.0
0.0
0.0
0.998
0.000
0.0
1
0.0
0.000
0.998
0.002
0.0
0.0
0.0
0.000
0.000
0.0
2
0.0
0.986
0.005
0.000
0.0
0.0
0.0
0.005
0.003
0.0
The configuration is stored as a dictionary. Note that the configuration can be converted to a dictionary using the as_dict() method:
# make note of hwo to obtain input_name and output_name
input_data_types = {'input_layer': 'FLOAT'}
input_names = list(input_data_types.keys())
output_names = ["output_layer/Softmax"]