jacobgil / keras-dcgan

Keras implementation of Deep Convolutional Generative Adversarial Networks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ValueError: Negative dimension size caused by subtracting 2 from 1

xin-xinhanggao opened this issue · comments

When I type python dcgan.py --mode train --batch_size 32, I will see this
File "dcgan.py", line 167, in
train(BATCH_SIZE=args.batch_size)
File "dcgan.py", line 80, in train
discriminator = discriminator_model()
File "dcgan.py", line 41, in discriminator_model
model.add(MaxPooling2D(pool_size=(2, 2)))
File "/usr/local/lib/python2.7/site-packages/keras/models.py", line 312, in add
output_tensor = layer(self.outputs[0])
File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 514, in call
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 572, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 149, in create_node
output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
File "/usr/local/lib/python2.7/site-packages/keras/layers/pooling.py", line 162, in call
dim_ordering=self.dim_ordering)
File "/usr/local/lib/python2.7/site-packages/keras/layers/pooling.py", line 212, in _pooling_function
border_mode, dim_ordering, pool_mode='max')
File "/usr/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1761, in pool2d
x = tf.nn.max_pool(x, pool_size, strides, padding=padding)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 850, in max_pool
name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1440, in _max_pool
data_format=data_format, name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2382, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1783, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 596, in call_cpp_shape_fn
raise ValueError(err.message)
ValueError: Negative dimension size caused by subtracting 2 from 1
I wonder how to fix it.

The model assumes theano dimension ordering.
You have a few options:

  1. You can use theano, or change to theano dimension ordering in ~/.keras/keras.json to "image_dim_ordering": "th", like this:
    {
    "image_dim_ordering": "th",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "theano"
    }
  2. Change the discriminator input_shape from (1, 28, 28) to (28, 28, 1). I think that should be enough.

Please confirm if that solved the problem. If so I will add it to the REDAME.

Thanks for your reply.
The first method works!
But the second one fails.
When I type the command, I see this
Using TensorFlow backend.
('Epoch is', 0)
('Number of batches', 1875)
Traceback (most recent call last):
File "dcgan.py", line 167, in
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

Ok, probably another place in the code that requires changing to work with tensorflow.

Maybe should also change this line:
model.add(Reshape((128, 7, 7), input_shape=(128_7_7,)))

Updated the readme with the theano dimension ordering requirment, closing this issue.