ValueError:" Flatten"的输入形状。没有完全定义

时间:2017-04-13 03:36:33

标签: python tensorflow neural-network keras conv-neural-network

我试图运行this code,但收到以下错误:

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.

我该如何解决这个问题?

感谢。

4 个答案:

答案 0 :(得分:5)

如果其他人遇到类似的问题,并且想知道为什么会引发相关错误,我会向@Simplicity's answer添加更多详细信息:

keras documentation所述,截至本文撰写时,Keras有两个后端Theano和Tensorflow。 Theano和Tensorflow数据/图像具有不同的维度顺序。这个排序如下:

  

TensorFlow:[批次,宽度,高度,通道]

     

Theano:[批次,渠道,宽度,高度]

如果您要使用Tensorflow订购(如OP的情况),您必须:

  1. keras.json配置文件(在Ubuntu的~/.keras/keras.json中找到)中指定此项。例如,要使用Theano运行,请在keras.json配置文件中添加以下行:

    "image_dim_ordering": "th" 
    
    "backend": "theano" 
    
  2. 指定您将在代码中使用的相关后端,例如:

    from keras import backend as K
    K.set_image_dim_ordering('th')   
    
  3. OP正在使用Tensorflow,因此他/她需要确保数据的格式为[batch, width, height, channels],因此您必须将输入张量的定义更改为:

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

    模型为:

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

    如果OP使用Theano后端,则输入张量必须定义为:

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

    - 在这种情况下,通道是第一个参数。

    模型的定义方式与:

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

    重申一下;我只是简单地说明了为什么/如何@ Simplicity的答案对他起作用。

    我希望这有助于某人:)。

    来源:

答案 1 :(得分:0)

根据@ umutto的评论,这些是解决问题的变化:

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

答案 2 :(得分:0)

我也遇到了同样的错误,即使更改了keras.backend和image_dim_ordering后也没有修复。似乎错误发生在应该写入pool_size参数的Maxpool层中。而不是:

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

应该是这样的:

 <div class="col-md-4">
 <div class="form-group">
 <label for="field-2" class="control-label">Dropdown</label>
 <select class="form-control selectpicker" multiple data-selected-text-format="count" data-live-search="true" data-style="btn-white" id="lstdrpdwn" name="lstdrpdwn">
 </select>
 </div>
 </div>

答案 3 :(得分:0)

当我遇到此错误时,是因为我没有指定我正在使用的经过预先训练的模型的input_tensorinput_shape,类似于@Simplicity。