mzechmeister / viper

GNU General Public License v3.0
5 stars 3 forks source link

Keras .h5 model to Tensorflow lite model .tflite (Not working) #4

Closed Kengazi closed 4 years ago

Kengazi commented 4 years ago

Having trouble turning in Google Colab python in turning a Keras "model.h5" using TFLiteConverter.from_keras_model("model.h5")

I am using tensorflow 2.2.0

First attempt script

import tensorflow as tf converter = tf.lite.TFLiteConverter.from_keras_model("model.h5") tflite_model = converter.convert() open("Robot_Eyes.tflite", "wb").write(tflite_model)

And Error gotten

AttributeError Traceback (most recent call last)

in () 13 #new_model= tf.keras.models.load_model(filepath="robot_eyes2.h5") 14 ---> 15 converter = tf.lite.TFLiteConverter.from_keras_model("robot_eyes2.h5") 16 17 tflite_model = converter.convert() /usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in from_keras_model(cls, model) 426 # to None. 427 # Once we have better support for dynamic shapes, we can remove this. --> 428 if not isinstance(model.call, _def_function.Function): 429 # Pass `keep_original_batch_size=True` will ensure that we get an input 430 # signature including the batch dimension specified by the user. AttributeError: 'str' object has no attribute 'call' # Second attempt import tensorflow as tf new_model= tf.keras.models.load_model(filepath="robot_eyes2.h5") converter = tf.lite.TFLiteConverter.from_keras_model(new_model) tflite_model = converter.convert() open("Robot_Eyes.tflite", "wb").write(tflite_model) # And 2nd Error gotten OSError Traceback (most recent call last) in () 11 import tensorflow as tf 12 ---> 13 new_model= tf.keras.models.load_model(filepath="robot_eyes2.h5") 14 15 converter = tf.lite.TFLiteConverter.from_keras_model(new_model) 3 frames /usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr) 171 if swmr and swmr_support: 172 flags |= h5f.ACC_SWMR_READ --> 173 fid = h5f.open(name, flags, fapl=fapl) 174 elif mode == 'r+': 175 fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl) h5py/_objects.pyx in h5py._objects.with_phil.wrapper() h5py/_objects.pyx in h5py._objects.with_phil.wrapper() h5py/h5f.pyx in h5py.h5f.open() OSError: Unable to open file (truncated file: eof = 40894464, sblock->base_addr = 0, stored_eof = 159686248) I don't what wrong and I've been trying to solve it.
Kengazi commented 4 years ago

Was able to be successful using this script

!pip install tensorflow==1.15.0

import tensorflow as tf

print(tf.version)

converter = tf.lite.TFLiteConverter.from_keras_model_file("robot_eyes2.h5") # model.h5

tflite_model = converter.convert()

open("Robot_Eyes.tflite", "wb").write(tflite_model)

chamarthisireesha commented 4 years ago

I am sorry that I did not check this issue thread earlier. I hope it is resolved now. I do not understand the relevance of the issue. Anyway, I am glad that it is fixed now :) I am closing it.