【问题标题】:tensorflow object detection API: generate TF record of custom data settensorflow对象检测API:生成自定义数据集的TF记录
【发布时间】:2019-02-04 08:58:26
【问题描述】:

我正在尝试用我自己的数据重新训练 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

train 文件夹包含 xml 和 jpg
注释文件夹包含我的自定义类的 labelmap.pbtxt
我想在桌面上发布TF记录文件

它似乎在我的图像和注释文件夹中找不到文件,但我不知道为什么 如果有人有想法,请提前感谢您

【问题讨论】:

  • 对不起,有文件/home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/VOC2007/ImageSets/Main/aeroplane_train.txt吗?
  • 嗨,不,没有这样的文件,是脚本本身在寻找这个名字 flle 但我不知道为什么

标签: python-2.7 tensorflow virtualenv object-detection


【解决方案1】:

发生此错误是因为您使用 PASCAL VOC 的代码,这需要特定的数据文件夹结构。基本上,您需要下载并解压 VOCdevkit 才能使脚本正常工作。正如用户 phd 指出的那样,您需要文件 VOC2007/ImageSets/Main/aeroplane_train.txt

我建议您为创建 tfrecords 编写自己的脚本,这并不难。您只需要两个关键组件:

  • 循环读取图像和注释的数据
  • 将数据编码为tf.train.Example 的函数。为此,您几乎可以重复使用dict_to_tf_example

在循环内部,创建了tf_example,将其传递给TFRecordWriter

writer.write(tf_example.SerializeToString())

【讨论】:

  • 您好,很抱歉回复晚了。太感谢了。确实,使用我自己的脚本,一切都变得更好了!
  • 从哪里获得 VOCdevkit
  • 您可以从 [host.robots.ox.ac.uk/pascal/VOC/] 以 zip 格式下载 Pascal VOC 数据集,解压后您将获得 VOCdevkit。
【解决方案2】:

好的,以供将来参考,这就是我将背景图像添加到数据集以允许模型对其进行训练的方式。 使用的函数来自:datitran/raccoon_dataset

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

第一步 - 为其创建 XML 文件

背景图片 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>

基本上你删除整个&lt;object&gt;(即没有注释)

第二步 - 生成 CSV 文件

使用xml_to_csv.py 我只是添加了一点更改,以考虑没有任何注释的 XML 文件(背景图像): 从原文: https://github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/xml_to_csv.py#L12-L22

我补充:

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)

如果 XML 文件中没有,我只是在边界框的坐标上添加负值,背景图像就是这种情况,在生成 TFRecords 时会很有用。

第三步也是最后一步 - 生成 TFRecords

现在,在创建 TFRecords 时,如果相应的行/图像具有负坐标,我只需将零值添加到记录中(以前,这甚至是不可能的)。

所以从原文: https://github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/generate_tfrecord.py#L60-L66

我补充:

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)

请注意,在class_textelse 语句的)中,由于背景图像没有边界框,您可以将字符串替换为您想要的任何内容,作为背景情况下,这不会出现在任何地方。

最后,对于classeselse 语句),您只需添加一个不属于您自己的类的数字标签。

对于那些想知道的人,我已经多次使用此过程,并且目前适用于我的用例。

希望它在某种程度上有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-13
    • 2020-02-26
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2018-04-19
    • 2018-12-01
    • 1970-01-01
    相关资源
    最近更新 更多