Keras replacing input layer

前端 未结 6 2089
清酒与你
清酒与你 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条回答
  •  借酒劲吻你
    2020-11-30 08:08

    This should be pretty easy with kerassurgeon. First you need to install the library; depending on if you are using Keras through TensorFlow (with tf 2.0 and up) or Keras as a separate library, it needs to be installed in different ways.

    For Keras in TF: pip install tfkerassurgeon (https://github.com/Raukk/tf-keras-surgeon). For standalone Keras: pip install kerassurgeon (https://github.com/BenWhetton/keras-surgeon)

    To replace the input (example with TF 2.0; currently untested code):

    from tensorflow import keras  # or import keras for standalone version
    from tensorflow.keras.layers import Input
    
    model = keras.models.load_model('my_model.h5')
    my_input_tensor = Input(input_shape=(260, 260, 3))
    
    # or kerassurgeon for standalone Keras
    from tfkerassurgeon import delete_layer, insert_layer
    
    model = delete_layer(model.layers[0])
    # inserts before layer 0
    model = insert_layer(model.layers[0], my_input_tensor)
    

提交回复
热议问题