How to convert keras(h5) file to a tflite file?

前端 未结 7 904
不思量自难忘°
不思量自难忘° 2021-01-31 19:25

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)

7条回答
  •  耶瑟儿~
    2021-01-31 19:49

    from tensorflow.contrib import lite
    converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5')
    tfmodel = converter.convert()
    open ("model.tflite" , "wb") .write(tfmodel)
    

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

    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. 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 )
      
    3. Run the cell. You will get a model.tflite file. Right click on the file and select "DOWNLOAD" option.

提交回复
热议问题