keras retinanet model converter to tflite

456 Views Asked by At

I trained a model for class recognition. I used fizyr's Keras for training: Fizyr - Keras Retinanet GitHub.

I managed to finish the training with excellent results. My goal is to integrate the obtained model into android.

I tried to convert the model using this script:

import tensorflow as tf
from keras_retinanet.models import load_model
from keras.layers import Input
from keras.models import Model
if __name__ == "__main__":
model = load_model("modelFINAL.h5")
   fixed_input = Input((1080,1920,3))
   fixed_model = Model(fixed_input,model(fixed_input))
   converter = tf.lite.TFLiteConverter.from_keras_model(fixed_model)
   tflite = converter.convert()
   # Save the model.
   with open('model.tflite', 'wb') as f:
     f.write(tflite)

But when I go to import the model into the android application, the application crashes.

Anyone know how to help me convert a keras .h5 model to a .tflite model?

Console output:

2021-10-07 12:09:17.221 21554-21599/org.tensorflow.codelabs.objectdetection E/tflite: Didn't find op for builtin opcode 'MUL' version '5'. An older version of this builtin might be supported. Are you using an old TFLite binary with a newer model? 2021-10-07 12:09:17.221 21554-21599/org.tensorflow.codelabs.objectdetection E/tflite: Registration failed. 2021-10-07 12:09:17.234 21554-21599/org.tensorflow.codelabs.objectdetection E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1 Process: org.tensorflow.codelabs.objectdetection, PID: 21554 java.lang.AssertionError: Error occurred when initializing ObjectDetector: Didn't find op for builtin opcode 'MUL' version '5'. An older version of this builtin might be supported. Are you using an old TFLite binary with a newer model?

at org.tensorflow.lite.task.vision.detector.ObjectDetector.initJniWithModelFdAndOptions(Native

Method) at org.tensorflow.lite.task.vision.detector.ObjectDetector.access$000(ObjectDetector.java:86) at org.tensorflow.lite.task.vision.detector.ObjectDetector$1.createHandle(ObjectDetector.java:152) at org.tensorflow.lite.task.vision.detector.ObjectDetector$1.createHandle(ObjectDetector.java:145) at org.tensorflow.lite.task.core.TaskJniUtils$1.createHandle(TaskJniUtils.java:70) at org.tensorflow.lite.task.core.TaskJniUtils.createHandleFromLibrary(TaskJniUtils.java:91) at org.tensorflow.lite.task.core.TaskJniUtils.createHandleFromFdAndOptions(TaskJniUtils.java:66) at org.tensorflow.lite.task.vision.detector.ObjectDetector.createFromFileAndOptions(ObjectDetector.java:143) at org.tensorflow.codelabs.objectdetection.MainActivity.runObjectDetection(MainActivity.kt:127) at org.tensorflow.codelabs.objectdetection.MainActivity.access$runObjectDetection(MainActivity.kt:48) at org.tensorflow.codelabs.objectdetection.MainActivity$setViewAndDetect$1.invokeSuspend(MainActivity.kt:165) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) 2021-10-07 12:09:17.245 21554-21599/org.tensorflow.codelabs.objectdetection I/Process: Sending signal. PID: 21554 SIG: 9

1

There are 1 best solutions below

1
On

Generally, you import the structural model first and then load the weights. Also, you are wrong to use the Model class. This accepts input (input_sample, output_sample)

Keras Model

Try it this way:

loaded_model = models.load_model(model_path.h5, backbone_name='resnet50')
converter = tf.lite.TFLiteConverter.from_keras_model(loaded_model)
tflite_model = converter.convert()
with tf.io.gfile.GFile('name.tflite', 'wb') as f:
  f.write(tflite_model)

In according to the method he proposes (cell 2)

link Example