注释格式实际上并不重要。我之前自己从 txt 文件创建了 tfrecord。要创建自定义 tfrecord,您必须编写自己的create_custom_tf_record.py
,就像此文件夹中显示的其他人一样。
但是由于您使用的是 coco 类似的注释,因此您可以使用文件create_coco_tf_record.py
. 您需要自己实施的重要事情是annotations_list
. 只是一个字典,annotations_list
因此您的目标是将您的 xml 文件解析为包含键值对的字典,然后将正确的值传递给feature_dict
,然后tf.train.Example
从feature_dict
. 拥有 tfrecord 后tf.train.Example created
,您可以轻松创建 tfrecord。
因此,对于您的确切示例,首先解析 xml 文件。
import xml.etree.ElementTree as ET
tree = ET.parse('annotations.xml')
然后像这样构造annotaions_list
:tree
annotations_list = {}
it = tree.iter()
for key in it:
annotations_list[str(key.tag)] = key.text
然后你可以feature_dict
从annotations_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),