TensorFlow:如何从两个数组生成数据集?

TensorFlow: How do I generate a dataset from two arrays?

我一直在尝试从两个数组生成自定义数据集。一个形状为 (128,128,6)(具有 6 个通道的卫星数据),另一个形状为 (128,128,1)(二进制掩码)。我一直在使用函数 tf.data.Dataset.from_tensor_slices:

train_dataset = tf.data.Dataset.from_tensor_slices((train_raster, train_mask))

我得到的是这样的:

<PrefetchDataset element_spec=(TensorSpec(shape=(128, 128, 6), dtype=tf.float32, name=None), TensorSpec(shape=(128, 128, 1), dtype=tf.float32, name=None))>

然而,当我尝试通过我的模型 运行 时,我得到了这个错误:

ValueError ValueError: `Shapes (None, 128, 128, 1) and (None, 2) are incompatible

(None, 2) 因为我的输出是 2 类.

之一

在教程中,我将数据集视为 <PrefetchDataset shapes: ((None, 128, 128, 3), (None, 128, 128, 1)), types: (tf.float32, tf.float32)>。有区别吗?如果有,我该如何解决?似乎只有两个张量之一通过模型 运行,但我不太明白为什么。

模型定义:

model = tf.keras.Sequential([ 
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu, input_shape=(128, 128, 6)), tf.keras.layers.MaxPooling2D((2, 2), strides=2), 
tf.keras.layers.Conv2D(64, (3,3), padding='same', activation=tf.nn.relu), 
tf.keras.layers.MaxPooling2D((2, 2), strides=2), 
tf.keras.layers.Flatten(), 
tf.keras.layers.Dense(128, activation=tf.nn.relu), 
tf.keras.layers.Dense(2, activation=tf.nn.sigmoid) 
])

在回答部分添加 @Kaveh 评论以社区的利益 因为这解决了用户的问题。 (谢谢@Kaveh)

I can guess that your last layer outputs 2 neurons (for binary mask). But since you have specified an array with the shape (128,128,1) in your dataset, it leads to an error. If you just passed the train_dataset to your model, it considers the first part of tuple (first array) as input and the second array (128,128,1) as your labels

If you print your model summary, you will see your model output is just two numbers (None,2). But you need output as (128,128,1). So, your dataset is ok. You need to modify your model architecture. For example an architecture like U-Net.