我想使用tf.data.Dataset
api读取this code生成的数据集。存储库显示它是这样写的:
def image_to_tfexample(image_data, image_format, height, width, class_id):
return tf.train.Example(features=tf.train.Features(feature={
'image/encoded': bytes_feature(image_data),
'image/format': bytes_feature(image_format),
'image/class/label': int64_feature(class_id),
'image/height': int64_feature(height),
'image/width': int64_feature(width),
}))
以(encoded byte-string, b'png', 32, 32, label)
作为参数。
因此,要读取.tfrecord文件,数据格式必须为:
example_fmt = {
'image/encoded': tf.FixedLenFeature((), tf.string, ""),
'image/format': tf.FixedLenFeature((), tf.string, ""),
'image/class/label': tf.FixedLenFeature((), tf.int64, -1),
'image/height': tf.FixedLenFeature((), tf.int64, -1),
'image/width': tf.FixedLenFeature((), tf.int64, -1)
}
parsed = tf.parse_single_example(example, example_fmt)
image = tf.decode_raw(parsed['image/encoded'], out_type=tf.uint8)
但是它不起作用。读取并生成迭代器并引发OutOfRangeError: End of sequence
后,该数据集为空。
可以找到一个简短的Python脚本进行复制here。我正在努力寻找有关此问题的确切文档或示例。
答案 0 :(得分:1)
因为我没有train.tfrecords文件,所以我无法测试您的代码。这段代码会创建一个空的数据集吗?
dataset = tf.data.TFRecordDataset('train.tfrecords')
dataset = dataset.map(parse_fn)
itr = dataset.make_one_shot_iterator()
with tf.Session() as sess:
while True:
try:
print(sess.run(itr.get_next()))
except tf.errors.OutOfRangeError:
break
如果这给您带来了错误,请让我知道是哪一行产生的。
答案 1 :(得分:1)
这个问题有点老了,但它帮助我阅读和加载标记图像(用 VoTT 标记)以训练 YOLOv4/v3。也许此代码是另一个可能对某人有所帮助的“示例”:
def load_single_boxed_tfrecord(record):
"""
Loads a single tfrecord with its boundary boxes and corresponding labels, from a single tfrecord.
Args:
record: as tfrecord (Tensor), as yielded from tf.data.TFRecordDataset
Returns:
(Tensor of image), (Tensor of labels), (Tensor of: x_top_left, x_lower_right, y_top_left, y_lower_right)
"""
feature = {'image/encoded': tf.io.FixedLenFeature([], tf.string),
'image/object/class/label': tf.io.VarLenFeature(tf.int64),
'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),
'image/filename': tf.io.FixedLenFeature([], tf.string),
'image/width': tf.io.FixedLenFeature([], tf.int64),
'image/height': tf.io.FixedLenFeature([], tf.int64)
}
tf_file = tf.io.parse_single_example(record, feature)
tf_img = tf.image.decode_image(tf_file["image/encoded"], channels=COLOR_CHANNELS)
tf_img = tf.image.convert_image_dtype(tf_img, tf.float32)
label = tf.sparse.to_dense(tf_file['image/object/class/label'], default_value=0)
# normalized values:
x1norm = tf.sparse.to_dense(tf_file['image/object/bbox/xmin'], default_value=0)
x2norm = tf.sparse.to_dense(tf_file['image/object/bbox/xmax'], default_value=0)
y1norm = tf.sparse.to_dense(tf_file['image/object/bbox/ymin'], default_value=0)
y2norm = tf.sparse.to_dense(tf_file['image/object/bbox/ymax'], default_value=0)
return tf_img, label, [x1norm, x2norm, y1norm, y2norm]
答案 2 :(得分:0)
我仍在学习TensorFlow和tfrecordfile的用法,因此我不是这些东西的精通者,但是我发现这个guide对我的情况很有用,也许对您也有用。