Keras CNN layers are throwing a ValueError

This is my first time working with CNNs and I’m getting the following error:

ValueError: Negative dimension size caused by subtracting 5 from 1 for 'conv2d_5/convolution' (op: 'Conv2D') with input shapes: [?,1,18,64], [5,5,64,64].

It is pointing to these lines in my code:

def build_model(args):
    model = Sequential()
    model.add(Lambda(lambda x: x / 127.5 - 1.0, input_shape=INPUT_SHAPE))
    model.add(Conv2D(24, (5, 5), activation='elu', strides=(2, 2)))
    model.add(Conv2D(36, (5, 5), activation='elu', strides=(2, 2)))
    model.add(Conv2D(48, (5, 5), activation='elu', strides=(2, 2)))
    model.add(Conv2D(64, (5, 5), activation='elu')) # These two lines
    model.add(Conv2D(64, (5, 5), activation='elu')) # These two lines
    model.add(Dense(100, activation='elu'))
    model.add(Dense(50, activation='elu'))
    model.add(Dense(10, activation='elu'))

    return model

The strides are 1,1 so I assume that (5,5) is causing a negative value but I don’t know how to solve that. This is my full stacktrace:

Using TensorFlow backend.
data_dir             := data
test_size            := 0.2
keep_prob            := 0.5
nb_epoch             := 10
samples_per_epoch    := 20000
batch_size           := 40
save_best_only       := True
learning_rate        := 0.0001
2020-02-10 14:02:04.502287: I tensorflow/core/platform/] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.
2020-02-10 14:02:04.525190: I tensorflow/core/platform/profile_utils/] CPU Frequency: 2294705000 Hz
2020-02-10 14:02:04.525521: I tensorflow/compiler/xla/service/] XLA service 0x56194f263770 executing computations on platform Host. Devices:
2020-02-10 14:02:04.525547: I tensorflow/compiler/xla/service/]   StreamExecutor device (0): Host, Default Version
2020-02-10 14:02:04.527056: I tensorflow/core/common_runtime/] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Traceback (most recent call last):
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/", line 1610, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 5 from 1 for 'conv2d_5/convolution' (op: 'Conv2D') with input shapes: [?,1,18,64], [5,5,64,64].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/onur/Documents/behavirol-cloning-carla/", line 91, in <module>
  File "/home/onur/Documents/behavirol-cloning-carla/", line 86, in main
    model = build_model(args)
  File "/home/onur/Documents/behavirol-cloning-carla/", line 32, in build_model
    model.add(Conv2D(64, (5, 5), activation='elu'))
  File "/home/onur/anaconda3/lib/python3.7/site-packages/keras/engine/", line 182, in add
    output_tensor = layer(self.outputs[0])
  File "/home/onur/anaconda3/lib/python3.7/site-packages/keras/backend/", line 75, in symbolic_fn_wrapper
    return func(*args, **kwargs)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/keras/engine/", line 489, in __call__
    output =, **kwargs)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/keras/layers/", line 171, in call
  File "/home/onur/anaconda3/lib/python3.7/site-packages/keras/backend/", line 3717, in conv2d
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/", line 917, in convolution_v2
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/", line 1009, in convolution_internal
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/", line 1071, in conv2d
    data_format=data_format, dilations=dilations, name=name)
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/", line 793, in _apply_op_helper
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/", line 548, in create_op
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/", line 3429, in _create_op_internal
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/", line 1773, in __init__
  File "/home/onur/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/", line 1613, in _create_c_op
    raise ValueError(str(e))
ValueError: Negative dimension size caused by subtracting 5 from 1 for 'conv2d_5/convolution' (op: 'Conv2D') with input shapes: [?,1,18,64], [5,5,64,64].