2017-04-13 63 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的答案爲他工作添加了一些清晰的內容。

我希望這可以幫助別人:)。

來源:

相關問題