2017-06-13 69 views
0

我想微調Keras中的預先訓練的Inceptionv3中的多標籤(17)預測問題。微調Keras中的Inception_v3時的ValueError

下面的代碼:

# create the base pre-trained model 
base_model = InceptionV3(weights='imagenet', include_top=False) 

# add a new top layer 
x = base_model.output 
predictions = Dense(17, activation='sigmoid')(x) 

# this is the model we will train 
model = Model(inputs=base_model.input, outputs=predictions) 

# we need to recompile the model for these modifications to take effect 
# we use SGD with a low learning rate 
from keras.optimizers import SGD 
model.compile(loss='binary_crossentropy', # We NEED binary here, since categorical_crossentropy l1 norms the output before calculating loss. 
       optimizer=SGD(lr=0.0001, momentum=0.9)) 

# Fit the model (Add history so that the history may be saved) 
history = model.fit(x_train, y_train, 
      batch_size=128, 
      epochs=1, 
      verbose=1, 
      callbacks=callbacks_list, 
      validation_data=(x_valid, y_valid)) 

但我鑽進了以下錯誤消息,並且有麻煩破譯它是說:

ValueError: Error when checking target: expected dense_1 to have 4 dimensions, but got array with shape (1024, 17)

這似乎有事情做與它不不喜歡我爲標籤作爲目標的熱門編碼。但是,我如何獲得4個維度的目標?

+0

看來,https://stackoverflow.com/questions/41764041/fine-tuning-pretrained-model-in-keras可能有答案? –

+0

或者https://stackoverflow.com/questions/43386463/keras-vgg16-fine-tuning?rq=1 –

回答

0

事實證明,從https://keras.io/applications/複製的代碼不能即裝即用。 以下職位已經幫助我: Keras VGG16 fine tuning

我需要做的修改如下:

  1. 添加輸入形狀模型定義base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3)),並

  2. 添加展平()層以張化張量輸出: x = base_model.output x = Flatten()(x) predictions = Dense(17, activation='sigmoid')(x)

然後模型適合我!