Is there any way to change a connection in a pre-trained tensorflow model and use them while keeping the all other layers above, in-between and below them unchanged?
Say I have a pre-trained model and I want to change a specific connection.
Say a model has:
`
Input
|
v
layer1
|
v
layer2 ----------------------
| |
v |
layer3 ----------- |
| | |
v | |
layer4 | |
| | |
v | |
layer5 <---------- |
| |
v |
layer6 <---------------------
|
v
Dense
`
They are all connected but there is also a skip connection between:
layer2
andlayer6
layer3
andlayer5
Now I want to insert a new layer say layer4_1
on the skip connection between layer3
and layer5
So it becomes:
Input
|
v
layer1
|
v
layer2 ----------------------
| |
v |
layer3 ----------- |
| | |
v v |
layer4 layer4_1 |
| | |
v | |
layer5 <---------- |
| |
v |
layer6 <---------------------
|
v
Dense
The rest of the connections remain the same even the connection between layer2
and layer6
.
I tried the following but it doesn’t work:
def mymodel(input_shape, n_classes):
pretrained_model = net(input_shape, n_classes)
desired_layer = pretrained_model.get_layer('block_13_expand_relu') # layer3
desired_layer_output = desired_layer.output
new_layer = NewLayer(top=2) # layer4_1
new_output = new_layer(desired_layer_output)
merged_output = new_output
upsampling_layer = pretrained_model.get_layer("decoder_stage0_upsampling") # layer4
upsampling_layer_output = upsampling_layer.output
merged_output = tf.keras.layers.Concatenate()([upsampling_layer_output, merged_output]) # tried changing layer5 as couldn't do it otherwise
decoder_conv_layer = pretrained_model.get_layer("decoder_stage0_concat") # layer6
new_model = Model(inputs=decoder_conv_layer.output, outputs=pretrained_model.output)
output = new_model(merged_output)
model = Model(inputs=pretrained_model.input, outputs=output)
return model
Sorry can’t give original pretrained model structure as it is too big.
Can someone please help me?
The error message I am receiving is:
ValueError: Found input tensor cannot be reached given provided
output tensors. Please make sure the tensor
KerasTensor(type_spec=TensorSpec(shape=(None, 256, 256, 3),
dtype=tf.float32, name='input_1'), name='input_1', description="created
by layer 'input_1'") is included in the model inputs when building
functional model.