将自定义数据加载到tensorflow管道中

2024-09-30 18:14:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现从
加载数据的代码 官方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_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']

它给了我一个错误,即它不可编写脚本。我能做些什么,以便以顶部代码段中显示的格式加载数据


Tags: 数据fromimagefalsetruesizemodetf
1条回答
网友
1楼 · 发布于 2024-09-30 18:14:10

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

相关问题 更多 >