如何在 Keras 中处理 RGB 图像



我正在尝试使用手动策划的图像数据集和相关标签来训练一个简单的神经网络。

我创建了一个 numpy 来创建名为 facey_label 的标签。

我使用 matplotlib 的 imread 函数将 811 张图像中的每一个转换为形状数组 (255, 255, 3

),然后计划使用 np.array 函数创建一个形状的张量img_array (811, 255, 255, 3)

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(811, 255, 255, 3)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(img_array, facey_label, epochs=5)

但是,我收到错误:

ValueError: Error when checking input: expected flatten_1_input to have 5 dimensions, but got array with shape (811, 250, 250, 3)

我做错了什么?

不应在input_shape中包含批大小。请尝试使用此模型:

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(255, 255, 3)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

最新更新