I'm trying to fine-tune a MobileNet but I'm receiving the following error:
ValueError, Error when checking target: expected dense_1 to have 4 dimensions, but got array with shape (10, 14)
Is there any conflict with how I set my directory iterators?
train_batches = ImageDataGenerator(preprocessing_function=keras.applications.mobilenet_v2.preprocess_input).flow_from_directory(
train_path, target_size=(224, 224), batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=keras.applications.mobilenet_v2.preprocess_input).flow_from_directory(
valid_path, target_size=(224, 224), batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=keras.applications.mobilenet_v2.preprocess_input).flow_from_directory(
test_path, target_size=(224, 224), batch_size=10, shuffle=False)
and my new bottleneck layer is as follows:
x=mobile.layers[-6].output
predictions = Dense(14, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=predictions)
Since the Dense layer is applied on the last axis of its input and considering the fact that x
is a 4D tensor, the predictions
tensor would also be a 4D tensor. That's why the model expects a 4D output (i.e. expected dense_1 to have 4 dimensions
) but your labels are 2D (i.e. but got array with shape (10, 14)
). To resolve this, you need to make x
as a 2D tensor. One way of doing it is to use a Flatten
layer:
x = mobile.layers[-6].output
x = Flatten()(x)
predictions = Dense(14, activation='softmax')(x)