How to fine-tune a functional model in Keras?

hirschme

Taking a pre-trained model in Keras and replacing the top classification layer to retrain the network to a new task has several examples using a Sequential model in Keras. A sequential model has methods model.pop() and model.add() which make this fairly easy.

However, how is this achieved when using a functional model? This framework does not have method model.add().

How can I load a pretrained functional model in Keras, crop the last layer and replace it with a new one?

Current approach so far:

model.load_weights('/my_model_weights.h5')

def pop_layer(model):
    if not model.outputs:
    raise Exception('Sequential model cannot be popped: model is empty.')

    model.layers.pop()
    if not model.layers:
        model.outputs = []
        model.inbound_nodes = []
        model.outbound_nodes = []
    else:
        model.layers[-1].outbound_nodes = []
        model.outputs = [model.layers[-1].output]
    model.built = False

# Remove last layer with custom function (from another post)
pop_layer(model)

# Now add a new layer to the model ???

model.add(Dense(2, activation='softmax', name='fc2'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd',
              metrics=['accuracy'])

AttributeError: 'Model' object has no attribute 'add'

giser_yugang

You can use a pretrained functional model with the last layer removed as a layer. You may think of a model as a "bigger layer". Then redefine a new model that wraps "bigger layer" and a new layer.

An example:

import tensorflow as tf
from keras.layers import Dense,Input
from keras.models import Sequential,Model

input_tensor = Input(shape=(64,))
x = Dense(32, activation='relu')(input_tensor)
x = Dense(32, activation='relu')(x)
output_tensor = Dense(10, activation=tf.nn.softmax)(x)
model = Model(input_tensor, output_tensor)
model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd',
              metrics=['accuracy'])
print(model.summary())
model.save_weights('my_model_weights.h5')
# 
model.load_weights('my_model_weights.h5')

def pop_layer(model):
    if not model.outputs:
        raise Exception('Sequential model cannot be popped: model is empty.')
    model.layers.pop()
    if not model.layers:
        model.outputs = []
        model.inbound_nodes = []
        model.outbound_nodes = []
    else:
        model.layers[-1].outbound_nodes = []
        model.outputs = [model.layers[-1].output]
    return model

# Remove last layer with custom function (from another post)
model_old = pop_layer(model)
# Now add a new layer to the model
model_new = Sequential()
model_new.add(model_old)
model_new.add(Dense(2, activation=tf.nn.softmax, name='fc2'))
model_new.compile(loss='sparse_categorical_crossentropy', optimizer='sgd',
              metrics=['accuracy'])
print(model_new.summary())

As a result, you can see that the parameters of the last layer of pretrained functional model are missing.

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                2080      
_________________________________________________________________
dense_2 (Dense)              (None, 32)                1056      
_________________________________________________________________
dense_3 (Dense)              (None, 10)                330       
=================================================================
Total params: 3,466
Trainable params: 3,466
Non-trainable params: 0
_________________________________________________________________
None

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
model_1 (Model)              multiple                  3136      
_________________________________________________________________
fc2 (Dense)                  (None, 2)                 66        
=================================================================
Total params: 3,202
Trainable params: 3,202
Non-trainable params: 0
_________________________________________________________________
None

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

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

分類Dev

Fine-Tuning Keras model

分類Dev

How to use the first layers of a pretrained model to extract features inside a Keras model (Functional API)

分類Dev

How to build a keras model

分類Dev

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

分類Dev

How to retrain/update keras model?

分類Dev

mlr: Tune model parameters with validation set

分類Dev

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

分類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

How to tune an Oracle SQL query

分類Dev

scheduler: how to tune cfq to favor interactive processes

分類Dev

Eclipse PDT: how to tune content assist proposals?

分類Dev

How can I convert a trained Tensorflow model to Keras?

分類Dev

How do I fit the model of two concatenate LSTM in keras?

分類Dev

In Keras, how to send each item in a batch through a model?

分類Dev

How to Create a Trained Keras Model Through Setting the Weights

分類Dev

LSTM, Keras : How many layers should the inference model have?

分類Dev

Is there a GPT-2 implementation that allows me to fine-tune and prompt for text completion?

分類Dev

keras functional api and multiple merge layers

分類Dev

Keras model not learning

分類Dev

Using a keras model in a custom keras loss

分類Dev

Keras model.compile: metrics to be evaluated by the model

分類Dev

student-teacher model in keras

分類Dev

Keras deep learning model to android

分類Dev

Keras model.fit UnboundLocalError

分類Dev

Train a model using lstm and keras

分類Dev

cannot instantiate an Xception model in Keras

Related 関連記事

  1. 1

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

  2. 2

    Fine-Tuning Keras model

  3. 3

    How to use the first layers of a pretrained model to extract features inside a Keras model (Functional API)

  4. 4

    How to build a keras model

  5. 5

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

  6. 6

    How to retrain/update keras model?

  7. 7

    mlr: Tune model parameters with validation set

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

    How to pass extracted features to keras model?

  12. 12

    How to tune an Oracle SQL query

  13. 13

    scheduler: how to tune cfq to favor interactive processes

  14. 14

    Eclipse PDT: how to tune content assist proposals?

  15. 15

    How can I convert a trained Tensorflow model to Keras?

  16. 16

    How do I fit the model of two concatenate LSTM in keras?

  17. 17

    In Keras, how to send each item in a batch through a model?

  18. 18

    How to Create a Trained Keras Model Through Setting the Weights

  19. 19

    LSTM, Keras : How many layers should the inference model have?

  20. 20

    Is there a GPT-2 implementation that allows me to fine-tune and prompt for text completion?

  21. 21

    keras functional api and multiple merge layers

  22. 22

    Keras model not learning

  23. 23

    Using a keras model in a custom keras loss

  24. 24

    Keras model.compile: metrics to be evaluated by the model

  25. 25

    student-teacher model in keras

  26. 26

    Keras deep learning model to android

  27. 27

    Keras model.fit UnboundLocalError

  28. 28

    Train a model using lstm and keras

  29. 29

    cannot instantiate an Xception model in Keras

ホットタグ

アーカイブ