Export Model

View in Colab     GitHub source

You can easily export your model the best model found by AutoKeras as a Keras Model.

The following example uses ImageClassifier as an example. All the tasks and the AutoModel has this export_model function.

!!pip install tensorflow==2.1.0
!!pip install autokeras
import tensorflow as tf

print(tf.__version__)

from tensorflow.keras.datasets import mnist
import tensorflow as tf
import autokeras as ak

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Initialize the image classifier.
clf = ak.ImageClassifier(max_trials=1)  # Try only 1 model.(Increase accordingly)
# Feed the image classifier with training data.
clf.fit(x_train, y_train, epochs=1)  # Change no of epochs to improve the model
# Export as a Keras Model.
model = clf.export_model()

print(type(model))  # <class 'tensorflow.python.keras.engine.training.Model'>

try:
    model.save("model_autokeras", save_format="tf")
except:
    model.save("model_autokeras.h5")

from tensorflow.keras.models import load_model

loaded_model = load_model("model_autokeras", custom_objects=ak.CUSTOM_OBJECTS)

predicted_y = loaded_model.predict(tf.expand_dims(x_test, -1))
print(predicted_y)