上次使用Google ML Engine跑了一下TensorFlow Object Detection API中的Quick Start(http://www.cnblogs.com/take-fetter/p/8384564.html),但是遇到了很多錯誤,索性放棄了
這兩天直接開始從自己的數據集開始制作手掌識別器。先放運行結果吧
所有代碼文件可在https://github.com/takefetter/hand-detection查看,歡迎star和issue
使用前所需要的准備:1.clone tensorflow models(site:https://github.com/tensorflow/models)
2.在model/research目錄下運行setup.py安裝object detection API
3.其余必要條件:安裝tensorflow(版本需大於等於1.4),opencv-python等必須的package
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)並修改文件中的內容
需要修改的地方有:
- num_classes: 改為pbtxt中類的數目
- PATH_TO_BE_CONFIGURED的部分改為相應的目錄
- 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
需要注意的地方有
- windows下需要放在同一行運行 並刪除\
- 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
感謝:
- https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_pets.md
- https://github.com/victordibia/handtracking
- https://pythonprogramming.net/testing-custom-object-detector-tensorflow-object-detection-api-tutorial/?completed=/training-custom-objects-tensorflow-object-detection-api-tutorial/
- https://github.com/datitran/raccoon_dataset
- https://www.mutah.edu.jo/biometrix/hand-images-databases.html