将自定义数据加载到张量流管道中

Load custom Data into a tensorflow pipeline

我正在尝试实现从
加载数据的代码 官方 tensorflow 数据集,用于加载我的 google 驱动器

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

我怎样才能将我的图像从 classes A 和 B 加载到 classes 到我的 train_horses 和 train_zebras class

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']

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

创建 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)

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