pplonski / keras2cpp

This is a bunch of code to port Keras neural network model into pure C++.
MIT License
679 stars 153 forks source link

ValueError: The shape of the input to "Flatten" is not fully defined #25

Open muthiyanbhushan opened 6 years ago

muthiyanbhushan commented 6 years ago

Hello @pplonski,

Thank you for this tutorial to convert simple keras to cpp.

when I run the first command to generate ".h5 " and ".json" file I get following error:

I tried changing backends to both Theano and Tensorflow in keras.json file but still could not resolve this error.

$ python example/mnist_cnn_one_iteration.py

error:

Using Theano backend. X_train shape: (60000, 1, 28, 28) 60000 train samples 10000 test samples example/mnist_cnn_one_iteration.py:49: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(4, (3, 3), padding="same", input_shape=(1, 28, 28...) model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='same',input_shape=(1, img_rows, img_cols))) example/mnist_cnn_one_iteration.py:51: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(4, (3, 3), padding="same") model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='same')) Traceback (most recent call last): File "example/mnist_cnn_one_iteration.py", line 56, in model.add(Flatten()) File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 476, in add output_tensor = layer(self.outputs[0]) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 615, in call output_shape = self.compute_output_shape(input_shape) File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 481, in compute_output_shape '(got ' + str(input_shape[1:]) + '. ' ValueError: The shape of the input to "Flatten" is not fully defined (got (0, 14, 4). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.

My keras.json file looks like:

{ "image_dim_ordering": "th", "epsilon": 1e-07, "floatx": "float32", "backend": "theano" }

Please let me know about it.

Thank you.

Regards, Bhushan

pplonski commented 6 years ago

Hi! I think it can be problem with keras 2.0, it is not tested, could you try with keras 1.2.2 ?

muthiyanbhushan commented 6 years ago

Hello pplonski,

I resolved that issue by changing to keras 1.2.2 but now another issue with Theano ifelse condition

$ python example/mnist_cnn_one_iteration.py

Using Theano backend. Downloading data from https://s3.amazonaws.com/img-datasets/mnist.pkl.gz 15302656/15296311 [==============================] - 2s
X_train shape: (60000, 1, 28, 28) 60000 train samples 10000 test samples Traceback (most recent call last): File "example/mnist_cnn_one_iteration.py", line 54, in model.add(Dropout(0.25)) File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 327, in add output_tensor = layer(self.outputs[0]) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 569, in call self.add_inbound_node(inbound_layers, node_indices, tensor_indices) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 632, in add_inbound_node Node.create_node(self, inbound_layers, node_indices, tensor_indices) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 164, in create_node output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0])) File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 90, in call x = K.in_train_phase(K.dropout(x, self.p, noise_shape), x) File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 1120, in in_train_phase x = theano.ifelse.ifelse(_LEARNING_PHASE, x, alt) AttributeError: 'module' object has no attribute 'ifelse'

Thanks for your help.

Bhushan

peacherwu commented 4 years ago

Probably you have resolved this issue, just for the sake of completeness.

You can modify the .../theano_backend.py according to this: https://stackoverflow.com/questions/46253744/getting-error-attributeerror-module-object-has-no-attribute-ifelse