簡體   English   中英

如何在 CNN 訓練的 model 中輸入單個圖像?

[英]How can I input single Image in CNN trained model?

我訓練了 CNN model 並嘗試使用單個圖像進行測試。 我保存了 .h5 文件並嘗試使用單個圖像進行測試。 但我收到如下錯誤消息。

ValueError:層序 1 的輸入 0 與層不兼容:輸入形狀的預期軸 -1 具有值 3,但接收到的輸入形狀為 (None, 48, 48, 1)

誰能幫我調整這個輸入數據到我的 model 嗎?

以下是我的 model 零件:

def create_model(x=None):
    # we initialize the model
    model = Sequential()

    # Conv Block 1
    model.add(Conv2D(64, (3, 3), input_shape=(48,48,3),  padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(64, (3, 3),   padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(64, (3, 3),  padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) ....

我閱讀了圖像並將其重新塑造如下:

face_image = cv2.resize(face_image, (48,48))
face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY)
face_image = np.reshape(face_image, [1, face_image.shape[0], face_image.shape[1], 1])

最后,我將重塑后的圖像放入 model 中,如下所示:

predicted_class = np.argmax(model.predict(face_image))

我該如何處理?

您已經使用 RGB 圖像(3 通道)訓練了 model,但嘗試在灰度上進行推理。 嘗試這個

face_image = cv2.resize(face_image, (48,48))
face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
face_image = np.reshape(face_image, [1, face_image.shape[0], face_image.shape[1], 3])
predicted_class = np.argmax(model.predict(face_image), -1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM