I have an efficientnet and I would like to insert a custom layer after each pooling layer, right before the ZeroPadding2D layer, and right after the body of the network (before the top).
def cyclify_efficientnet(model):
model = keras.models.clone_model(model)
# Cyclization from this paper http://proceedings.mlr.press/v48/dieleman16.pdf
for layer in model.layers:
# add custom layer 1 after each pooling layer
if isinstance(layer, keras.layers.GlobalAveragePooling2D):
prev_input = layer.input
inserted_layer = CustomLayer()(prev_input)
layer = layer(inserted_layer)) # Problem: reassignment doesn't change the layer itself
# layer.input = inserted_layer
#??? does not run. Readonly.
if isinstance(layer, keras.layers.ZeroPadding2D):
...
# new_model = keras.Model(???)
# return new_model
cyclify_efficientnet(my_model)
In particular, I'm looking at the #Probelm: comment. I would like to do something like this:
input -> layer1 -> layer2 ->layer3 => input -> layer1 -> customlayer -> layer2 ->layer3
But after I get input -> layer1 -> customlayer I don't know how to make layer2 take customlayer as an input while still being connected to layer3 (or more or less the rest of the network). Efficientnet is also nonsequential.
How is this done? I've looked into kerassurgeon but it doesn't seem to do what I'm looking for.