2

Here I am trying to run CNN model on image classification.

This is the batch size and 13 labels

Image batch shape:  (32, 32, 32, 3)
Label batch shape:  (32, 13)
['Watch_Back' 'Watch_Chargers' 'Watch_Earpods' 'Watch_Front'
 'Watch_Lifestyle' 'Watch_Others' 'Watch_Packages' 'Watch_Side'
 'Watch_Text' 'Watch_Tilted' 'Watch_With_Accessories'
 'Watch_With_Ear_Pods' 'Watch_With_People']

Following are the model for cnn

model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(32,32,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(500, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(250, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy', 
              optimizer='adam',
              metrics=['accuracy'])

From the following part of code, error comes:

steps_per_epoch = np.ceil(train_generator.samples/train_generator.batch_size)
val_steps_per_epoch = np.ceil(valid_generator.samples/valid_generator.batch_size)
hist = model.fit(
train_generator,
epochs=10,
verbose=1,
steps_per_epoch=steps_per_epoch,
validation_data=valid_generator,
validation_steps=val_steps_per_epoch).history

Following is the error

Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-64-b89d5efc8aaf> in <module>()
      7 steps_per_epoch=steps_per_epoch,
      8 validation_data=valid_generator,
----> 9 validation_steps=val_steps_per_epoch).history

8 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:  logits and labels must be broadcastable: logits_size=[32,10] labels_size=[32,13]
     [[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at <ipython-input-64-b89d5efc8aaf>:9) ]] [Op:__inference_train_function_6504]

Function call stack:
train_function

How to resolve this categorical error

1 Answers1

2

The error is caused by this line:

model.add(Dense(10, activation='softmax'))

It's important that the last layer contains as many neurons as you have categories in your dataset. I'm guessing you have 13 categories, and so it should be 13. You can also use

model.add(Dense(len(train_generator.classes), activation='softmax'))
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • Thanks @Nicolas Gervais, you nailed it for me. I changed it to the number of categories there are and it worked! – zelfde Aug 29 '21 at 23:42