title: Ensemble resnet50 and densenet121 in keras
body: <p>I want to do a ensemble of resnet50 and desnsenet121, but got an error:</p>
  
  <p>Graph disconnected: cannot obtain value for tensor Tensor("input_8:0", shape=(?, 224, 224, 3), dtype=float32) at layer "input_8". The following previous layers were accessed without issue: []</p>
  
  <p>Below is my code for ensembling:</p>
  
  
  
  <pre class="lang-python prettyprint-override"><code>from keras import applications
  from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
  from keras.models import Model, Input
  #from keras.engine.topology import Input
  from keras.layers import Average
  
  def resnet50():
      base_model = applications.resnet50.ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
      last = base_model.output
      x = Flatten()(last)
      x = Dense(2000, activation='relu')(x)
      preds = Dense(200, activation='softmax')(x)
      model = Model(base_model.input, preds)
      return model
  
  def densenet121():
      base_model = applications.densenet.DenseNet121(weights='imagenet', include_top=False, input_shape=(224,224, 3))
      last = base_model.output
      x = Flatten()(last)
      x = Dense(2000, activation='relu')(x)
      preds = Dense(200, activation='softmax')(x)
      model = Model(base_model.input, preds)
      return model
  
  resnet50_model = resnet50()
  densenet121_model = densenet121()
  ensembled_models = [resnet50_model,densenet121_model]
  def ensemble(models,model_input):
      outputs = [model.outputs[0] for model in models]
      y = Average()(outputs)
      model = Model(model_input,y,name='ensemble')
      return model
  
  model_input = Input(shape=(224,224,3))
  ensemble_model = ensemble(ensembled_models,model_input)
  </code></pre>
  
  <p>I thought the reason is when I combine reset50 and densenet121, they have their own input layer, even though I make the input shape to be the same. Different input layer leads to conflict. That's just my guess and I am not sure how to fix it</p>