就存储在model.h5中的模型权重和存储在model.json中的模型体系结构而言,我有Keras模型,我的目标是将构成Keras模型的这两个文件隐匿到tensorflow Lite模型中,我尝试了几种方法,但它似乎不起作用。
当我将Tensoflow 1.15.0与以下代码一起使用时,我得到“ NameError:未定义名称'lite'”,而当我降级到Tensoflow 1.15.0时,我得到了 “ AttributeError:类型对象'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()
答案 0 :(得分:0)
对于此问题,我有以下解决方案:
将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)