简体   繁体   English

keras retin.net model 转换器到 tflite

[英]keras retinanet model converter to tflite

I trained a model for class recognition.我训练了一个 model 来识别 class。 I used fizyr's Keras for training: Fizyr - Keras Retin.net GitHub .我使用 fizyr 的 Keras 进行训练: Fizyr - Keras Retin.net GitHub

I managed to finish the training with excellent results.我设法以优异的成绩完成了培训。 My goal is to integrate the obtained model into android.我的目标是将得到的model整合成android。

I tried to convert the model using this script:我尝试使用此脚本转换 model:

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.但是当我 go 将 model 导入 android 应用程序时,应用程序崩溃了。

Anyone know how to help me convert a keras.h5 model to a.tflite model?任何人都知道如何帮助我将 keras.h5 model 转换为 a.tflite model?

Console output:控制台 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'. 2021-10-07 12:09:17.221 21554-21599/org.tensorflow.codelabs.objectdetection E/tflite:未找到内置操作码“MUL”版本“5”的操作。 An older version of this builtin might be supported.可能支持此内置函数的旧版本。 Are you using an old TFLite binary with a newer model?您是否将旧的 TFLite 二进制文件与较新的 model 一起使用? 2021-10-07 12:09:17.221 21554-21599/org.tensorflow.codelabs.objectdetection E/tflite: Registration failed. 2021-10-07 12:09:17.221 21554-21599/org.tensorflow.codelabs.objectdetection E/tflite:注册失败。 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'. 2021-10-07 12:09:17.234 21554-21599/org.88453637644488.codelabs.objectdetection E/AndroidRuntime:致命异常:DefaultDispatcher-worker-1 进程:org.tensorflow.codelabs.objectdetection,PID:2155844.lang.5823 AssertionError:初始化 ObjectDetector 时发生错误:没有找到内置操作码“MUL”版本“5”的操作。 An older version of this builtin might be supported.可能支持此内置函数的旧版本。 Are you using an old TFLite binary with a newer model?您是否将旧的 TFLite 二进制文件与较新的 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.cod方法)在org.tensorflow.lite.task.vision.detector.ObjectDetector.access$000(ObjectDetector.java:86)在org.tensorflow.lite.task.vision.detector.ObjectDetector$1.createHandle(ObjectDetector.88213246945)88:152在 org.tensorflow.lite.task.vision.detector.ObjectDetector$1.createHandle(ObjectDetector.java:145) 在 org.tensorflow.lite.task.core.TaskJniUtils$1.createHandle(TaskJniUtils.882132469484488g) or .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) 在 org.tensorflow.codelabs.objectdetection.MainActivity.runObjectDetection(MainActivity.kt:127) 在 org.tensorflow.codelabs.objectdetection.MainActivity.access:$inActivity8.k(t ) 在 org.tensorflow.cod elabs.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. elabs.objectdetection.MainActivity$setViewAndDetect$1.invokeSuspend(MainActivity.kt:165) 在 kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 在 kotlinx.coroutines.Dispatched:Task.run(DispatchedTask.run. ) 在 kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) 在 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) 在 kotlinx.coroutines.scheduling.CoroutineWorker.Scheduler$Worker CoroutineScheduler.kt:678) 在 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) 2021-10-07 12:09:17.245 21554-21599/org.88453637644488.codelabs.objectdetection I/过程:发送信号。 PID: 21554 SIG: 9 PID:21554 SIG:9

Generally, you import the structural model first and then load the weights.一般先导入结构体model再加载权重。 Also, you are wrong to use the Model class. This accepts input (input_sample, output_sample)另外,你错误的使用Model class 这个接受输入(input_sample, output_sample)

Keras Model 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)根据他提出的方法(单元格 2)

link Example 链接示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM