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