如何将keras(h5)文件转换为tflite文件? [英] How to convert keras(h5) file to a tflite file?

查看:446
本文介绍了如何将keras(h5)文件转换为tflite文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个keras(h5)文件.我需要将其转换为tflite? 我研究了一下,首先我需要通过h5-> pb-> tflite (因为h5-tflite有时会导致某些问题)

I got an keras(h5) file. I need to convert it to tflite?? I researched, First i need to go via h5 -> pb -> tflite (because h5 - tflite sometimes results in some issue)

推荐答案

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.

You can use the TFLiteConverter to directly convert .h5 files to .tflite file. This does not work on Windows.

对于Windows,使用此 Google Colab笔记本进行转换.上载.h5文件,它将转换为.tflite文件.

For Windows, use this Google Colab notebook to convert. Upload the .h5 file and it will convert it .tflite file.

跟随,如果您想自己尝试:

Follow, if you want to try it yourself :

  1. 创建一个Google Colab笔记本.在左上角,点击上传"按钮,然后上传您的.h5文件.
  2. 创建一个代码单元并插入此代码.

  1. Create a Google Colab Notebook. In the left top corner, click the "UPLOAD" button and upload your .h5 file.
  2. Create a code cell and insert this code.

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文件.右键单击该文件,然后选择下载"选项.

  • Run the cell. You will get a model.tflite file. Right click on the file and select "DOWNLOAD" option.

    这篇关于如何将keras(h5)文件转换为tflite文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆