Reputation: 11
Using Keras and Tensorflow with a 28x28x1 dataset. when I run the following code, it works just fine:
model = Sequential()
model.add(Convolution2D(8, 3, strides = 3, activation='relu', input_shape=(28,28,1),data_format = 'channels_last',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(16, 3, strides = 3, activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(32, 3, strides = 3, activation='relu',padding='same'))
#model.add(AveragePooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
#Training the model in kreas
history = model.fit(train_dataset_kr,train_labels_kr,epochs = 500, validation_data = (valid_dataset_kr,vaild_labels_kr),batch_size = 256)
#model.train_on_batch(train_dataset_kr,train_labels_kr)
score = model.evaluate(test_dataset_kr, test_labels_kr, verbose=0)
as soon as I put in that final pooling2d(in this case, an AveragePooling2d), I get the following error:
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'average_pooling2d_1/AvgPool' (op: 'AvgPool') with input shapes: [?,1,1,32].
Based on the size of my input file, I think I should be able to do 3 pooling2D. Any thoughts on what I might be doing wrong?
Upvotes: 1
Views: 437
Reputation: 40516
You set way too big strides for your model. Let's check the output shapes of your network:
model = Sequential()
model.add(Convolution2D(8, 3, strides = 3, activation='relu', input_shape=(28,28,1),data_format = 'channels_last',padding='same'))
# Shapes: (28, 28, 1) -> (10, 10, 8)
model.add(MaxPooling2D(pool_size=(2,2)))
# Shapes: (10, 10, 8) - > (5, 5, 8)
model.add(Dropout(0.25))
model.add(Convolution2D(16, 3, strides = 3, activation='relu',padding='same'))
# Shapes: (5, 5, 8) -> (2, 2, 16)
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(32, 3, strides = 3, activation='relu',padding='same'))
# Shapes: (2, 2, 16) -> (1, 1, 32)
#model.add(AveragePooling2D(pool_size=(2,2)))
# (1, 1, 32) - this shape is too small for pooling.
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
Are you sure you want to set your strides
parameter to has value 3
? It decreases output of each layer by a factor of 3
.
Upvotes: 1