Reputation: 5557
I'm following this github repository for implementing simple object detection. In that repository, no convolution layers are used, but I want to add two convolution layers before Dense layer. So I've changed the code of model
as following:
def baseline_model():
# create model
model = Sequential()
model.add(Conv2D(32, (2,2), input_shape=(1, 16, 16), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(32, (2,2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(2, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
And also I've changed the dimensions of images from 8*8
to 16*16
.
So here my dimension of the input data set will be (40000* 16 * 16), Where 40000 is number of images.
Now I can not decide whether to use conv1D
or conv2D
. I've read this keras official docs, but can't understand much about relationship between dimension of conv. layer and input data shape.
If I use above model configuration, then I'm getting following error:
Error when checking input: expected conv2d_21_input to have 4 dimensions, but got array with shape (40000, 16, 16)
So what I'm missing here? And in general how to decide whether to use conv1D
or conv2D
? Thanks for help I'm complete newbie in this field.
Upvotes: 2
Views: 2443
Reputation: 86600
You have to shape your images properly. All convolutional layers expect an additional dimension for channels
.
RGB images have 3 channels. But if you don't have channels, actually you have 1 channel then. You must make this appear in your data for it to work:
#if using channels_last - the default configuration in keras
x_train = x_train.reshape(40000,16,16,1)
#if using channels_first
x_train = x_train.reshape(40000,1, 16,16)
Notice that the input_shape
parameter must match exactly the shape of the data, excluding the batch size (40000).
For images, you're certainly going to use 2D convolutions (unless you've got some fancy non standard ideas). The 1D convolutions are good for sequences. It's pretty simple:
All of them will have channels
, though, adding an extra dimension to the input.
The 2D convolutional layers expect two possibilities, depending on your keras configuration:
(BatchSize, pixelsX, pixelsY, channels)
(BatchSize, channels, pixelsX, pixelsY)
You don't pass the batch size to input_shape
, so you may use one of these:
#channels last (if you have 1 channel only, but if you have RGB, use 3 instead of 1
model.add(Conv2D(32, (2,2), input_shape=(16, 16, 1), activation='relu'))
#channels first (if you have 1 channel only, but if you have RGB, use 3 instead of 1
model.add(Conv2D(32, (2,2), input_shape=(1,16, 16), activation='relu'))
You can find your keras default configuration in the file located in <yourUserFolder>/.keras/keras.json
.
You can also pass an individual data_format
parameter to each convolutional layer if you need.
Upvotes: 8