2016-11-24 160 views
1

我正在使用Resnet50做轉移學習。後端是張量流。 我試圖在RESNET疊放三個層,但失敗,以下錯誤:Keras轉移學習與Resnet50失敗,異常

Exception: The shape of the input to "Flatten" is not fully defined (got (None, None, 2048). 
Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model. 

堆疊的代碼兩種型號如下:

model = ResNet50(include_top=False, weights='imagenet') 

top_model = Sequential() 
top_model.add(Flatten(input_shape=model.output_shape[1:])) 
top_model.add(Dense(256, activation='relu')) 
top_model.add(Dropout(0.5)) 
top_model.add(Dense(1, activation='sigmoid')) 
top_model.load_weights(top_model_weights_path) 

model = Model(input=model.input, output=top_model(model.output)) 

回答

0

RESNET與include_top最後一層= False選項已經變平並且不需要另一個平坦化層。

+0

你確定嗎? https://github.com/fchollet/keras/blob/89e6eb01f200ef6fa8db926ecfee68b37b229fbc/keras/applications/resnet50.py#L234 – Eduardo

0

在實例化Resnet時,您必須顯式地使用input_shape。 model = ResNet50(include_top=False, weights='imagenet',input_shape=(224,224,3))

如果您有theano作爲後端必須設置爲第一頻道的數量: model = ResNet50(include_top=False, weights='imagenet',input_shape=(3,224,224))