user3050590
user3050590

Reputation: 1716

How can I fit binary keras models into multiclass models?

I am working on OVA (one vs all) classification problem. For that, I have trained Keras binary classifiers with sigmoid function and binary_crossentropy. I need to ensemble them to multiclass model similar to here When I am trying to do that I get the following error

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

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

I am getting this error on the fit function and the problem is models are trained on 2 classes whereas, I need to fit it now to 3 classes.

model.shape() # (32, 2)

My data looks like this

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

This creates a conflict between 2 and 3 classes. I don't know how to deal with this problem and I don't know if it is possible in keras

After the provided solution, now my model looks like

        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
__________________________________________________________________________________________________

Error is

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

Upvotes: 0

Views: 440

Answers (1)

Daniel M&#246;ller
Daniel M&#246;ller

Reputation: 86600

You need models of 1 class (you can't ensemble models of 2 classes and reach 3)

If you trained models of 1 class in the list models, you need to transform them into a single model:

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)

Fit the ensemble model.

Upvotes: 2

Related Questions