【问题标题】:How to handle RGB images in Keras如何在 Keras 中处理 RGB 图像
【发布时间】:2019-01-30 10:54:28
【问题描述】:

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

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

我已经使用 matplotlib 的 imread 函数将 811 个图像中的每一个转换为一个形状为 (255, 255, 3) 的数组,然后计划使用 np.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)

我做错了什么?

【问题讨论】:

    标签: image machine-learning keras


    【解决方案1】:

    您不应在 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)
    ])
    

    【讨论】:

    • 批量大小的“占位符”可以用keras.layers.Flatten(input_shape=(None, 255, 255, 3))显式放置。
    • 它有效,但内存不足。是时候调整图像了。不过,谢谢你们。
    猜你喜欢
    • 2021-02-10
    • 1970-01-01
    • 2019-06-11
    • 2019-03-20
    • 2017-04-23
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    相关资源
    最近更新 更多