wizard
wizard

Reputation: 47

What should be the shape of the validation data?

Here is the training data shape and validation data shape.

print(data.shape)
print(val_data.shape)

(20000, 120, 120, 1)
(4946, 120, 120, 1)

Which means i have 20000 training data and 4946 validation data with the picture height and width of 120. In the first conv layer i am supposed to pass data in the shape of input_shape = data.shape[1:] (120, 120, 1) which i have done here. In terms of the validation data inside model.fit() function, when i pass validation_data=(val_data.shape[1:], val_label) is giving error but when i pass validation_data=(val_data.shape, val_label) it works. I am a bit confused about this. Sorry is this is a minor query but as i am new to CNN, i am having trouble. I have few questions.

  1. How is the model being trained and also giving validation accuracy with this parameter validation_data=(val_data.shape, val_label)?
  2. Shouldn't i need to change the shape of the validation data similar to the training data as i did in the first conv layer?
  3. How can i change the validation data in the shape of val_data.shape[1:]?

Here is the complete code of the model.

model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), padding='same', activation="relu", input_shape = data.shape[1:])) 
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2))) 

model.add(Conv2D(64, kernel_size=(3,3), padding='same', strides=(1,1), activation="relu") )
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2))) #max pool window 2x2

model.add(Conv2D(128, kernel_size=(3,3), padding='same', strides=(1,1), activation="relu"))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2))) #max pool window 2x2

model.add(Conv2D(256, kernel_size=(3,3), padding='same', strides=(1,1), activation="relu"))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2))) #max pool window 2x2

model.add(Conv2D(512, kernel_size=(3,3), padding='same', strides=(1,1), activation="relu"))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))

model.add(Flatten()) 

model.add(Dense(512, activation="relu", name='firstDenseLayer'))
model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation="sigmoid"))

model.summary()

model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

model.fit(data, label, batch_size=16, epochs=10, validation_data=(val_data.shape[1:], val_label))

Upvotes: 0

Views: 764

Answers (2)

Gerry P
Gerry P

Reputation: 8112

model.fit expects you to provide validation data in the form of a tuple (data,label). Your validation data is already in the correct shape. model.fit expects the validation data to have the SAME dimensions as the training data for height, width, bands. If it is not the same it will throw an error.

Upvotes: 1

Andrey
Andrey

Reputation: 6377

You are using .fit() incorrectly. You have to pass data not shape:

model.fit(data, label, batch_size=16, epochs=10, validation_data=(val_data, val_label))

Upvotes: 1

Related Questions