機(jī)器學(xué)習(xí)tensorflow object detection 實(shí)現(xiàn)人臉識(shí)別

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文件是和文件名稱保存在同一目錄。

image.png

把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)的文件路徑春畔。

image.png

最終的文件目錄結(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的值

image.png

監(jiān)測(cè)訓(xùn)練效果

左邊的預(yù)測(cè)的效果讯壶,右邊是我們?cè)O(shè)定的正確效果料仗,一定要有耐心,我也曾經(jīng)一度懷疑是不是我代碼寫錯(cuò)了伏蚊,跑了二十幾個(gè)小時(shí)才看到預(yù)測(cè)正確立轧。


image.png

生成.pb模型文件

下面是訓(xùn)練生成的目錄結(jié)構(gòu)

image.png

需要把下面命令的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.jpgimage3.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é)果了

image.png

總結(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)注段化。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市造成,隨后出現(xiàn)的幾起案子显熏,更是在濱河造成了極大的恐慌,老刑警劉巖晒屎,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件喘蟆,死亡現(xiàn)場(chǎng)離奇詭異缓升,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)蕴轨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門港谊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人橙弱,你說(shuō)我怎么就攤上這事歧寺。” “怎么了棘脐?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵斜筐,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我蛀缝,道長(zhǎng)顷链,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任屈梁,我火速辦了婚禮嗤练,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘在讶。我一直安慰自己煞抬,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布真朗。 她就那樣靜靜地躺著,像睡著了一般僧诚。 火紅的嫁衣襯著肌膚如雪遮婶。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天湖笨,我揣著相機(jī)與錄音旗扑,去河邊找鬼。 笑死慈省,一個(gè)胖子當(dāng)著我的面吹牛臀防,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播边败,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼袱衷,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了笑窜?” 一聲冷哼從身側(cè)響起致燥,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎排截,沒(méi)想到半個(gè)月后嫌蚤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體辐益,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年脱吱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了智政。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡箱蝠,死狀恐怖续捂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情抡锈,我是刑警寧澤疾忍,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站床三,受9級(jí)特大地震影響一罩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜撇簿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一聂渊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧四瘫,春花似錦汉嗽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至洗做,卻和暖如春弓叛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背诚纸。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工撰筷, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人畦徘。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓毕籽,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親井辆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子关筒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354