如何在 Keras 中处理 RGB 图像

elk*_*000 3 image machine-learning keras

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

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

我已经使用 matplotlib 的 imread 函数将 811 个图像中的每一个都转换为一个形状为 (255, 255, 3) 的数组,然后计划使用 np.array 函数创建一个形状为 (811, 255, 255, 3) 的张量 img_array )

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)
Run Code Online (Sandbox Code Playgroud)

但是,我收到错误:

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

我做错了什么?

Ann*_*ger 5

您不应在 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)
])
Run Code Online (Sandbox Code Playgroud)