Skip to main content
 首页 » 编程设计

python之值错误 : The shape of the input to "Flatten" is not fully defined

2025年05月04日14shangdawei

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

我该如何解决这个问题?

谢谢。

请您参考如下方法:

以防万一其他人遇到类似的问题,并且想知道为什么会抛出所讨论的错误,我将在 @Simplicity's answer 中添加更多详细信息。 :

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

TensorFlow: [batch, width,height, channels]

Theano: [batch,channels, width, height]

如果您要使用 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)) 

-注意在这种情况下 channel 是第一个参数。

模型的定义方式与:

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

重申一下;我只是想澄清一下为什么/@Simplicity 的回答对他有用。

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

来源: