Reputation: 149
I'm working on a sign language interpretation using deep learning & for that, i'm building a CNN and i am getting an error like,
ValueError: Error when checking target: expected dense_20 to have shape (24,) but got array with shape (1,)
structure of my neural net:
Layer (type) Output Shape Param #
conv2d_62 (Conv2D) (None, 64, 64, 64) 1088
_________________________________________________________________
conv2d_63 (Conv2D) (None, 32, 32, 64) 65600
_________________________________________________________________
dropout_31 (Dropout) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_64 (Conv2D) (None, 32, 32, 128) 131200
_________________________________________________________________
conv2d_65 (Conv2D) (None, 16, 16, 128) 262272
_________________________________________________________________
dropout_32 (Dropout) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_66 (Conv2D) (None, 16, 16, 256) 524544
_________________________________________________________________
conv2d_67 (Conv2D) (None, 8, 8, 256) 1048832
_________________________________________________________________
flatten_11 (Flatten) (None, 16384) 0
_________________________________________________________________
dropout_33 (Dropout) (None, 16384) 0
_________________________________________________________________
dense_19 (Dense) (None, 512) 8389120
_________________________________________________________________
dense_20 (Dense) (None, 24) 12312
code:
model = Sequential()
model.add(Conv2D(64, kernel_size=4, strides=1, activation='relu', input_shape = (64,64,1),padding = 'same'))
model.add(Conv2D(64, kernel_size=4, strides=2, activation='relu',padding = 'same'))
model.add(Dropout(0.2))
model.add(Conv2D(128, kernel_size=4, strides=1, activation='relu',padding = 'same'))
model.add(Conv2D(128, kernel_size=4, strides=2, activation='relu',padding = 'same'))
model.add(Dropout(0.2))
model.add(Conv2D(256, kernel_size=4, strides=1, activation='relu',padding = 'same'))
model.add(Conv2D(256, kernel_size=4, strides=2, activation='relu',padding = 'same'))
model.add(Flatten())
model.add(Dropout(0.3))
model.add(Dense(512, activation='relu'))
model.add(Dense(24, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, validation_data=(x_test, y_test),batch_size=64,epochs=8)
dimensions of arrays used:
x_train: (3977, 64, 64, 1)
y_train: (3977, 1)
x_test: (995, 64, 64, 1)
y_test: (995, 1)
Upvotes: 0
Views: 87
Reputation: 2331
Your last layer output shape need to match your label's vector shape
So you need to one_hot encode your y_train in order to fit your network.
You can do this like that :
from keras.utils import to_categorical
y_train = to_categorical(y_train, 24)
This will encode each of your label to a vector of size 24 (or whatever you need), filled with 0s and a 1 at the position of the corresponding label.
To learn more about it :
https://keras.io/utils/
Upvotes: 1