'How to Develop Voting Ensembles With Python of Pre-trained models?

I'm attempting to create an ensemble of a custom CNN and pre-trained inceptionV3,MobileNetV2 and Xception for a medical image classification task using Keras with Tensorflow. The code is given below for loaded models after saved:

def load_all_models():
all_models = []
model_names = ['model1.h5', 'model2.h5', 'model3.h5']
for model_name in model_names:
    filename = os.path.join('models', model_name)
    model = tf.keras.models.load_model(filename)
    predictsDepth = np.argmax(model.predict(X_test), axis=1)
    all_models.append(model)
    print('loaded:', filename)
    labels = np.array(labels)
return all_models
models = load_all_models()
for i, model in enumerate(models):
for layer in model.layers:
 if layer._name == "Flatten":
    layer._name = "Flatten_{}".format( i )
    layer.trainable = False

enter image description here

and after load the models I am try to build an ensemble model using voting. The code is given below for voting:

from sklearn.ensemble import RandomForestClassifier , VotingClassifier

  model = VotingClassifier(estimators=[('InceptionV3_Accuracy', model1), ('MobileNetV2_Accuracy', 
  model2), ('Xception_Accuracy', model3)], voting='hard')
  model.fit(X_train, y_train)

When fit the ensemble I get the following error:

enter image description here

Blockquote



Solution 1:[1]

Scikit does not ensemble pre-trained models. Scikit voting ensemble (and stacking ensemble) object requires un-trained models ('estimators') as the input and a final meta model ('final estimator'). Calling the .fit function then trains the 'estimators' and the 'final estimator'.

Since you are using trained models, you can refer to the below article which provides 2 coded examples of how to ensemble trained models.

For each example, the output from each the trained model is required as input for the final meta model.

https://machinelearningmastery.com/stacking-ensemble-for-deep-learning-neural-networks/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1