首页 > 解决方案 > 如何对来自 `image_dataset_from_directory()` 的图像进行图像转换,以便将其转换为 Lab 并返回 L 通道?

问题描述

我有一个图像数据集,我正在使用tf.keras.preprocessing.image_dataset_from_directory批量加载图像并可能创建一个数据管道。我正在尝试处理图像并将它们转换为 Lab 颜色空间。我可以通过使用.take()和解析张量来手动完成,但我无法做到这一点,因此我可以直接在管道中使用它。我的代码看起来像这样:

def convert_to_lab(image):
  # Convert to Lab colourspace
  Lab = color.rgb2lab(image.numpy().astype('uint8')) #<- Throws error because .numpy() isn't supported

  L, a, b = Lab[:,:,0], Lab[:,:,1], Lab[:,:,2]

  L = np.array(L)

  a, b = np.array(a), np.array(b)

  return L, (a,b)

def preprocess_train(image, label):
  image, label = convert_to_lab(image)
  return image,label

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  'dataset',
  shuffle=False,
  image_size=(256, 256),
  batch_size=32)

train_ds = train_ds.map(preprocess_train)

但我猜dataset创建的对象不支持EagerTensorOps,因为我收到以下错误,

AttributeError: 'Tensor' object has no attribute 'numpy'

我希望能够创建一个管道,我基本上可以将这个数据集用于 GAN。任何帮助表示赞赏:D

标签: pythonopencvtensorflow2.0tensorflow-datasetstf.keras

解决方案


推荐阅读