feature -> Features -> Example, 三者按顺序为包含关系
example = tf.train.Example(features=tf.train.Features(feature={...}))
tf_writer.write(example.SerializeToString()) # 序列化写入tfrecord
注意: feature必须是list, 可以先转成ndarray再转回来
def _convert_to_example(image_name, labels, labels_text, bboxes, oriented_bboxes, shape,
difficult=0, truncated=0):
"""Build an Example proto for an image example.
Args:
image_data: string, JPEG encoding of RGB image;
labels: list of integers, identifier for the ground truth;
labels_text: list of text-string;
bboxes: list of bounding boxes; each box is a list of integers;
specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
to the same label as the image label.
shape: 3 integers, image shapes in pixels.
Returns:
Example proto
"""
image_data = tf.gfile.FastGFile(image_name, 'rb').read()
image_format = b'JPG'
# Example protocol对象就是对图片加上bbox等位置信息的一种封装
example = tf.train.Example(features=tf.train.Features(feature={
'image/shape': int64_feature(shape),
'image/object/bbox/xmin': float_feature(bboxes[0]),
'image/object/bbox/ymin': float_feature(bboxes[1]),
'image/object/bbox/xmax': float_feature(bboxes[2]),
'image/object/bbox/ymax': float_feature(bboxes[3]),
'image/object/bbox/x1': float_feature(list(oriented_bboxes[:, 0])),
'image/object/bbox/x2': float_feature(list(oriented_bboxes[:, 1])),
'image/object/bbox/x3': float_feature(list(oriented_bboxes[:, 2])),
'image/object/bbox/x4': float_feature(list(oriented_bboxes[:, 3])),
'image/object/bbox/y1': float_feature(list(oriented_bboxes[:, 4])),
'image/object/bbox/y2': float_feature(list(oriented_bboxes[:, 5])),
'image/object/bbox/y3': float_feature(list(oriented_bboxes[:, 6])),
'image/object/bbox/y4': float_feature(list(oriented_bboxes[:, 7])),
'image/object/bbox/label': int64_feature(labels),
'image/object/bbox/label_text': bytes_feature(labels_text),
'image/object/bbox/difficult': int64_feature(difficult),
'image/object/bbox/truncated': int64_feature(truncated),
'image/format': bytes_feature(image_format),
'image/filename': bytes_feature(image_name.encode()),
'image/encoded': bytes_feature(image_data)
})
)
return example
参考https://blog.csdn.net/hfutdog/article/details/86244944
python3下string和byte编码问题
对Example中的features进行feature输入时, python3会报错,'*.jpg' has type str, but expected one of: bytes, 是因为python3中string和byte不能互相转换, 必须通过str.encode() -> byte或者byte.decode() -> string, 如:
'image/format': bytes_feature(image_format),
'image/filename': bytes_feature(image_name.encode()),
'image/encoded': bytes_feature(image_data)
或在一开始读文件和输入字符串的时候就指定成二进制, 通过加b实现:
image_data = tf.gfile.FastGFile(image_name, 'rb').read()
image_format = b'JPG'