【问题标题】:Changing label_mode of tf.keras.preprocessing.image_dataset_from_directory after setting it once设置一次后更改 tf.keras.preprocessing.image_dataset_from_directory 的 label_mode
【发布时间】:2020-11-07 04:28:55
【问题描述】:

我正在使用此代码加载我必须传递给卷积变分自动编码器的图像:

import tensorflow as tf

train = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir + 'Train/', label_mode=None,
  image_size=(img_height, img_width),
  batch_size=batch_size)

为了能够将它传递给自动编码器,我必须设置label_mode = None。此外,解码器接收到的图像将进一步传递到 CNN 进行分类,我需要标签。

我怎样才能让train 在最初为label_mode=None 时也为CNN 返回标签。

【问题讨论】:

    标签: python tensorflow tf.data.dataset


    【解决方案1】:

    您可以加载带有标签的图像,然后创建另一个不带标签的数据集。

    import tensorflow as tf
    
    train = tf.keras.preprocessing.image_dataset_from_directory(
      data_dir + 'Train/', label_mode='categorical',
      image_size=(img_height, img_width),
      batch_size=batch_size)
    
    X = np.array([])
    for x, y in testData:
      if X.size == 0:
        X = x.numpy()
        continue
      X = np.concatenate([X, x.numpy()])
    dataset = tf.data.Dataset.from_tensor_slices(X)
    
    

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      相关资源
      最近更新 更多