yu4u / age-gender-estimation

Keras implementation of a CNN network for age and gender estimation
MIT License
1.46k stars 501 forks source link

training on groups and values #94

Open aezco opened 5 years ago

aezco commented 5 years ago

I am trying to improve the age estimation model by first training different age group models and then combine into 1 final model to predict 101 classes. The code is below:

modelA = load_model('/content/8-base_model_1.h5') modelA.load_weights('/content/8-weights.002-0.991-0.406.hdf5')

modelB = load_model('/content/30-base_model_1.h5') modelB.load_weights('/content/30-weights.010-0.499-0.374.hdf5')

modelC = load_model('/content/80-base_model_1.h5') modelC.load_weights('/content/80-weights.002-1.064-0.305.hdf5') for layer in modelA.layers: layer.trainable = False layer.name = layer.name + str("_1")

for layer in modelB.layers: layer.trainable = False layer.name = layer.name + str("_2")

for layer in modelC.layers: layer.trainable = False layer.name = layer.name + str("_3")

mod_1 = concatenate([modelA.layers[-1].output, modelB.layers[-1].output,modelC.layers[-1].output])
mod_1 = Dropout(0.2)(mod_1) mod_1 = Dense(15, activation='relu')(mod_1) predictions = Dense(9, activation='softmax', name='pred_age')(mod_1)

top_model = Model(inputs=[modelA.input, modelB.input, modelC.input], outputs=predictions)

opt = get_optimizer('adam', lr_rate) top_model.compile(loss=['categorical_crossentropy'], optimizer=opt, metrics=['accuracy','mae'])

model.fit_generator(inputgenerator, epochs=EPOCHS, steps_per_epoch = STEPS_PER_EPOCH, validation_data=validgenerator, validation_steps = VALIDATION_STEPS, verbose=1, callbacks=callbacks)

I first trained 3 different models, for example:

model 1 (age group 0-29) = 3 labels (0,1,....,8,9,10....), contains labels from 0 until 29 model 2 (age group 30-79)= 2 labels (30,31.....), contains labels from 30 until 79 model 3 (age group 80-100) = 4 labels (80,81,82,83.....), contains labels from 80 until 100

I would like to combine these 3 models into a final model with 1 output containing the 101 labels (0,1,2, ... ,100). However the model expects 3 inputs instead of 1. For the final model would just like to give 1 image.

The final input needs to get 1 input image instead of 3 images. But I still get stuck. I am building a kind of hierarchy here to improve the accuracy.

Finetune imagenet with age_groups > fine-tune age_groups model with 101 images (combined N models).

I am doing this to increase the age estimation model for older age groups while these are predicted very young for older people. Another question is about the fine-tuning, when i fine-tune a trained model, the weights of the trained model are forgotten, which I do not want it.

yu4u commented 5 years ago

Sorry, I did not understand what you want. You might have to estimate age group (out of three groups) using the other model first.

aezco commented 5 years ago

For example,

I have an image with age label 36. I want to train a hierarchical cnn, where I first predict the age_group and then the apparent age.

image label 36 > layers > softmax classification age_group > softmax classification apparant age.

You might have to estimate age group (out of three groups) using the other model first.

So, you mean first predict the age group model. And for example if age group is 30-40, then select the model of (30-40) and predict ages between 30-40. right?

yu4u commented 5 years ago

So, you mean first predict the age group model. And for example if age group is 30-40, then select the model of (30-40) and predict ages between 30-40. right?

Yes. There is no need to put all functions into a single model. (Anyway, I'm not sure this approach can improve the accuracy)