ValueError: A target array with shape (32, 3) was passed for an output of shape (None, 2) while using as loss `binary_crossentropy`. In Keras model

被刻印的时光 ゝ 提交于 2020-01-25 06:48:06

问题


I am trying to ensemble the Keras binary pre-trained models into one multi-class model by the voting system. Binary pre-trained models are trained on different classes each. To ensemble the model, I am referring to this blog for the same

Here is the code

for i in os.listdir(model_root): //loading all the models
    print(i)
    filename = model_root + "/" + i
    # load model
    model = load_model(filename, custom_objects={'KerasLayer': hub.KerasLayer})
    models.append(model)
print(len(models))  //3

  #To fit the loaded models to the data and saving it to an array fit_models

steps_per_epoch = image_data.samples // image_data.batch_size
batch_stats = CollectBatchStats()
validation_steps = image_data_val.samples / image_data_val.batch_size
for i in range(len(models)):
    model[i].fit_generator((item for item in image_data), epochs=2,
              steps_per_epoch=steps_per_epoch, #callbacks=[batch_stats],
              validation_data=(item for item in image_data_val), validation_steps=validation_steps, verbose=2)
    fit_models.append(model[i])

Here is the traceback to the error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Pawandeep/Desktop/Python projects/ensemble_image.py", line 89, in <module>
    validation_data=(item for item in image_data_val), validation_steps=validation_steps, verbose=2)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1433, in fit_generator
    steps_name='steps_per_epoch')
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 264, in model_iteration
    batch_outs = batch_function(*batch_data)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1153, in train_on_batch
    extract_tensors_from_dataset=True)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2692, in _standardize_user_data
    y, self._feed_loss_fns, feed_output_shapes)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 549, in check_loss_and_target_compatibility
    ' while using as loss `' + loss_name + '`. '
ValueError: A target array with shape (32, 3) was passed for an output of shape (None, 2) while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the output.

Data definition

#define data
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1 / 255, validation_split=0.20)
IMAGE_SIZE= (224,224)
image_data = image_generator.flow_from_directory(str(data_root), target_size=IMAGE_SIZE, subset='training')
image_data_val = image_generator.flow_from_directory(str(data_root), target_size=IMAGE_SIZE, subset='validation')

My data looks like this:

Image batch shape:  (32, 224, 224, 3)
Label batch shape:  (32, 3)

I tried to print out the shape of each model in models array. It is

(32, 2)

Now I understand the problem. So the problem is I have binary models trained on each class. That is why its shape is 32*3. I want to ensemble these binary models into an ensemble model so that the addition of each model(class) becomes a multiclass model. Then based on the prediction of this model I want to label my dataset. So how can I achieve this now?

来源:https://stackoverflow.com/questions/57561966/valueerror-a-target-array-with-shape-32-3-was-passed-for-an-output-of-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!