android - 在 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
.
loadBuffer
loadArray
我无法弄清楚如何(1x100x12)
通过loadBuffer
.
loadArray
似乎不能int[1][100][12]
作为输入。
请让我知道这样做的正确方法。
解决方案
您可以从 inputFeature0 的 getBuffer 方法中获取一个 ByteBuffer。
您可以操纵 ByteBuffer 对象来填充该值。
或者你也可以创建一个带有数组的 TensorBuffer。您可以从https://github.com/tensorflow/tflite-support/blob/86687d36d93ad5a2e16c0da0ddb55adc6d1c7af1/tensorflow_lite_support/java/src/java/org/tensorflow/lite/support/tensorbuffer/TensorBuffer.java#L87找到示例
推荐阅读
- openstack - `cinder-manage db sync` 和 `cinder-manage db online_data_migrations` 命令之间有什么区别?
- php - 在 Wordpress 中强制下载 PDF
- django - 我在 Django 中得到(无法分配“11”:“Notification.user_to_notify”必须是“用户”实例。)
- python - 从 yahoofinancials 读取输出
- excel - 简化总 excel 公式
- javascript - Javascript 多类继承(以书店为例,这三个类:Member、Book、Transaction)
- c# - 插入数据表或更新(如果存在)
- python - 在 Raspi 上使用带有 Python 的 C 库
- ios - 如何在 iOS App 中使用 TDD 方法测试私有方法?
- c# - 对象引用未设置为用于 MVC 文件(图像)上传的对象实例