Autokeras: Not able to load best AutoModel after saving

Created on 13 Feb 2020  路  7Comments  路  Source: keras-team/autokeras

Hi everyone!

I am using autokeras functional API on Cifar10 dataset and it ran succesfully saving the best model checkpoint in a model.index file. I am unable to load the model from the checkpoint as it did not save a .h5 file.

I would need some basic code for loading saved models by autokeras. The documentation does not contain such loading code: https://autokeras.com/tutorial/export/
The automode.export_model() function does not accept parameters (such as a .h5 file name) when using the AutoModel class.

algorithm documentation wontfix

Most helpful comment

Hi

Thank you for your reply. I loaded the json from the trial.json file from the trial id folder,
path = '...path_to_trial_id...'
json_file = open(path, 'r')
loaded_model_json = json_file.read()
json_file.close()

However, I am not able to call
loaded_model=model_from_json(loaded_model)

image

The code that you provided does not work with the path or the name of the json file or the loaded_model_json. The error is:
File "...\kerastuner\engine\tuner.py", line 150, in load_model
model = self.hypermodel.build(trial.hyperparameters)
AttributeError: 'str' object has no attribute 'hyperparameters'

I would be grateful if you could provide a more detailed sample code on how to load an intermediate trial model.

All 7 comments

I managed to find a compromise by looking into auto_model.py and observing that the AutoModel.export_model() returns a Keras model.
However if an intermediate trial is needed, is still not clear how to load the corresponding model from the saved files (.index and checkpoint).

@picsag Currently, we are not providing this interface. However, I assume you are an advanced user who can dive into the source code. You can do a hacking solution like this:

auto_model.tuner.load_model(trial_id)

The trial_id can be found somewhere in the saving directory. (The name of the json file).

Hi

Thank you for your reply. I loaded the json from the trial.json file from the trial id folder,
path = '...path_to_trial_id...'
json_file = open(path, 'r')
loaded_model_json = json_file.read()
json_file.close()

However, I am not able to call
loaded_model=model_from_json(loaded_model)

image

The code that you provided does not work with the path or the name of the json file or the loaded_model_json. The error is:
File "...\kerastuner\engine\tuner.py", line 150, in load_model
model = self.hypermodel.build(trial.hyperparameters)
AttributeError: 'str' object has no attribute 'hyperparameters'

I would be grateful if you could provide a more detailed sample code on how to load an intermediate trial model.

We will see if more people are looking to use this, we will add this in the FAQ section on the website.

@picsag
may I ask you how good of your trained model performed on cifar10?
I only achieve 74% accuracy by default ak.ImageClassifier... (script as belows)

from tensorflow.keras.datasets import cifar10
import autokeras as ak

# Prepare the dataset.
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Initialize the ImageClassifier.
clf = ak.ImageClassifier()
# Search for the best model.
clf.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=300, batch_size=128)
# Evaluate on the testing data.
print('Accuracy: {accuracy}'.format(
    accuracy=clf.evaluate(x_test, y_test)))

if it need a lot of custom config to get higher performance, I will feel this is not handy, comparing to apply resnet18 to get 94%+ accuracy.

The accuracy is low for now mainly because the image data augmentation layers are not released yet. I expect the new version after tf 2.2 would be much better.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SunYanCN picture SunYanCN  路  5Comments

xuzhang5788 picture xuzhang5788  路  4Comments

marooncn picture marooncn  路  4Comments

arthursdays picture arthursdays  路  5Comments

vincent-hui picture vincent-hui  路  5Comments