简体   繁体   English

tensorflow 无法将 keras 模型转换为 tensorflow lite

[英]tensorflow can not convert keras model to tensorflow lite

so this is my model所以这是我的模型

model = tf.keras.Sequential([
layers.Dense(40, activation='tanh'),
layers.Dense(9)   
])
learning_rate=tf.keras.optimizers.schedules.ExponentialDecay(
                                                                                                        
initial_learning_rate=0.001,
                                                                                                        
decay_steps=640000,
                                                                                                        
decay_rate=0.001,
                                                                                                                        
 )
 model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
                      loss='MSE',
                    metrics = distance,
 )  

save_callback = keras.callbacks.ModelCheckpoint(
'modle_new',
monitor='distance',
save_best_only=True
 )
model.fit(x, y, epochs=100,batch_size=64, callbacks=save_callback, verbose=2)

and I try to convert this model to tensorflow lite model.我尝试将此模型转换为 tensorflow lite 模型。

converter = tf.lite.TFLiteConverter.from_saved_model('modle_new')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.float16]
tflite_model = converter.convert()

and it does not work.它不起作用。

this is errors这是错误

and I try to use tf.lite.TFLiteConverter.from_keras_model(model) , it does not work neither.I hope someone can solve this problem, and thanks in advance.我尝试使用tf.lite.TFLiteConverter.from_keras_model(model) ,它也不起作用。我希望有人能解决这个问题,并提前感谢。

I have already solved this problem, though I don't know the priciple.我已经解决了这个问题,虽然我不知道原理。

converter = tf.lite.TFLiteConverter.from_saved_model('modle_new/')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.float16]
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()
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)

if you have the same problem, you can try my code, it may solve your problem.如果你有同样的问题,你可以试试我的代码,它可能会解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM