简体   繁体   English

检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 (28708, 1) 的数组

[英]Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (28708, 1)

I have a data set which looks something like:我有一个数据集,看起来像:

  emotion   images
0   0      [[70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, ...
1   0      [[151, 150, 147, 155, 148, 133, 111, 140, 170,...
2   2      [[231, 212, 156, 164, 174, 138, 161, 173, 182,...
3   4      [[24, 32, 36, 30, 32, 23, 19, 20, 30, 41, 21, ...
4   6      [[4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 15, 2...

The emotion column is a categorical variable and images contain numpy array which represent images (size = (48, 48)).情感列是一个分类变量,图像包含代表图像的 numpy 数组(大小 = (48, 48))。

My task is Image Classification, for which I'm using keras.我的任务是图像分类,为此我使用了 keras。

When I try:当我尝试:

model.fit(df['images'], df['emotion'], epochs= 10, batch_size = 32)

I get a Value Error:我收到一个值错误:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (28708, 1) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (28708, 1) 的数组

I understand that fit() expects numpy objects and I have tried using 'df.values' as suggested here .我知道fit()需要 numpy 对象,我尝试使用此处建议的 'df.values' 。 But it doesn't really work for me.但这对我来说真的不起作用。

I'd like to preprocess in a way that also batches my input with size 32. I don't know how to preprocess or reshape my data from here so that I can train it on my network with keras.我想以一种方式进行预处理,同时批量处理大小为 32 的输入。我不知道如何从这里预处理或重塑我的数据,以便我可以使用 keras 在我的网络上训练它。

how do I change my data to have 4 dimensions as is expected by my network?如何将我的数据更改为我的网络预期的 4 个维度?

model.summary()

    _________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_7 (Conv2D)            (None, 46, 46, 64)        640       
_________________________________________________________________
activation_7 (Activation)    (None, 46, 46, 64)        0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 44, 44, 32)        18464     
_________________________________________________________________
activation_8 (Activation)    (None, 44, 44, 32)        0         
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 22, 22, 32)        0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 20, 20, 32)        9248      
_________________________________________________________________
activation_9 (Activation)    (None, 20, 20, 32)        0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 18, 18, 32)        9248      
_________________________________________________________________
activation_10 (Activation)   (None, 18, 18, 32)        0         
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 9, 9, 32)          0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 7, 7, 32)          9248      
_________________________________________________________________
activation_11 (Activation)   (None, 7, 7, 32)          0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 5, 5, 32)          9248      
_________________________________________________________________
activation_12 (Activation)   (None, 5, 5, 32)          0         
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 2, 2, 32)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 128)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               16512     
_________________________________________________________________
activation_13 (Activation)   (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 7)                 903       
_________________________________________________________________
activation_14 (Activation)   (None, 7)                 0         
=================================================================
Total params: 73,511
Trainable params: 73,511
Non-trainable params: 0

My model code:我的型号代码:

model = Sequential()
model.add(Conv2D(64, (3,3), input_shape = (48, 48, 1)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))

model.add(Conv2D(32, (3,3)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))

model.add(Conv2D(32, (3,3)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D (pool_size = (2, 2)))


model.add(Flatten())
model.add(Dense(units = 128))
model.add(Activation('relu'))
model.add(Dense(units= 7))
model.add(Activation('softmax'))
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
  1. You should perform one-hot encoding of labels if you want to use categorical crossentropy.如果您想使用分类交叉熵,您应该对标签执行一次性编码。 So y shape should be equal to (28709, K) , where K - is a number of emotions (I suppose K=7 in your case).所以y形状应该等于(28709, K) ,其中K - 是一些情绪(我想K=7在你的情况下)。
  2. I don't fully understand why python says that df['images'] has shape (28708, 1) .我不完全理解为什么 python 说df['images']有形状(28708, 1) I suppose this column is interpreted as 1D array of lists.我想这个列被解释为一维列表数组。 If I right, you have to convert df['images'] to numpy 3d array in proper way.如果我是对的,您必须以正确的方式将df['images']转换为 numpy 3d 数组。
  3. You also have to add extra channels dimension at the end of resultin 3D array.您还必须在 resultin 3D 数组的末尾添加额外的通道维度。 I supppose your images are grayscale.我假设你的图像是灰度的。 That's why in your case last dimension size is equal to 1.这就是为什么在您的情况下最后一个维度大小等于 1。

To fix issues 2-3 you have to perform smth like this:要解决问题 2-3,您必须像这样执行 smth:

np.expand_dims(np.array(df['images'].tolist()), axis=3)

You can try something like this and then fit() the data你可以尝试这样的事情,然后 fit() 数据

df['images'] = np.array(df['images'].values.tolist()).reshape((-1, 48, 48, 1)

It changes the shape from (28708,1) to (28708, 48, 48, 1) since conv2d expects 4D array [ total_Items, rows, cols, channel ]它将形状从(28708,1)更改为(28708, 48, 48, 1)因为 conv2d 需要 4D 数组 [ total_Items, rows, cols, channel ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了具有形状的数组 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组具有形状(无,1) - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (None, 1) Keras model.predict检查输入时出错:预期conv2d_input具有4维,但数组的形状为(128,56) - Keras model.predict Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (128, 56) 未捕获的错误:检查时出错:预期conv2d_input具有4维,但数组的形状为[275,183,3] - Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [275,183,3] ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但得到的形状为数组(8020,1) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (8020, 1) 检查模型输入时出错:预期conv2d_175_input具有4个维,但数组的形状为(0,1) - Error when checking model input: expected conv2d_175_input to have 4 dimensions, but got array with shape (0, 1) 检查输入时出错:预期 conv2d_4_input 有 4 个维度,但得到了形状为 (32, 32) 的数组 - Error when checking input: expected conv2d_4_input to have 4 dimensions, but got array with shape (32, 32) ValueError:检查输入时出错:预期conv3d_1_input具有5个维,但数组的形状为(7,9,384,1) - ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (7, 9, 384, 1) ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions 使用 conv1D “检查输入时出错:预期 conv1d_input 有 3 个维度,但得到了形状为 (213412, 36) 的数组” - Using conv1D “Error when checking input: expected conv1d_input to have 3 dimensions, but got array with shape (213412, 36)”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM