【问题标题】:How can I fit binary keras models into multiclass models?如何将二进制 keras 模型拟合到多类模型中?
【发布时间】:2019-08-19 19:15:01
【问题描述】:

我正在研究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])

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

model.shape() # (32, 2)

我的数据是这样的

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

这会在 2 和 3 个类之间产生冲突。这个问题我不知道怎么处理,也不知道在keras中是否可行

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

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

【问题讨论】:

  • 如果模型训练了两个类,那么它训练了两个类。你无法改变它。
  • 但我的要求是集成二进制模型来解决多类问题
  • 您需要为每个 OVA 子问题制作适合的标签,您似乎没有这样做

标签: python tensorflow keras ensemble-learning


【解决方案1】:

你需要 1 个类的模型(你不能集成 2 个类的模型并达到 3 个)

如果你训练了列表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 型号。

【讨论】:

  • 连接看起来不像是一个写解决方案,因为它正在加起来。请看更新
  • 如答案中所说,您需要1 class 的模型,而不是2 class。加入 2 类模型以输出奇数类是不可能的。如果它们是二元模型,它们可能应该只有 1 个类。这不是代码问题,它让您清楚地知道您想做什么以及如何做。
  • 如果您不想再次训练这些模型,也许(取决于 2 个类的含义)您可以尝试仅获取其中一个类(如果它是分类类 1 x 类 2,则表示class1 = 1是class1,class1 = 0是class 2)。您可以为每个输出使用Lambda(lambda x: x[:,:1])(tensor)
  • Daniel Möller,我再次为这个程序训练了模型,现在又得到另一个错误 tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'sequential_input' with dtype float and形状 [?,224,224,3] [[{{节点顺序输入}}]]
猜你喜欢
  • 1970-01-01
  • 2022-01-25
  • 2020-05-12
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 2021-09-30
  • 2020-03-24
  • 1970-01-01
相关资源
最近更新 更多