0

I have train & test image data for which Shapes are given below.

X_test.shape , y_test.shape , X_train.shape , y_train.shape
    ((277, 128, 128, 3), (277, 1), (1157, 128, 128, 3), (1157, 1))

I am training a model

def baseline_model():
    filters = 100
    model = Sequential()
    model.add(Conv2D(filters, (3, 3), input_shape=(128, 128, 3), padding='same', activation='relu'))
    #model.add(Dropout(0.2))
    model.add(BatchNormalization())
    model.add(Conv2D(filters, (3, 3), activation='relu', padding='same'))
    model.add(BatchNormalization())

    model.add(MaxPooling2D(pool_size=(2, 2)))
    #model.add(Flatten())

    model.add(Conv2D(filters, (3, 3), activation='relu', padding='same'))
    model.add(BatchNormalization())
    model.add(Conv2D(filters, (3, 3), activation='relu', padding='same'))
    model.add(Activation('linear'))
    model.add(BatchNormalization())

    model.add(Dense(512, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    # Compile model
    lrate = 0.01
    epochs = 10
    decay = lrate/epochs
    sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
    model.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

    print(model.summary())
    return model

But I am getting an error Given below

Error when checking target: expected dense_35 to have 4 dimensions, but got array with shape (1157, 1)

Please tell me what mistake I am making and how to fix this. I have attached snapshot of model summary enter image description here

today
  • 32,602
  • 8
  • 95
  • 115
Bharat Sharma
  • 1,081
  • 3
  • 11
  • 23

2 Answers2

1

One thing you have probably forgotten to do is adding a Flatten layer right before the first Dense layer:

model.add(BatchNormalization())

model.add(Flatten()) # flatten the output of previous layer before feeding it to Dense layer
model.add(Dense(512, activation='relu'))

You need it because Dense layer does not flatten its input; rather, it is applied on the last dimension.

today
  • 32,602
  • 8
  • 95
  • 115
0

Although dense_35 needs to feed with 4 dimension data, according to the error, the network feed with 2 dimension data which is the label vector.

Can
  • 145
  • 1
  • 11