【问题标题】:ValueError: The shape of the input to "Flatten" is not fully definedValueError:“Flatten”的输入形状未完全定义
【发布时间】:2017-04-13 03:36:32
【问题描述】:

我正在尝试运行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.

我该如何解决这个问题?

谢谢。

【问题讨论】:

  • 您是否尝试过来自同一页面的this fix
  • 是的,这解决了问题。非常感谢。

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


【解决方案1】:

以防万一其他人遇到类似问题,并且想知道为什么会引发该错误,我将在@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')   
    

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 的答案为何/如何对他有用。

我希望这对某人有帮助:)。

来源:

【讨论】:

    【解决方案2】:

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

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

    应该是这样的:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2018-12-29
          • 2019-08-23
          • 2019-04-01
          • 2019-07-06
          • 1970-01-01
          • 2017-08-14
          • 2019-04-02
          • 1970-01-01
          • 2019-01-24
          相关资源
          最近更新 更多