The code that I have (that I can't change) uses the Resnet with my_input_tensor
as the input_tensor.
model1 = keras.applications.resnet50.ResNet50(input_tensor=my_input_tensor, weights='imagenet')
Investigating the source code, ResNet50 function creates a new keras Input Layer with my_input_tensor
and then create the rest of the model. This is the behavior that I want to copy with my own model. I load my model from h5 file.
model2 = keras.models.load_model('my_model.h5')
Since this model already has an Input Layer, I want to replace it with a new Input Layer defined with my_input_tensor
.
How can I replace an input layer?
When you saved your model using:
old_model.save('my_model.h5')
it will save following:
So then, when you load the model:
res50_model = load_model('my_model.h5')
you should get the same model back, you can verify the same using:
res50_model.summary() res50_model.get_weights()
Now you can, pop the input layer and add your own using:
res50_model.layers.pop(0) res50_model.summary()
add new input layer:
newInput = Input(batch_shape=(0,299,299,3)) # let us say this new InputLayer newOutputs = res50_model(newInput) newModel = Model(newInput, newOutputs) newModel.summary() res50_model.summary()
Layers.pop(0) or anything like that doesn't work.
You have two options that you can try:
1.
You can create a new model with the required layers.
A relatively easy way to do this is to i) extract the model json configuration, ii) change it appropriately, iii) create a new model from it, and then iv) copy over the weights. I'll just show the basic idea.
i) extract the configuration
model_config = model.get_config()
ii) change the configuration
input_layer_name = model_config['layers'][0]['name']
model_config['layers'][0] = {
'name': 'new_input',
'class_name': 'InputLayer',
'config': {
'batch_input_shape': (None, 300, 300),
'dtype': 'float32',
'sparse': False,
'name': 'new_input'
},
'inbound_nodes': []
}
model_config['layers'][1]['inbound_nodes'] = [[['new_input', 0, 0, {}]]]
model_config['input_layers'] = [['new_input', 0, 0]]
ii) create a new model
new_model = model.__class__.from_config(model_config, custom_objects={}) # change custom objects if necessary
ii) copy weights
# iterate over all the layers that we want to get weights from
weights = [layer.get_weights() for layer in model.layers[1:]]
for layer, weight in zip(new_model.layers[1:], weights):
layer.set_weights(weight)
2.
You can try a library like kerassurgeon (I am linking to a fork that works with the tensorflow keras version). Note that insertion and deletion operations only work under certain conditions such as compatible dimensions.
from kerassurgeon.operations import delete_layer, insert_layer
model = delete_layer(model, layer_1)
# insert new_layer_1 before layer_2 in a model
model = insert_layer(model, layer_2, new_layer_3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With