keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.86k stars 19.44k forks source link

Is there a problem with MobileNet -in keras applications- with its input shape? #8301

Closed pedghz closed 6 years ago

pedghz commented 6 years ago

I am trying to implement number of networks using Keras applications. Here I am attaching a piece of code and this code works fine for ResNet50 and VGG16 but when it comes to MobileNet it generate the error:

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

# basic_model = ResNet50()
# basic_model = VGG16()
basic_model = MobileNet()
classes = list(iter(train_generator.class_indices))
basic_model.layers.pop()
for layer in basic_model.layers[:25]:
    layer.trainable = False
last = basic_model.layers[-1].output
temp = Dense(len(classes), activation="softmax")(last)

fineTuned_model = Model(basic_model.input, temp)
fineTuned_model.classes = classes
fineTuned_model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
fineTuned_model.fit_generator(
        train_generator,
        steps_per_epoch=3764 // batch_size,
        epochs=100,
        validation_data=validation_generator,
        validation_steps=900 // batch_size)
fineTuned_model.save('mobile_model.h5')
titu1994 commented 6 years ago

This is because MobileNets uses pointwise Conv2D to classify the images (Instead of Dense), and then reshapes the 2D output to 1D as expected in Dense.

You have two choices - 1) pop 3 times, add Dense and then Flatten layer. 2) pop 5 times, add Dropout and then Dense.

To look at why you need so many pop operations, refer to the actual code of MobileNets - beginning from line 485