converter = tf.lite.TFLiteConverter.from_keras_model( './signet.h5')
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
AttributeError Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_2464/3288577076.py in <module>
1 converter = tf.lite.TFLiteConverter.from_keras_model( './signet.h5')
----> 2 tflite_model = converter.convert()
3 # Save the model.
4 with open('model.tflite', 'wb') as f:
5 f.write(tflite_model)
~anaconda3envstensorflowlibsite-packagestensorflowlitepythonlite.py in convert(self)
795 # to None.
796 # Once we have better support for dynamic shapes, we can remove this.
--> 797 if not isinstance(self._keras_model.call, _def_function.Function):
798 # Pass `keep_original_batch_size=True` will ensure that we get an input
799 # signature including the batch dimension specified by the user.
AttributeError: 'str' object has no attribute 'call'
我正试图将h5 keras模型转换为tflite,以便在react native应用程序中使用,但显示此错误。我在论坛上尝试了一些解决方案,其中一个是在github上,但不起作用。有没有办法将我的h5模型转换为flite ?
应该将实际的Keras模型对象传递给转换器的' from_keras_model方法:-)。看到的例子:
import tensorflow as tf
# Create a model using high-level tf.keras.* APIs
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1]),
tf.keras.layers.Dense(units=16, activation='relu'),
tf.keras.layers.Dense(units=1)
])
model.compile(optimizer='sgd', loss='mean_squared_error') # compile the model
model.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model
# (to generate a SavedModel) tf.saved_model.save(model, "saved_model_keras_dir")
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the model.
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
所以你首先需要从H5文件加载。