TrainingExtensions/tensorflow/src/python/aimet_tensorflow/keras/utils/common.py
def replace_layer_in_functional_model(model: tf.keras.Model, old_layer: tf.keras.layers.Layer,
                                      new_layers: typing.Union[typing.List, tf.keras.layers.Layer]):
    """
    Replace a layer in a model with a list of new layers to be called in sequential order.
    :param model: Model containing layer to replace
    :param old_layer: Layer to replace
    :param new_layers: Layer or list of new layers to insert into the model
    """
    # pylint: disable=protected-access
    if old_layer in model._input_layers:
        _logger.error('Replacement for input layers not currently supported')
        raise NotImplementedError

    if len(old_layer.inbound_nodes) > 1:
        _logger.error('Replacement for layers used multiple times not currently supported')
        raise NotImplementedError

    if not isinstance(new_layers, list):
        new_layers = [new_layers]

    residing_model = _get_residing_model_of_layer(model, old_layer)

    # Find layers before and after the old layer to replace
    # Need to use keras_inputs instead of input_layers due to bug where only one input layer will be shown
    parent_layers = [keras_input._keras_history.layer for keras_input in old_layer.inbound_nodes[0].keras_inputs]
    following_layers_and_inputs_dict = _get_following_layers_and_inputs(residing_model, old_layer)

    # Clear out certain outbound nodes of parent layers, inbound node of old layer, outbound nodes of old layer, and
    # certain inbound nodes of following layers.
    _clear_inbound_and_outbound_nodes(old_layer, parent_layers, following_layers_and_inputs_dict)

    # Remove network nodes from the residing model for old_layer and following layers
    _remove_network_nodes(residing_model, [old_layer] + list(following_layers_and_inputs_dict.keys()))

    # Call new layers in sequence to create new nodes
    out_tensor = _call_new_layers_sequentially(parent_layers, new_layers)

    # Connect the last new layer back to old child layer, creating a new node in the process
    if following_layers_and_inputs_dict:
        _link_following_layers_to_new_layer_output(out_tensor, following_layers_and_inputs_dict, old_layer)
    else:
        _update_model_output_info(residing_model, old_layer, out_tensor)

    # Update model's layers and network nodes
    residing_model._insert_layers(new_layers + list(following_layers_and_inputs_dict.keys()))

    Replace a layer in a model with a list of new layers to be called in sequential order.
    :param model: Model containing layer to replace
    :param old_layer: Layer to replace
    :param new_layers: Layer or list of new layers to insert into the model
    
Replace a layer in a model with a list of new layers to be called in sequential order.
Replace a layer in a model with a list of new layers to be called in sequential order .
def replace_layer_in_functional_model(model: tf.keras.Model, old_layer: tf.keras.layers.Layer,
                                      new_layers: typing.Union[typing.List, tf.keras.layers.Layer]):
    if old_layer in model._input_layers:
        _logger.error('Replacement for input layers not currently supported')
        raise NotImplementedError
    if len(old_layer.inbound_nodes) > 1:
        _logger.error('Replacement for layers used multiple times not currently supported')
        raise NotImplementedError
    if not isinstance(new_layers, list):
        new_layers = [new_layers]
    residing_model = _get_residing_model_of_layer(model, old_layer)
    parent_layers = [keras_input._keras_history.layer for keras_input in old_layer.inbound_nodes[0].keras_inputs]
    following_layers_and_inputs_dict = _get_following_layers_and_inputs(residing_model, old_layer)
    _clear_inbound_and_outbound_nodes(old_layer, parent_layers, following_layers_and_inputs_dict)
    _remove_network_nodes(residing_model, [old_layer] + list(following_layers_and_inputs_dict.keys()))
    out_tensor = _call_new_layers_sequentially(parent_layers, new_layers)
    if following_layers_and_inputs_dict:
        _link_following_layers_to_new_layer_output(out_tensor, following_layers_and_inputs_dict, old_layer)
    else:
        _update_model_output_info(residing_model, old_layer, out_tensor)
    residing_model._insert_layers(new_layers + list(following_layers_and_inputs_dict.keys()))
def replace_layer_in_functional_model ( model : tf . keras . Model , old_layer : tf . keras . layers . Layer , new_layers : typing . Union [ typing . List , tf . keras . layers . Layer ] ) : if old_layer in model . _input_layers : _logger . error ( 'Replacement for input layers not currently supported' ) raise NotImplementedError if len ( old_layer . inbound_nodes ) > 1 : _logger . error ( 'Replacement for layers used multiple times not currently supported' ) raise NotImplementedError if not isinstance ( new_layers , list ) : new_layers = [ new_layers ] residing_model = _get_residing_model_of_layer ( model , old_layer ) parent_layers = [ keras_input . _keras_history . layer for keras_input in old_layer . inbound_nodes [ 0 ] . keras_inputs ] following_layers_and_inputs_dict = _get_following_layers_and_inputs ( residing_model , old_layer ) _clear_inbound_and_outbound_nodes ( old_layer , parent_layers , following_layers_and_inputs_dict ) _remove_network_nodes ( residing_model , [ old_layer ] + list ( following_layers_and_inputs_dict . keys ( ) ) ) out_tensor = _call_new_layers_sequentially ( parent_layers , new_layers ) if following_layers_and_inputs_dict : _link_following_layers_to_new_layer_output ( out_tensor , following_layers_and_inputs_dict , old_layer ) else : _update_model_output_info ( residing_model , old_layer , out_tensor ) residing_model . _insert_layers ( new_layers + list ( following_layers_and_inputs_dict . keys ( ) ) )
Replace a layer in a model with a list of new layers to be called in sequential order.
Replace a layer in a model with a list of new layers to be called in sequential order .
"""
    Replace a layer in a model with a list of new layers to be called in sequential order.
    :param model: Model containing layer to replace
    :param old_layer: Layer to replace
    :param new_layers: Layer or list of new layers to insert into the model
    """ # pylint: disable=protected-access # Find layers before and after the old layer to replace # Need to use keras_inputs instead of input_layers due to bug where only one input layer will be shown # Clear out certain outbound nodes of parent layers, inbound node of old layer, outbound nodes of old layer, and # certain inbound nodes of following layers. # Remove network nodes from the residing model for old_layer and following layers # Call new layers in sequence to create new nodes # Connect the last new layer back to old child layer, creating a new node in the process # Update model's layers and network nodes
model
tf.keras.Model
Model containing layer to replace
Model containing layer to replace
old_layer
tf.keras.layers.Layer
Layer to replace
Layer to replace
new_layers
typing.Union[typing.List, tf.keras.layers.Layer]
Layer or list of new layers to insert into the model
Layer or list of new layers to insert into the model