1. 數(shù)據(jù)集介紹
??The Oxford-IIIT Pet Dataset是一個寵物圖像數(shù)據(jù)集,包含37種寵物,其中有犬類25類,貓類12類,每種寵物200張左右寵物圖片,并同時包含寵物輪廓標(biāo)注信息。
??地址:http://www.robots.ox.ac.uk/~vgg/data/pets/
2. 準(zhǔn)備工作
??為了避免不必要的麻煩,先說一下我的系統(tǒng)情況:
??Python 3.6Tensorflow 1.10 windows10(64Bit)
??顯卡: GTX 750Ti
??注: Python和Tensorflow采用Anaconda安裝。
2.1 下載模型
?https://github.com/tensorflow/models
?models-master.zip
?解壓到E:\models-master
2.2 下載數(shù)據(jù)
?http://www.robots.ox.ac.uk/~vgg/data/pets/
?下載annotations.tar.gz并解壓到E:\PETSSelf\annotations
?下載images.tar.gz并解壓到E:\PETSSelf\images
?PETSSelf名稱是任取的
順帶介紹一下本次訓(xùn)練中采用trainval.txt文件,大體格式如下:
| Name | Class ID | SPECIES | BREED ID |
|---|---|---|---|
| Abyssinian_100 | 1 | 1 | 1 |
| Abyssinian_101 | 1 | 1 | 1 |
| ... | |||
| Siamese_157 | 33 | 1 | 11 |
| Siamese_158 | 33 | 1 | 11 |
| ... | |||
| yorkshire_terrier_189 | 37 | 2 | 25 |
| yorkshire_terrier_18 | 37 | 2 | 25 |
| yorkshire_terrier_190 | 37 | 2 | 25 |
- Class ID 是對應(yīng)于pet_label_map.pbtxt的ID值
- SPECIES是總分類:1:貓 2:狗
- BREED ID :在分類下面的子分類序號,對于總分類1貓其序號為1-25;對于總分類2狗,其序號為1-12。
2.3 建立.pth文件
??在 Anaconda\Lib\site-packages 這個文件夾目錄下,寫一個.pth文件,內(nèi)容為兩條路徑,即在C:\Users\Administrator\Anaconda3\Lib\site-packages底下建立tensorflow_model.pth文件,內(nèi)容為
E:\models-master\research
E:\models-master\research\slim
如果沒有這一步,在后面運行過程中則會出現(xiàn) ImportError: No module named 'object_detection' 這種錯誤
2.4 編譯protos**
將protoc-3.4.0-win32.zip的bin部分解壓到E:\protoc-3.4.0-win32
轉(zhuǎn)到找到model-master中的research目錄下,執(zhí)行:
E:\models-master\research>E:\protoc-3.4.0-win32\bin\protoc.exe object_detection/protos/*.proto --python_out=.
之后會在protos文件夾中生成一大堆.proto文檔:
如果是采用PATH方式調(diào)用protoc,易出現(xiàn)
object_detection/protos/*.proto: No such file or directory錯誤-
如果缺少這一步,在后面運行過程中則會出現(xiàn)
ImportError: cannot import name 'string_int_label_map_pb2'
****后來將*.py移至其它目錄,發(fā)現(xiàn)又沒有什么關(guān)系,還是需要進一步了解透徹。
2.5 運行model_builder_test.py
E:\models-master\research>python object_detection/builders/model_builder_test.py**
C:\Users\Administrator\Anaconda3\lib\site-packages\tensorflow\python\util\tf_inspect.py:75: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
return _inspect.getargspec(target)
.....
Ran 22 tests in 0.220s
OK
E:\models-master\research>
2.6 測試object_detection工作是否正常
E:\models-master>jupyter-notebook
會自動打開瀏覽器,訪問http://localhost:8888/
進行瀏覽器,按Shift+Enter一直到最后一個框
等一會兒,出現(xiàn)狗和沙灘的識別圖形。

3. 數(shù)據(jù)準(zhǔn)備
3.1 建立轉(zhuǎn)換程序
在E:\models-master\research\object_detection\dataset_tools目錄底下,有一系列轉(zhuǎn)換工具,包括create_pet_tf_record.py
- 將create_pet_tf_record.py復(fù)制到E:\PETSSelf
- 復(fù)制數(shù)據(jù)集的標(biāo)簽圖文件,目前用到的是
將object_detection/data/pet_label_map.pbtxt復(fù)制到E:\PETSSelf\data
為了簡單起見,我們只保留第1種
item {
id: 1
name: 'Abyssinian'
}
這是一種稱為"阿比西尼亞貓"的動物:

注意:這個轉(zhuǎn)換工具默認(rèn)只識別臉部信息,它從annotations\xmls目錄底下自動找到相匹配的<bnxbox>用于界定寵物的臉部范圍,大體如下圖:

3.2 執(zhí)行轉(zhuǎn)換
在E:\PETSSelf目錄下運行
E:\PETSSelf>Python create_pet_tf_record.py \
--data_dir=E:\PETSSelf \
--output_path=E:\PETSSelf
運行完成以后,會產(chǎn)生下列文件
- pet_faces_train.record-00000-of-00010
- pet_faces_train.record-00001-of-00010
- pet_faces_train.record-00002-of-00010
- pet_faces_train.record-00003-of-00010
- pet_faces_train.record-00004-of-00010
- pet_faces_train.record-00005-of-00010
- pet_faces_train.record-00006-of-00010
- pet_faces_train.record-00007-of-00010
- pet_faces_train.record-00008-of-00010
- pet_faces_train.record-00009-of-00010
- pet_faces_val.record-00000-of-00010
- pet_faces_val.record-00001-of-00010
- pet_faces_val.record-00002-of-00010
- pet_faces_val.record-00003-of-00010
- pet_faces_val.record-00004-of-00010
- pet_faces_val.record-00005-of-00010
- pet_faces_val.record-00006-of-00010
- pet_faces_val.record-00007-of-00010
- pet_faces_val.record-00008-of-00010
- pet_faces_val.record-00009-of-00010
3.3 引入訓(xùn)練模型
我們采用ssd_mobilenet模型進行訓(xùn)練
3.3.1 模型下載
其可以從:
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
指定的模型表格中進行下載,這個模型是用微軟COCO數(shù)據(jù)集訓(xùn)練的。
下載完成以后,名稱為:ssd_mobilenet_v1_coco_2018_01_28.tar.gz
當(dāng)然,時間不同,文件名可能有變化,時代總是在進步的。
3.3.2 復(fù)制模型文件
將臨時目錄里面的model.ckpt.*的三個文件, 即下面三個文件:
- model.ckpt.data-00000-of-00001
- model.ckpt.index
- model.ckpt.meta
復(fù)制到E:\PETSSelf
3.3.3 復(fù)制配置文件并修改
模型配置文件為ssd_mobilenet_v1_pets.config
其可以在E:\models-master\research\object_detection\samples\configs找到。將其復(fù)制到E:\PETSSelf,并作如下修改:
- num_classes
num_classes=1
我們在此只采用1類
- 將所有PATH_TO_BE_CONFIGURED的地方類比修改為自己之前設(shè)置的路徑
fine_tune_checkpoint: "E:/PETSSelf/model.ckpt"
train_input_reader: {
tf_record_input_reader {
input_path: "E:/PETSSelf/pet_faces_train.record*"
}
label_map_path: "E:/PETSSelf/data/pet_label_map.pbtxt"
}
eval_input_reader: {
tf_record_input_reader {
input_path: "E:/PETSSelf/pet_faces_val.record*"
}
- num_steps
原來num_steps: 200000,這個需要花費大量時間,為了測試方便,改成一個較小值,以驗證命令是否能通過:
num_steps: 1000
- batch_size
原來的batch_size: 24,目前GTX 750Ti顯示會內(nèi)存不足,先以最小計
batch_size: 12
4. 訓(xùn)練數(shù)據(jù)
4.1 引入訓(xùn)練程序train.py
將E:\models-master\research\object_detection\legacy\train.py
復(fù)制到E:\PETSSelf
注意:網(wǎng)上有很多文檔,認(rèn)為train.py腳本在object detection文件夾下面,估計是TensorFlow版本升級了,train.py現(xiàn)在位置在legacy文件下面.
4.2 運行train.py
E:\PETSSelf>Python train.py -train_dir=E:\PETSSelf -pipeline_config_path=E:\PETSSelf\ssd_mobilenet_v1_pets.config
5. 模型輸出
E:\PETSSelf\ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix E:\PETSSelf\model.ckpt-1000 --output_directory output_model
運行完成以后,形成以下文件(沒有全列出來)
- frozen_inference_graph.pb
- model.ckpt.data-00000-of-00001
- model.ckpt.index
- model.ckpt.meta
- pipeline.config
···
6. 模型測試
6.1 測試環(huán)境準(zhǔn)備
轉(zhuǎn)到E:\models-master目錄,然后執(zhí)行
E:\models-master>jupyter-notebook
轉(zhuǎn)入瀏覽器,在瀏覽器頁面中作一些配置修改操作:
修改in[4]
# What model to download.
#MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
MODEL_NAME = 'E:/PETSSelf/output_model'
#MODEL_FILE = MODEL_NAME + '.tar.gz'
#DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'pet_label_map.pbtxt')
將Download Model即in [5]全部屏蔽
#opener = urllib.request.URLopener()
#opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
#tar_file = tarfile.open(MODEL_FILE)
#for file in tar_file.getmembers():
# file_name = os.path.basename(file.name)
# if 'frozen_inference_graph.pb' in file_name:
# tar_file.extract(file, os.getcwd())
修改Detection即In [9]部分
PATH_TO_TEST_IMAGES_DIR = 'E:/PETSSelf/Test'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 6) ]
注意:上面代碼中range(1, 6) ,表示測試5張圖片
6.2 測試數(shù)據(jù)準(zhǔn)備
建立E:\PETSSelf\Test目錄,將從網(wǎng)上找?guī)讖垐D片

Image1: 從網(wǎng)上找的Abyssinian/阿比西尼亞貓圖片
Image2: 愛心的中華田園貓
Image3: american_bulldog/美國斗牛犬
Image4: 舒服的中華田園貓
Image5: 人類
6.3 測試
回到Jupyter最初始,點擊選中Object Detection Demo,然后一直按Shift+Enter,直到最后:

這個是對的,確實是阿比西尼亞貓

哈哈,認(rèn)錯了,它是中華田園貓,不過系統(tǒng)也認(rèn)為置信度不高。

這個是不對了,貓和狗怎么混淆了呢?

這個準(zhǔn)確(也可能根本沒有認(rèn)出來是只貓,呵呵)。

這個也準(zhǔn)確。