0

我肯定对这个感到头疼。我一直在玩 Tensorflow 的各种图像生成器教程,它很棒,但是当我尝试将自己的图像数据集用于 DC GAN 教程时,我遇到了困难。MNIST 效果很好,名人面孔也是如此。但是当我建立自己的文件夹结构时,我无能为力地导入要使用的图像。这是我到目前为止的位置:

#IMPORT PHOTOS
import pathlib
data_dir = "/Users/..../PATTERNS"
data_dir = pathlib.Path(data_dir)



train_images = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    labels="inferred",
    label_mode="int",
    class_names=None,
    color_mode="rgb",
    batch_size=32,
    image_size=(100, 100),
    shuffle=True,
    seed=None,
    validation_split=None,
    subset=None,
    interpolation="bilinear",
    follow_links=False,
    )

# up until this point everything works

BUFFER_SIZE = 60000
BATCH_SIZE = 256

train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
train_images = (train_images - 127.5) / 127.5 # Normalize the images to [-1, 1]


# Batch and shuffle the data
train_dataset = tf.data.Dataset.from_tensor_slices(dataset).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

然后我收到错误消息: AttributeError: 'BatchDataset' object has no attribute 'reshape'

我已经尝试了一些其他教程并进行了大量的谷歌搜索,但还没有答案。任何帮助将不胜感激。谢谢!

4

1 回答 1

0

你能克服你的问题吗?

问题是 image_dataset_from_directory() 方法返回的批处理数据集对象没有重塑方法

根据张量流文档

一个 tf.data.Dataset 对象。
如果 label_mode 为 None,它会产生 float32 形状的张量(batch_size、image_size[0]、image_size[1]、num_channels)、编码图像(有关 num_channels 的规则,请参见下文)。
否则,它会产生一个元组 (images, labels),其中 images 具有形状 (batch_size, image_size[0], image_size[1], num_channels),并且标签遵循下面描述的格式。

您可以尝试将其作为元组而不是张量访问

于 2021-05-19T05:39:29.573 回答