在keras python中拟合模型时检查模型输入时出错

时间:2020-05-22 09:37:03

标签: python numpy tensorflow keras

我正在尝试创建一个可处理图像的keras NN 当我尝试拟合模型时,出现此错误 检查模型输入时出错:传递给模型的Numpy数组列表不是模型预期的大小。预计会看到1个数组,但获得了以下10个数组的列表:[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)

1 个答案:

答案 0 :(得分:0)

只需将您的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)