Tensorflow 模型转 tflite ,在安卓端使用

自己在将tensorflow模型移动端部署的时候(使用 tensorflow lite),踩了很多坑,查了很多资料,现在做个记录,所有参考资料在文章最后 参考 处列出。

tensorflow lite是TensorFlow Lite 是 Google I/O 2017 大会上的其中一个重要宣布,有了TensorFlow Lite,应用开发者可以在移动设备上部署人工智能。
tensorflow lite 【github】

这里写图片描述

基本思路:

  1. 在pc端进行 Tensorflow 模型训练,保存训练模型
  2. 使用 工具将该模型转换为 Tensorflow lite 模型
  3. 在Android上使用

tensorflow模型持久化

在tensorflow中进行模型训练,得到适合自己项目的模型。Tensorflow 模型训练好之后会生成三个文件:

  • model.ckpt.meta :保存Tensorflow计算图结构,可以理解为神经网络的网络结构
  • model.ckpt :保存Tensorflow程序中每一个变量的取值,变量是模型中可训练的部分
  • checkpoint :保存一个目录下所有模型文件列表
# 使用tf.train.write_graph导出GraphDef文件
tf.train.write_graph(sess.graph_def, "./", "mz_graph.pb", as_text=False)
# 使用tf.train.save导出checkpoint文件
saver.save(sess, model_path)

生成的模型文件如下图所示:

  • 6
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
以下是在Android上部署Yolov5模型的步骤: 1. 下载Yolov5模型tflite文件,并将其放置在Android项目的assets文件夹中。 2. 在Android项目中添加TensorFlow Lite库的依赖项,以便在应用程序中使用该库。 3. 创建一个TensorFlow Lite解释器对象并加载模型。 ``` // Load the Yolov5 model from the assets folder. Interpreter.Options options = new Interpreter.Options(); Interpreter interpreter = new Interpreter(loadModelFile(), options); ``` 4. 对输入图像进行预处理,以使其适合模型的输入要求。 ``` // Preprocess the image to fit the model input requirements. Bitmap resizedImage = Bitmap.createScaledBitmap(image, INPUT_SIZE, INPUT_SIZE, true); ByteBuffer inputBuffer = convertBitmapToByteBuffer(resizedImage); ``` 5. 运行模型并获取输出结果。 ``` // Run the model on the input image and get the output. float[][][] output = new float[1][GRID_SIZE][GRID_SIZE * ANCHORS_PER_GRID][NUM_CLASSES + 5]; interpreter.run(inputBuffer, output); ``` 6. 处理输出结果,以便可以在图像上绘制边界框并显示检测结果。 ``` // Postprocess the output to draw bounding boxes and display the detection results. List<Recognition> results = postprocess(output, image.getWidth(), image.getHeight()); drawBoundingBoxes(canvas, results); ``` 以上是在Android上部署Yolov5模型的基本步骤。需要注意的是,部署过程中可能会遇到一些问题,例如输入图像的大小和格式不正确,输出结果的解析不正确等等。因此,需要仔细阅读TensorFlow Lite的文档,并根据具体情况进行调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值