首页 > 解决方案 > 无论如何使用TensorFlow分别加载数据和标签

问题描述

我正在研究一个分类问题,我有一堆图像存储在两个目录中:

my_data/Total/M0

my_data/Total/B0

其中 M0 是一个类的子目录,B0 是另一个类的子目录。

当我使用:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  train_dir,
  validation_split=0.1,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=30336) 

它返回一批元组,第一个元素是数据,第二个元素是标签我想知道是否有任何方式加载这些数据,以便数据和标签分开,如:

x_train , y_train = ...

标签: pythontensorflow

解决方案


如果我理解正确,train_ds应该会产生所需的输出。

看看这段代码:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    train_dir,
    validation_split=0.1,
    subset="training",
    seed=123,
    image_size=(300, 300), # Note that I changed the image size to (300, 300)
    batch_size=30 # Note that I changed the batch size to 30
)

for x, y in train_ds:
    print(x.shape)
    break

这将输出(30, 300, 300, 3)应该与您想要的(batch, width, height, dim).


推荐阅读