检查模型输入时出错:预期卷积 2d_input_1 有 4 个维度,但得到形状为 (32, 32, 3) 的数组 [英] Error when checking model input: expected convolution2d_input_1 to have 4 dimensions, but got array with shape (32, 32, 3)

查看:31
本文介绍了检查模型输入时出错:预期卷积 2d_input_1 有 4 个维度,但得到形状为 (32, 32, 3) 的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下层开始训练一个深度网络:

I want to train a deep network starting with the following layer:

model = Sequential()
model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3)))

使用

history = model.fit_generator(get_training_data(),
                samples_per_epoch=1, nb_epoch=1,nb_val_samples=5,
                verbose=1,validation_data=get_validation_data()

使用以下生成器:

def get_training_data(self):
     while 1:
        for i in range(1,5):
            image = self.X_train[i]
            label = self.Y_train[i]
            yield (image,label)

(验证生成器看起来很相似).

(validation generator looks similar).

在训练期间,我收到错误:

During training, I get the error:

Error when checking model input: expected convolution2d_input_1 to have 4 
dimensions, but got array with shape (32, 32, 3)

怎么可能,有第一层

 model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3)))

?

推荐答案

您定义的输入形状是单个样本的形状.模型本身需要一些样本数组作为输入(即使它是一个长度为 1 的数组).

The input shape you have defined is the shape of a single sample. The model itself expects some array of samples as input (even if its an array of length 1).

你的输出真的应该是 4-d,用第一个维度来枚举样本.即对于单个图像,您应该返回 (1, 32, 32, 3) 的形状.

Your output really should be 4-d, with the 1st dimension to enumerate the samples. i.e. for a single image you should return a shape of (1, 32, 32, 3).

您可以在此处在Convolution2D"/"下找到更多信息.输入形状"

You can find more information here under "Convolution2D"/"Input shape"

编辑:根据下面丹尼的评论,如果您想要批量大小为 1,您可以使用以下方法添加缺失的维度:

Edit: Based on Danny's comment below, if you want a batch size of 1, you can add the missing dimension using this:

image = np.expand_dims(image, axis=0)

这篇关于检查模型输入时出错:预期卷积 2d_input_1 有 4 个维度,但得到形状为 (32, 32, 3) 的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆