object detection
是Tensorflow很常用的api,功能強(qiáng)大呻拌,很有想象空間葱轩,人臉識(shí)別,花草識(shí)別藐握,物品識(shí)別等靴拱。下面是我做實(shí)驗(yàn)的全過(guò)程,使用自己收集的胡歌圖片猾普,實(shí)現(xiàn)人臉識(shí)別袜炕,找出胡歌。
安裝tensorflow
官方的教程已經(jīng)寫得非常好了初家,這里就不多說(shuō)偎窘,但是有一點(diǎn)必須注意的是乌助,必須安裝python3.6版本,不能安裝最新的python3.7版本陌知,不然會(huì)出現(xiàn)很多不兼容的問(wèn)題難以處理他托。盡可能選擇一臺(tái)顯卡性能較好的電腦做機(jī)器學(xué)習(xí),盡量選擇gpu訓(xùn)練仆葡,不然訓(xùn)練過(guò)程非常的慢赏参。
https://tensorflow.google.cn/install/pip
安裝object detection api
我的另外一篇文章寫得非常詳細(xì),這一步也是必須的沿盅,非常重要把篓。
http://www.reibang.com/p/23113a4a48be
收集圖片
我這里保存了一份胡歌的照片,一共50張腰涧,而且已經(jīng)標(biāo)記號(hào)了纸俭,但是我建議我們開發(fā)者應(yīng)該自己動(dòng)手來(lái)標(biāo)記一份,盡可能的多些圖片南窗,越多越好。
https://pan.baidu.com/s/13Ln1FinjxX9ANkopBM6kLQ
安裝標(biāo)記工具labelImg
labelImg必須運(yùn)行在python3.6郎楼,不然無(wú)法運(yùn)行起來(lái)万伤,這里就不展開了。
https://github.com/tzutalin/labelImg
brew install qt
brew install libxml2
make qt5py3
python3 labelImg.py
標(biāo)記圖片
打開了標(biāo)記工具呜袁,選擇Open Dir
把圖片添加進(jìn)來(lái)敌买,然后點(diǎn)擊Create RectBox
勾選我們要選擇的目標(biāo),輸入對(duì)應(yīng)的label阶界,然后ctrl s
保存虹钮。最終xml
文件是和文件名稱保存在同一目錄。
把xml轉(zhuǎn)換成csv格式
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# xml2csv.py
import glob
import pandas as pd
import xml.etree.ElementTree as ET
path = 'data/images/train'
def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
root = tree.getroot()
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)
xml_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
def main():
image_path = path
xml_df = xml_to_csv(image_path)
xml_df.to_csv(path + '/train.csv', index=None)
print('Successfully converted xml to csv.')
main()
把圖片和csv轉(zhuǎn)換成tfrecord格式
記得要修改部分地方
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# generate_tfrecord.py
# -*- coding: utf-8 -*-
"""
Usage:
# From tensorflow/models/
# Create train data:
python generate_tfrecord.py --csv_input=data/tv_vehicle_labels.csv --output_path=train.record
# Create test data:
python generate_tfrecord.py --csv_input=data/test_labels.csv --output_path=test.record
"""
import os
import io
import pandas as pd
import tensorflow as tf
import cv2
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict
os.chdir('data')
flags = tf.app.flags
flags.DEFINE_string('csv_input', 'images/train/train.csv', 'Path to the CSV input')
flags.DEFINE_string('output_path', 'images/train.record', 'Path to output TFRecord')
FLAGS = flags.FLAGS
# TO-DO replace this with label map
def class_text_to_int(row_label):
if row_label == 'huge': # 需改動(dòng)
return 1
else:
None
def split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def create_tf_example(group, path):
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = Image.open(encoded_jpg_io)
width, height = image.size
filename = group.filename.encode('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for index, row in group.object.iterrows():
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class']))
tf_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(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))
return tf_example
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
path = os.path.join(os.getcwd(), 'images/train') # 需改動(dòng)
examples = pd.read_csv(FLAGS.csv_input)
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, path)
writer.write(tf_example.SerializeToString())
writer.close()
output_path = os.path.join(os.getcwd(), FLAGS.output_path)
print('Successfully created the TFRecords: {}'.format(output_path))
if __name__ == '__main__':
tf.app.run()
創(chuàng)建.pbtxt文件
在object_detection/data
創(chuàng)建一個(gè)train.pbtxt
文件膘融,和上面生成tfrecord的改動(dòng)是對(duì)應(yīng)的芙粱。
item {
id: 1
name: 'huge'
}
復(fù)制修改ssd_mobilenet_v1_coco.config
research/object_detection/samples/configs/ssd_mobilenet_v1_coco.config
在上面的目錄可以找到ssd_mobilenet_v1_coco.config
配置文件,復(fù)制出來(lái)放到object_detection/data
目錄氧映,然后把文件里面帶有PATH_TO_BE_CONFIGURED
的地方都修改成我們對(duì)應(yīng)的文件路徑春畔。
最終的文件目錄結(jié)構(gòu)
創(chuàng)建文件夾data/training
,最后文件結(jié)構(gòu)如下
data
├── images
│ ├── train
│ │ ├── 1.jpg
│ │ ├── 1.xml
│ │ ├── 2.jpg
│ │ ├── 2.xml
│ │ ├── ...
│ │ ├── train.csv
│ ├── training
├── train.record
├── train.pbtxt
└── ssd_mobilenet_v1_coco.config
開始訓(xùn)練
cd research/object_detection
python3 model_main.py \
--pipeline_config_path=data/ssd_mobilenet_v1_coco.config \
--model_dir=data/training \
--num_train_steps=60000 \
--num_eval_steps=20 \
--alsologtostderr
啟動(dòng)訓(xùn)練之后岛都,research/object_detection/data/training
文件夾就會(huì)陸陸續(xù)續(xù)創(chuàng)建了一些文件律姨。
loss
需要低于1.0才可以達(dá)到很好的效果,訓(xùn)練過(guò)程非常的漫長(zhǎng)臼疫,這個(gè)和電腦的性能有很大關(guān)系择份,我訓(xùn)練了二十多小時(shí)才訓(xùn)練了30000多次step
,效果才讓loss
降低到1.0以下烫堤,有條件就使用gpu進(jìn)行訓(xùn)練荣赶,記得使用nohup
命令后臺(tái)訓(xùn)練凤价。
監(jiān)測(cè)訓(xùn)練tensorboard
tensorboard --logdir=object_detection/data/training
在瀏覽器輸入地址查看:http://localhost:6006,從右邊的圖表可以看到訓(xùn)練的loss的值
監(jiān)測(cè)訓(xùn)練效果
左邊的預(yù)測(cè)的效果讯壶,右邊是我們?cè)O(shè)定的正確效果料仗,一定要有耐心,我也曾經(jīng)一度懷疑是不是我代碼寫錯(cuò)了伏蚊,跑了二十幾個(gè)小時(shí)才看到預(yù)測(cè)正確立轧。
生成.pb模型文件
下面是訓(xùn)練生成的目錄結(jié)構(gòu)
需要把下面命令的
28189
改成training文件夾訓(xùn)練的最后數(shù)字
cd research/object_detection
python3 export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path=data/ssd_mobilenet_v1_coco.config \
--trained_checkpoint_prefix=data/training/model.ckpt-28189 \
--output_directory=data/training
等命令執(zhí)行完畢后,就可以看到生成了我們要的frozen_inference_graph.pb
文件躏吊。
測(cè)試模型
在data/test_images
增加三張胡歌的圖片image1.jpg
氛改、image2.jpg
、image3.jpg
比伏,執(zhí)行下面代碼
(在research/object_detection/object_detection_tutorial.ipynb
可以看到這些代碼)
# object_detection_tutorial.py
import os
from distutils.version import StrictVersion
import numpy as np
import tensorflow as tf
from PIL import Image
import cv2
from object_detection.utils import ops as utils_ops
if StrictVersion(tf.__version__) < StrictVersion('1.9.0'):
raise ImportError('Please upgrade your TensorFlow installation to v1.9.* or later!')
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
MODEL_NAME = 'data/'
PATH_TO_FROZEN_GRAPH = MODEL_NAME + 'training/frozen_inference_graph.pb'
PATH_TO_LABELS = MODEL_NAME + "train.pbtxt"
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
PATH_TO_TEST_IMAGES_DIR = MODEL_NAME + '/test_images'
TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 4)]
IMAGE_SIZE = (12, 8)
def run_inference_for_single_image(image, graph):
with graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.uint8)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
return output_dict
for image_path in TEST_IMAGE_PATHS:
print(image_path)
image = Image.open(image_path)
image_np = load_image_into_numpy_array(image)
image_np_expanded = np.expand_dims(image_np, axis=0)
output_dict = run_inference_for_single_image(image_np, detection_graph)
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8)
image = Image.fromarray(image_np.astype('uint8')).convert('RGB')
image.show()
cv2.waitKey(0)
那么就可以看到執(zhí)行的結(jié)果了
總結(jié)
這次探索了幾天的時(shí)候胜卤,一開始是沒(méi)有安裝object detection,想直接在源碼上運(yùn)行赁项,但是這是行不通的葛躏,必須安裝,這是我遇到的第一個(gè)坑悠菜。然后就是標(biāo)記圖片的時(shí)候部分圖片標(biāo)簽錯(cuò)了舰攒,導(dǎo)致運(yùn)行了十幾個(gè)小時(shí)都毫無(wú)進(jìn)展,標(biāo)記圖片必須好好檢查一遍悔醋。由于我是用mac電腦摩窃,無(wú)法使用gpu訓(xùn)練,特別的慢芬骄,一旦訓(xùn)練起來(lái)猾愿,我的電腦就不能做其他事情,運(yùn)行了十幾個(gè)小時(shí)沒(méi)有結(jié)果就一點(diǎn)账阻,一度以為是我訓(xùn)練不對(duì)蒂秘,就沒(méi)有耐心停掉了。后來(lái)弄了一臺(tái)Linux電腦淘太,但是沒(méi)有顯卡材彪,通過(guò)在后臺(tái)訓(xùn)練了二十幾個(gè)小時(shí)終于看到了成功,后續(xù)我會(huì)在Android和iOS運(yùn)用我們這次的訓(xùn)練成功琴儿,敬請(qǐng)關(guān)注段化。