0

I am trying to fine-tune pre-trained Inceptionv3 in Keras for a multi-label (17) prediction problem.

Here's the code:

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

But I got into the following error message and had trouble deciphering what it is saying:

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

It seems to have something to do with that it doesn't like my one-hot encoding for the labels as target. But how do I get 4 dimensions target?

Pang
  • 9,564
  • 146
  • 81
  • 122
Clark Chong
  • 121
  • 2

1 Answers1

2

It turns out that the code copied from https://keras.io/applications/ would not run out-of-the-box. The following post has helped me: Keras VGG16 fine tuning

The changes I need to make are the following:

  1. Add in the input shape to the model definition base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3)), and

  2. Add a Flatten() layer to flatten the tensor output: x = base_model.output x = Flatten()(x) predictions = Dense(17, activation='sigmoid')(x)

Then the model works for me!

Clark Chong
  • 121
  • 2