SadmanSakib93 / Stratified-k-fold-cross-validation-Image-classification-keras

This python program demonstrates image classification with stratified k-fold cross validation technique.
33 stars 15 forks source link

Ensemble Models #5

Closed diniebalqis closed 1 year ago

diniebalqis commented 1 year ago

Dear Sir,

I am new learner in this area. I am very delight if you could teach me how to do ensemble as a continuation of this code. Because actually I have tried by myself but the results I get is same or lesser than individual model. Is there any other ensemble method that is suitable to be applied with your code? Could you teach me. I hope you can help me to understand this well. Thank you sir.

I have used this code in order to generate my ensemble model:

import tensorflow.keras import tensorflow as tf

Load all models

model_ori_vgg16 = load_model(MODEL_FILENAME1) model_ori_inceptionv3 = load_model(MODEL_FILENAME2)

Initiating the usage of individual models

keras_model1 = tensorflow.keras.models.load_model(MODEL_FILENAME1, compile=False) keras_model1._name = 'model_ori_vgg16' keras_model2 = tensorflow.keras.models.load_model(MODEL_FILENAME2, compile=False) keras_model2._name = 'model_ori_inceptionv3'

Stacking individual models in a list

models = [keras_model1, keras_model2] model_input = tf.keras.Input(shape=(150, 150, 3)) #takes a list of tensors as input, all of the same shape model_outputs = [model(model_input) for model in models] #collects outputs of models in a list ensemble_output = tf.keras.layers.Average()(model_outputs) #averaging outputs ensemble_model_ori_vgg16_inceptionv3 = tf.keras.Model(inputs=model_input, outputs=ensemble_output)

ensemble_model_ori_vgg16_inceptionv3.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

history=ensemble_model_ori_vgg16_inceptionv3.fit(train_generator, epochs=epoch)

print("==============TEST RESULTS============") test_generator = test_datagen.flow_from_directory( test_path, target_size=(img_rows, img_cols), batch_size=batch_size, class_mode=None, shuffle=False) pred_ensemble_model_ori_vgg16_inceptionv3 = ensemble_model_ori_vgg16_inceptionv3.predict(test_generator, verbose=1) yPred_ensemble_model_ori_vgg16_inceptionv3 = np.argmax(pred_ensemble_model_ori_vgg16_inceptionv3, axis=1) true_classes_ensemble_model_ori_vgg16_inceptionv3 = test_generator.classes

testAcc,testPrec, testFScore, testCR = my_metrics(true_classes_ensemble_model_ori_vgg16_inceptionv3, yPred_ensemble_model_ori_vgg16_inceptionv3)

SadmanSakib93 commented 1 year ago

Please refer to this repo for Ensemble DL models with stratified cross-validation. https://github.com/SadmanSakib93/Stratified-k-fold-Cross-Validation-Ensemble-Deep-Learning-Image-Classification-Keras