ValueError: Graph disconnected: cannot obtain value for tensor Tensor

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.

Topic ensemble-learning keras deep-learning neural-network classification

Category Data Science

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.