首页 > 解决方案 > 在 Android 中使用 Tensorflow Lite 模型运行推理

问题描述

我正在尝试对 TFLite 模型进行推理。

我的 TensorFlow 模型规范是:

Input Shape(Int) - (None, 100, 12) [Batch Size will be 1 while inferencing, So, Input will be 1x100x12] 
Output Shape(Float) - (None, 3) [If Batch Size is 1, output will be 1x3]

我按照此处概述的步骤通过 UI 导入模型(New -> Other -> Tensorflow Lite Model)

之后,我使用了模型规范提供的示例代码 -

HarClassification model = HarClassification.newInstance(this.getApplicationContext());

// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 100, 12}, DataType.FLOAT32);

//inputFeature0.loadBuffer(byteBuffer);

// Runs model inference and gets result.
HarClassification.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();

Log.i("Tensorflow","Ran successfully");

// Releases model resources if no longer used.
model.close();

请注意,我不是要推断图像。我更喜欢推断一个整数数组。

我尝试过的事情:

我知道我有两种操作TensorBuffer.

  1. loadBuffer
  2. loadArray

我无法弄清楚如何(1x100x12)通过loadBuffer. loadArray似乎不能int[1][100][12]作为输入。

请让我知道这样做的正确方法。

标签: androidtensorflow-lite

解决方案



推荐阅读