Simplicity
Simplicity

Reputation: 48966

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

I'm trying to run this code, but getting the following error:

Using TensorFlow backend.
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "BestSplits" device_type: "CPU"') for unknown op: BestSplits
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "CountExtremelyRandomStats" device_type: "CPU"') for unknown op: CountExtremelyRandomStats
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "FinishedNodes" device_type: "CPU"') for unknown op: FinishedNodes
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "GrowTree" device_type: "CPU"') for unknown op: GrowTree
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ReinterpretStringToFloat" device_type: "CPU"') for unknown op: ReinterpretStringToFloat
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "SampleInputs" device_type: "CPU"') for unknown op: SampleInputs
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ScatterAddNdim" device_type: "CPU"') for unknown op: ScatterAddNdim
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNInsert" device_type: "CPU"') for unknown op: TopNInsert
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNRemove" device_type: "CPU"') for unknown op: TopNRemove
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TreePredictions" device_type: "CPU"') for unknown op: TreePredictions
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "UpdateFertileSlots" device_type: "CPU"') for unknown op: UpdateFertileSlots
Model loaded.
Traceback (most recent call last):
  File "classifier_from_little_data_script_3.py", line 64, in <module>
    top_model.add(Flatten(input_shape=model.output_shape[1:]))
  File "C:\Python35\lib\site-packages\keras\models.py", line 430, in add
    layer(x)
  File "C:\Python35\lib\site-packages\keras\engine\topology.py", line 583, in __call__
    output_shape = self.compute_output_shape(input_shape)
  File "C:\Python35\lib\site-packages\keras\layers\core.py", line 488, in compute_output_shape
    '(got ' + str(input_shape[1:]) + '. '
ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 512). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.

How can I solve this issue?

Thanks.

Upvotes: 1

Views: 8795

Answers (4)

NateW
NateW

Reputation: 3006

When I encountered this error it was because I hadn't specified the input_tensor or the input_shape of the pre-trained model I was using, similar to what @Simplicity mentioned.

Upvotes: 0

Kartik
Kartik

Reputation: 81

I also had the same error and did not fix even after changing keras.backend and image_dim_ordering. Seems that the error was in the Maxpool layer where the pool_size argument should be written. Instead of this:

model.add(MaxPooling2D((2,2)))

It should be this:

model.add(MaxPooling2D(pool_size=(2, 2)))

Upvotes: 1

Tshilidzi Mudau
Tshilidzi Mudau

Reputation: 7899

Just in case someone else is facing a similar problem, and is wondering why the error in question was thrown, I will just add more details to @Simplicity's answer:

As mentioned in the keras documentation, Keras has two backend Theano and Tensorflow as of this writing. Theano and Tensorflow data/images have different ordering of dimensions. This orderings are as follows:

TensorFlow: [batch, width,height, channels]

Theano: [batch,channels, width, height]

If you are gonna be using the Tensorflow ordering(as is the case with the OP), you either have to :

  1. Specify this in you keras.json config file(found in ~/.keras/keras.json in Ubuntu). For example to run using Theano, in your keras.json config file, you put the following lines:

    "image_dim_ordering": "th" 
    
    "backend": "theano" 
    
  2. Specify the relevant backend you will be using on your code, eg:

    from keras import backend as K
    K.set_image_dim_ordering('th')   
    

The OP is using Tensorflow, thus he/she needs to make sure that the data is in the form [batch, width, height, channels], hence you have to change the define the input tensor as:

input_tensor = Input(shape=(150,150,3))

And the model as:

model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)

Had the OP been using Theano backend then the input tensor would have to be defined as:

input_tensor = Input(shape=(3, 150, 150))

-Notice that the channel is the first argument in this case.

And the model defined the same way as:

model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)

Just to reiterate; I'm merely adding some clarity to why/how @Simplicity's answer worked for him.

I hope this helps someone :).

Sources:

Upvotes: 5

Simplicity
Simplicity

Reputation: 48966

Based on @umutto's comment, those were the changes that solved the issue:

input_tensor = Input(shape=(150,150,3))
# build the VGG16 network
model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)

Upvotes: 0

Related Questions