Keras (TensorFlow)

Deploy a model built with the Keras framework.
Keras is a deep learning API built on top of TensorFlow. Baseten supports deploying Keras models out of the box.
All you need to do first is install the Baseten client and create an API key.
Baseten officially supports tensorflow version 2.4.0 or higher. Especially if you're using an online notebook environment like Google Colab or a bundle of packages like Anaconda, ensure that the version you are using is supported. If it's not, use the --upgrade flag and pip will install the most recent version.


Using this Jupyter notebook, you can build and deploy a Keras model to your Baseten account.

Deploying a Keras model

Deploying a Keras model is as simple as:
import baseten
baseten_model = baseten.deploy(
model_name='My Keras model'
If you have already saved your model, just load it back into memory, test it to ensure it works, and deploy as in the above.

Example deployment

This code sample deploys a convolutional neural network built in Keras to do OCR on hand-written arabic numerals.
# Source:
import baseten
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
num_classes = 10
input_shape = (28, 28, 1)
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Scale images to the [0, 1] range
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = keras.Sequential(
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Dense(num_classes, activation="softmax"),
batch_size = 128
epochs = 3
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]), y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
baseten.login("*** INSERT API KEY ***") #
baseten_model = baseten.deploy(
model_name='MNIST Keras Convnet'