一、配置深度学习开发环境(Windows10 GPU)

参考链接:https://zhuanlan.zhihu.com/p/40094498

我的配置环境如下:

1、操作系统:Windows 10 64位

2、GPU:NVIDIA GeForce GTX 1050

3、GPU驱动: 385.54

4、visual studio community 2015

5、CUDA 9.0

6、cuDNN7.1.4

7、python 3.5.2

8、tensorflow-gpu 1.8

不用gpu和话也可以运行,但是训练速度太慢,个人实践的对比结果是:其他条件完全相同,只是CPU与GPU的区别,在CPU上训练要十天左右,而在GPU上只需要三个小时左右。

二、安装Tensorflow object detection API

TensorFlow object detection API要求使用其GitHub库中提供的特定目录结构。

具体步骤参见下面的网页:

https://zhuanlan.zhihu.com/p/40450102

三、标记数据集

图片数据的整理(切割,重命名)

品颜完月:图片批量重命名和按像素分割​zhuanlan.zhihu.comTensorFlow object detection API训练自己的数据集Windows10_人工智能

图片标记工具下载地址:

LabelImg​tzutalin.github.io

 

下载解压后得到:

TensorFlow object detection API训练自己的数据集Windows10_人工智能_02

这是一个绿色软件,点击labeling.exe 即可使用。

选择要标记的图片进行标注:

TensorFlow object detection API训练自己的数据集Windows10_人工智能_03

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_04

TensorFlow object detection API训练自己的数据集Windows10_人工智能_05

labelImg常用功能的快捷键

标注成功后生成对应的.xml文件:

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_06

三、生成可训练数据

这一步的主要工作是:【标注生产的.xml】 -->【.csv 】-->【 .record】(可用于训练的数据)

1、.xml -->.csv:

运行一下代码

import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET


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 train():
    image_path = os.path.join(os.getcwd(), 'train')
    xml_df = xml_to_csv(image_path)
    xml_df.to_csv('train_labels.csv', index=None)
    print('Successfully converted xml to csv (train).')

def test():
    image_path = os.path.join(os.getcwd(), 'test')
    xml_df = xml_to_csv(image_path)
    xml_df.to_csv('test_labels.csv', index=None)
    print('Successfully converted xml to csv (test).')    
    
train()
test()

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_07

程序运行后在jupyter notebook 中显示的输出结果

代码与‘train’‘test’文件以及生成的.csv文件的位置关系如下图所示:

TensorFlow object detection API训练自己的数据集Windows10_人工智能_08

把‘train’‘test’和‘train_labels’‘test_labels'文件放在文件夹images中

 

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_09

最后把文件夹images放在了这个目录下:D:\tensorflow1\models\research\object_detection

2、.csv --> .record:

下载generate_tfrecord.py 文件

下载地址:

datitran/raccoon_dataset​github.comTensorFlow object detection API训练自己的数据集Windows10_人工智能_10

下载后,我把generate_tfrecord.py 文件放在了这个目录下:D:\tensorflow1\models\research\object_detection。

把代码修改到适合自己的目标分类:

修改前:

TensorFlow object detection API训练自己的数据集Windows10_人工智能_11

修改后:

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_12

把文件夹images和generate_tfrecord.py 文件放在这个目录下:D:\tensorflow1\models\research\object_detection

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_13

在控制台运行如下代码:

 # Create train data:
  python generate_tfrecord.py --csv_input=images/train_labels.csv --image_dir=images/train --output_path=train.record

  # Create test data:
  python generate_tfrecord.py --csv_input=images/test_labels.csv  --image_dir=images/test --output_path=test.record
"""

TensorFlow object detection API训练自己的数据集Windows10_人工智能_14

在这个目录下:D:\tensorflow1\models\research\object_detection会产生test.record和train.record文件。

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_15

四、建立 Label Map 和 训练配置

1、建立 Label Map

item {
  id: 1
  name: 'meter'
}
item {
  id: 2
  name: 'red light'
}
item {
  id: 3
  name: 'blue light'
}
item {
  id: 4
  name: 'disconnector_open'
}
item {
  id: 5
  name: 'disconnector_close'
}

建立一个新的文件,文件名为 labelmap.pbtxt ,文件的id要与generate_tfrecord.py保持一致性

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_16

新建的taining文件夹,把新建的labelmap.pbtxt保存到taining文件夹下。

2、下载模型

下载地址:

tensorflow/models​github.comTensorFlow object detection API训练自己的数据集Windows10_人工智能_17

从上面的网站中下载 Faster-RCNN-Inception-V2 model

把文件faster_rcnn_inception_v2_coco_2018_01_28放到D:\tensorflow1\models\research\object_detection 的路径下

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_18

3、配置training pipeline

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_19

把faster_rcnn_inception_v2_pets.config复制到新建的taining文件夹下

TensorFlow object detection API训练自己的数据集Windows10_人工智能_20

配置文件:faster_rcnn_inception_v2_pets.config

1、要检测的目标个数

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_21

2、fine_tune_checkpoint:

"D:/tensorflow1/models/research/object_detection/faster_rcnn_inception_v2_coco_2018_01_28/model.ckpt"

TensorFlow object detection API训练自己的数据集Windows10_人工智能_22

3、train_input_reader:

input_path: "D:/tensorflow1/models/research/object_detection/train.record"
label_map_path: "D:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"

TensorFlow object detection API训练自己的数据集Windows10_人工智能_23

4、num_examples

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_24

5、eval_input_reader:

input_path: "D:/tensorflow1/models/research/object_detection/test.record"
label_map_path: "D:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"

TensorFlow object detection API训练自己的数据集Windows10_人工智能_25

最后把包含上述两个文件的taining文件夹放到这个目录下:D:\tensorflow1\models\research\object_detection

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_26

五、训练

在anaconda prompt下配置环境变量:

set PYTHONPATH=D:\tensorflow1\models;D:\tensorflow1\models\research;D:\tensorflow1\models\research\slim

TensorFlow object detection API训练自己的数据集Windows10_人工智能_27

如果关闭了环境要重新配置。

在anaconda prompt下改变路径到

D:\tensorflow1\models\research\object_detection  

然后运行下面的语句

python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_28

开始训练:

TensorFlow object detection API训练自己的数据集Windows10_人工智能_29

六、用tensorboard观察模型

在anaconda prompt中改变路径到D:\tensorflow1\models\research\object_detection

然后输入下面的语句(其中traning是保存训练过程中的数据的地方)

tensorboard --logdir=training

TensorFlow object detection API训练自己的数据集Windows10_人工智能_30

把网址复制到浏览器中,就可以看到如下图所示的画面;

TensorFlow object detection API训练自己的数据集Windows10_人工智能_31

七、保存训练成功的模型

在anaconda prompt中改变路径到D:\tensorflow1\models\research\object_detection

然后输入下面的语句

python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-XXXX --output_directory inference_graph

其中model.ckpt-XXXX中的XXXX应该被training文件夹中的最大数值的.ckpt的文件代替

如下图所示,应该用29639代替XXXX

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_32

python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-29639 --output_directory inference_graph

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_33

在路径D:\tensorflow1\models\research\object_detection 生成inference_graph文件夹,并且包含文件frozen_inference_graph.pb

TensorFlow object detection API训练自己的数据集Windows10_人工智能_34

八、用训练好的目标检测分类器

从参考网址中下载下面这三个文件,分别是从图片,视频和摄像头实现检测目标的功能

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_35

做适当修改后运行Object_detection_image的效果图如下图所示:

TensorFlow object detection API训练自己的数据集Windows10_人工智能_36

参考网址:

eric-erki/How-To-Train-an-Object-Detection-Classifier-for-Multiple-Objects-Using-TensorFlow-GPU-on-Windows-1​github.comTensorFlow object detection API训练自己的数据集Windows10_人工智能_37

错误总结 :

品颜完月:用tensorflow-gpu训练自己的神经网络(错误总结)​zhuanlan.zhihu.com

关注【OpenCV与AI深度学习】获得更多资讯

扫描下面二维码即可关注

TensorFlow object detection API训练自己的数据集Windows10_TensorFLow_38