我有一个keras(h5)文件。我需要将其转换为tflite? 我研究了一下,首先我需要通过h5-> pb-> tflite (因为h5-tflite有时会导致某些问题)
答案 0 :(得分:9)
这在使用Tensorflow 2.1.0和Keras 2.3.1的Windows 10上对我有用
import tensorflow as tf
model = tf.keras.models.load_model('model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
答案 1 :(得分:3)
from tensorflow.contrib import lite
converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5')
tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)
您可以使用TFLiteConverter将.h5文件直接转换为.tflite文件。 这不适用于Windows。
对于Windows,使用此Google Colab notebook进行转换。上载.h5文件,它将转换为.tflite文件。
跟随,如果您想自己尝试:
创建一个代码单元并插入此代码。
from tensorflow.contrib import lite
converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5' ) # Your model's name
model = converter.convert()
file = open( 'model.tflite' , 'wb' )
file.write( model )
运行单元格。您将获得一个model.tflite文件。右键单击该文件,然后选择“下载”选项。
答案 2 :(得分:0)
使用此工具转换为Tensorflow: Keras to Tensorflow Converter
然后使用以下3个代码段之一(取决于您保存TF文件的方式)将其转换为TF-Lite:
# Converting a GraphDef from session.
converter = lite.TFLiteConverter.from_session(sess, in_tensors, out_tensors)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
# Converting a GraphDef from file.
converter = lite.TFLiteConverter.from_frozen_graph(
graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
# Converting a SavedModel.
converter = lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
答案 3 :(得分:0)
有一个因素,您必须要考虑。在转换之前,您需要更改学习阶段。当您具有Dropout或Batch Normalization时,这非常重要。您可以看一下'Keras model to tflite'或'Problem after converting keras model into Tensorflow pb'的讨论
答案 4 :(得分:0)
仅Tensorflow和Keras的某些特定版本可在所有操作系统中正常工作。我什至尝试了toco命令行,但是它也有问题。使用tensorflow == 1.13.0-rc1和keras == 2.1.3
然后这将起作用
from tensorflow.contrib import lite
converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5' ) # Your model's name
model = converter.convert()
file = open( 'model.tflite' , 'wb' )
file.write( model )
答案 5 :(得分:0)
如果您使用的是Google Colab Notebook,请尝试以下操作:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model_file('model.h5')
tfmodel = converter.convert()
open ('model.tflite' , "wb") .write(tfmodel)
答案 6 :(得分:0)
import tensorflow as tf
from keras_retinanet.models import load_model
from keras.layers import Input
from keras.models import Model
def get_file_size(file_path):
size = os.path.getsize(file_path)
return size
def convert_bytes(size, unit=None):
if unit == "KB":
return print('File size: ' + str(round(size / 1024, 3)) + ' Kilobytes')
elif unit == "MB":
return print('File size: ' + str(round(size / (1024 * 1024), 3)) + ' Megabytes')
else:
return print('File size: ' + str(size) + ' bytes')
def convert_model_to_tflite(model_path = "/content/drive/MyDrive/Model/resnet152_csv_180_inference.h5", filename = "converted_model.tflite"):
model = load_model(model_path)
fixed_input = Input((416,416,3))
fixed_model = Model(fixed_input,model(fixed_input))
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
tflite_model = converter.convert()
open(filename, "wb").write(tflite_model)
print(convert_bytes(get_file_size("converted_model.tflite"), "MB"))
答案 7 :(得分:0)
刚刚在笔记本中使用此代码从 CoLab 完成此操作:
import tensorflow as tf
model = tf.keras.models.load_model('yourmodel.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflmodel = converter.convert()
file = open( 'yourmodel.tflite' , 'wb' )
file.write( tflmodel )
我无法通过 CoLab 上传 h5 模型,因此我安装了 Google Drive,将其上传到那里,然后将其移至笔记本内容文件夹。