【发布时间】:2020-07-01 06:33:33
【问题描述】:
我有一个 TFRecord 文件,我在其中存储了图像包装字节为字符串,标签为 ints64。我正在使用下面的代码来操作图像和标签:
# Create dataset from TFRecord file
records_path = DATA_DIR + 'TFRecords/train_0.tfrecords'
dataset = tf.data.TFRecordDataset(filenames=records_path)
# Map dataset from parsing function
parsed_dataset = dataset.map(parsing_fn)
print(parsed_dataset)
# Take a testing sample
image,label = parsed_dataset.take(2)
print(image,label)
哪些输出:
Tensor("ParseSingleExample/ParseSingleExample:1", shape=(), dtype=int64)
<MapDataset shapes: ((None,), ()), types: (tf.float32, tf.int64)>
((<tf.Tensor: id=635, shape=(185256,), dtype=float32, numpy=array([162., 162., 170., ..., 17., 17., 255.], dtype=float32)>,
<tf.Tensor: id=636, shape=(), dtype=int64, numpy=183350>),
(<tf.Tensor: id=637, shape=(153120,), dtype=float32, numpy=array([208., 207., 202., ..., 240., 240., 242.], dtype=float32)>,
<tf.Tensor: id=638, shape=(), dtype=int64, numpy=183350>))
这意味着 image 和 label 是 包含张量的元组,每个元组对应于 image 和 label 两张不同的图片,而不是每张都有来自同一张图片的分别的图片和标签数据。
image[0] = image bytes 来自 image 1
image[1] = label 来自 image 1
的信息label[0] = image bytes 来自 image 2
label[1] = label 来自 image 2
的信息有谁知道为什么使用“image = take(1)”返回一个TakeDataset,而不是一个只包含一个数据样本的元组,其张量对应于图像字节和标签数据?
帮手fn的
# Data stored format
data = {
'image': wrap_bytes(img_bytes),
'label': wrap_int64(label)
}
# Parsing function
def parsing_fn(serialized):
# Define a dict with the data-names and types we expect to
# find in the TFRecords file.
features = \
{
'image': tf.io.FixedLenFeature([], tf.string),
'label': tf.io.FixedLenFeature([], tf.int64)
}
# Parse the serialized data so we get a dict with our data.
parsed_example = tf.io.parse_single_example(serialized=serialized,
features=features)
# Get the image as raw bytes.
image_raw = parsed_example['image']
# Decode the raw bytes so it becomes a tensor with type.
image = tf.io.decode_raw(image_raw, tf.uint8)
# The type is now uint8 but we need it to be float.
image = tf.cast(image, tf.float32)
# Get the label associated with the image.
label = parsed_example['label']
# The image and label are now correct TensorFlow types.
return image, label
【问题讨论】:
标签: tensorflow tensorflow2.0 tensorflow-datasets