6

I want to use vgg16 pre-trained model of keras. I have notice some strange behavior when trying to change the model.

1) I have add some layers of the per-trained model. My problem is that tensorboard is showing the layers of the model that I didn't add into the sequence model. This is strange because I have also deleted the imported model. I think this have to do with the dependency between layers so I want to remove this dependencies. How can I do this?

enter image description here

For example in this picture there is two layers that I didn't add but they are showing in the graph

vgg16_model = keras.applications.vgg16.VGG16()


cnnModel = keras.models.Sequential()

for layer in vgg16_model.layers[0:13]:
    cnnModel.add(layer)

for layer in vgg16_model.layers[14:16]:
    cnnModel.add(layer)

for layer in vgg16_model.layers[17:21]:
    cnnModel.add(layer)

cnnModel.add(keras.layers.Dense(2048, name="compress_1"))
cnnModel.add(keras.layers.Dense(1024, name="compress_2"))
cnnModel.add(keras.layers.Dense(512, name="compress_3"))


for layer in cnnModel.layers[0:4]:
    layer.trainable = False

del vgg16_model

2) the second problem occurs when using cnnModel.pop(). In fact I have add all the layers but I do a pop to the layer I don't want before adding the next one this is the error I get.

Layer block4_conv2 has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `get_output_at(node_index)` instead.

And this is the code I am using:

for layer in vgg16_model.layers[0:14]:
    cnnModel.add(layer)

cnnModel.pop()

for layer in vgg16_model.layers[14:17]:
    cnnModel.add(layer)

cnnModel.pop()

for layer in vgg16_model.layers[17:21]:
    cnnModel.add(layer)

cnnModel.pop() is working the problem only occurs when trying to add the next layer.

Thank you for your help.

2
  • Did you solve it? i'm facing the same case
    – Luis Leal
    Aug 1, 2020 at 5:57
  • I also have the same issue. But funny thing is that, when I plot the model the discarded layers don't show up although TensorBoard still shows them. Use from tensorflow.keras.utils import plot_model plot_model(model, to_file = 'model_plot.png', show_shapes = True, show_layer_names = True) to plot the model and see. My only concern is that, I don't want the unnecessary layers increase the computational cost.
    – hafiz031
    Dec 2, 2020 at 13:30

1 Answer 1

3

You can try using Model instead of Sequential, like:

vgg16_model = keras.applications.vgg16.VGG16()

drop_layers = [13, 16]

input_layer = x = vgg16_model.input

for i, layer in enumerate(vgg16_model.layers[1:], 1):
    if i not in drop_layers:
        x = layer(x)

x = keras.layers.Dense(2048, name="compress_1")(x)
x = keras.layers.Dense(1024, name="compress_2")(x)
x = keras.layers.Dense(512, name="compress_3")(x)

cnnModel = keras.models.Model(inputs = input_layer, outputs = x)

for layer in cnnModel.layers[0:4]:
    layer.trainable = False

del vgg16_model
5
  • it didn't work. I still have the parallel layer I don't think del vgg16_model is working. Apr 5, 2018 at 19:54
  • And if you do cnnModel.summary() what do you see it?
    – ebeneditos
    Apr 5, 2018 at 19:57
  • I see the correct layers. it cannot show me the parallel layer because they are related to vgg16_model. I have also tried del layer in the loop for the two layer that I should drop but this is not working Apr 5, 2018 at 20:00
  • Thank you .. Model instead of Sequential can solve Layer block4_conv2 has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use get_output_at(node_index) instead.
    – N.IT
    Dec 16, 2018 at 17:21
  • Does this work if one of the layers requires multiple inputs? Mar 30, 2021 at 2:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.