Madhuri Panwar
Madhuri Panwar

Reputation: 39

How to do transfer-learning on our own models?

I am trying to apply the transfer-learning on my CNN model, I am getting the below error.

model = model1(weights = "model1_weights", include_top=False)

—-

TypeError: __call__() takes exactly 2 arguments (1 given)

Thanks

Upvotes: 1

Views: 9068

Answers (1)

Mitiku
Mitiku

Reputation: 5412

If you are trying to use transfer-learning using custom model, the answer depends on the way you saved your model architecture(description) and weights.

1. If you saved the description and weights of the model on single .h5 file.

You can easily load model, using keras's load_model method.

from keras.models import load_model
model = load_model("model_path.h5")

2. If you saved the description and weights of the model on separate file (e.g in json and .h5 files respectively).

You can first load the model description from json file and then load model weights.

form keras.models import model_from_json
with open("path_to_json_file.json") as json_file:
    model = model_from_json(json_file.read())
    model.load_weights("path_to_weights_file.h5")

After the old model is loaded you can now decide which layers to discard(usually these layers are top fully connected layers) and which layers to freeze. Let's assume you want to use the first five layers of the model without training again, the next three to be trained again, the last layers to be discarded(here it is assumed that the number of the network layers is greater than eight), and add three fully connected layer after the last layer. This can be done as follows.

Freeze the first five layers

for i in range(5):
    model.layers[i].trainable = False

Make the next three layers trainable, this can be ignored if all layers are trainable.

for i in range(5,8):
    model.layers[i].trainable = True

Add three more layers

ll = model.layers[8].output
ll = Dense(32)(ll)
ll = Dense(64)(ll)
ll = Dense(num_classes,activation="softmax")(ll)

new_model = Model(inputs=model.input,outputs=ll)

Upvotes: 16

Related Questions