jacobgil / keras-dcgan

Keras implementation of Deep Convolutional Generative Adversarial Networks
973 stars 413 forks source link

ValueError: Negative dimension size caused by subtracting 2 from 1 #8

Closed xin-xinhanggao closed 8 years ago

xin-xinhanggao commented 8 years ago

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.

jacobgil commented 8 years ago

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.
jacobgil commented 8 years ago

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

xin-xinhanggao commented 8 years ago

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

jacobgil commented 8 years ago

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

xin-xinhanggao commented 8 years ago

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

jacobgil commented 8 years ago

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