如何验证TensorFlow数据集中的图像?

时间:2019-03-13 15:43:46

标签: python tensorflow image-segmentation tensorboard tensorflow-datasets

我正在使用以下来自TensorFlow文档here的代码创建标签和图像的张量流数据集。

# Reads an image from a file, decodes it into a dense tensor, and resizes it
# to a fixed shape.
def _parse_function(filename, label):
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_jpeg(image_string)
  image_resized = tf.image.resize_images(image_decoded, [28, 28])
  return image_resized, label

# A vector of filenames.
filenames = tf.constant(["/var/data/image1.jpg", "/var/data/image2.jpg", ...])

# `labels[i]` is the label for the image in `filenames[i].
labels = tf.constant([0, 37, ...])

dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset = dataset.map(_parse_function)

我现在要验证是否已将图像添加到数据集中并检查尺寸。我该怎么做?

1 个答案:

答案 0 :(得分:0)

访问数据集中元素的标准方法是制作一个迭代器

iterator = dataset.make_one_shot_iterator()
image, label = iterator.get_next()

with tf.Session() as sess:
  print(sess.run(label))
  print(sess.run(image.get_shape()))