将autokeras模型转换为tensorflowjs模型

时间:2020-09-26 05:56:20

标签: tensorflow tensorflow.js

我正在尝试通过autokeras进行建模,并希望将其部署到移动设备上。 为此,我需要使用tensorflowjs_converter将生成的模型转换为移动格式。 在尝试模型之前,我使用autokeras回归器example进行了测试,但是转换失败并显示以下错误消息。

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

我只测试了保存的tf格式,因为我在tensorflowjs文档中看到,保存的keras格式可能无法完全转换(冻结模型与tf保存的模型相比效果不佳,因此被跳过)。

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

这是创建模型文件的代码(与示例相同,除了最后一行用于保存模型)。

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")

模型转换步骤。 我还使用了转换后的顺序模型(tf.keras.Sequential(model.layers)),但是会产生相同的错误。

<< 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 个答案:

没有答案