Python 将tf模型转换为TFlite模型时出错
我目前正在建立一个模型,用它在我的nano 33 BLE感应板上,通过测量湿度、压力、温度来预测天气,我有5门课。 我使用了一个kaggle数据集来训练它Python 将tf模型转换为TFlite模型时出错,python,tensorflow,keras,arduino,tensorflow-lite,Python,Tensorflow,Keras,Arduino,Tensorflow Lite,我目前正在建立一个模型,用它在我的nano 33 BLE感应板上,通过测量湿度、压力、温度来预测天气,我有5门课。 我使用了一个kaggle数据集来训练它 df_labels = to_categorical(df.pop('Summary')) df_features = np.array(df) from sklearn.model_selection import train_test_split X_train, X_test, y_train,
df_labels = to_categorical(df.pop('Summary'))
df_features = np.array(df)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(df_features, df_labels, test_size=0.15)
normalize = preprocessing.Normalization()
normalize.adapt(X_train)
activ_func = 'gelu'
model = tf.keras.Sequential([
normalize,
tf.keras.layers.Dense(units=6, input_shape=(3,)),
tf.keras.layers.Dense(units=100,activation=activ_func),
tf.keras.layers.Dense(units=100,activation=activ_func),
tf.keras.layers.Dense(units=100,activation=activ_func),
tf.keras.layers.Dense(units=100,activation=activ_func),
tf.keras.layers.Dense(units=5, activation='softmax')
])
model.compile(optimizer='adam',#tf.keras.optimizers.Adagrad(lr=0.001),
loss='categorical_crossentropy',metrics=['acc'])
model.summary()
model.fit(x=X_train,y=y_train,verbose=1,epochs=15,batch_size=32, use_multiprocessing=True)
然后对模型进行训练,我想在运行convert命令时将其转换为tflite模型,我得到以下消息:
# Convert the model to the TensorFlow Lite format without quantization
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the model to disk
open("gesture_model.tflite", "wb").write(tflite_model)
import os
basic_model_size = os.path.getsize("gesture_model.tflite")
print("Model is %d bytes" % basic_model_size)
<unknown>:0: error: failed while converting: 'main': Ops that can be supported by the flex runtime (enabled via setting the -emit-select-tf-ops flag):
tf.Erf {device = ""}
#将模型转换为TensorFlow Lite格式,无需量化
converter=tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model=converter.convert()
#将模型保存到磁盘
打开(“手势\模型.tflite”,“wb”).write(tflite \模型)
导入操作系统
basic\u model\u size=os.path.getsize(“手势\u model.tflite”)
打印(“型号为%d字节”%basic\u Model\u size)
:0:错误:转换“main”时失败:flex运行时支持的操作(通过设置-emit select tf Ops标志启用):
tf.Erf{device=”“}
供您参考,我使用google colab设计模型
如果有人对这个问题有任何想法或解决方案,我将很高兴听到 我解决了这个问题!这是TFlite尚不支持的激活函数“gelu”。我将其更改为“relu”,不再有问题。当您没有设置转换器支持的操作时,通常会发生这种情况 以下是一个例子:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
此支持操作列表不断变化,因此,如果错误仍然出现,您也可以尝试按如下方式设置实验转换器功能:
converter.experimental_new_converter = True
谢谢我已经通过将gelu函数更改为relu解决了这个问题,Tflite还不支持gelu。