Keras replacing input layer

前端 未结 6 2131
清酒与你
清酒与你 2020-11-30 07:29

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.ResNe         


        
6条回答
  •  猫巷女王i
    2020-11-30 08:07

    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)
    

提交回复
热议问题