Keras
Example of Keras framework with CUSTOM endpoints
Adding package to the classpath
Firstly, we require to include the main package to the classpath so that the notebook can load every one of the important libraries from Konduit-Serving into the Jupyter notebook kernel.
%classpath add jar ../../konduit.jarViewing Python script code
We're creating a Keras model from scratch here and then converting that into .h5 (HDF5) format.
%%bash
less train.pyYou can view and follow through the code to get more information on how the model is trained.
import tensorflow as tf
from keras.datasets import mnist
tensorflow_version = tf.__version__
print(tensorflow_version)
# Load data
train_data, test_data = mnist.load_data()
x_train, y_train = train_data
x_test, y_test = test_data
# Normalize
x_train = x_train / 255.0
x_test = x_test / 255.0
def get_model():
inputs = tf.keras.layers.Input(shape=(28, 28), name="input_layer")
x = tf.keras.layers.Flatten()(inputs)
x = tf.keras.layers.Dense(200, activation="relu")(x)
x = tf.keras.layers.Dense(100, activation="relu")(x)
x = tf.keras.layers.Dense(60, activation="relu")(x)
x = tf.keras.layers.Dense(30, activation="relu")(x)
outputs = tf.keras.layers.Dense(10, activation="softmax", name="output_layer")(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.compile(
optimizer='sgd',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
return model
def train(epochs=8):
model = get_model()
model.fit(x_train, y_train, epochs=epochs)
model.summary()
print("\n\n---\n"
"Inputs: {}".format(model.inputs))
print("Outputs: {}\n---".format(model.outputs))
return model
train(8).save("keras.h5", save_format="h5")Starting a server
Let's start to serve the model in Konduit-Serving.
You'll be able to see a similar output like below.
List the active servers available by using konduit list command.
You'll see the following list of the active Konduit servers.
View the logs for the last 100 lines for a given id by using the konduit logs command.
Logging is printed on the notebook once you run the above command.
Feeding an input to test the model
View the test image before testing the model
We're going to use available test image in this directory.

Let's predict the output from the server with the above input image.
The output of classification:
output_layer: probabilities of possible outputsprob: highest probabilityindex: the location of an item in an arraylabel: label of image classification
Stopping the server
Once we're finished with the server, we can stop using the konduit stop command following the id's server.
You'll be able to see the following message.
Last updated
Was this helpful?