How to remove the FC layer off of a fine turned model keras

MNM

So I have finetuned a Resnet50 model with the following architecture:

model = models.Sequential()
model.add(resnet)
model.add(Conv2D(512, (3, 3), activation='relu'))
model.add(Conv2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(layers.Dense(2048, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(736, activation='softmax')) # Output layer

So now I have a saved model (.h5) which I want to use as input into another model. But I don't want the last layer. I would normally do it like this with a base resnet50 model:

def base_model():
    resnet = resnet50.ResNet50(weights="imagenet", include_top=False)
    x = resnet.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(4096, activation='relu')(x)
    x = Dropout(0.6)(x)
    x = Dense(4096, activation='relu')(x)
    x = Dropout(0.6)(x)
    x = Lambda(lambda  x_: K.l2_normalize(x,axis=1))(x)
    return Model(inputs=resnet.input, outputs=x)

but that does not work for the model as it gives me an error. I am trying it like this right now but still, it does not work.

def base_model():
    resnet = load_model("../Models/fine_tuned_model/fine_tuned_resnet50.h5")
    x = resnet.layers.pop()
    #resnet = resnet50.ResNet50(weights="imagenet", include_top=False)
    #x = resnet.output
    #x = GlobalAveragePooling2D()(x)
    x = Dense(4096, activation='relu')(x)
    x = Dropout(0.6)(x)
    x = Dense(4096, activation='relu')(x)
    x = Dropout(0.6)(x)
    x = Lambda(lambda  x_: K.l2_normalize(x,axis=1))(x)
    return Model(inputs=resnet.input, outputs=x)
enhanced_resent = base_model()

This is the error that it gives me.

Layer dense_3 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.core.Dense'>. Full input: [<keras.layers.core.Dense object at 0x000001C61E68E2E8>]. All inputs to the layer should be tensors.

I don't know if I can do this or not.

MNM

I have finally figured it out after quitting for an hour. So this is how you will do it.

def base_model():
    resnet = load_model("../Models/fine_tuned_model/42-0.85.h5")
    x = resnet.layers[-2].output
    x = Dense(4096, activation='relu', name="FC1")(x)
    x = Dropout(0.6, name="FCDrop1")(x)
    x = Dense(4096, activation='relu', name="FC2")(x)
    x = Dropout(0.6, name="FCDrop2")(x)
    x = Lambda(lambda  x_: K.l2_normalize(x,axis=1))(x)
    return Model(inputs=resnet.input, outputs=x)
enhanced_resent = base_model()

And this works perfectly. I hope this helps out someone else as I have never seen this done in any tutorial before.

x = resnet.layers[-2].output

This will get the layer you want, but you need to know which index the layer you want is at. -2 is the 2nd to last FC layer that I wanted as I wanted the feature extractions, not the final classification. This can be found doing a

model.summary()

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to fine-tune a functional model in Keras?

分類Dev

How to replace (or insert) intermediate layer in Keras model?

分類Dev

Fine-Tuning Keras model

分類Dev

How to find out what turned off the computer?

分類Dev

How to ignore some input layer, while predicting, in a keras model trained with multiple input layers?

分類Dev

How to get sensor data in service when screen is turned off?

分類Dev

How to cache layer activations in Keras?

分類Dev

How to build a keras model

分類Dev

Keras model working fine locally but won't work on Flask API

分類Dev

Can I safely remove a usb device from a win 7 computer that is turned off?

分類Dev

notebook turned off while upgrading

分類Dev

How to retrain/update keras model?

分類Dev

How can -Wgnu-zero-variadic-macro-arguments warning be turned off with Clang?

分類Dev

TensorFlow-tf.keras.layers.Layerとtf.keras.Modelの違い

分類Dev

How to correctly use an intermediate layer of a vgg model

分類Dev

SCRIPT1030: Conditional compilation is turned off

分類Dev

Would there be any problems with DEP turned off?

分類Dev

How to specify the axis when using the softmax activation in a Keras layer?

分類Dev

how to build Sequence-to-sequence autoencoder in keras with embedding layer?

分類Dev

How to use keras embedding layer with 3D tensor input?

分類Dev

How to support masking in custom tf.keras.layers.Layer

分類Dev

How to specify padding with keras in Conv2D layer?

分類Dev

How to implement custom output layer with dynamic shape in Keras?

分類Dev

Why layer number in keras does not start from 1 after deleting and recreating model?

分類Dev

Segmentation violation (SIGSEGV) when loading a Keras/Tensorflow model thats uses a Lambda layer

分類Dev

How to enforce monotonicity for (regression) model outputs in Keras?

分類Dev

How to save and reuse all settings for a keras model?

分類Dev

How to pass extracted features to keras model?

分類Dev

Modify layer parameters in Keras

Related 関連記事

  1. 1

    How to fine-tune a functional model in Keras?

  2. 2

    How to replace (or insert) intermediate layer in Keras model?

  3. 3

    Fine-Tuning Keras model

  4. 4

    How to find out what turned off the computer?

  5. 5

    How to ignore some input layer, while predicting, in a keras model trained with multiple input layers?

  6. 6

    How to get sensor data in service when screen is turned off?

  7. 7

    How to cache layer activations in Keras?

  8. 8

    How to build a keras model

  9. 9

    Keras model working fine locally but won't work on Flask API

  10. 10

    Can I safely remove a usb device from a win 7 computer that is turned off?

  11. 11

    notebook turned off while upgrading

  12. 12

    How to retrain/update keras model?

  13. 13

    How can -Wgnu-zero-variadic-macro-arguments warning be turned off with Clang?

  14. 14

    TensorFlow-tf.keras.layers.Layerとtf.keras.Modelの違い

  15. 15

    How to correctly use an intermediate layer of a vgg model

  16. 16

    SCRIPT1030: Conditional compilation is turned off

  17. 17

    Would there be any problems with DEP turned off?

  18. 18

    How to specify the axis when using the softmax activation in a Keras layer?

  19. 19

    how to build Sequence-to-sequence autoencoder in keras with embedding layer?

  20. 20

    How to use keras embedding layer with 3D tensor input?

  21. 21

    How to support masking in custom tf.keras.layers.Layer

  22. 22

    How to specify padding with keras in Conv2D layer?

  23. 23

    How to implement custom output layer with dynamic shape in Keras?

  24. 24

    Why layer number in keras does not start from 1 after deleting and recreating model?

  25. 25

    Segmentation violation (SIGSEGV) when loading a Keras/Tensorflow model thats uses a Lambda layer

  26. 26

    How to enforce monotonicity for (regression) model outputs in Keras?

  27. 27

    How to save and reuse all settings for a keras model?

  28. 28

    How to pass extracted features to keras model?

  29. 29

    Modify layer parameters in Keras

ホットタグ

アーカイブ