Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_1/MaxPool' #14

Open
Instassa opened this issue May 26, 2017 · 11 comments

Comments

@Instassa
Copy link

When I try to run: python dcgan.py --mode train --batch_size 100 I get the following:


Using TensorFlow backend.
dcgan.py:41: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (5, 5), input_shape=(1, 28, 28..., padding="same")`
  input_shape=(1, 28, 28)))
Traceback (most recent call last):
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 671, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "/home/marija/anaconda3/envs/tensorflow-gpu-3.5/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [?,1,28,64].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "dcgan.py", line 169, in <module>
    train(BATCH_SIZE=args.batch_size)
  File "dcgan.py", line 82, in train
    discriminator = discriminator_model()
  File "dcgan.py", line 43, in discriminator_model
    model.add(MaxPooling2D(pool_size=(2, 2)))
  File "/home/marija/.local/lib/python3.5/site-packages/keras/models.py", line 466, in add
    output_tensor = layer(self.outputs[0])
  File "/home/marija/.local/lib/python3.5/site-packages/keras/engine/topology.py", line 585, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/marija/.local/lib/python3.5/site-packages/keras/layers/pooling.py", line 154, in call
    data_format=self.data_format)
  File "/home/marija/.local/lib/python3.5/site-packages/keras/layers/pooling.py", line 217, in _pooling_function
    pool_mode='max')
  File "/home/marija/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 3245, in pool2d
    x = tf.nn.max_pool(x, pool_size, strides, padding=padding)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 1821, in max_pool
    name=name)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1638, in _max_pool
    data_format=data_format, name=name)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2338, in create_op
    set_shapes_for_outputs(ret)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1719, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1669, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [?,1,28,64].

Any ideas what is going wrong here?

@ywy0090
Copy link

ywy0090 commented Jun 2, 2017

I also got these problem.......

@FacelessManipulator
Copy link

This caused by the different version of Keras. You could do the following changes.

def discriminator_model():
    model = Sequential()
    model.add(Convolution2D(
                        64, 5, 5,
                        border_mode='same',
                        input_shape=(28, 28, 1))) #modify this variable

@MacwinWin
Copy link

It doesn't work. After change input_shape to (28, 28, 1), got the following
Traceback (most recent call last): File "dcgan.py", line 167, in <module> train(BATCH_SIZE=args.batch_size) File "dcgan.py", line 105, in train X = np.concatenate((image_batch, generated_images)) ValueError: all the input array dimensions except for the concatenation axis must match exactly

@FacelessManipulator
Copy link

The default parameter of 'data_format' for Conv2d layer and pooling layer is 'channel_last', see [Keras doc] While the input data format is 'channel_first', it causes the conflict. You can either change the parameters, or change the input shape.

This is my version and it works fine in my environment( Keras==2.0.4 && Python==3.6).

@ywy0090
Copy link

ywy0090 commented Jun 16, 2017

I just modified the code to fit tensorflow backend, you can try to use this one.
This is my code(Keras==2.0.4 && Python==3.5)

@ArghyaPal
Copy link

Hi,
An easy fix is:

  1. Open ~/.keras/keras.json file. In Ubuntu do: vi ~/.keras/keras.json
  2. change your input data format from channels_last to channels_first
  3. save it.
  4. Run: python dcgan.py --mode train --batch_size 100

@zhaopanpan73
Copy link

I also got these problem.......,have you solved?

@agcala
Copy link

agcala commented Dec 23, 2017

You need to change the order of the data. Tensorflow uses [samples][height][width][channels] order, while Theano is in reverse order.

@agcala
Copy link

agcala commented Dec 23, 2017

The shape of the input should have the channel last as you are using Tensorflow.

@agcala
Copy link

agcala commented Dec 23, 2017

@MacwinWin You forgot to add the batch size which needs to be the first element.

@knownstranger03
Copy link

When I try to run: python dcgan.py --mode train --batch_size 100 I get the following:


Using TensorFlow backend.
dcgan.py:41: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (5, 5), input_shape=(1, 28, 28..., padding="same")`
  input_shape=(1, 28, 28)))
Traceback (most recent call last):
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 671, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "/home/marija/anaconda3/envs/tensorflow-gpu-3.5/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [?,1,28,64].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "dcgan.py", line 169, in <module>
    train(BATCH_SIZE=args.batch_size)
  File "dcgan.py", line 82, in train
    discriminator = discriminator_model()
  File "dcgan.py", line 43, in discriminator_model
    model.add(MaxPooling2D(pool_size=(2, 2)))
  File "/home/marija/.local/lib/python3.5/site-packages/keras/models.py", line 466, in add
    output_tensor = layer(self.outputs[0])
  File "/home/marija/.local/lib/python3.5/site-packages/keras/engine/topology.py", line 585, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/marija/.local/lib/python3.5/site-packages/keras/layers/pooling.py", line 154, in call
    data_format=self.data_format)
  File "/home/marija/.local/lib/python3.5/site-packages/keras/layers/pooling.py", line 217, in _pooling_function
    pool_mode='max')
  File "/home/marija/.local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 3245, in pool2d
    x = tf.nn.max_pool(x, pool_size, strides, padding=padding)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 1821, in max_pool
    name=name)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1638, in _max_pool
    data_format=data_format, name=name)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2338, in create_op
    set_shapes_for_outputs(ret)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1719, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1669, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [?,1,28,64].

Any ideas what is going wrong here?

The problem you have is very simple. The MaxPooling2D layer in Tf uses padding='valid' as default. When the input shape is defined, model is built alongside checking the architecture, in some cases the architecture raises a computing problem (when few functions are mathematically incooperative).

To solve the problem, you need not change the input shape(HxWxchannel - 224, 224, 1), but just go over to the code line and change the parameter padding='valid' to padding='same'. If there is no padding='valid in your code, then add it as below.

model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))

more info on the topic can be found here - https://www.tensorflow.org/api_docs/python/tf/keras/layers/MaxPool2D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants