简体   繁体   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. 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. Expected to see 1 array(s), but instead got the following list of 10 arrays: [array([[[ 69, 71, 73, ..., 63, 70, 70],预计会看到 1 个数组,但得到了以下 10 个 arrays 列表:[array([[[ 69, 71, 73, ..., 63, 70, 70],

why is that?这是为什么?

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)

Just convert your y_train_red and train_images to np.ndarray:只需将您的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