NO IMAGE

本專案是在利用models/tensorflow中SSD_MobileNet網路模型進行的實驗。
github連結:https://github.com/tensorflow/models/tree/master/research/object_detection

主要分為資料預處理、訓練和測試部分(驗證部分直接用github上的教程)

1. 資料預處理
在github教程上有對pets資料和pascalVOC資料的轉.record檔案的相關程式碼,但是由於一般普通資料通常只能獲得.jpg(.jpeg)和.xml檔案資料,那麼不能直接使用github上現成的create_record程式碼,這裡採用官方程式碼並進行了修改。

r"""Convert raw PASCAL dataset to TFRecord for object_detection.
Example usage:
./create_pascal_tf_record --data_dir=/home/user/VOCdevkit \
--year=VOC2012 \
--output_path=/home/user/pascal.record
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import hashlib
import io
import logging
import os
from lxml import etree
import PIL.Image
import tensorflow as tf
from object_detection.utils import dataset_util
from object_detection.utils import label_map_util
def dict_to_tf_example(data,
dataset_directory,
label_map_dict,
ignore_difficult_instances=False,
image_subdirectory='image'):
"""Convert XML derived dict to tf.Example proto.
Notice that this function normalizes the bounding box coordinates provided
by the raw data.
Args:
data: dict holding PASCAL XML fields for a single image (obtained by
running dataset_util.recursive_parse_xml_to_dict)
dataset_directory: Path to root directory holding PASCAL dataset
label_map_dict: A map from string label names to integers ids.
ignore_difficult_instances: Whether to skip difficult instances in the
dataset  (default: False).
image_subdirectory: String specifying subdirectory within the
PASCAL dataset directory holding the actual image data.
Returns:
example: The converted tf.Example.
Raises:
ValueError: if the image pointed to by data['filename'] is not a valid JPEG
"""
img_path = os.path.join(image_subdirectory, data['filename'])
full_path = os.path.join(dataset_directory, img_path)
with tf.gfile.GFile(full_path, 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = PIL.Image.open(encoded_jpg_io)
if image.format != 'JPEG':
raise ValueError('Image format not JPEG')
key = hashlib.sha256(encoded_jpg).hexdigest()
width = int(data['size']['width'])
height = int(data['size']['height'])
xmin = []
ymin = []
xmax = []
ymax = []
classes = []
classes_text = []
truncated = []
poses = []
difficult_obj = []
for obj in data['object']:
difficult = bool(int(obj['difficult']))
xmin.append(float(obj['bndbox']['xmin']) / width)
ymin.append(float(obj['bndbox']['ymin']) / height)
xmax.append(float(obj['bndbox']['xmax']) / width)
ymax.append(float(obj['bndbox']['ymax']) / height)
classes_text.append(obj['name'].encode('utf8'))
classes.append(label_map_dict[obj['name']])
truncated.append(int(obj['truncated']))
poses.append(obj['pose'].encode('utf8'))
example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(
data['filename'].encode('utf8')),
'image/source_id': dataset_util.bytes_feature(
data['filename'].encode('utf8')),
'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmin),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmax),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymin),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymax),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
'image/object/difficult': dataset_util.int64_list_feature(difficult_obj),
'image/object/truncated': dataset_util.int64_list_feature(truncated),
'image/object/view': dataset_util.bytes_list_feature(poses),
}))
return example
def main(_):
data_dir = '插入data資料路徑'
output_path = '插入輸出record路徑和檔名'
writer = tf.python_io.TFRecordWriter(output_path)
label_map_path = '插入label—map路徑和檔名'
label_map_dict = label_map_util.get_label_map_dict(label_map_path)
examples_path = '插入trainval.txt'
annotations_dir = '插入xml檔案路徑/annotations/'
examples_list = dataset_util.read_examples_list(examples_path)
for idx, example in enumerate(examples_list):
if idx % 100 == 0:
logging.info('On image %d of %d', idx, len(examples_list))
path = os.path.join(annotations_dir, example   '.xml')
print(path)
with tf.gfile.GFile(path, 'r') as fid:
xml_str = fid.read()
xml = etree.fromstring(xml_str)
data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']
# print(data)
tf_example = dict_to_tf_example(data, data_dir, label_map_dict)
writer.write(tf_example.SerializeToString())
writer.close()
if __name__ == '__main__':
tf.app.run()

把裡面的路徑和資料夾改成自己的就可以生成record檔案了!
但是在此之前,需要準備trainval.txt檔案。就是把整體圖片的名稱讀出一個列表,具體程式碼如下:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
from os import listdir, getcwd
from os.path import join
if __name__ == '__main__':
source_folder='models/research/object_detection/data/image/'      #地址是所有圖片的儲存地點
dest='models/research/object_detection/data/annotations/trainval.txt' #儲存train.txt的地址
file_list=os.listdir(source_folder)       #賦值圖片所在資料夾的檔案列表
train_file=open(dest,'a')                 #開啟檔案
for file_obj in file_list:                #訪問檔案列表中的每一個檔案
file_path=os.path.join(source_folder,file_obj) 
file_name,file_extend=os.path.splitext(file_obj)
#file_name 儲存檔案的名字,file_extend儲存副檔名
#file_num=int(file_name) 
train_file.write(file_name '\n') 
train_file.close()#關閉檔案

執行上面這個可以生成所需要的trainval.txt檔案。

2. 訓練
訓練部分需要修改的原始檔有:配置檔案.config和預訓練模型。
config檔案需要改的部分主要有6個地方:

num_classes: 3
fine_tune_checkpoint: "models/research/object_detection/models/model/train/model.ckpt-200000"
訓練:
input_path: "/models/research/object_detection/data/train_gold.record"
label_map_path: "/models/research/object_detection/data/gold_label_map.pbtxt"
驗證:
input_path: "models/research/object_detection/data/train_gold.record"
label_map_path: "models/research/object_detection/data/gold_label_map.pbtxt"

然後預訓練模型的model需要在網上下載,這裡一定要下載相同網路模型的預訓練模型。
然後輸入教程中的四行指令執行即可開始訓練。(貌似由於是分散式原因,這個程式會佔用伺服器所有GPU,而且會把其他的東西擠掉==)
訓練過程中會儲存最近五次的中途訓練結果。
3. 測試
測試部分參考github中相關程式:
https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb