Tensorflow訓練自己的Object Detection模型並進行目標檢測
阿新 • • 發佈:2019-01-05
0.準備工作
1.專案目錄概覽
圖1 object detection專案目錄
2.準備資料集和相關檔案
- 下載VOC2007資料集,解壓放到dataset目錄下,如圖1。
- 複製
models\research\object_detection\dataset_tools\create_pascal_tf_record.py
檔案到dataset目錄下,如圖1。 - 複製
models\research\object_detection\data\pascal_label_map.pbtxt
檔案到dataset目錄下,如圖1。 - 解壓預訓練模型
ssd_inception_v2_coco_11_06_2017.tar.gz
- 複製
models\research\object_detection\samples\configs\ssd_inception_v2_coco.config
到專案根目錄下。 - 複製
models\research\object_detection
目錄下的train.py、eval.py和export_inference_graph.py
檔案到專案根目錄下。 - 複製
models\research\object_detection
資料夾下的utils目錄到專案根目錄下,create_pascal_tf_record.py會用到。
3.製作TFRecord
- create_pascal_tf_record.py第160行 :
examples_path = os.path.join(data_dir, year, 'ImageSets', 'Main', 'aeroplane_' + FLAGS.set + '.txt')
為:
examples_path = os.path.join(data_dir, year, 'ImageSets', 'Main/' + FLAGS.set + '.txt')
- 執行如下指令:
python dataset/create_pascal_tf_record.py \
--data_dir=dataset/VOCtrainval_06 -Nov-2007/VOCdevkit \
--year=VOC2007 \
--set=train \
--output_path=record/pascal_train.record
python dataset/create_pascal_tf_record.py \
--data_dir=dataset/VOCtrainval_06-Nov-2007/VOCdevkit \
--year=VOC2007 \
--set=val \
--output_path=record/pascal_val.record
在record資料夾下生成pascal_train.record、pascal_val.record
檔案,如圖1。
4.修改配置檔案<
- 修改
ssd_inception_v2_coco.config
的關鍵語句:
...
model {
ssd {
num_classes: 20
...
train_config: {
batch_size: 24
optimizer {
rms_prop_optimizer: {
learning_rate: {
exponential_decay_learning_rate {
initial_learning_rate: 0.004
decay_steps: 10000
decay_factor: 0.95
}
...
num_steps: 20000
...
train_input_reader: {
tf_record_input_reader {
input_path: "record/pascal_train.record"
}
label_map_path: "dataset/pascal_label_map.pbtxt"
}
eval_config: {
num_examples: 4952
# Note: The below line limits the evaluation process to 10 evaluations.
# Remove the below line to evaluate indefinitely.
max_evals: 10
}
eval_input_reader: {
tf_record_input_reader {
input_path: "record/pascal_val.record"
}
label_map_path: "dataset/pascal_label_map.pbtxt"
shuffle: false
num_readers: 1
num_epochs: 1
}
5.修改train.py檔案
- 去除警告:
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
- 如果GPU記憶體不夠大,務必使用CPU clones
flags.DEFINE_boolean('clone_on_cpu', True,
'Force clones to be deployed on CPU. Note that even if '
'set to False (allowing ops to run on gpu), some ops may '
'still be run on the CPU if they have no GPU kernel.')
- 訓練模型輸出資料夾:
flags.DEFINE_string('train_dir', 'train',
'Directory to save the checkpoints and training summaries.')
- 設定pipeline_config_path:
flags.DEFINE_string('pipeline_config_path', 'ssd_inception_v2_coco.config',
'Path to a pipeline_pb2.TrainEvalPipelineConfig config '
'file. If provided, other configs are ignored')
- 訓練:
專案根目錄下執行:
python train.py --logtostderr
6.tensorboard檢視執行情況
專案根目錄下執行:
tensorboard --logdir=train
7.生成pb檔案
- 將train資料夾下的如下檔案複製到pb資料夾下,並去除ckpt後面的“-數字”,checkpoint檔案內相應也要改:
checkpoint
model.ckpt.data-00000-of-00001
model.ckpt.index
model.ckpt.meta
- 在專案根目錄下執行:
python export_inference_graph.py \
--pipeline_config_path ssd_inception_v2_coco.config \
--trained_checkpoint_prefix pb/model.ckpt \
--output_directory pb
- 在pb目錄下可以找到生成的pb檔案:
frozen_inference_graph.pb
8.攝像頭目標檢測
- 修改webcamdetect.py檔案:
PATH_TO_CKPT = 'pb/frozen_inference_graph.pb'
- 遮蔽:
# 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())
- 在專案根目錄下執行:
python webcamdetect.py