user10638958
user10638958

Reputation:

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

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)

Upvotes: 44

Views: 64620

Answers (11)

Joyanta J. Mondal
Joyanta J. Mondal

Reputation: 1047

For Tensorflow GPU 2.6.2 and Keras 2.6.0:

import tensorflow as tf
model=tf.keras.models.load_model("./your_model.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(model) 
converter.experimental_new_converter = True
tflite_model = converter.convert()
file = open( 'converted_model.tflite' , 'wb' ) 
file.write( tflite_model )

Upvotes: 0

Ayanabha
Ayanabha

Reputation: 51

If you are using Tensorflow-2 then you can follow these steps:

import tensorflow as tf
from keras.models import load_model
model = load_model("model.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tfmodel = converter.convert()
open("model.tflite", "wb") .write(tfmodel)

In TF-2, it requires to load the Keras model instance and returns a converted instance. Check out this link for more details.

Upvotes: 1

Rinkesh panwar
Rinkesh panwar

Reputation: 51

import tensorflow as tf
from tensorflow import lite
from tensorflow.keras.models import load_model
converter = lite.TFLiteConverter.from_keras_model(model)
tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)

This works for me. I am using keras==2.6.0 and tensorflow-cpu==2.5.0 version. For more information, you can visit https://www.tensorflow.org/guide/keras/save_and_serialize .

Upvotes: 1

Joe Mattioni
Joe Mattioni

Reputation: 99

Just did this from CoLab using this code in a notebook:

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 )

I had difficulty uploading the h5 model via CoLab so I mounted my Google Drive, uploaded it there, and then moved it over to the notebook content folder.

Upvotes: 6

Ashish patel
Ashish patel

Reputation: 1

Convert RetinaNet to tflite

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"))

Upvotes: 0

mlenthusiast
mlenthusiast

Reputation: 1204

Converting a GraphDef from the 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()

Upvotes: 1

Baalateja Kataru
Baalateja Kataru

Reputation: 869

This worked for me on Windows 10 using Tensorflow 2.1.0 and Keras 2.3.1

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)

Upvotes: 34

Yanay Hollander
Yanay Hollander

Reputation: 427

If You are using Google Colab Notebook try this:

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

Upvotes: 1

KETAN KANJIYA
KETAN KANJIYA

Reputation: 1

Only some specific version of Tensorflow and Keras works properly in all the os. I even tried toco command line but it has issues too. Use tensorflow==1.13.0-rc1 and keras==2.1.3

and then after this will work

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 )

Upvotes: 0

Hazarapet Tunanyan
Hazarapet Tunanyan

Reputation: 2865

There is one factor, which you must to consider. You need to change the learning phase, before converting. It's super important, when you have Dropout or Batch Normalization. You can take a look at 'Keras model to tflite' or 'Problem after converting keras model into Tensorflow pb' discussions

Upvotes: 0

user9477964
user9477964

Reputation:

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.

Upvotes: 37

Related Questions