Tensorflow lite task library 实战

本文利用Tensorflow lite task library 进行端侧推理,主要参考官网以及以下代码:

 官网API:

集成图像分割器  |  TensorFlow Lite 

网上案例:

https://github.com/khanhlvg/tflite-task-library-demo

之所以给出网上案例,估计刚接触tf的朋友们,只参考官网,可能无法执行具体的示例,所以用该例进行说明:

执行该代码时,手机界面会出现以下错误,可按如下方式解决,

Tensorflow lite task library 实战_第1张图片

先放下成功执行的界面,有两个功能:IMAGESEGMENTER和OBJECTDETECTOR两个功能:

Tensorflow lite task library 实战_第2张图片

Tensorflow lite task library 实战_第3张图片

Tensorflow lite task library 实战_第4张图片

错误1:

Execution failed for task ':app:stripDebugDebugSymbols'.

> No version of NDK matched the requested version 21.0.6113669. Versions available locally: 25.1.8937393

Tensorflow lite task library 实战_第5张图片

将Gradle Scripts->build.gradle android部分添加如下代码:

ndkVersion "25.1.8937393"

 

Tensorflow lite task library 实战_第6张图片

错误2:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.khanhlvg.taskapidemo, PID: 2730
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/tensorflow/lite/support/image/ColorSpaceType;
        at org.tensorflow.lite.task.vision.segmenter.OutputType$1.createMasksFromBuffer(OutputType.java:74)
        at org.tensorflow.lite.task.vision.segmenter.ImageSegmenter.segment(ImageSegmenter.java:224)
        at org.tensorflow.lite.task.vision.segmenter.ImageSegmenter.segment(ImageSegmenter.java:176)
        at com.khanhlvg.taskapidemo.ImageSegmenterActivity.onCreate(ImageSegmenterActivity.java:42)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.tensorflow.lite.support.image.ColorSpaceType" on path: DexPathList[[zip file "/data/app/com.khanhlvg.taskapidemo-yEwPKlVcydWFgmJJsp6FuQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.khanhlvg.taskapidemo-yEwPKlVcydWFgmJJsp6FuQ==/lib/x86, /data/app/com.khanhlvg.taskapidemo-yEwPKlVcydWFgmJJsp6FuQ==/base.apk!/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at org.tensorflow.lite.task.vision.segmenter.OutputType$1.createMasksFromBuffer(OutputType.java:74) 
        at org.tensorflow.lite.task.vision.segmenter.ImageSegmenter.segment(ImageSegmenter.java:224) 
        at org.tensorflow.lite.task.vision.segmenter.ImageSegmenter.segment(ImageSegmenter.java:176) 
        at com.khanhlvg.taskapidemo.ImageSegmenterActivity.onCreate(ImageSegmenterActivity.java:42) 
        at android.app.Activity.performCreate(Activity.java:6975) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

如下图:
Tensorflow lite task library 实战_第7张图片

将Gradle Scripts->build.gradle中,dependencies部分,修改tensorflow-lite-task-vision和tensorflow-lite-task-text版本,即可解决问题,修改后的版本要先到下述仓库中查询,选择合适版本,此处分别选择0.3.1和0.4.2版本:

Maven Central Repository Search

Tensorflow lite task library 实战_第8张图片

//    implementation('org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly') { changing = true}
//    implementation('org.tensorflow:tensorflow-lite-task-text:0.0.0-nightly') { changing = true}
    implementation('org.tensorflow:tensorflow-lite-task-vision:0.3.1') { changing = true}
    implementation('org.tensorflow:tensorflow-lite-task-text:0.4.2') { changing = true}

Tensorflow lite task library 实战_第9张图片

你可能感兴趣的:(边缘计算,tensorflow学习,深度学习,tflite,边缘计算,深度学习)