0

下面的示例摘自TensorFlow 官方数据管道教程。基本上,将一堆 JPG 的大小调整为(128, 128, 3). 由于某种原因,在应用该操作时,在检查数据集的形状时map(),颜色维度(即 3)变成了 a 。None为什么要挑出第三个维度?(我检查了是否有任何没有(128, 128, 3)但没有找到的图像。)

如果有的话,None应该只显示第一个维度,即计算示例数量的维度,并且不应该影响示例的各个维度,因为——作为嵌套结构——它们应该具有无论如何,相同的形状以便存储为tf.data.Datasets。

TensorFlow 2.1 中的代码是

import pathlib
import tensorflow as tf

# Download the files.
flowers_root = tf.keras.utils.get_file(
    'flower_photos',
    'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
    untar=True)
flowers_root = pathlib.Path(flowers_root)

# Compile the list of files.
list_ds = tf.data.Dataset.list_files(str(flowers_root/'*/*'))

# Reshape the images.
# Reads an image from a file, decodes it into a dense tensor, and resizes it
# to a fixed shape.
def parse_image(filename):
  parts = tf.strings.split(file_path, '\\') # Use the forward slash on Linux
  label = parts[-2]

  image = tf.io.read_file(filename)
  image = tf.image.decode_jpeg(image)
  image = tf.image.convert_image_dtype(image, tf.float32)
  image = tf.image.resize(image, [128, 128])
  print("Image shape:", image.shape)
  return image, label

print("Map the parse_image() on the first image only:")
file_path = next(iter(list_ds))
image, label = parse_image(file_path)

print("Map the parse_image() on the whole dataset:")
images_ds = list_ds.map(parse_image)

和产量

Map the parse_image() on the first image only:
Image shape: (128, 128, 3)
Map the parse_image() on the whole dataset:
Image shape: (128, 128, None)

为什么None在最后一行?

4

1 回答 1

0

在本教程中,您缺少这部分

for image, label in images_ds.take(5):
    show(image, label)

线

images_ds = list_ds.map(parse_image)

仅创建一个占位符,如果您放置打印文件,则不会将图像传递给函数,但如果您使用

for image, label in images_ds.take(5)

它遍历通过 parse_image 函数传递的每个图像。

于 2020-04-27T19:30:05.983 回答