簡體   English   中英

ValueError:檢查輸入時出錯:預期 conv2d_9_input 的形狀為 (64, 64, 3) 但得到的數組的形狀為 (32, 32, 1)

[英]ValueError: Error when checking input: expected conv2d_9_input to have shape (64, 64, 3) but got array with shape (32, 32, 1)

import cv2
import tensorflow as tf

CATEGORIES = ["Dog", "Cat"]  # will use this to convert prediction num to string value


def prepare(filepath):
    IMG_SIZE = 32  # 50 in txt-based
    img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)  # read in the image, convert to grayscale
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))  # resize image to match model's expected sizing
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)  # return the image with shaping that TF wants.

import pickle 

with open ('module','rb') as f:
     model=pickle.load(f)

prediction = model.predict([prepare('dog.5000.jpg')])
print(prediction)  # will be a list in a list.
print(CATEGORIES[int(prediction[0][0])])

當我執行此代碼prediction =model.predict([prepare('dog.5000.jpg')])我收到錯誤 ValueError:

檢查輸入時出錯:預期 conv2d_9_input 的形狀為 (64, 64, 3) 但得到的數組的形狀為 (32, 32, 1)

首先,您需要了解您的網絡除了 64x64 圖像,而不是 32x32 圖像,更改您的

IMG_SIZE變量為64而不是32

其次,網絡除了輸入圖像要着色而不是灰度,因此通道數應該是 3,而不是 1,用於移除

來自這一行的cv2.IMREAD_GRAYSCALE img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)

總之,這是你的新 prepare_image function

def prepare(filepath):
    IMG_SIZE = 64
    img_array = cv2.imread(filepath)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)

改變這個:

IMG_SIZE = 32
new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

為了這:

IMG_SIZE = 64
new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)

暫無
暫無

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

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