tf.张量的定义良好的维度莫名其妙地变成了 "无"。

问题描述 投票:0回答:1

下面的例子摘自 TensorFlow官方教程 在数据管道上。基本上,一个人调整了一堆JPG的大小,使其成为 (128, 128, 3). 由于某些原因,当应用 map() 操作,颜色维度,即3,变成了一个。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 在最后一行?

python tensorflow tensorflow2.0 tensorflow-datasets data-wrangling
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函数遍历每个图像。

© www.soinside.com 2019 - 2024. All rights reserved.