1

我按照本教程在 coco 数据集上训练对象检测模型。本教程包含一个下载和使用coco 数据集及其注释并将它们转换为TFRecord的步骤。

我需要使用自己的自定义数据进行训练,我使用labelimg工具进行了注释,该工具生成了包含 (w,h,xmin,ymin,xmax,ymax) 的图像的 xml 文件。

但是coco 数据集具有 JSON格式,带有用于创建TFRecord的图像分割字段。

训练 resnet、retinanet 是否必须进行分段?

那么,任何人都可以指导我从没有分段值的 XML 注释创建 JSON 注释的过程吗?

xml:

<annotation>
    <folder>frames</folder>
    <filename>83.jpg</filename>
    <path>/home/tdadmin/Downloads/large/f/frames/83.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>640</width>
        <height>480</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>person</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>246</xmin>
            <ymin>48</ymin>
            <xmax>350</xmax>
            <ymax>165</ymax>
        </bndbox>
    </object>
</annotation>
4

2 回答 2

1

你现在做的有点像我之前做过的一个项目。所以我有一些建议给你。

当我训练我的 Mask RCNN 模型时,我使用了 VGG Image Annotator(你可以在 Google 上轻松找到它)。通过使用该工具,可以轻松创建 json 注释文件。然后将其插入您的训练中。

希望对您有所帮助。如果您仍有疑问,请随时对此发表评论。

罗文

于 2019-03-07T00:37:54.670 回答
0

注释格式实际上并不重要。我之前自己从 txt 文件创建了 tfrecord。要创建自定义 tfrecord,您必须编写自己的create_custom_tf_record.py,就像此文件夹中显示的其他人一样。

但是由于您使用的是 coco 类似的注释,因此您可以使用文件create_coco_tf_record.py. 您需要自己实施的重要事情是annotations_list. 只是一个字典,annotations_list因此您的目标是将您的 xml 文件解析为包含键值对的字典,然后将正确的值传递给feature_dict,然后tf.train.Examplefeature_dict. 拥有 tfrecord 后tf.train.Example created,您可以轻松创建 tfrecord。

因此,对于您的确切示例,首先解析 xml 文件。

import xml.etree.ElementTree as ET
tree = ET.parse('annotations.xml')

然后像这样构造annotaions_listtree

annotations_list = {}
it = tree.iter()
for key in it:
    annotations_list[str(key.tag)] = key.text

然后你可以feature_dictannotations_list

feature_dict = {
  'image/height':
      dataset_util.int64_feature(annotatios_list['height']),
  'image/width':
      dataset_util.int64_feature(...),
  'image/filename':
      dataset_util.bytes_feature(...),
  'image/source_id':
      dataset_util.bytes_feature(...),
  'image/key/sha256':
      dataset_util.bytes_feature(...),
  'image/encoded':
      dataset_util.bytes_feature(...),
  'image/format':
      dataset_util.bytes_feature(...),
  'image/object/bbox/xmin':
      dataset_util.float_list_feature(...),
  'image/object/bbox/xmax':
      dataset_util.float_list_feature(...),
  'image/object/bbox/ymin':
      dataset_util.float_list_feature(...),
  'image/object/bbox/ymax':
      dataset_util.float_list_feature(...),
  'image/object/class/text':
      dataset_util.bytes_list_feature(....),
  'image/object/is_crowd':
      dataset_util.int64_list_feature(...),
  'image/object/area':
      dataset_util.float_list_feature(...),
  }

只需确保该feature_dict字段与annotations_list和 中的正确字段相对应label_map

您可能想知道为什么这些字段feature_dict是必需的,根据使用您自己的数据集的官方文档,以下字段是必需的,其他是可选的。

'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_image_data),
  '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),
于 2019-03-06T14:45:13.433 回答