2

I'm trying to create an ensemble with three pre-trained VGG16, InceptionV3, and EfficientNetB0 for a medical image classification task. Here is my code based on Keras with Tensorflow backend:

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)
        all_models.append(model)
        print('loaded:', filename)
    return all_models


models = load_all_models()
for i, model in enumerate(models):
    for layer in model.layers:
        layer.trainable = False

print("[INFO] evaluation network ...")
model.evaluate(X_test, verbose=1)
predIdxs = model.predict(X_test, verbose=1)

predprobabilities = model.predict(X_test, verbose=1)
predIdxs = np.argmax(predprobabilities, axis=1)

print(classification_report(y_test.argmax(axis=1), predIdxs, target_names=lb.classes_))

The previous code provides the following output: enter image description here

Then, I feed a Dense layer with the concatenation of the outputs of the three networks, as showed in the code below:

ensemble_visible = [model.input for model in models]
ensemble_outputs = [model.output for model in models]
merge = tf.keras.layers.concatenate(ensemble_outputs)
merge = tf.keras.layers.Dense(10, activation='relu')(merge)
output = tf.keras.layers.Dense(3, activation='sigmoid')(merge)
model = tf.keras.models.Model(inputs=ensemble_visible, outputs=output)

But when I execute the code, I get this error: enter image description here

Any help or suggestion is appreciated, thanks!

PieCot
  • 3,564
  • 1
  • 12
  • 20

1 Answers1

0

We are loading three models and the error says that the name of the Flatten layer is repeated three times. We simply need to change the names,

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

So, we'll have unique names for the three Flatten layers like Flatten_0, Flatten_1 and Flatten_2.

Shubham Panchal
  • 4,061
  • 2
  • 11
  • 36