Imageai: ValueError: A target array with shape (32, 3) was passed for an output of shape (None, 4) while using as loss `categorical_crossentropy`. This loss expects targ ets to have the same shape as the output.

Created on 29 Oct 2019  路  3Comments  路  Source: OlafenwaMoses/ImageAI

Hi,

I am trying to do a custom model training with the tutorial.

I am getting the following error message:

ValueError: A target array with shape (32, 3) was passed for an output of shape (None, 4) while using as loss categorical_crossentropy. This loss expects targ ets to have the same shape as the output.

I have 3 classes:

  • CustomModelshumantrainface
  • CustomModelshumantrainhand
  • CustomModelshumantrainfoot

and the tests:

  • CustomModelshumantestface
  • CustomModelshumantesthand
  • CustomModelshumantestfoot

Python-Code:

from imageai.Prediction.Custom import ModelTraining
model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory("CustomModels/human")
model_trainer.trainModel(num_objects=4, num_experiments=100, enhance_data=True, batch_size=32, show_network_summary=True)

What am I doing wrong?

Complete Commandline Output of what I have saved:


batch_normalization_35 (BatchNo (None, 14, 14, 256) 1024 conv2d_35[0][0]



activation_32 (Activation) (None, 14, 14, 256) 0 batch_normaliza
tion_35[0][0]



conv2d_36 (Conv2D) (None, 14, 14, 1024) 263168 activation_32[0
][0]



batch_normalization_36 (BatchNo (None, 14, 14, 1024) 4096 conv2d_36[0][0]



add_10 (Add) (None, 14, 14, 1024) 0 batch_normaliza
tion_36[0][0]
activation_30[0
][0]



activation_33 (Activation) (None, 14, 14, 1024) 0 add_10[0][0]



conv2d_37 (Conv2D) (None, 14, 14, 256) 262400 activation_33[0
][0]



batch_normalization_37 (BatchNo (None, 14, 14, 256) 1024 conv2d_37[0][0]



activation_34 (Activation) (None, 14, 14, 256) 0 batch_normaliza
tion_37[0][0]



conv2d_38 (Conv2D) (None, 14, 14, 256) 590080 activation_34[0
][0]



batch_normalization_38 (BatchNo (None, 14, 14, 256) 1024 conv2d_38[0][0]



activation_35 (Activation) (None, 14, 14, 256) 0 batch_normaliza
tion_38[0][0]



conv2d_39 (Conv2D) (None, 14, 14, 1024) 263168 activation_35[0
][0]



batch_normalization_39 (BatchNo (None, 14, 14, 1024) 4096 conv2d_39[0][0]



add_11 (Add) (None, 14, 14, 1024) 0 batch_normaliza
tion_39[0][0]
activation_33[0
][0]



activation_36 (Activation) (None, 14, 14, 1024) 0 add_11[0][0]



conv2d_40 (Conv2D) (None, 14, 14, 256) 262400 activation_36[0
][0]



batch_normalization_40 (BatchNo (None, 14, 14, 256) 1024 conv2d_40[0][0]



activation_37 (Activation) (None, 14, 14, 256) 0 batch_normaliza
tion_40[0][0]



conv2d_41 (Conv2D) (None, 14, 14, 256) 590080 activation_37[0
][0]



batch_normalization_41 (BatchNo (None, 14, 14, 256) 1024 conv2d_41[0][0]



activation_38 (Activation) (None, 14, 14, 256) 0 batch_normaliza
tion_41[0][0]



conv2d_42 (Conv2D) (None, 14, 14, 1024) 263168 activation_38[0
][0]



batch_normalization_42 (BatchNo (None, 14, 14, 1024) 4096 conv2d_42[0][0]



add_12 (Add) (None, 14, 14, 1024) 0 batch_normaliza
tion_42[0][0]
activation_36[0
][0]



activation_39 (Activation) (None, 14, 14, 1024) 0 add_12[0][0]



conv2d_44 (Conv2D) (None, 7, 7, 512) 524800 activation_39[0
][0]



batch_normalization_44 (BatchNo (None, 7, 7, 512) 2048 conv2d_44[0][0]



activation_40 (Activation) (None, 7, 7, 512) 0 batch_normaliza
tion_44[0][0]



conv2d_45 (Conv2D) (None, 7, 7, 512) 2359808 activation_40[0
][0]



batch_normalization_45 (BatchNo (None, 7, 7, 512) 2048 conv2d_45[0][0]



activation_41 (Activation) (None, 7, 7, 512) 0 batch_normaliza
tion_45[0][0]



conv2d_46 (Conv2D) (None, 7, 7, 2048) 1050624 activation_41[0
][0]



conv2d_43 (Conv2D) (None, 7, 7, 2048) 2099200 activation_39[0
][0]



batch_normalization_46 (BatchNo (None, 7, 7, 2048) 8192 conv2d_46[0][0]



batch_normalization_43 (BatchNo (None, 7, 7, 2048) 8192 conv2d_43[0][0]



add_13 (Add) (None, 7, 7, 2048) 0 batch_normaliza
tion_46[0][0]
batch_normaliza
tion_43[0][0]



activation_42 (Activation) (None, 7, 7, 2048) 0 add_13[0][0]



conv2d_47 (Conv2D) (None, 7, 7, 512) 1049088 activation_42[0
][0]



batch_normalization_47 (BatchNo (None, 7, 7, 512) 2048 conv2d_47[0][0]



activation_43 (Activation) (None, 7, 7, 512) 0 batch_normaliza
tion_47[0][0]



conv2d_48 (Conv2D) (None, 7, 7, 512) 2359808 activation_43[0
][0]



batch_normalization_48 (BatchNo (None, 7, 7, 512) 2048 conv2d_48[0][0]



activation_44 (Activation) (None, 7, 7, 512) 0 batch_normaliza
tion_48[0][0]



conv2d_49 (Conv2D) (None, 7, 7, 2048) 1050624 activation_44[0
][0]



batch_normalization_49 (BatchNo (None, 7, 7, 2048) 8192 conv2d_49[0][0]



add_14 (Add) (None, 7, 7, 2048) 0 batch_normaliza
tion_49[0][0]
activation_42[0
][0]



activation_45 (Activation) (None, 7, 7, 2048) 0 add_14[0][0]



conv2d_50 (Conv2D) (None, 7, 7, 512) 1049088 activation_45[0
][0]



batch_normalization_50 (BatchNo (None, 7, 7, 512) 2048 conv2d_50[0][0]



activation_46 (Activation) (None, 7, 7, 512) 0 batch_normaliza
tion_50[0][0]



conv2d_51 (Conv2D) (None, 7, 7, 512) 2359808 activation_46[0
][0]



batch_normalization_51 (BatchNo (None, 7, 7, 512) 2048 conv2d_51[0][0]



activation_47 (Activation) (None, 7, 7, 512) 0 batch_normaliza
tion_51[0][0]



conv2d_52 (Conv2D) (None, 7, 7, 2048) 1050624 activation_47[0
][0]



batch_normalization_52 (BatchNo (None, 7, 7, 2048) 8192 conv2d_52[0][0]



add_15 (Add) (None, 7, 7, 2048) 0 batch_normaliza
tion_52[0][0]
activation_45[0
][0]



activation_48 (Activation) (None, 7, 7, 2048) 0 add_15[0][0]



global_avg_pooling (GlobalAvera (None, 2048) 0 activation_48[0
][0]



dense (Dense) (None, 4) 8196 global_avg_pool
ing[0][0]



activation_49 (Activation) (None, 4) 0 dense[0][0]

================================================================================

Total params: 23,595,908
Trainable params: 23,542,788
Non-trainable params: 53,120



WARNING:tensorflow:period argument is deprecated. Please use save_freq to sp
ecify the frequency in number of samples seen.
Using Enhanced Data Generation
Found 3573 images belonging to 3 classes.
Found 392 images belonging to 3 classes.
JSON Mapping for the model classes saved to CustomModels/humanjsonmodel_class
.json
Number of experiments (Epochs) : 100
Epoch 1/100
Traceback (most recent call last):
File "CustomModelTraining.py", line 5, in
model_trainer.trainModel(num_objects=4, num_experiments=100, enhance_data=Tr
ue, batch_size=32, show_network_summary=True)
File "C:UsersHimkenLaptop.condaenvsimageailibsite-packagesimageaiPred
ictionCustom__init__.py", line 342, in trainModel
validation_steps=int(num_test / batch_size), callbacks=[checkpoint, lr_sched
uler, tensorboard])
File "C:UsersHimkenLaptop.condaenvsimageailibsite-packagestensorflow_c
orepythonkerasenginetraining.py", line 1297, in fit_generator
steps_name='steps_per_epoch')
File "C:UsersHimkenLaptop.condaenvsimageailibsite-packagestensorflow_c
orepythonkerasenginetraining_generator.py", line 265, in model_iteration
batch_outs = batch_function(*batch_data)
File "C:UsersHimkenLaptop.condaenvsimageailibsite-packagestensorflow_c
orepythonkerasenginetraining.py", line 992, in train_on_batch
extract_tensors_from_dataset=True)
File "C:UsersHimkenLaptop.condaenvsimageailibsite-packagestensorflow_c
orepythonkerasenginetraining.py", line 2538, in _standardize_user_data
y, self._feed_loss_fns, feed_output_shapes)
File "C:UsersHimkenLaptop.condaenvsimageailibsite-packagestensorflow_c
orepythonkerasenginetraining_utils.py", line 743, in check_loss_and_target_c
ompatibility
' while using as loss ' + loss_name + '. '
ValueError: A target array with shape (32, 3) was passed for an output of shape
(None, 4) while using as loss categorical_crossentropy. This loss expects targ
ets to have the same shape as the output.

Most helpful comment

You are trying to define 3 classes. However, you set your output classes into 4. Try changing num_objects=3.

All 3 comments

You are trying to define 3 classes. However, you set your output classes into 4. Try changing num_objects=3.

Yes thanks!

Sir but still i get an same error , while my no. of classes is equals to my no. of object ,

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bjente picture bjente  路  3Comments

letchfordnicholas picture letchfordnicholas  路  6Comments

Combinacijus picture Combinacijus  路  3Comments

flozi00 picture flozi00  路  4Comments

mbaguant picture mbaguant  路  6Comments