ValueError: Input form for "Flatten" is not fully defined
I am 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 problem?
Thank.
source to share
Just in case someone comes across a similar issue and is wondering why the error was thrown , I'll just add more details to @ Simplicity's answer :
As mentioned in the keras documentation , Keras has two backends Theano and Tensorflow at the time of this writing. Theano and Tensorflow data / images are in different size order. These orderings are as follows:
TensorFlow: [batch, width, height, channels]
Theano: [batch, channels, width, height]
If you are going to use Tensorflow ordering (as is the case with the OP), you either need to:
-
Specify this in the config file
keras.json
(found~/.keras/keras.json
in Ubuntu). For example, to run using Theano in the config filekeras.json
you add the following lines:"image_dim_ordering": "th" "backend": "theano"
-
Specify the appropriate backend that you will use in your code, for example:
from keras import backend as K K.set_image_dim_ordering('th')
The OP is using Tensorflow, so he needs to make sure the data is in the form [batch, width, height, channels]
, so you need to change the definition of the input tensor as:
input_tensor = Input(shape=(150,150,3))
And the model is like:
model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)
If the OP were using the Theano backend, then the input tensor should be defined as:
input_tensor = Input(shape=(3, 150, 150))
-Notice that pipe is the first argument in this case.
And the model is defined in the same way as:
model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)
I'll just repeat; I'm just adding some clarity on why / how @ Simplicity's answer worked for it.
Hope this helps someone :).
Sources:
source to share