ValueError:将形状为(32,3)的目标数组传递给形状为(None,2)的输出,同时将其用作损耗`binary_crossentropy`。在Keras模型中

时间:2019-08-19 18:12:14

标签: python tensorflow keras ensemble-learning

我正在尝试通过投票系统将Keras二进制预训练模型整合为一个多类模型。二进制预训练模型分别在不同的类别上训练。为了整合模型,我指的是与此相同的blog

这是代码

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])

这是该错误的回溯:

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.

数据定义

#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')

我的数据如下:

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

我尝试在models数组中打印出每个模型的形状。是

(32, 2)

现在我明白了这个问题。所以问题是我在每节课上训练了二进制模型。这就是为什么其形状为32 * 3的原因。我想将这些二进制模型集成到一个集成模型中,以便每个模型(类)的添加成为一个多类模型。然后基于该模型的预测,我想标记我的数据集。那么我现在如何实现呢?

0 个答案:

没有答案