TensorFlow Lite 入门样例,亲测有效

参考链接:

Running on mobile with TensorFlow Lite

 

以物体检测模型为例,生成一个APK文件,可以调用TensorFlow Lite模型,进而实现手机端能够运行物体检测模型。

整体分为以下几个步骤:

    步骤一、训练一个TensorFlow 模型(或者从下载他人训练好的模型)

    步骤二、生成.pb文件

    步骤三、将.pb文件转化为.tflite文件

    步骤四、使用Android Studio 导入TensorFlow安卓样例 ,替换成我们生成的tflite文件,重新编译APK

    

 

#配置依赖

https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md

 

#下载样例模型

https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md

 

 

下载文件并解压

$wget http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2018_01_28.tar.gz

$ tar -xzvf ssd_mobilenet_v1_coco_2018_01_28.tar.gz

 

 

 

#设置环境变量

#指定pipeline.config、model.ckpt文件、输出文件的保存路径; 本次测试将样例模型文件放到了/root/tensorflow/mobile/目录下面

#并将 model.ckpt.data-00000-of-00001 重命名为model.ckpt

 

export CONFIG_FILE=/root/tensorflow/mobile/pipeline.config

export CHECKPOINT_PATH=/root/tensorflow/mobile/model.ckpt

export OUTPUT_DIR=/tmp/tflite

 

#Add Libraries to PYTHONPATH

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

 

#生成.pb文件

python object_detection/export_tflite_ssd_graph.py \

--pipeline_config_path=$CONFIG_FILE \

--trained_checkpoint_prefix=$CHECKPOINT_PATH \

--output_directory=$OUTPUT_DIR \

--add_postprocessing_op=true

 

#查看生成结果,会发现有tflite_graph.pb 、 tflite_graph.pbtxt这两个结果文件

 

#将.pb文件转化为.tflite文件

# TensorFlow Lite Converter https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/toco

 

#生成整数型版本tflite模型

bazel run tensorflow/lite/toco:toco -- \

--input_file=$OUTPUT_DIR/tflite_graph.pb \

--output_file=$OUTPUT_DIR/detect.tflite \

--input_shapes=1,300,300,3 \

--input_arrays=normalized_input_image_tensor \

--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' \

--inference_type=QUANTIZED_UINT8 \

--mean_values=128 \

--std_values=128 \

--change_concat_input_ranges=false \

--allow_custom_ops \

--default_ranges_min=0 \

--default_ranges_max=255 \

 

#生成float 版本tflite模型

bazel run -c opt tensorflow/lite/toco:toco -- \

--input_file=$OUTPUT_DIR/tflite_graph.pb \

--output_file=$OUTPUT_DIR/float_detect.tflite \

--input_shapes=1,300,300,3 \

--input_arrays=normalized_input_image_tensor \

--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3'  \

--inference_type=FLOAT \

—allow_custom_ops

 

#查看生成结果,会发现有detect.tflite、 float_detect.tflite 这两个结果文件

 

Running our model on Android

 

项目工程文件:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/android

可以git clone https://github.com/tensorflow/tensorflow.git 然后到找到/tensorflow/lite/examples/android目录,直接import到 Android Studio 里面

 

将detect.tflite文件拷贝到 android/app/src/main/assets 目录下,并将coco_labels_list.txt复制一份,改名为labels_list.txt,结果如下图所示

 

对 DetectorActivity.java 进行修改,将物体检测模型替换成我们生成的模型(detect.tflite),并将模型对应的分类列表文件也进行替换(labels_list.txt, 因为我们下载的样例模型就是coco-trained模型,label_list.txt内容与 原来的coco_labels_list.txt一致即可)

 

 

 

重新编译运行,即可生成测试APK

 

 

查看build.gradle文科,可以确定该demo指定的目标运行系统的api level 为26

 

启动模拟器(如果没有模拟器,就创建一个api level为26的,cpu/abi 为x86架构的)

 

 

模拟器启动界面

 

  • 7
    点赞
  • 84
    收藏
    觉得还不错? 一键收藏
  • 26
    评论
TensorFlow Lite是一个适用于移动设备和嵌入式设备的轻量级机器学习框架。要进行手部检测,您可以使用TensorFlow Lite来构建和部署一个神经网络模型。 以下是一个基本的步骤指南: 1. 收集和准备数据集:收集大量手部图像,并将它们标记为手部和非手部。确保数据集具有多样性,以便模型能够准确地识别不同的手部姿势和背景。 2. 构建和训练模型:使用TensorFlow或其他深度学习框架来构建和训练一个手部检测模型。您可以选择使用现有的预训练模型,如YOLO(You Only Look Once)或SSD(Single Shot MultiBox Detector),并将其微调到您的数据集上。另外,您还可以使用一些开源的手部检测模型作为起点,然后根据需要进行修改和优化。 3. 将模型转换为TensorFlow Lite格式:一旦您训练好了模型,您需要将其转换为TensorFlow Lite格式,以便在移动设备上进行部署。TensorFlow Lite提供了一些工具和API来进行模型的转换和优化。 4. 部署模型到移动设备:将TensorFlow Lite模型嵌入到您的移动应用程序中,并使用相应的API来加载和运行模型。您可以使用TensorFlow Lite官方提供的Android和iOS示例代码作为起点,并根据您的需求进行定制和扩展。 需要注意的是,手部检测是一个复杂的任务,需要充分的数据和计算资源来训练和部署模型。在实际应用中,您可能还需要考虑实时性能、模型大小和能耗等方面的因素。
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值