简体   繁体   中英

InvalidArgumentError: Incompatible shapes: [32] vs. [8] - Keras Transfer Learning

I am trying to perform transfer learning using Mobilenet Here is my code

from keras.applications.mobilenet import preprocess_input
from keras.applications.mobilenet import MobileNet
import keras.backend as K

x_train_final=preprocess_input(x_train)
x_test_final=preprocess_input(x_test)
pretrained_weights='imagenet'

#2
mobile=MobileNet(weights=pretrained_weights,include_top=False,input_shape=(416,416,3))
x=mobile.output
x=keras.layers.Flatten()(x)
x=keras.layers.Dense(512)(x)
x=keras.layers.Activation("relu")(x)
x=keras.layers.Dense(256)(x)
x=keras.layers.Activation("sigmoid")(x)
x=keras.layers.Dense(8)(x)
output=x
model=keras.models.Model(inputs=mobile.input,outputs=output)

def custom_loss(y_true, y_pred):

    loss = K.square(y_pred - y_true)  # (batch_size, 8)
    # summing both loss values along batch dimension
    loss = K.sum(loss, axis=0)        # (batch_size,)

    return loss

model.compile(optimizer = keras.optimizers.Adam(learning_rate=0.002), 
          loss = custom_loss,
          metrics = ['accuracy', 'mse'])

model.fit(x_train_final, y_train, epochs = 100)

But I am encountering an error.

Epoch 1/100
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-37-55c4376c8a25> in <module>()
----> 1 model.fit(x_train_final, y_train, epochs = 100)

7 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  Incompatible shapes: [32] vs. [8]
     [[node gradients_4/loss_4/dense_18_loss/custom_loss/weighted_loss/mul_grad/Mul_1 (defined at /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_111168]

Function call stack:
keras_scratch_graph

My Train dataset shape is (3066, 416, 416, 3)

Test dataset shape is (100, 416, 416, 3)

I can't figure out the error.

I also faced the same issue and found that there was issue with my pooling layer. It was giving out incorrect output. Changing maxpool2d to globalaevaragepool solved the issue. If you want to stick to maxpool, then change it's output shape.

I am new to this, anyway i got the same error message

  • tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name,op_name,tensorflow.python.framework.errors_impl.InvalidArgumentError: - - Incompatible shapes: [8,10] vs. [8,6] [[node categorical_crossentropy/mul....

Understanding

  • IT is error of output neural layer shape not the input to the model. ie error shows the dimension of the output neural n/w layer.
  • ie : [what is should be] vs [what the problematic dimension found]

Problem in my case

  1. in my case - i modified old code:
    • classes list defined: 10 - correct
    • output_layer = Dense( units=6 ) - this was the problem Answer:
  2. correction : output_layer = Dense( units=10 ) as my classes length is 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM