保存模型
以下三种方法均可以保存模型结构和参数
①保存为.h5
net.save('net.h5',save_format='h5')
②保存为.pb
tf.saved_model.save(net,"pb1/")
③保存为.pb
tf.keras.models.save_model(net,'pb2/')
加载模型
new_model = tf.keras.models.load_model('net.h5')
new_model = tf.keras.models.load_model("pb1")
new_model = tf.keras.models.load_model('pb2')
模型转换
① .h5 转 .tflite
loaded_keras_model = tf.keras.models.load_model('net.h5')
keras_to_tflite_converter =
tf.lite.TFLiteConverter.from_keras_model(loaded_keras_model)
keras_tflite = keras_to_tflite_converter.convert()
with open('net.tflite', 'wb') as f:
f.write(keras_tflite)
② .pb转.tflite
saved_model_to_tflite_converter =
tf.lite.TFLiteConverter.from_saved_model('pb1')
saved_model_tflite = saved_model_to_tflite_converter.convert()
with open('net.tflite', 'wb') as f:
f.write(saved_model_tflite)