1

I'm trying to perform a stacking ensemble of three VGG-16 models, all custom-trained on my personal dataset and having the same input shape. This is the code:

input_shape = (256,256,3)
model_input = Input(shape=input_shape)

def load_all_models(n_models):
    all_models = list()
    model_top1 = load_model('weights/vgg16_1.h5')
    all_models.append(model_top1)
    model_top2 = load_model('weights/vgg16_2.h5')
    all_models.append(model_top2)
    model_top3 = load_model('weights/vgg16_3.h5')
    all_models.append(model_top3)
    return all_models

n_members = 3
members = load_all_models(n_members)
print('Loaded %d models' % len(members))

#perform stacking
def define_stacked_model(members):
    for i in range(len(members)):
        model = members[i]
        for layer in model.layers:
        # make not trainable
            layer.trainable = False
            # rename to avoid 'unique layer name' issue
            layer.name = 'ensemble_' + str(i+1) + '_' + layer.name
    # define multi-headed input
    ensemble_visible = [model.input]
    # concatenate merge output from each model
    ensemble_outputs = [model.output for model in members]
    merge = keras.layers.concatenate(ensemble_outputs)
    hidden = Dense(6, activation='relu')(merge) 
    output = Dense(2, activation='softmax')(hidden)
    model = Model(inputs=ensemble_visible, outputs=output)
    # compile
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

# define ensemble model
stacked_model = define_stacked_model(members)
stacked_model.summary()

The ensemble model is expected to have a multi-headed input and a single output. Upon running the code, I get the graph disconnected error like this:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_2_2:0", shape=(?, 256, 256, 3), dtype=float32) at layer "ensemble_2_input_2". The following previous layers were accessed without issue: ['ensemble_3_input_2']

Kindly help resolve the error.

shiva
  • 311
  • 3
  • 6
  • 15
  • 1
    You have a major bug. I believe it is ensemble_visible=[model_input]. But I think the graph still disconnected since the model parts input is still not connected to input layer so you still need to construct the whole stuff. I cannot write you the complete code right now but hopefully you get the idea. – Yohanes Alfredo Dec 31 '19 at 19:57

0 Answers0