使用TensorFlow Object Detection API+Google ML Engine训练本身的手掌识别器

  上次使用Google ML Engine跑了一下TensorFlow Object Detection API中的Quick Start(http://www.cnblogs.com/take-fetter/p/8384564.html),可是遇到了不少错误,索性放弃了html

这两天直接开始从本身的数据集开始制做手掌识别器。先放运行结果吧python

   

 全部代码文件可在https://github.com/takefetter/hand-detection查看,欢迎star和issuegit

 

使用前所须要的准备:1.clone tensorflow models(site:https://github.com/tensorflow/models)github

          2.在model/research目录下运行setup.py安装object detection APIwindows

          3.其他必要条件:安装tensorflow(版本需大于等于1.4),opencv-python等必须的packageapi

          4.安装Google Cloud SDK,激活免费试用300美金(须要一张信用卡来验证)和在命令行中使用gcloud init设置等机器学习

  •  准备数据集

  (关于手的图片的dataset仍旧使用的dlib训练(site:http://www.cnblogs.com/take-fetter/p/8321158.html)中的Hand Images Databases - https://www.mutah.edu.jo/biometrix/hand-images-databases.html提供的数据集,只不过此次使用了WEHI系列的图片(MOHI的图片我也试过,导入后会致使standard-gpu版的训练没法进行(内存不足)),做为示例目前我只使用了1-50人的共计250张图片)工具

   tensorflow训练的数据集需为TFRecord格式,咱们须要对训练数据进行标注,可是我并无找到直接能够标注生成的工具,还好有工具能够生成Pascal VOC格式的xml文件      https://github.com/tzutalin/labelImg,推荐将图片文件放于research/images中,保存xml文件夹位于research/images/xmls中学习

根据你要训练的数据集,建立.pbtxt文件测试

  • 转换为tfrecord格式

   完成图片标注后在xmls文件夹中运行xml_to_csv.py便可生成csv文件,再经过create_hand_tfrecord.py便可将图片转换为hand.record文件

   须要注意的是,若是你须要训练的数据集和我这里的不同的话,create_hand_tfrecord.py的todo部分须要与你的.pbtxt文件内的内容一致

   (方法参考至https://github.com/datitran/raccoon_dataset 使用本做者的文件还能够完成划分测试集和分析数据等功能,固然我这里并无使用)

  •  下载预训练模型

   从新开始一个模型的训练时间是很长的时间,而tensorflow model zoo为咱们提供好了预训练的模型(site:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md#coco-trained-models-coco-models),选择并下载一个 我选择的是

速度最快的ssd_mobilenet_v1,下载后解压可找到3个含有ckpt的文件,如图

  以后还需下载并配置model对应的config文件(https://github.com/tensorflow/models/tree/master/research/object_detection/samples/configs)并修改文件中的内容

须要修改的地方有:

  1. num_classes: 改成pbtxt中类的数目
  2. PATH_TO_BE_CONFIGURED的部分改成相应的目录
  3. num_steps定义了学习的上限 默认是200000 可本身更改,训练过程当中也能够随时中止
  • 上传文件并在Google Cloud Platform中训练

  1.上传3个ckpt文件以及config文件和.record文件 

      到google cloud控制台-存储目录下,建立存储分区(这里使用takefetter_hand_detector),并新建data文件夹,拖拽上传到该目录中完成后的目录和文件以下

+ takefetter_hand_detector/
  + data/
    - ssd_mobilenet_v1_hand.config - model.ckpt.index - model.ckpt.meta - model.ckpt.data-00000-of-00001
    - hand_label_map.pbtxt - hand.record

 

  2. 打包tf slim和object detection

     在research目录下运行

python setup.py sdist (cd slim && python setup.py sdist)

  3.建立机器学习任务

    在research目录下运行此命令 开始训练

gcloud ml-engine jobs submit training `whoami`_object_detection_`date +%s` \ --runtime-version 1.4 \ --job-dir=gs://takefetter_hand_detector/train \ --packages dist/object_detection-0.1.tar.gz,slim/dist/slim-0.1.tar.gz \ --module-name object_detection.train \ --region us-central1 \ --config object_detection/samples/cloud/cloud.yml \ -- \ --train_dir=gs://takefetter_hand_detector/train \ --pipeline_config_path=gs://takefetter_hand_detector/data/ssd_mobilenet_v1_hand.config

须要注意的地方有 

  1. windows下须要放在同一行运行 并删除\
  2. cloud.yml文件中的内容能够自行更改,我这里的设置为
    trainingInput: runtimeVersion: "1.4" scaleTier: CUSTOM masterType: standard_gpu workerCount: 2 workerType: standard_gpu parameterServerCount: 2 parameterServerType: standard

在提交任务后在 机器学习引擎-做业中便可看到具体状况,每运行几千次后在 takefetter_hand_detector/train中存储对应cheakpoint的文件 如图

以后下载须要的cheak的3个文件 复制到research目录下(这里用30045的3个文件做为示例),并将research/object_detectIon目录下的export_inference_graph.py复制到research目录下 运行例如

python object_detection/export_inference_graph.py \ --input_type image_tensor \ --pipeline_config_path object_detection/samples/configs/ssd_mobilenet_v1_hand.config \ --trained_checkpoint_prefix model.ckpt-30045 \ --output_directory exported_graphs

在运行完成后research目录中会生成文件夹exported_graphs_30045 包含的文件如图所示

拷贝frozen_inference_graph.pb和pbtxt文件到test/hand_inference_graph文件夹,并运行hand_detector.py 便可获得如文章开头的结果

后记:

1.若是须要视频实时的hand tracking,可以使用https://github.com/victordibia/handtracking 在个人渣本上FPS过低了......

2.我目前使用的数据集仍是较小训练次数也比较少,很容易出现一些误识别的状况,以后还会加大数据集和训练次数

3.换用其余model应该也会显著改善识别精确度

4.遇到任何问题,欢迎提问(虽然感受大多数stack overflow都有)

5.本地训练要好不少,若是使用在Google Cloud训练中可能会遇到问题,可是解决方法是将tensorflow版本改成1.2,可是1.2版本的object detection在准备阶段就会遇到问题,目前来看确实无解。(毕竟API Caller)

6.本地训练建议使用tensorflow版本为1.2

感谢:

  1. https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_pets.md
  2. https://github.com/victordibia/handtracking
  3. https://pythonprogramming.net/testing-custom-object-detector-tensorflow-object-detection-api-tutorial/?completed=/training-custom-objects-tensorflow-object-detection-api-tutorial/
  4. https://github.com/datitran/raccoon_dataset
  5. https://www.mutah.edu.jo/biometrix/hand-images-databases.html
相关文章
相关标签/搜索