2
$\begingroup$

My model summary is:

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 62, 62, 32)        896       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 31, 31, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 29, 29, 32)        9248      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 14, 14, 32)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 6272)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               802944    
_________________________________________________________________
dense_2 (Dense)              (None, 4)                 516       

While I am re-training this model using the below function:

enter image description here

Im facing this error:

ValueError: Input 0 is incompatible with layer flatten_1: expected min_ndim=3, found ndim=2
$\endgroup$

1 Answer 1

0
$\begingroup$

The reason for this error is that you are trying to flatten an already flat layer. The output of your model is (batch_size, 4), which cannot be flattened further. To simply fix the error remove the flatten layer from your code.

However, when fine-tuning a pretrained model, you should first remove the top layers of that model before adding your own. The reason is that these layers are trained for classification on a task different than yours.

If I were you I'd drop the last two layers of your pretrained model:

# code same as before ...

x = model.layers[-3].output  # Flatten layer output

# don't add flatten again

for fc in fc_layers:
    x = Dense(fc, activation='relu')(x)
    x = Dropout(dropout)(x)

predictions = Dense(num_classes, activation='softmax')(x)

finetune_model = Model(inputs=model.input, outputs=predictions)
$\endgroup$
4
  • $\begingroup$ That's a good suggestion. When i ran my re-train.py script, i have faced one more issue. RuntimeError: You must compile your model before using it. It says that the model needs to be compiled. But as far i know, if i compile a model, all the previous trained data will be lost, and the model will be trained from scratch. I don't want this to happen because i want to use this model for further training purposes. Can you help me in this. model.fit_generator( training_set ,steps_per_epoch = 520, epochs = 20, validation_data = test_set,validation_steps = 2000) $\endgroup$ Jul 1, 2019 at 7:15
  • $\begingroup$ Before you can use model.fit() you need to first run a model.compile() command. This requires you to specify the loss function and the optimizer. Also here you specify any metrics you want to use for evaluation. For example by running model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) you're telling keras that you want to train your model on a cross entropy loss with the Adam optimizer and also measure the model's accuracy while training. $\endgroup$
    – Djib2011
    Jul 1, 2019 at 7:38
  • $\begingroup$ But as far i know, if i compile a model, all the previous trained data will be lost, and the model will be trained from scratch. I don't want this to happen because i want to use this model for further re-training purposes. Can you help me in this. Can I re-train the model without compiling it and ,save it and use this model for future training ? $\endgroup$ Jul 1, 2019 at 7:45
  • $\begingroup$ A model is initialized during its instantiation. You can't train a model without compiling it. If you want to retrain a model you have to load the weights from the previously trained model and then fit it again. $\endgroup$
    – Djib2011
    Jul 1, 2019 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.