IRusanov
IRusanov

Reputation: 13

Negative dimension size caused by subtracting 3 from 1 for 'max_pooling2d_8/MaxPool'

I got this error message while trying to implement AlexNet to solve GTSRB: Negative dimension size caused by subtracting 3 from 1 for 'max_pooling2d_8/MaxPool' Error is located in the block marked with #3

Code:

def buildAlex(width, height, depth, classes, reg=0.0002):
    # initialize the model along with the input shape to be
    # "channels last" and the channels dimension itself
    model = keras.Sequential()
    inputShape = (height, width, depth)
    chanDim = -1

    # Block #1: first CONV => RELU => POOL layer set
    model.add(Conv2D(96, (11, 11), strides=(4, 4),
        input_shape=inputShape, padding="same",
        kernel_regularizer=l2(reg)))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) #1
    model.add(Dropout(0.25))

    # Block #2: second CONV => RELU => POOL layer set
    model.add(Conv2D(256, (5, 5), padding="same",
        kernel_regularizer=l2(reg)))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) #2
    model.add(Dropout(0.25))

    # Block #3: CONV => RELU => CONV => RELU => CONV => RELU
    model.add(Conv2D(384, (3, 3), padding="same",
        kernel_regularizer=l2(reg)))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(Conv2D(384, (3, 3), padding="same",
        kernel_regularizer=l2(reg)))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(Conv2D(256, (3, 3), padding="same",
        kernel_regularizer=l2(reg)))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) #3
    model.add(Dropout(0.25))

    # Block #4: first set of FC => RELU layers
    model.add(Flatten())
    model.add(Dense(4096, kernel_regularizer=l2(reg)))
    model.add(Activation("relu"))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    # Block #5: second set of FC => RELU layers
    model.add(Dense(4096, kernel_regularizer=l2(reg)))
    model.add(Activation("relu"))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    # softmax classifier
    model.add(Dense(classes, kernel_regularizer=l2(reg)))
    model.add(Activation("softmax"))

    # return the constructed network architecture
    return model

modelAlex = buildAlex(28, 28, 1, 43)

Traceback:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1550   try:
-> 1551     c_op = c_api.TF_FinishOperation(op_desc)
   1552   except errors.InvalidArgumentError as e:

InvalidArgumentError: Negative dimension size caused by subtracting 3 from 1 for 'max_pooling2d_8/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,256].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-36-7205823f509c> in <module>
----> 1 modelAlex = buildAlex(28, 28, 1, 43)

<ipython-input-35-e4a6adb73a0a> in buildAlex(width, height, depth, classes, reg)
     36     model.add(Activation("relu"))
     37     model.add(BatchNormalization(axis=chanDim))
---> 38     model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) #3
     39     model.add(Dropout(0.25))
     40 

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    456     self._self_setattr_tracking = False  # pylint: disable=protected-access
    457     try:
--> 458       result = method(self, *args, **kwargs)
    459     finally:
    460       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
    191       # If the model is being built continuously on top of an input layer:
    192       # refresh its output.
--> 193       output_tensor = layer(self.outputs[0])
    194       if len(nest.flatten(output_tensor)) != 1:
    195         raise TypeError('All layers in a Sequential model '

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    660                     not base_layer_utils.is_in_eager_or_tf_function()):
    661                   with auto_control_deps.AutomaticControlDependencies() as acd:
--> 662                     outputs = call_fn(inputs, *args, **kwargs)
    663                     # Wrap Tensors in `outputs` in `tf.identity` to avoid
    664                     # circular dependencies.

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/keras/layers/pooling.py in call(self, inputs)
    246         strides=strides,
    247         padding=self.padding.upper(),
--> 248         data_format=conv_utils.convert_data_format(self.data_format, 4))
    249     return outputs
    250 

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py in max_pool(value, ksize, strides, padding, data_format, name, input)
   3750         padding=padding,
   3751         data_format=data_format,
-> 3752         name=name)
   3753 
   3754 

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_nn_ops.py in max_pool(input, ksize, strides, padding, data_format, name)
   5670   _, _, _op = _op_def_lib._apply_op_helper(
   5671         "MaxPool", input=input, ksize=ksize, strides=strides, padding=padding,
-> 5672                    data_format=data_format, name=name)
   5673   _result = _op.outputs[:]
   5674   _inputs_flat = _op.inputs

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    786         op = g.create_op(op_type_name, inputs, dtypes=None, name=scope,
    787                          input_types=input_types, attrs=attr_protos,
--> 788                          op_def=op_def)
    789       return output_structure, op_def.is_stateful, op
    790 

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in create_op(***failed resolving arguments***)
    463     return super(FuncGraph, self).create_op(
    464         op_type, inputs, dtypes, input_types, name, attrs, op_def,
--> 465         compute_device=compute_device)
    466 
    467   def capture(self, tensor, name=None):

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in create_op(***failed resolving arguments***)
   3294           input_types=input_types,
   3295           original_op=self._default_original_op,
-> 3296           op_def=op_def)
   3297       self._create_op_helper(ret, compute_device=compute_device)
   3298     return ret

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
   1712           op_def, inputs, node_def.attr)
   1713       self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1714                                 control_input_ops)
   1715 
   1716     # Initialize self._outputs.

~/Documents/Environments/my_env/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1552   except errors.InvalidArgumentError as e:
   1553     # Convert to ValueError for backwards compatibility.
-> 1554     raise ValueError(str(e))
   1555 
   1556   return c_op

ValueError: Negative dimension size caused by subtracting 3 from 1 for 'max_pooling2d_8/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,256].

Upvotes: 1

Views: 247

Answers (1)

thushv89
thushv89

Reputation: 11333

Well, makes sense doesn't it?

Original AlexNet operates on (224,224,3) sized images. Here, you are passing (28,28,1) which is way small. What the error's saying is that not far into your model, it ends up with an input size < 0. To be more specific, here's how the output get reduced (height, width) dimensions.

               (28, 28)
                   |
                   V
    model.add(Conv2D(96, (11, 11), strides=(4, 4), ...)
                   |
                   V
                (7, 7)
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) #1
                   |
                   V
                (3, 3)
    model.add(Conv2D(256, (5, 5), padding="same",
        kernel_regularizer=l2(reg)))
    model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) #2
                   |
                   V
                (1, 1)

    model.ad(Dropout(0.25))

    # Block #3: CONV => RELU => CONV => RELU => CONV => RELU
    # Block #3: CONV => RELU => CONV => RELU => CONV => RELU
    model.add(Conv2D(384, (3, 3), padding="same",
        kernel_regularizer=l2(reg)))
    model.add(Conv2D(384, (3, 3), padding="same",
        kernel_regularizer=l2(reg)))
    model.add(Conv2D(256, (3, 3), padding="same",
        kernel_regularizer=l2(reg)))
(Error) --->model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) #3
    model.add(Dropout(0.25))

Here, this last MaxPooling2D layer cannot operate with stride 2 on a (1,1) sized image. Thus the error. And also remember that MaxPooling2D has padding='valid' by default.

Remedies:

  • Increase your input size,
  • Get rid of some Maxpooling2D layers.

Upvotes: 1

Related Questions