问题描述
我有两个预训练模型,一个用于年龄分类,另一个用于性别分类.我想制作年龄-性别分类器网络,所以我想合并两个网络并从同一网络中预测年龄和性别.
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 .
我尝试的是
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
推荐答案
这取决于合并"对您意味着什么.
如果你想从单个输入中输出年龄和性别,那么你需要多个头":
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])
这篇关于如何在keras tensoflow中合并两个模型以制作一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!