When I try to define my model, I get the following error message: ValueError: Negative dimension size caused by subtracting 5 from 1 for 'conv2d_19/convolution' (op: 'Conv2D') with input shapes: [?,1,1,32], [5,5,32,64].
Here is a code snippet to reproduce the issue:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, AveragePooling2D
import numpy as np
nb_filters = 32
batch_size = 128
epochs = 5
nb_classes = 10
pool_size = (3, 3)
kernel_size = (5, 5)
img_rows = 32
img_cols = 32
input_shape = (img_rows, img_cols, 1)
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size,
input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Convolution2D(nb_filters, kernel_size))
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=pool_size))
model.add(Convolution2D(nb_filters*2, kernel_size))
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=pool_size))
model.add(Flatten())
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
The issue has been observed on MacOS, with Python 2.7.11 and Keras 2.1.1.
Is there something wrong in the way I build my model?
Here is the architecture I want to build:
- Convolutional layer with 32 filters of size 5 × 5, max-pooling over 3 × 3 regions, and ReLU activations
- Convolutional layer with 32 filters of size 5 × 5, ReLU activations, and average-pooling over 3 × 3 regions
- Convolutional layer with 64 filters of size 5 × 5, ReLU activations, and average-pooling over 3 × 3 regions
- Fully connected layer with softmax.