报错AttributeError: type object ‘TFLiteConverterV2‘ has no attribute ‘from"/>
TensorFlow报错AttributeError: type object ‘TFLiteConverterV2‘ has no attribute ‘from
文章目录
- 问题描述
- 解决方案
- 参考文献
问题描述
pip list | grep tensorflowtensorflow-gpu==2.3.0
使用 TensorFlow2 转换 Keras 模型为 TensorFlow Lite 模型时报错 AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_keras_model_file'
import numpy as np
import tensorflow as tfxs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 0.0, 3.0, 5.0, 7.0], dtype=float)
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
modelpile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500)
print(model.predict([10.0]))keras_file = 'linear.h5'
tf.keras.models.save_model(model, keras_file)
converter = tf.lite.TFLiteConverter.from_keras_model_file(model)
tflite_model = converter.convert()
with open('linear.tflite', 'wb') as f:f.write(tflite_model)
解决方案
- 加载 Keras 模型
tf.keras.models.load_model()
- 将
from_keras_model_file()
改成from_keras_model()
import numpy as np
import tensorflow as tfxs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 0.0, 3.0, 5.0, 7.0], dtype=float)
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
modelpile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500)
print(model.predict([10.0]))keras_file = 'linear.h5'
tf.keras.models.save_model(model, keras_file)
model = tf.keras.models.load_model(keras_file)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('linear.tflite', 'wb') as f:f.write(tflite_model)
参考文献
- TensorFlow Lite 转换器
- tf.lite.TFLiteConverter
- AttributeError: type object ‘TFLiteConverterV2’ has no attribute ‘from_keras_model_file’
更多推荐
TensorFlow报错AttributeError: type object ‘TFLiteConverterV2‘ has no attribute ‘fr
发布评论