简体   繁体   中英

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)).

My task is Image Classification, for which I'm using 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)

I understand that fit() expects numpy objects and I have tried using 'df.values' as suggested here . 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.

how do I change my data to have 4 dimensions as is expected by my network?

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).
  2. I don't fully understand why python says that df['images'] has shape (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.
  3. You also have to add extra channels dimension at the end of resultin 3D array. I supppose your images are grayscale. That's why in your case last dimension size is equal to 1.

To fix issues 2-3 you have to perform smth like this:

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

You can try something like this and then fit() the data

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 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM