在自己的数据集上训练Tensorflow对象检测 [英] Train Tensorflow Object Detection on own dataset

查看:69
本文介绍了在自己的数据集上训练Tensorflow对象检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了几天时间尝试完成此任务后,我想分享一下我如何回答这个问题的经验:

如何使用 TS对象检测进行训练使用我自己的数据集?

解决方案

这假定模块已经安装.如果没有,请参阅其文档. /p>

免责声明

此答案并非是训练对象检测模块的正确唯一的方式.这仅仅是我分享我的经验以及对我有用的东西.我很乐于提出建议,并且可以学习更多有关此的知识,因为我仍然对ML还是陌生的.

TL; DR

  1. 创建自己的PASCAL VOC格式数据集
  2. 从中生成TFRecords
  3. 配置管道
  4. 可视化

此答案的每个部分都包含一个相应的Edit(请参见下文).阅读完每个部分后,请同时阅读其编辑"以获取说明.为每个部分添加了更正和提示.

使用的工具

LabelImg :一种用于创建PASCAL VOC格式注释的工具.

1.创建自己的PASCAL VOC数据集

PS: 为简单起见,我回答的文件夹命名约定遵循Pascal VOC 2012的命名约定

2012年5月数据集,您会注意到该文件夹​​具有以下结构

+VOCdevkit +VOC2012 +Annotations +ImageSets +Action +Layout +Main +Segmentation +JPEGImages +SegmentationClass +SegmentationObject

目前,已对以下文件夹进行了修改:

注释 :这是所有图像的相应XML文件将被放置的地方.使用上面建议的工具创建注释.不必担心<truncated><difficulty>标签,因为它们会被训练和评估二进制文件忽略.

JPEGImages :实际图像的位置.确保它们是JPEG类型,因为当前支持使用它们提供的脚本创建TFRecords.

ImageSets-> Main :这仅由文本文件组成.对于每个类,都有一个对应的 train.txt trainval.txt val.txt .以下是VOC 2012文件夹中的 aeroplane_train.txt 内容的示例

2008_000008 -1
2008_000015 -1
2008_000019 -1
2008_000023 -1
2008_000028 -1
2008_000033  1

该结构基本上是图像名称,后跟一个布尔值,表示该图像中是否存在相应的对象.例如,图像 2008_000008 不是由一架飞机组成,因此标有 -1 ,但是图像 2008_000033 是.

我写了一个小的Python脚本来生成这些文本文件.简单地遍历图像名称,并在图像名称旁边分配1或-1以表示对象的存在.我通过改组图像名称在文本文件之间添加了一些随机性.

{classname} _val.txt 文件由测试验证数据集组成.可以将其视为训练期间的测试数据.您想将数据集分为训练和验证.可以在此处找到更多信息. .这些文件的格式类似于培训的格式.

此时,您的文件夹结构应为

+VOCdevkit +VOC2012 +Annotations --(for each image, generated annotation) +ImageSets +Main --(for each class, generated *classname*_train.txt and *classname*_val.txt) +JPEGImages --(a bunch of JPEG images)


1.1生成标签图

在准备好数据集之后,我们需要创建相应的标签图. 导航到 models/object_detection/data 并打开 pascal_label_map.pbtxt .

此文件由JSON组成,该JSON为每个项目分配ID和名称.对该文件进行修改以反映您想要的对象.


2.生成TFRecords

如果您特别研究他们的代码,请,他们仅显式获取 aeroplane_train.txt .出于好奇,这就是为什么.将此文件名更改为您的任何班级训练文本文件.

确保 VOCdevkit 位于 models/object_detection 中,然后您可以继续进行说明应该是不言自明的覆盖这一部分.可以在 object_detection/samples/configs <中找到示例配置/em> .

对于那些像我一样从头开始训练的人,只需确保删除fine_tune_checkpointfrom_detection_checkpoint节点即可. 这是我的配置文件的外观供参考.

从这里开始,您可以继续教程并运行培训过程.


4.可视化

请确保与培训并行进行评估,以便能够可视化学习过程.引用 Jonathan Huang

最好的方法是只运行eval.py二进制文件.我们通常运行 与培训并行的二进制文件,将其指向目录 正在训练的检查点. eval.py二进制文件将写入 记录到您指定的eval_dir,然后可以指向 使用Tensorboard.

您希望看到在最初的几个小时中,移动接入点已经起飞", 然后您想查看它何时收敛.没有它很难说 在这些图中查看您需要多少步骤.


编辑一(17年7月28日):

我从没想到自己的反应会引起如此多的关注,所以我决定回来回顾一下.

工具

对于我的其他Apple用户,您实际上可以使用 RectLabel 进行注释.

Pascal VOC

深入研究之后,我终于意识到 trainval.txt 实际上是训练和验证数据集的结合.

请查看他们的官方开发工具包,以更好地理解该格式.

>

标签地图生成

在撰写本文时,ID 0表示none_of_the_above.建议您的ID从1开始.

可视化

运行评估并将张量板定向到Eval目录后,它将向您显示每个类别的mAP以及每个类别的性能.很好,但我也希望同时与Eval一起查看训练数据.

为此,请在其他端口上运行tensorboard并将其指向您的火车目录

tensorboard --logdir=${PATH_TO_TRAIN} --port=${DESIRED_NUMBER}

After spending a couple days trying to achieve this task, I would like to share my experience of how I went about answering the question:

How do I use TS Object Detection to train using my own dataset?

解决方案

This assumes the module is already installed. Please refer to their documentation if not.

Disclaimer

This answer is not meant to be the right or only way of training the object detection module. This is simply I sharing my experience and what has worked for me. I'm open to suggestions and learning more about this as I am still new to ML in general.

TL;DR

  1. Create your own PASCAL VOC format dataset
  2. Generate TFRecords from it
  3. Configure a pipeline
  4. Visualize

Each section of this answer consists of a corresponding Edit (see below). After reading each section, please read its Edit as well for clarifications. Corrections and tips were added for each section.

Tools used

LabelImg: A tool for creating PASCAL VOC format annotations.

1. Create your own PASCAL VOC dataset

PS: For simplicity, the folder naming convention of my answer follows that of Pascal VOC 2012

A peek into the May 2012 dataset, you'll notice the folder as having the following structure

+VOCdevkit +VOC2012 +Annotations +ImageSets +Action +Layout +Main +Segmentation +JPEGImages +SegmentationClass +SegmentationObject

For the time being, amendments were made to the following folders:

Annotations: This is were all the images' corresponding XML files will be placed in. Use the suggested tool above to create the annotations. Do not worry about <truncated> and <difficulty> tags as they will be ignored by the training and eval binaries.

JPEGImages: Location of your actual images. Make sure they are of type JPEG because that's what is currently supported in order to create TFRecords using their provided script.

ImageSets->Main: This simply consists of text files. For each class, there exists a corresponding train.txt, trainval.txt and val.txt. Below is a sample of the contents of the aeroplane_train.txt in the VOC 2012 folder

2008_000008 -1
2008_000015 -1
2008_000019 -1
2008_000023 -1
2008_000028 -1
2008_000033  1

The structure is basically image name followed by a boolean saying whether the corresponding object exists in that image or not. Take for example image 2008_000008 does not consist of an aeroplane hence marked with a -1 but image 2008_000033 does.

I wrote a small Python script to generate these text files. Simply iterate through the image names and assign a 1 or -1 next to them for object existence. I added some randomness among my text files by shuffling the image names.

The {classname}_val.txt files consist of the testing validation datasets. Think of this as the test data during training. You want to divide your dataset into training and validation. More info can be found here. The format of these files is similar to that of training.

At this point, your folder structure should be

+VOCdevkit +VOC2012 +Annotations --(for each image, generated annotation) +ImageSets +Main --(for each class, generated *classname*_train.txt and *classname*_val.txt) +JPEGImages --(a bunch of JPEG images)


1.1 Generating label map

With the dataset prepared, we need to create the corresponding label maps. Navigate to models/object_detection/data and open pascal_label_map.pbtxt.

This file consists of a JSON that assigns an ID and name to each item. Make amendments to this file to reflect your desired objects.


2. Generate TFRecords

If you look into their code especially this line, they explicitly grab the aeroplane_train.txt only. For curios minds, here's why. Change this file name to any of your class train text file.

Make sure VOCdevkit is inside models/object_detection then you can go ahead and generate the TFRecords.

Please go through their code first should you run into any problems. It is self explanatory and well documented.


3. Pipeline Configuration

The instructions should be self explanatory to cover this segment. Sample configs can be found in object_detection/samples/configs.

For those looking to train from scratch as I did, just make sure to remove the fine_tune_checkpoint and from_detection_checkpoint nodes. Here's what my config file looked like for reference.

From here on you can continue with the tutorial and run the training process.


4. Visualize

Be sure to run the eval in parallel to the training in order to be able to visualize the learning process. To quote Jonathan Huang

the best way is to just run the eval.py binary. We typically run this binary in parallel to training, pointing it at the directory holding the checkpoint that is being trained. The eval.py binary will write logs to an eval_dir that you specify which you can then point to with Tensorboard.

You want to see that the mAP has "lifted off" in the first few hours, and then you want to see when it converges. It's hard to tell without looking at these plots how many steps you need.


EDIT I (28 July '17):

I never expected my response to get this much attention so I decided to come back and review it.

Tools

For my fellow Apple users, you could actually use RectLabel for annotations.

Pascal VOC

After digging around, I finally realized that trainval.txt is actually the union of training and validation datasets.

Please look at their official development kit to understand the format even better.

Label Map Generation

At the time of my writing, ID 0 represents none_of_the_above. It is recommended that your IDs start from 1.

Visualize

After running your evaluation and directed tensorboard to your Eval directory, it'll show you the mAP of each category along with each category's performance. This is good but I like seeing my training data as well in parallel with Eval.

To do this, run tensorboard on a different port and point it to your train directory

tensorboard --logdir=${PATH_TO_TRAIN} --port=${DESIRED_NUMBER}

这篇关于在自己的数据集上训练Tensorflow对象检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆