将tflite格式的模型部署在安卓移动端详细步骤

将tflite格式的模型部署在安卓移动端详细步骤

步骤一

先将tflite模型放到安卓项目的assets中。
在这里插入图片描述

步骤二

将tensorflow-lite库添加到您的应用程序中。这可以通过将以下行添加到build.gradle文件的依赖项部分中来完成:

implementation 'org.tensorflow:tensorflow-lite:+'

步骤三

完成此操作后,您可以导入TensorFlow Lite解释器。解释器通过提供一组输入来加载模型并允许您运行它。TensorFlow Lite然后将执行模型并编写输出,实际上就是这么简单。

import org.tensorflow.lite.Interpreter;

步骤四

protected Interpreter tflite;
tflite = new Interpreter(loadModelFile(activity));

步骤五

GitHub上的TensorFlow Lite示例中有一个辅助功能。只需确保getModelPath()返回一个指向资源文件夹中文件的字符串,然后模型就会加载。

/** Memory-map the model file in Assets. */
private MappedByteBuffer loadModelFile(Activity activity) throws IOException {
	  AssetFileDescriptor fileDescriptor = activity.getAssets().openFd(getModelPath());
	  FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
	  FileChannel fileChannel = inputStream.getChannel();
	  long startOffset = fileDescriptor.getStartOffset();
	  long declaredLength = fileDescriptor.getDeclaredLength();
	  return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}

步骤六

然后,要对图像进行分类,您所需要做的就是在Interpeter上调用run方法,将图像数据和标签数组传递给它,其余的工作将完成:

tflite.run(imgData, labelProbArray);

TensorFlow 官方示例

git clone https://www.github.com/tensorflow/tensorflow

  • 0
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

特立独行の猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值