2017-04-13 62 views
0

我试图运行this code,但得到以下错误:ValueError异常:输入“平坦”的形状不完全定义

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. 

我怎样才能解决这个问题呢?

谢谢。

+1

你试过[此修复程序(https://gist.github.com/fchollet/7eb39b44eb9e16e59632d25fb3119975#gistcomment- 2031679)从同一页? – umutto

+1

是的,解决了这个问题。非常感谢。 – Simplicity

回答

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) 
3

万一别人正面临着类似的问题,并想知道为什么有问题的错误被抛出,我会只是增加更多细节到@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)) 

-Notice该信道是所述第一在这种情况下的论点。

和模型中定义的一样:

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

只是重申;我只是为了解他为什么/如何@ Simplicity的答案为他工作添加了一些清晰的内容。

我希望这可以帮助别人:)。

来源:

相关问题