The following code generates no error on a virtualbox Ubuntu 14.04 (CPU only). The code is an tutorial from www.pyimagesearch.com
from sklearn.cross_validation import train_test_split
from sklearn import datasets
from keras.optimizers import SGD
from keras.utils import np_utils
import numpy as np
import argparse
import cv2
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
def LeNet_build(width, height, depth, classes, weightsPath=None):
print width, height, depth
model = Sequential()
# first set of CONV => RELU => POOL
model.add(Convolution2D(20, 5, 5, border_mode="same",
input_shape=(depth, height, width)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# second set of CONV => RELU => POOL
model.add(Convolution2D(50, 5, 5, border_mode="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# set of FC => RELU layers
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
# softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
# if a weights path is supplied (inicating that the model was
# pre-trained), then load the weights
if weightsPath is not None:
model.load_weights(weightsPath)
# return the constructed network architecture
return model
dataset = datasets.fetch_mldata("MNIST Original")
# reshape the MNIST dataset from a flat list of 784-dim vectors, to
#28 x 28 pixel images, then scale the data to the range [0, 1.0]
# and construct the training and testing splits
data = dataset.data.reshape((dataset.data.shape[0], 28, 28))
data = data[:, np.newaxis, :, :]
(trainData, testData, trainLabels, testLabels) = train_test_split(
data / 255.0, dataset.target.astype("int"), test_size=0.33)
# transform the training and testing labels into vectors in the
# range [0, classes] -- this generates a vector for each label,
# where the index of the label is set to `1` and all other entries
# to `0`; in the case of MNIST, there are 10 class labels
trainLabels = np_utils.to_categorical(trainLabels, 10)
testLabels = np_utils.to_categorical(testLabels, 10)
# initialize the optimizer and model
print("[INFO] compiling model...")
opt = SGD(lr=0.01)
model = LeNet_build(width=28, height=28, depth=1, classes=10, weightsPath=None)
However, it generates the following error inside a fresh installed Ubuntu 14.04 with Titan X Pascal and Cuda 8.0 (tested both on CPU and GPU - Theano backend) - the same error is reported before and remained unsolved (https://github.com/fchollet/keras/issues/1592):
Exception Traceback (most recent call last)
<ipython-input-3-c61646d5fa78> in <module>()
23 print("[INFO] compiling model...")
24 opt = SGD(lr=0.01)
---> 25 model = LeNet_build(width=28, height=28, depth=1, classes=10, weightsPath=None)
<ipython-input-2-a36c66483235> in LeNet_build(width, height, depth, classes, weightsPath)
16
17 # set of FC => RELU layers
---> 18 model.add(Flatten())
19 model.add(Dense(500))
20 model.add(Activation("relu"))
/home/hadi/.virtualenvs/keras/local/lib/python2.7/site-packages/keras/models.pyc in add(self, layer)
306 output_shapes=[self.outputs[0]._keras_shape])
307 else:
--> 308 output_tensor = layer(self.outputs[0])
309 if type(output_tensor) is list:
310 raise Exception('All layers in a Sequential model '
/home/hadi/.virtualenvs/keras/local/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask)
512 if inbound_layers:
513 # this will call layer.build() if necessary
--> 514 self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
515 input_added = True
516
/home/hadi/.virtualenvs/keras/local/lib/python2.7/site-packages/keras/engine/topology.pyc in add_inbound_node(self, inbound_layers, node_indices, tensor_indices)
570 # creating the node automatically updates self.inbound_nodes
571 # as well as outbound_nodes on inbound layers.
--> 572 Node.create_node(self, inbound_layers, node_indices, tensor_indices)
573
574 def get_output_shape_for(self, input_shape):
/home/hadi/.virtualenvs/keras/local/lib/python2.7/site-packages/keras/engine/topology.pyc in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices)
150 output_masks = to_list(outbound_layer.compute_mask(input_tensors[0], input_masks[0]))
151 # TODO: try to auto-infer shape if exception is raised by get_output_shape_for
--> 152 output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes[0]))
153 else:
154 output_tensors = to_list(outbound_layer.call(input_tensors, mask=input_masks))
/home/hadi/.virtualenvs/keras/local/lib/python2.7/site-packages/keras/layers/core.pyc in get_output_shape_for(self, input_shape)
400 raise Exception('The shape of the input to "Flatten" '
401 'is not fully defined '
--> 402 '(got ' + str(input_shape[1:]) + '. '
403 'Make sure to pass a complete "input_shape" '
404 'or "batch_input_shape" argument to the first '
Exception: The shape of the input to "Flatten" is not fully defined (got (0, 7, 50). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
Update: I have checked all of my previous Keras codes which were working in the virtualbox and all of them give the same error:
Exception: The shape of the input to "Flatten" is not fully defined (got (0, 7, 512). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
Had a similar issue. In my case, the one machine was set up to use tensorflow image_dim_ordering and the other using the theano ordering. Somehow the install routines selected different defaults.
Compare the file ~/.keras/keras.json between your workstations. If they differ set image_dim_ordering to "th" (your tutorial seems to be using the theano backend) and try again.
You saved my day... image_dim_ordering was 'tf'
I had the same problem and I checked all of comments, but my problem did not solved!!
@MaryamAkhavan have you checked your ~/.keras/keras.json file? Are you sure if both backend and image_dim_ordering are set to the same library? they both should be Tensorflow , TF or Theano, TH
Yes, I checked. Backend is theano and image_dim_ordering is (chanels, dim1, dim2)
Are you running the same example? If not, please post the model construction and the error message.
I ran both the example of Simple Convolutional Neural Network for MNIST located at this link : http://machinelearningmastery.com/handwritten-digit-recognition-using-convolutional-neural-networks-python-keras/
and my code. Both examples have the same error.
Please post the error here...
I think this issue can closed, it's due to not having either:
Theano
as backendimage_dim_ordering
in the ~/.keras/keras.json
file.'{
"image_dim_ordering": "th",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "theano"
}'
relevant issue #1592
This two lines solved my issue
from keras import backend as K
K.set_image_dim_ordering('th')
I'm having a similar problem, but I don't see K.set_image_dim_ordering
. I see a image_data_format": "channels_last" however.
yes thats because in keras 2 it is image_data_format instead of
image_dim_ordering
On Tue, Apr 25, 2017 at 8:19 PM, Moondra notifications@github.com wrote:
I'm having a similar problem, but I don't see K.set_image_dim_ordering. I
see a image_data_format": "channels_last" however.—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/3850#issuecomment-297054848,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AT2bYa6YHA1rrfvLiC-yM7FP1IfeyEeeks5rzggIgaJpZM4KEQM-
.
--
Best Regards,
Anuj Shah
([email protected])
Thanks. Yeah, I had to manually delete the json file before reinstalling Keras 1.2.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
I have tensorflow backend both in my json configuration file and it is what keras outputs on import. My model is super simple, and i still get this error:
model = Sequential()
model.add(InputLayer(input_shape=[512,512,1]))
model.add(Flatten())
model.add(Dense(n_classes, activation='relu', ))
What is the problem? What to do if I need to use TF, not Theano?
@DSLituiev Maybe it's the channel related issue, did you try including this on top of the model:
from keras import backend as K
K.set_image_dim_ordering('tf')
check here if that's the issue (https://keras.io/backend/)
@0bserver07: thank you! That was not the issue.
My bad! I fed tensors from TFRecords
pipeline with undefined shapes.
Most helpful comment
Had a similar issue. In my case, the one machine was set up to use tensorflow image_dim_ordering and the other using the theano ordering. Somehow the install routines selected different defaults.
Compare the file ~/.keras/keras.json between your workstations. If they differ set image_dim_ordering to "th" (your tutorial seems to be using the theano backend) and try again.