Deeplearning4j (DL4J)
This document illustrates how to create Konduit Serving configurations with the Java SDK:
Deeplearning4j (DL4J)
Using Java to create a configuration
import ai.konduit.serving.InferenceConfiguration;
import ai.konduit.serving.config.ServingConfig;
import ai.konduit.serving.configprovider.KonduitServingMain;
import ai.konduit.serving.configprovider.KonduitServingMainArgs;
import ai.konduit.serving.model.ModelConfig;
import ai.konduit.serving.model.ModelConfigType;
import ai.konduit.serving.model.TensorDataTypesConfig;
import ai.konduit.serving.pipeline.step.ModelStep;
import ai.konduit.serving.verticles.inference.InferenceVerticle;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.io.FileUtils;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.io.ClassPathResource;
import org.nd4j.serde.binary.BinarySerde;
import org.nd4j.tensorflow.conversion.TensorDataType;Overview
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.
Set the dl4j model path to dl4jmodelfilePath.
Configure the step
Define the DL4J configuration as a ModelConfig object.
tensorDataTypesConfig: The ModelConfig object requires a HashMapinput_data_types. Its keys should represent column names, and the values should represent data types as strings, e.g."INT32","FLOAT",etc,. See here for a list of supported data types.modelConfigType: This argument requires aModelConfigTypeobject. In the Java program above, we recognised that SimpleCNN is configured as a MultiLayerNetwork, in contrast with the ComputationGraph class, which is used for more complex networks. SpecifymodelTypeasMULTI_LAYER_NETWORK, andmodelLoadingPathto point to the location of DL4J weights saved in the ZIP file format.
For the ModelStep object, the following parameters are specified:
modelConfig: pass the ModelConfig object hereinput_names: names for the input dataoutput_names: names for the output data
Configure the server
Specify the following:
httpPort: specify any port number that is not reserved.
The ServingConfig has to be passed to Server in addition to the steps as a Java list. In this case, there is a single step: dl4jModelStep.
The inferenceConfiguration is stored as a JSON File. Set the KonduitServingMainArgs with the saved config.json file path as configPath and other necessary server configuration arguments.
Start server by calling KonduitServingMain with the configurations mentioned in the KonduitServingMainArgs using Callback Function(as per the code mentioned in the Inference Section below)
Inference
We generate a (3, 224, 224) array of random numbers between 0 and 255 as input to the model for prediction.
Before requesting for a prediction, we normalize the image to be between 0 and 1:
To configure the client, set the required URL to connect server and specify any port number that is not reserved (as used in server configuration).
A Callback Function onSuccess is implemented in order to post the Client request and get the HttpResponse, only after the successful run of the KonduitServingMain Server.
Confirm the Output
After executing the above, in order to confirm the successful start of the Server, check for the below output text:
The Output of the program is as follows:
The complete inference configuration in JSON format is as follows:
Last updated
Was this helpful?