autokeras model conversion to tensorflowjs model

241 Views Asked by At

I'm trying to make model through autokeras, and want to deploy it to mobile device. For that, I need to convert generated model to mobile format using tensorflowjs_converter. Before trying my model, I tested with autokeras regressor example, but conversion failed with following error message.

ValueError: The same saveable will be restored with two names: layer_with_weights-0/encoding_layers/6/_table/.ATTRIBUTES/table

I tested only saved tf-format because I saw in the tensorflowjs document, saved keras-format might not be converted completely(frozen model was not made well from tf-saved model, so skipped).

Test env. when generate and save model:
  - python : 3.6.8
  - tensorflow : 2.3.0
  - autokeras : 1.0.8
  - OS : wsl2, Ubuntu 20.06
  - conda : 4.8.3
Test env. when convert model:
  - python : 3.6.8
  - tensorflow-cpu : 2.3.0 (pip list shows like this)
  - tensorflowjs : 2.4.0
  - OS : wsl2, Ubuntu 20.06

This is the code to create model file(same as example except the last line to save the model).

from sklearn.datasets import fetch_california_housing
import numpy as np
import pandas as pd
import tensorflow as tf
import autokeras as ak

house_dataset = fetch_california_housing()
df = pd.DataFrame(
    np.concatenate((
        house_dataset.data, 
        house_dataset.target.reshape(-1,1)),
        axis=1),
    columns=house_dataset.feature_names + ['Price'])
train_size = int(df.shape[0] * 0.9)
df[:train_size].to_csv('train.csv', index=False)
df[train_size:].to_csv('eval.csv', index=False)
train_file_path = 'train.csv'
test_file_path = 'eval.csv'
import pandas as pd
import numpy as np
# x_train as pandas.DataFrame, y_train as pandas.Series
x_train = pd.read_csv('train.csv')
print(type(x_train)) # pandas.DataFrame
y_train = x_train.pop('Price')
print(type(y_train)) # pandas.Series

# Preparing testing data.
x_test = pd.read_csv('eval.csv')
y_test = x_test.pop('Price')
# You can also use numpy.ndarray for x_train and y_train.
x_train = x_train.to_numpy().astype(np.unicode)
y_train = y_train.to_numpy()
x_test = x_test.to_numpy().astype(np.unicode)
y_test = y_test.to_numpy()
# It tries 10 different models.
reg = ak.StructuredDataRegressor(max_trials=3, overwrite=True)
reg.fit(x_train, y_train, epochs=10)
model = reg.export_model()
type(model)
tf.keras.models.save_model(model, "model_test/pb_california", save_format="tf")

Model convert step. I also used converted sequential model (tf.keras.Sequential(model.layers)), but same error produces.

<< tf_saved_model -> tfjs_graph_model >>
(tfjsv) D:\0.Projects\autokeras_test\tfjs\tfjsv>tensorflowjs_converter --input_format tf_saved_model --output_format tfjs_graph_model D:\0.Projects\autokeras_test\model_test\pb_california D:\0.Projects\autokeras_test\model_test\res\

0

There are 0 best solutions below