Israel-abebe
Israel-abebe

Reputation: 578

How to merge two models in keras tensoflow to make one model

I have two pre-trained models one is for age classification and the other is for Gender Classification. I wanted to make age-gender classifier network so i wanted to merge the two networks and pridict both age and gender from same network .

what i tried was

from keras.models import load_model

model_age = load_model(model_age_path)
model_gender = load_model(model_gender_path)

how can i merge the two models and train network that do both

Upvotes: 3

Views: 1992

Answers (1)

ldavid
ldavid

Reputation: 2552

It depends on what "merge" means to you.
If you want to output both age and gender from a single input, then you need multiple "heads":

from keras import Input
from keras.models import load_model

model_age = load_model('age.hdf5')
model_gender = load_model('gender.hdf5')

x = Input(shape=[299, 299, 3])
y_age = model_age(x)
y_gen = model_gender(x)

model = Model(inputs=x, outputs=[y_age, y_gen])

data, target = load_data()
p_age, p_gender = model.predict(data)

print(p_age)
# [[ 0.57398415,  0.42601582],
#  [ 0.5397228 ,  0.46027723],
#  [ 0.6648131 ,  0.33518684],
#  [ 0.5917415 ,  0.4082585 ]]

print(p_gender)
# [[ 0.13119246],
#  [ 0.        ],
#  [ 0.1875571 ],
#  [ 0.        ]]

But now considers this: both tasks (regress age, classify gender) have -- to some extent -- some degree of similarity, right? If your data is composed by images, for example, both need to detect lines, patches and simple geometric forms to make their decision. In other words, a lot of conv layers' weights could be reused by both networks, making the whole process more efficient. You can achieve this training a new model that do both things at the same time:

from keras.applications import VGG19

base_model = VGG19(weights='imagenet')  # or any model, really.
y = base_model.output
y_age = Dense(1, activation='relu')(y)

y = base_model.output
y = Dense(128, activation='relu')(y)
y = Dense(128, activation='relu')(y)
y_gender = Dense(2, activation='softmax')(y)

model = Model(inputs=base_model.inputs, outputs=[y_age, y_gender])

Upvotes: 2

Related Questions