[英]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'])
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
在你的情况下)。df['images']
has shape (28708, 1)
.df['images']
有形状(28708, 1)
。 I suppose this column is interpreted as 1D array of lists.df['images']
to numpy 3d array in proper way.df['images']
转换为 numpy 3d 数组。 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.