0
$\begingroup$

Whenever I try to execute this vgg16 code I get an error like this:

Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'
logits and labels must be broadcastable: logits_size=[144,3] labels_size=[32,3]
     [[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_6286]    
InvalidArgumentError: Graph execution error:
Detected at node 'categorical_crossentropy/softmax_cross_entropy_with_logits' defined at (most recent call last):
File "C:\Users\ DL4\anaconda3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,

I am using the FER2013 dataset and am classifying face emotion in three classes i.e, happy, sad, neutral.

I posted the code below; kindly help me to solve the error.

     train_datagen = ImageDataGenerator(rescale = 1./255,
                               validation_split = 0.2, #no validation only train n test
                              
    rotation_range=5,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    #zoom_range=0.2,
    horizontal_flip=True,
    #vertical_flip=True,
    fill_mode='nearest')

     valid_datagen = ImageDataGenerator(rescale = 1./255,
                              validation_split = 0.2)  #validation is zero

      test_datagen  = ImageDataGenerator(rescale = 1./255
                              )

      train_dataset  = train_datagen.flow_from_directory(directory = 'D:/FER 2013 3 
                                                CLASSES/train',
                                               target_size = (48,48),
                                               color_mode='grayscale',
                                               class_mode = 'categorical',
                                               subset = 'training',
                                               batch_size = 32)

        valid_dataset = valid_datagen.flow_from_directory(directory = 'D:/FER 2013 3 
                                          CLASSES/train',
                                              target_size = (48,48),
                                              color_mode='grayscale',
                                              class_mode = 'categorical',
                                              subset = 'validation',
                                              batch_size = 32)

          test_dataset = test_datagen.flow_from_directory(directory = 'D:/FER 2013 3 
                                             CLASSES/train',
                                              target_size = (48,48),
                                              color_mode='grayscale',
                                              class_mode = 'categorical',
                                              batch_size = 32)

         vgg16 = tf.keras.applications.VGG16(input_shape= 
                                  (48,48,3),include_top=False,weights="imagenet")

         vgg16.summary()

         # Freezing Layers

          for layer in base_model.layers[:-4]:
          layer.trainable=True

         # Building Model

        flatten = layers.Flatten()(vgg16.output)
        fc1 = layers.Dense(units=4096,activation="relu")(flatten)
        fc2 = layers.Dense(units=4096,activation="relu")(fc1)
        fc3 = layers.Dense(units=256,activation="relu")(fc2)
        softmax = layers.Dense(units=3, activation="softmax")(fc3)

        model = Model(inputs =  vgg16.output ,outputs = [softmax, fc3])

       # Model Summary

        model.summary()

         METRICS = [
                    tf.keras.metrics.BinaryAccuracy(name='accuracy')
                        ]

         lrd = ReduceLROnPlateau(monitor = 'val_loss',patience = 20,verbose = 1,factor = 0.50, 
                                   min_lr = 1e-10)
         mcp = ModelCheckpoint('vgg16_1.h5')
         #es = EarlyStopping(verbose=1, patience=20)

         model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4), 
                        loss='categorical_crossentropy',metrics=METRICS)

          history=model.fit(train_dataset,validation_data=valid_dataset,epochs = 100,verbose = 
                                   1,callbacks=[lrd,mcp])
$\endgroup$
1

0

Your Answer

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