简体   繁体   English

tensorflow对象检测API:生成自定义数据集的TF记录

[英]tensorflow object detection API: generate TF record of custom data set

I am trying to retrain the tensorflow object detection API with my own data i have labelled my image with labelImg but when i am using the script create_pascal_tf_record.py which is included in the tensorflow/models/research, i got some errors and i dont really know why it happens我正在尝试用我自己的数据重新训练 tensorflow 对象检测 API 我已经用 labelImg 标记了我的图像,但是当我使用包含在 tensorflow/models/research 中的脚本create_pascal_tf_record.py 时,我遇到了一些错误,我真的没有知道为什么会发生

python object_detection/dataset_tools/create_pascal_tf_record.py --data_dir=/home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/ --label_map_path=/home/jim/Documents/tfAPI/workspace/training_cabbage/annotations/label_map.pbtxt --output_path=/home/jim/Desktop/cabbage_pascal.record --set=train --annotations_dir=/home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/ --year=merged
Traceback (most recent call last):
  File "object_detection/dataset_tools/create_pascal_tf_record.py", line 185, in <module>
    tf.app.run()
  File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "object_detection/dataset_tools/create_pascal_tf_record.py", line 167, in main
    examples_list = dataset_util.read_examples_list(examples_path)
  File "/home/jim/Documents/tfAPI/models/research/object_detection/utils/dataset_util.py", line 59, in read_examples_list
    lines = fid.readlines()
  File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 188, in readlines
    self._preread_check()
  File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 85, in _preread_check
    compat.as_bytes(self.__name), 1024 * 512, status)
  File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: /home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/VOC2007/ImageSets/Main/aeroplane_train.txt; No such file or directory

the train folder contains the xml and the jpg train 文件夹包含 xml 和 jpg
the annotation folder contains my labelmap.pbtxt for my custom class注释文件夹包含我的自定义类的 labelmap.pbtxt
and i want to publish the TF record file on the desktop我想在桌面上发布TF记录文件

it seems that it cant find a file in my images and annotations folder but i dont know why If someone has idea, thank you in advance似乎在我的图像和注释文件夹中找不到文件,但我不知道为什么如果有人有想法,请提前感谢

This error happens because you use the code for PASCAL VOC, which requires certain data folders structure.发生此错误是因为您使用了 PASCAL VOC 的代码,该代码需要特定的数据文件夹结构。 Basically, you need to download and unpack VOCdevkit to make the script work.基本上,您需要下载并解压 VOCdevkit 才能使脚本工作。 As user phd pointed you out, you need the file VOC2007/ImageSets/Main/aeroplane_train.txt .正如用户 phd 指出的那样,您需要文件VOC2007/ImageSets/Main/aeroplane_train.txt

I recommend you to write your own script for tfrecords creation, it's not difficult.我建议您为 tfrecords 创建编写自己的脚本,这并不难。 You need just two key components:您只需要两个关键组件:

  • Loop over your data that reads the images and annotations循环读取图像和注释的数据
  • A function that encodes the data into tf.train.Example .将数据编码为tf.train.Example For that you can pretty much re-use the dict_to_tf_example为此,您几乎可以重用dict_to_tf_example

Inside the loop, having created the tf_example , pass it to TFRecordWriter :在循环内部,创建了tf_example ,将其传递给TFRecordWriter

writer.write(tf_example.SerializeToString())

OK for future references, this is how i add background images to the dataset allowing the model to train on it.好的,供将来参考,这就是我将背景图像添加到数据集的方式,允许模型对其进行训练。 Functions used from: datitran/raccoon_dataset使用的函数来自: datitran/raccoon_dataset

  1. Generate CSV file -> xml_to_csv.py生成 CSV 文件 -> xml_to_csv.py
  2. Generate TFRecord from CSV file -> generate_tfrecord.py从 CSV 文件生成 TFRecord -> generate_tfrecord.py

First Step - Creating XML file for it第一步 - 为其创建 XML 文件

Example of background image XML file背景图像 XML 文件示例

<annotation>
    <folder>test/</folder>
    <filename>XXXXXX.png</filename>
    <path>your_path/test/XXXXXX.png</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>640</width>
        <height>640</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
</annotation>

Basically you remove the entire <object> (ie no annotation )基本上你删除整个<object> (即没有注释)

Second Step - Generate CSV file第二步 - 生成 CSV 文件

Using the xml_to_csv.py I just add a little change, to consider the XML file that do not have any annotation (the background images) as so: From the original: https://github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/xml_to_csv.py#L12-L22使用xml_to_csv.py我只是添加了一些更改,以考虑没有任何注释(背景图像)的 XML 文件:来自原文: https : //github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f67 /xml_to_csv.py#L12-L22

I add:我加:

value = None
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)
        if value is None:
            value = (root.find('filename').text,
                     int(root.find('size')[0].text),
                     int(root.find('size')[1].text),
                     '-1',
                     '-1',
                     '-1',
                     '-1',
                     '-1'
                     )
            xml_list.append(value)

I'm just adding negative values to the coordinates of the bounding box if there is no in the XML file, which is the case for the background images, and it will be usefull when generating the TFRecords.如果 XML 文件中没有,我只是向边界框的坐标添加负值,背景图像就是这种情况,并且在生成 TFRecords 时会很有用。

Third and Final Step - Generating the TFRecords第三步也是最后一步 - 生成 TFRecords

Now, when creating the TFRecords, if the the corresponding row/image has negative coordinates, i just add zero values to the record (before, this would not even be possible).现在,在创建 TFRecords 时,如果相应的行/图像具有负坐标,我只需向记录添加零值(以前,这甚至不可能)。

So from the original: https://github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/generate_tfrecord.py#L60-L66所以来自原文: https : //github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/generate_tfrecord.py#L60-L66

I add:我加:

for index, row in group.object.iterrows():
        if int(row['xmin']) > -1:
            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']))
        else:
            xmins.append(0)
            xmaxs.append(0)
            ymins.append(0)
            ymaxs.append(0)
            classes_text.append('something'.encode('utf8'))  # this doe not matter for the background
            classes.append(5000)

To note that in the class_text (of the else statement), since for the background images there are no bounding boxes, you can replace the string with whatever you would like, for the background cases, this will not appear anywhere.请注意,在class_textelse语句的)中,由于背景图像没有边界框,您可以用您想要的任何内容替换字符串,对于背景情况,这不会出现在任何地方。

And lastly for the classes (of the else statement) you just need to add a number label that does not belong to neither of your own classes.最后,对于( else语句的) classes ,您只需要添加一个不属于您自己的类的数字标签。

For those who are wondering, I've used this procedure many times, and currently works for my use cases.对于那些想知道的人,我已经多次使用此过程,并且目前适用于我的用例。

Hope it helped in some way.希望它在某种程度上有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM