机器学习tensorflow object detection 实现人脸识别

object detection是Tensorflow很常用的api,功能强大,很有想象空间,人脸识别,花草识别,物品识别等。下面是我做实验的全过程,使用自己收集的胡歌图片,实现人脸识别,找出胡歌。

安装tensorflow

官方的教程已经写得非常好了,这里就不多说,但是有一点必须注意的是,必须安装python3.6版本,不能安装最新的python3.7版本,不然会出现很多不兼容的问题难以处理。尽可能选择一台显卡性能较好的电脑做机器学习,尽量选择gpu训练,不然训练过程非常的慢。
https://tensorflow.google.cn/install/pip

安装object detection api

我的另外一篇文章写得非常详细,这一步也是必须的,非常重要。
https://www.jianshu.com/p/23113a4a48be

收集图片

我这里保存了一份胡歌的照片,一共50张,而且已经标记号了,但是我建议我们开发者应该自己动手来标记一份,尽可能的多些图片,越多越好。
https://pan.baidu.com/s/13Ln1FinjxX9ANkopBM6kLQ

安装标记工具labelImg

labelImg必须运行在python3.6,不然无法运行起来,这里就不展开了。
https://github.com/tzutalin/labelImg

brew install qt
brew install libxml2
make qt5py3
python3 labelImg.py

标记图片

打开了标记工具,选择Open Dir把图片添加进来,然后点击Create RectBox勾选我们要选择的目标,输入对应的label,然后ctrl s保存。最终xml文件是和文件名称保存在同一目录。

image.png

把xml转换成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转换成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':     # 需改动
        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')         #  需改动
    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()

创建.pbtxt文件

object_detection/data创建一个train.pbtxt文件,和上面生成tfrecord的改动是对应的。

item {
  id: 1
  name: 'huge'
}

复制修改ssd_mobilenet_v1_coco.config

research/object_detection/samples/configs/ssd_mobilenet_v1_coco.config

在上面的目录可以找到ssd_mobilenet_v1_coco.config配置文件,复制出来放到object_detection/data目录,然后把文件里面带有PATH_TO_BE_CONFIGURED的地方都修改成我们对应的文件路径。

image.png

最终的文件目录结构

创建文件夹data/training,最后文件结构如下

data
├── images
│ ├── train
│ │ ├── 1.jpg
│ │ ├── 1.xml
│ │ ├── 2.jpg
│ │ ├── 2.xml
│ │ ├── ...
│ │ ├── train.csv
│ ├── training
├── train.record
├── train.pbtxt
└── ssd_mobilenet_v1_coco.config

开始训练

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

启动训练之后,research/object_detection/data/training文件夹就会陆陆续续创建了一些文件。

loss需要低于1.0才可以达到很好的效果,训练过程非常的漫长,这个和电脑的性能有很大关系,我训练了二十多小时才训练了30000多次step,效果才让loss降低到1.0以下,有条件就使用gpu进行训练,记得使用nohup命令后台训练。

监测训练tensorboard

tensorboard --logdir=object_detection/data/training

在浏览器输入地址查看:http://localhost:6006,从右边的图表可以看到训练的loss的值

image.png

监测训练效果

左边的预测的效果,右边是我们设定的正确效果,一定要有耐心,我也曾经一度怀疑是不是我代码写错了,跑了二十几个小时才看到预测正确。


image.png

生成.pb模型文件

下面是训练生成的目录结构

image.png

需要把下面命令的28189改成training文件夹训练的最后数字

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

等命令执行完毕后,就可以看到生成了我们要的frozen_inference_graph.pb文件。

测试模型

data/test_images增加三张胡歌的图片image1.jpgimage2.jpgimage3.jpg,执行下面代码
(在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)

那么就可以看到执行的结果了

image.png

总结

这次探索了几天的时候,一开始是没有安装object detection,想直接在源码上运行,但是这是行不通的,必须安装,这是我遇到的第一个坑。然后就是标记图片的时候部分图片标签错了,导致运行了十几个小时都毫无进展,标记图片必须好好检查一遍。由于我是用mac电脑,无法使用gpu训练,特别的慢,一旦训练起来,我的电脑就不能做其他事情,运行了十几个小时没有结果就一点,一度以为是我训练不对,就没有耐心停掉了。后来弄了一台Linux电脑,但是没有显卡,通过在后台训练了二十几个小时终于看到了成功,后续我会在Android和iOS运用我们这次的训练成功,敬请关注。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,423评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,339评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,241评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,503评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,824评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,262评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,615评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,337评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,989评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,300评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,829评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,193评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,753评论 3 230
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,970评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,708评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,295评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,207评论 2 258