Skip to main content

Log a Keras Model

Log a Keras Model with Katonic-SDK Log package.

Import necessary packagesโ€‹

import os

import tensorflow as tf
from katonic.log.logmodel import LogModel
from katonic.log.client import load_model

Define Experiment nameโ€‹

experiment_name= "keras_model_logging"

Initiate LogModel with experiment nameโ€‹

lm = LogModel(experiment_name, source_name='keras_model_logging.ipynb')

Check Metadata of the created / existing experimentโ€‹

# experiment id
exp_id = lm.id

print("experiment name: ", lm.name)
print("experiment location: ", lm.location)
print("experiment id: ", lm.id)
print("experiment status: ", lm.stage)

Artifact path where you want to log your modelโ€‹

artifact_path = "mnist-keras-model"

Define your Keras Modelโ€‹

mnist = tf.keras.datasets.mnist
(X_train_full, y_train_full), (X_test, y_test) = mnist.load_data()

X_valid, X_train = X_train_full[:5000] / 255, X_train_full[5000:] / 255
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]

X_test = X_test / 255

LAYERS = [
tf.keras.layers.Flatten(input_shape=(28, 28), name='inputLayer'),
tf.keras.layers.Dense(300, activation='relu', name='hiddenLayer1'),
tf.keras.layers.Dense(100, activation='relu', name='hiddenLayer2'),
tf.keras.layers.Dense(10, activation='softmax', name='outputLayer')
]

model_clf = tf.keras.models.Sequential(LAYERS)

loss_function = 'sparse_categorical_crossentropy'
optimizers = 'SGD'
metric = ['accuracy']

model_clf.compile(
loss=loss_function,
optimizer=optimizers,
metrics=metric
)

ckpt_path = "data/model_ckpt.h5"

ckpt_cb = tf.keras.callbacks.ModelCheckpoint(ckpt_path, save_best_only=True)

Train Your Keras Modelโ€‹

EPOCHS = 5
VALIDATION = (X_valid, y_valid)

history = model_clf.fit(
X_train, y_train,
epochs=EPOCHS,
validation_data=VALIDATION,
callbacks=[ckpt_cb]
)

Evalute Model Performanceโ€‹

res = model_clf.evaluate(X_test, y_test, verbose=2)
model_mertics = {
model_clf.metrics_names[0]: res[0],
model_clf.metrics_names[1]: res[1]
}

Log Custom Keras Modelโ€‹

lm.model_logging(
model_name="mnistkeras",
model_type="keras",
model=model_clf,
artifact_path=artifact_path,
current_working_dir=f'{os.getcwd()}/keras_model_logging.ipynb',
metrics=model_mertics
)

Check all the logged Experimentsโ€‹

You can search and get all the logged experiments with experiment ID. ss

df_runs = lm.search_runs(exp_id)
print("Number of runs done : ", len(df_runs))

Load your logged keras model with URIโ€‹

model_uri=f"{df_runs['artifact_uri'][0]}/{df_runs['run_name'][0]}"
loaded_model = load_model(model_uri)

Prediction with Loaded modelโ€‹

loaded_model.predict(X_test)