Reputation: 615
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?
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.
Upvotes: 6
Views: 5115
Reputation: 2612
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
Upvotes: 3