【问题标题】:Load TFLite model in android project using android studio使用 android studio 在 android 项目中加载 TFLite 模型
【发布时间】:2020-12-02 14:57:18
【问题描述】:

我正在将一个 tflite 模型(SsdMobilenetV2320x320Coco17Tpu8 从 TF detection zoo 和 ocnverter 在 tflite 转换器的帮助下导入到 tf lite。)到 Android 工作室的应用程序中。我使用 new->other->tf lite 模型方法来导入模型并添加依赖项。然而,有一个我不知道的“背景”。

SsdMobilenetV2320x320Coco17Tpu8 model = SsdMobilenetV2320x320Coco17Tpu8.newInstance(context);

来自 tf (https://www.tensorflow.org/lite/guide/android) 的帮助文档没有说明这个“上下文”是什么!

我发现的其他解决方案是 Classifier 类,我认为它对我没有用处,因为这是一个对象检测网络。

这是 .ml.SsdMobilenetV2320x320Coco17Tpu8 文件中给出的代码示例:

try {
        SsdMobilenetV2320x320Coco17Tpu8 model = SsdMobilenetV2320x320Coco17Tpu8.newInstance(context);

        // Creates inputs for reference.
        TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 1, 1, 3}, DataType.UINT8);
        inputFeature0.loadBuffer(byteBuffer);

        // Runs model inference and gets result.
        SsdMobilenetV2320x320Coco17Tpu8.Outputs outputs = model.process(inputFeature0);
        TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
        TensorBuffer outputFeature1 = outputs.getOutputFeature1AsTensorBuffer();
        TensorBuffer outputFeature2 = outputs.getOutputFeature2AsTensorBuffer();
        TensorBuffer outputFeature3 = outputs.getOutputFeature3AsTensorBuffer();
        TensorBuffer outputFeature4 = outputs.getOutputFeature4AsTensorBuffer();
        TensorBuffer outputFeature5 = outputs.getOutputFeature5AsTensorBuffer();
        TensorBuffer outputFeature6 = outputs.getOutputFeature6AsTensorBuffer();
        TensorBuffer outputFeature7 = outputs.getOutputFeature7AsTensorBuffer();

        // Releases model resources if no longer used.
        model.close();
    } catch (IOException e) {
        // TODO Handle the exception
    }
}

关于inputFeature0.loadBuffer(byteBuffer); 的任何帮助也都得到了应用,我猜这是在创建实例后从内存中引导数据。

【问题讨论】:

    标签: android tensorflow tensorflow-lite


    【解决方案1】:

    但是有一个我不知道的“上下文”。

    我还没有尝试过这个新的Android studio功能,但是android提供了getApplicationContext()的方法,我会试试看

    关于 inputFeature0.loadBuffer(byteBuffer); 的任何帮助;也是 appritiated,我猜那是从内存中引导数据之后 实例已创建。

    TensorBuffer inputFeature0 是你的输入,看看可能的加载方法(它消耗字节缓冲区,以及普通的一维数组)。还可以通过更改TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 1, 1, 3}, DataType.UINT8); 来指定您的输入大小/类型预先加载请参阅details 它在需要预处理/后处理时会派上用场。

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多