0

我训练了一个用于类识别的模型。我使用 fizyr 的 Keras 进行训练:Fizyr - Keras Retinanet GitHub

我设法以优异的成绩完成了培训。我的目标是将获得的模型集成到android中。

我尝试使用此脚本转换模型:

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)

但是当我将模型导入到android应用程序时,应用程序崩溃了。

任何人都知道如何帮助我将 keras .h5 模型转换为 .tflite 模型?

控制台输出:

2021-10-07 12:09:17.221 21554-21599/org.tensorflow.codelabs.objectdetection E/tflite:找不到内置操作码“MUL”版本“5”的操作。可能支持此内置函数的旧版本。您是否在使用带有较新模型的旧 TFLite 二进制文件?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:致命异常:DefaultDispatcher-worker-1 进程:org.tensorflow.codelabs.objectdetection,PID:21554 java.lang。 AssertionError:初始化 ObjectDetector 时发生错误:未找到内置操作码“MUL”版本“5”的操作。可能支持此内置函数的旧版本。您是否在使用带有较新模型的旧 TFLite 二进制文件?

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

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 ) 在 kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) 在 kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) 在 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt: 750) 在 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) 在 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) 2021-10-07 12:09:17.245 21554-21599/org.tensorflow.codelabs.objectdetection I/Process:发送信号。PID:21554 SIG:9 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: 发送信号. PID:21554 SIG:9 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: 发送信号. PID:21554 SIG:9 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:发送信号。PID:21554 SIG:9 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:发送信号。PID:21554 SIG:9 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:发送信号。PID:21554 SIG:9 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:发送信号。PID:21554 SIG:9

4

1 回答 1

0

通常,您首先导入结构模型,然后加载权重。另外,使用 Model 类是错误的。这接受输入 (input_sample, output_sample)

凯拉斯模型

试试这种方式:

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)

根据他提出的方法(单元格2)

链接示例

于 2021-10-07T15:51:25.763 回答