我有model.h5中存储的模型权重和model.json中存储的模型体系结构,我的目标是将构成Keras模型的这两个文件隐藏到tensorflow Lite模型中,我尝试过几种方法,但似乎不起作用。
当我在以下代码中使用Tensoflow 1.15.0时,我得到了"NameError: name‘lite“,当我降级到Tensoflow 1.15.0时,我得到了"AttributeError: type对象'TFLiteConverter’没有属性'from_keras_model'”
有人能事先帮忙谢谢吗?
#from tensorflow.contrib import lite
import tensorflow as tf
from tensorflow.contrib import lite
from keras.models import model_from_json
# Model reconstruction from JSON file
with open('drive/My Drive/Colab Notebooks/model.json', 'r') as f:
model = model_from_json(f.read())
# Load weights into the new model
model.load_weights('drive/My Drive/Colab Notebooks/model.h5')
# Converting a tf.Keras model to a TensorFlow Lite model.
converter = lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()发布于 2019-12-18 09:57:50
对于这个问题,我有以下解决办法:
将tensorflow更新为我当前使用的2.1.0-rc0
然后,代替
model = model_from_json(f.read())使用
model = tf.keras.models.model_from_json(f.read())整个守则将是:
import tensorflow as tf
with open('../input/melanoma-cancer-h5-model/model.json', 'r') as f:
model = tf.keras.models.model_from_json(f.read())
# Load weights into the new model
model.load_weights('../input/melanoma-cancer-h5-model/model.h5')
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("model.tflite","wb").write(tflite_model)https://stackoverflow.com/questions/59354030
复制相似问题