Search…
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.
1
%classpath add jar ../../konduit.jar
Copied!
Classpaths can be considered similar to site-packages in the python ecosystem where each library that's to be imported to your code is loaded from.

Viewing Python script code

We're creating a Keras model from scratch here and then converting that into .h5 (HDF5) format.
1
%%bash
2
less train.py
Copied!
You can view and follow through the code to get more information on how the model is trained.
1
import tensorflow as tf
2
3
from keras.datasets import mnist
4
5
6
tensorflow_version = tf.__version__
7
print(tensorflow_version)
8
9
# Load data
10
train_data, test_data = mnist.load_data()
11
x_train, y_train = train_data
12
x_test, y_test = test_data
13
14
# Normalize
15
x_train = x_train / 255.0
16
x_test = x_test / 255.0
17
18
19
def get_model():
20
inputs = tf.keras.layers.Input(shape=(28, 28), name="input_layer")
21
x = tf.keras.layers.Flatten()(inputs)
22
x = tf.keras.layers.Dense(200, activation="relu")(x)
23
x = tf.keras.layers.Dense(100, activation="relu")(x)
24
x = tf.keras.layers.Dense(60, activation="relu")(x)
25
x = tf.keras.layers.Dense(30, activation="relu")(x)
26
outputs = tf.keras.layers.Dense(10, activation="softmax", name="output_layer")(x)
27
model = tf.keras.Model(inputs=inputs, outputs=outputs)
28
model.compile(
29
optimizer='sgd',
30
loss='sparse_categorical_crossentropy',
31
metrics=['accuracy']
32
)
33
34
return model
35
36
37
def train(epochs=8):
38
model = get_model()
39
model.fit(x_train, y_train, epochs=epochs)
40
41
model.summary()
42
43
print("\n\n---\n"
44
"Inputs: {}".format(model.inputs))
45
print("Outputs: {}\n---".format(model.outputs))
46
47
return model
48
49
50
train(8).save("keras.h5", save_format="h5")
Copied!

Starting a server

Let's start to serve the model in Konduit-Serving.
1
%%bash
2
konduit serve -id keras-mnist -c keras.json -rwm -b
Copied!
You'll be able to see a similar output like below.
1
Starting konduit server...
2
Expected classpath: /root/konduit/bin/../konduit.jar
3
INFO: Running command /root/miniconda/jre/bin/java -Dkonduit.logs.file.path=/root/.konduit-serving/command_logs/keras-mnist.log -Dlogback.configurationFile=/tmp/logback-run_command_4c3da934e0334efc.xml -cp /root/konduit/bin/../konduit.jar ai.konduit.serving.cli.launcher.KonduitServingLauncher run --instances 1 -s inference -c keras.json -Dserving.id=keras-mnist
4
For server status, execute: 'konduit list'
5
For logs, execute: 'konduit logs keras-mnist'
Copied!
List the active servers available by using konduit list command.
1
%%bash
2
konduit list
Copied!
You'll see the following list of the active Konduit servers.
1
Listing konduit servers...
2
3
# | ID | TYPE | URL | PID | STATUS
4
1 | keras-mnist | inference | localhost:33997 | 24142 | started
Copied!
View the logs for the last 100 lines for a given id by using the konduit logs command.
1
%%bash
2
konduit logs keras-mnist --lines 100
Copied!
Logging is printed on the notebook once you run the above command.
1
.
2
.
3
.
4
5
####################################################################
6
# #
7
# | / _ \ \ | _ \ | | _ _| __ __| | / | / #
8
# . < ( | . | | | | | | | . < . < #
9
# _|\_\ \___/ _|\_| ___/ \__/ ___| _| _|\_\ _) _|\_\ _) #
10
# #
11
####################################################################
12
13
.
14
.
15
.
16
15:15:13.074 [vert.x-eventloop-thread-0] INFO a.k.s.v.p.h.v.InferenceVerticleHttp - Inference HTTP server is listening on host: 'localhost'
17
15:15:13.074 [vert.x-eventloop-thread-0] INFO a.k.s.v.p.h.v.InferenceVerticleHttp - Inference HTTP server started on port 33997 with 4 pipeline steps
18
Copied!

Feeding an input to test the model

View the test image before testing the model
1
%%html
2
<img src="test-image.jpg" alt="title">
Copied!
We're going to use available test image in this directory.
Let's predict the output from the server with the above input image.
1
%%bash
2
konduit predict keras-mnist -it multipart '[email protected]'
Copied!
The output of classification:
  • output_layer : probabilities of possible outputs
  • prob : highest probability
  • index : the location of an item in an array
  • label : label of image classification
1
{
2
"output_layer" : [ [ 9.0376153E-7, 1.0595608E-8, 1.3115231E-5, 0.44657645, 6.748624E-12, 0.5524258, 1.848306E-7, 2.7652052E-9, 9.76023E-4, 7.5933513E-6 ] ],
3
"prob" : 0.5524258017539978,
4
"index" : 5,
5
"label" : "5"
6
}
Copied!

Stopping the server

Once we're finished with the server, we can stop using the konduit stop command following the id's server.
1
%%bash
2
konduit stop keras-mnist
Copied!
You'll be able to see the following message.
1
Stopping konduit server 'keras-mnist'
2
Application 'keras-mnist' terminated with status 0
Copied!