宝可梦 图片识别python_使用Tensorflow从0开始搭建精灵宝可梦的检测APP
使用Tensorflow從0開始搭建精靈寶可夢(mèng)的檢測(cè)APP
本文為本人原創(chuàng),轉(zhuǎn)載請(qǐng)注明來源鏈接
環(huán)境要求
Tensorflow1.12.0
cuda 9.0
python3.6.10
Android Studio
Anaconda
安裝Tensorflow
使用conda 安裝GPU版Tensorflow
conda install tensorflow-gpu=1.12.0
找到tensorflow的安裝位置
我的位置在: home/jiading/.conda/envs/tensorflow12/lib/python3.6/site-packages/tensorflow
通過conda安裝的tensorflow是不包括models這一模塊的,需要從Github上下載: https://github.com/tensorflow/models
將它克隆到tensorflow文件夾下:
打開models\research\object_detection,按照https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md提示的進(jìn)行安裝
運(yùn)行 python object_detection/builders/model_builder_test.py 測(cè)試是否安裝成功
下載和處理數(shù)據(jù)集
我們采用的數(shù)據(jù)集是https://www.kaggle.com/lantian773030/pokemonclassification。如果你使用colab訓(xùn)練,可以直接將數(shù)據(jù)集下載到colab中: https://blog.csdn.net/qq_35654046/article/details/87621396
原始的數(shù)據(jù)集只有圖像和類別,可以用于分類,但是用于目標(biāo)檢測(cè)的話需要在此基礎(chǔ)上進(jìn)一步標(biāo)定數(shù)據(jù),在圖像中框出神奇寶貝的位置。
這里我們使用labelme這個(gè)軟件進(jìn)行標(biāo)定。labelme可以直接通過pip安裝: pip install labelme -i https://pypi.tuna.tsinghua.edu.cn/simp le
在終端(Bash和Windows的Powershell都可以)中直接輸出Labelme即可打開軟件.labelme的簡(jiǎn)單教程可以看這里: https://www.cnblogs.com/wangxiaocvpr/p/9997690.html
標(biāo)定數(shù)據(jù)后,我們?cè)诟鱾€(gè)神奇寶貝的文件夾中得到了和原圖像同名的 Json 文件:
打開json文件,我們可以看到有很長(zhǎng)的imageData:
這其實(shí)就是對(duì)原圖像的儲(chǔ)存,所以我們之后處理時(shí)只需要這個(gè)json文件即可,由此可以還原出原圖像
如果要達(dá)到比較好的效果,要標(biāo)定的數(shù)據(jù)還是不少的。
將labelme轉(zhuǎn)換為voc格式
我們最終要把數(shù)據(jù)集轉(zhuǎn)換為tfrecord,但是在此之前我們需要將其轉(zhuǎn)換為規(guī)范的voc格式,以便于再轉(zhuǎn)為tfrecord
最后得到VOC格式的數(shù)據(jù)如下:
注意原項(xiàng)目的代碼中有一兩個(gè)小bug,這其實(shí)無傷大雅,改了就好了,但是原項(xiàng)目沒有生成val數(shù)據(jù)集的功能,只能生成training和test.所以我改了一點(diǎn):
原來的split_dataset只有 test_ratio :測(cè)試集比例,我加上了'val_ratio'
注意,其實(shí)理論上可以直接用這個(gè) 工具 生成coco形式的數(shù)據(jù),然后使用tensorflow中tensorflow/models/research/object_detection/dataset_tools/create_coco_tf_record.py來生成tfrecord,但在我實(shí)際使用中發(fā)現(xiàn)create_coco_tf_record.py制作出來的是分散的數(shù)據(jù),如下:
當(dāng)然人家在代碼中也說了: Please note that this tool creates sharded output files. ,是我自己沒仔細(xì)看。這個(gè)格式應(yīng)該也是能用的,但是我目前不知道方法,所以最后就沒有用這個(gè)方法
將voc格式數(shù)據(jù)轉(zhuǎn)換為tfrecord
最終我采用的是這篇博客中的代碼,生成的tfrecord如下:
開始訓(xùn)練
這里我訓(xùn)練使用的是Tensorflow lite教程中推薦的COCO SSD MobileNet v1:
當(dāng)然也可以不選擇預(yù)訓(xùn)練模型,而是從頭訓(xùn)練。這樣的話就不需要下載上面的文件,你只需要一個(gè)config文件即可。該網(wǎng)絡(luò)的config文件在object_detection/samples/config/ssd_mobilenet_v1_coco.config。如何配置依然可以看這篇博文: https://www.cnblogs.com/gezhuangzhuang/p/10613468.html
訓(xùn)練完成后,我們就可以在train_dir中看到得到的模型:
導(dǎo)出圖
我們可以使用object_detection下的export_inference_graph.py導(dǎo)出圖,但是對(duì)于ssd模型, 官方推薦 使用export_tflite_ssd_graph.py(親測(cè)用上面的那個(gè)腳本導(dǎo)出的模型無法轉(zhuǎn)換為tflite格式):
python export_tflite_ssd_graph.py --input_type image_tensor --pipeline_config_path /home/jiading/Pokemon/ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync.config --trained.checkpoint_prefix /home/jiading/Pokemon/train/model.ckpt-2955 --output_directory /home/jiading/Pokemon/frozen_inference_graph.pb -add_postprocessing_op True --max_detection 10
測(cè)試
我們可以使用tensorflow的object_detection自帶的jupyter notebook腳本來做測(cè)試:
將PATH_TO_FROZEN_GRAPH改為pb文件的位置
需要一個(gè)labelmap文件,內(nèi)容如下:
用一個(gè)腳本很容易寫出來,這個(gè)就不提了
加載一張圖片
運(yùn)行結(jié)果
轉(zhuǎn)換為tensorflow lite模型
~/.conda/envs/tensorflow12/lib/python3.6/site-packages/tensorflow/models/research/object_detection$ tflite_convert --output_file=/home/jiading/Pokemon/tflite/detect.tflite --graph_def_file=/home/jiading/Pokemon/frozen_inference_graph/tflite_graph.pb --input_arrays='normalized_input_image_tensor' --output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' --input_shape=1,300,300,3 --allow_custom_ops
部署在安卓端
安卓的例子在 ObjectDetection-Android\examples-master\lite\examples\object_detection\android 下,打開后我們首先需要制作一個(gè)labelmap:
原本的例子會(huì)利用gradle下載模型,我們可以將地址替換掉
,將我們自己的這兩個(gè)文件放進(jìn)去:
部署時(shí)可能遇到的bug
import numpy as np
import tensorflow as tf
# Load TFLite model and allocate tensors.
interpreter = tf.contrib.lite.Interpreter(model_path="")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print(input_details)
print(output_details)
像我之前在導(dǎo)出圖時(shí)設(shè)置的 --max_detection 5 ,但是看輸出發(fā)現(xiàn):
上面是我的,下面是原本模型的,改為10后再導(dǎo)出就沒有問題了
如果還有問題,可以考慮將DetectorActivity中的 private static final boolean TF_OD_API_IS_QUANTIZED 設(shè)置為false。同時(shí),如果出現(xiàn)維度錯(cuò)誤,可以考慮修改TFLiteObjectDetectionAPIModel.java下的 private static final int NUM_DETECTIONS
最終效果:
一點(diǎn)點(diǎn)換皮
將原項(xiàng)目中的圖標(biāo)和軟件名換掉之后:
總結(jié)
以上是生活随笔為你收集整理的宝可梦 图片识别python_使用Tensorflow从0开始搭建精灵宝可梦的检测APP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript 日期日历控件
- 下一篇: 阿里云旺即时通信OpenIM(一)基础学