title: keras : add layers to another model
body: <p>I need to add layers to an existing model. However, I need to add the layers at &quot;the main model level&quot;, that is I can't use the classic functional approach. For example, if I use something like:</p>
  <pre><code>from keras.layers import Dense,Reshape, Input
  inp = Input(shape=(15,))
  d1 = Dense(224*224*3, activation='linear')(inp)
  r1 = Reshape(input_shape)
  from keras import Model
  model_mod = r1(d1)
  model_mod = mobilenet(model_mod)
  model_mod = Model(inp, model_mod)
  </code></pre>
  <p>I obtain:</p>
  <pre><code>Layer (type)                 Output Shape              Param #   
  =================================================================
  input_5 (InputLayer)         (None, 15)                0         
  _________________________________________________________________
  dense_4 (Dense)              (None, 150528)            2408448   
  _________________________________________________________________
  reshape_4 (Reshape)          (None, 224, 224, 3)       0         
  _________________________________________________________________
  mobilenet_1.00_224 (Model)   (None, 1000)              4253864 
  </code></pre>
  <p>So, I obtain a model with a nested submodel. Instead, I would the nested submodel's layers (mobilenet) &quot;added&quot; to the new top layers (that is, after reshape_4). I tried with:</p>
  <pre><code>modelB_input = modelB.input
  for layer in modelB.layers:
      if layer == modelB_input:
          continue
      modelA.add(layer)  
  </code></pre>
  <p>It works for simple sequential models (e.g., vgg, mobilenet) but with more complex models with connections not strictly sequential (e.g., inception,resnet) this code is not good.
  Any ideas?</p>