如何将二进制Keras模型拟合到多类模型中?

时间:2019-08-19 19:15:01

标签: python tensorflow keras ensemble-learning

我正在研究OVA(一个对所有)分类问题。为此,我训练了具有Sigmoid函数和binary_crossentropy的Keras二进制分类器。我需要将它们整合到类似于here的多类模型中,当我尝试这样做时,出现以下错误

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

程序代码

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

我在拟合函数中遇到此错误,问题是模型在2个类上训练,而我现在需要将其拟合到3个类。

model.shape() # (32, 2)

我的数据看起来像这样

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

这将在2个和3个类之间产生冲突。我不知道如何解决这个问题,也不知道在喀拉拉邦是否有可能

提供解决方案后,现在我的模型看起来像

        Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 224, 224, 3) 0                                            
__________________________________________________________________________________________________
sequential_4 (Sequential)       (None, 1)            3541267     input_1[0][0]                    
__________________________________________________________________________________________________
sequential_8 (Sequential)       (None, 1)            3541267     input_1[0][0]                    
__________________________________________________________________________________________________
sequential_2 (Sequential)       (None, 1)            3541267     input_1[0][0]                    
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 3)            0           sequential_4[1][0]               
                                                                 sequential_8[1][0]               
                                                                 sequential_2[1][0]               
==================================================================================================
Total params: 10,623,801
Trainable params: 3,006
Non-trainable params: 10,620,795
__________________________________________________________________________________________________

错误是

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 80, 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 673, in fit
    initial_epoch=initial_epoch)
  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 1175, in train_on_batch
    outputs = self.train_function(ins)  # pylint: disable=not-callable
  File "C:\Python\lib\site-packages\tensorflow\python\keras\backend.py", line 3292, in __call__
    run_metadata=self.run_metadata)
  File "C:\Python\lib\site-packages\tensorflow\python\client\session.py", line 1458, in __call__
    run_metadata_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'sequential_input' with dtype float and shape [?,224,224,3]
     [[{{node sequential_input}}]]

1 个答案:

答案 0 :(得分:2)

您需要1类的模型(您不能将2类的模型合二为一)

如果您在列表models中训练了1类模型,则需要将它们转换为单个模型:

inputs = Input(common_input_shape)
outputs = [m(inputs) for m in models]
outputs = Concatenate()(outputs)
#maybe outputs = Activation("softmax")(outputs) if the problem is categorical    
ensemble = Model(inputs, outputs)

适合ensemble模型。