簡體   English   中英

在 keras python 中安裝 model 時檢查 model 輸入時出錯

[英]Error when checking model input when fitting model in keras python

i am trying to create a keras NN which work with images when i try to fit the model i get this error Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. 預計會看到 1 個數組,但得到了以下 10 個 arrays 列表:[array([[[ 69, 71, 73, ..., 63, 70, 70],

這是為什么?

train_size = 10
test_size = 100 
validation_size = 50
height = 50
width = 50


class ImageOperation:
    @staticmethod
    def grayImg(image_obj: np.ndarray):
        return cv2.cvtColor(image_obj, cv2.COLOR_BGR2GRAY)

    @staticmethod
    def colorImg(path: str):
        return cv2.imread(path)

    @staticmethod
    def resizeImage(img: np.ndarray, height: int, width: int):
        return cv2.resize(img, (height, width))

# load images
train_path = r"D:/Study/200-200/train/train"

train_images = [ImageOperation.resizeImage(ImageOperation.colorImg(train_path + str(i) + ".jpg"),height,width) for i in range(train_size)]

y_train_red = [np.array(img[:, :, 2]/255).flatten() for img in train_images]

train_images = [np.expand_dims(ImageOperation.grayImg(item), axis=0) for item in train_images]


model1 = Sequential()
model1.add(Conv2D(64, (3,3), activation='relu', padding='same', strides=2,input_shape=(1,50,50)))
model1.add(Conv2D(128, (3,3), activation='relu', padding='same', strides=2))
model1.add(UpSampling2D((2, 2)))
model1.add(Flatten())
model1.add(Dense(height*width, activation='tanh'))
model1.compile(optimizer='adam', loss='mse')
clean_images = model1.fit(train_images,y_train_red, epochs=10)

只需將您的y_train_redtrain_images轉換為 np.ndarray:

y_train_red = [np.array(img[:, :, 2]/255).flatten() for img in train_images]
y_train_red = np.array(y_train_red)

train_images = [np.expand_dims(ImageOperation.grayImg(item), axis=0) for item in train_images]
train_images = np.array(train_images)

暫無
暫無

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

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