1

我正在尝试实现此代码,该代码从
官方 tensorflow 数据集中加载数据,以使其加载放置在我的谷歌驱动器上的数据

dataset, metadata = tfds.load('cycle_gan/horse2zebra',
                              with_info=True, as_supervised=True)
train_horses, train_zebras = dataset['trainA'], dataset['trainB']

我怎样才能让它将我的图像加载到从 A 类和 B 类到我的 train_horses 和 train_zebras 类的类中

train_dataset=tf.keras.utils.image_dataset_from_directory(
    '/content/drive/MyDrive/ColorGan', labels='inferred', label_mode='int',
    class_names=None, color_mode='rgb', batch_size=32, image_size=(256,
    256), shuffle=True, seed=2000, validation_split=0.2, subset='training',
    interpolation='bilinear', follow_links=False,
    crop_to_aspect_ratio=False)
test_dataset=tf.keras.utils.image_dataset_from_directory(
    '/content/drive/MyDrive/ColorGan', labels='inferred', label_mode='int',
    class_names=None, color_mode='rgb', batch_size=32, image_size=(256,
    256), shuffle=True, seed=2000, validation_split=0.2, subset='validation',
    interpolation='bilinear', follow_links=False,
    crop_to_aspect_ratio=False)

train_horses, train_zebras = train_dataset['A'],train_dataset['B']

它给了我一个错误,它不是可编写脚本的,我可以怎样才能以顶部代码片段中显示的格式加载数据

4

2 回答 2

1

我建议您使用tf.data来预处理您的数据集,因为事实证明它比ImageDataGeneratorimage_dataset_from_directory更有效。博客描述了您应该使用的目录结构,并且它还包含从 tf.data 从头开始​​为自定义数据集实现的代码。

于 2021-10-29T11:22:27.503 回答
1

创建 Tensorflow 数据管道的示例工作代码

import pathlib
data_dir = pathlib.Path('/content/images/horses')
import tensorflow as tf
batch_size = 16
img_height = 180
img_width = 180
train_horses = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

import pathlib
data_dir = pathlib.Path('/content/images/zebras')

import tensorflow as tf
batch_size = 16
img_height = 180
img_width = 180
train_zebras = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

ds = train_horses.concatenate(train_zebras)
于 2021-10-19T03:08:17.950 回答