【问题标题】:Using [image,label] = dataset.take(2) returns two tuples instead of a single one使用 [image,label] = dataset.take(2) 返回两个元组而不是一个
【发布时间】: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>))

这意味着 imagelabel包含张量的元组,每个元组对应于 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


    【解决方案1】:

    方法采取创建数据集。它不会从数据集中提取元素。 See documentation.

    如果要从数据中提取元素,可以使用: tf.compat.v1.data.make_one_shot_iterator() 我没有找到一种更简洁的方法来从数据集中提取元素。

    例子:

    iterator = tf.compat.v1.data.make_one_shot_iterator(parsed_dataset)
    image, label = iterator.get_next()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      相关资源
      最近更新 更多