how to train/test custom cnn model using transfer learning based on resnet50 model?

Viewed 24

I'm trying to implement an app that can predict bmi, age, gender using facial image. To be able to do this i'm trying to use the resnet-50 cnn model knowledge and change the input and output of the network.

For Training/Testing the new model i'm using FACE2BMI Dataset contain almost 60k images with bmi, age, gender data.

face2bmi csv file

f

I've used this code to create the model

IMG_SHAPE = (256,256, 3)
base_model = tf.keras.applications.EfficientNetB6(weights='imagenet', include_top=False, input_shape=IMG_SHAPE)
base_model.trainable = False

optimizer = tf.keras.optimizers.Adam()

model_inputs = tf.keras.Input(shape=(256, 256, 3))
x = base_model(model_inputs, training=False)
x = tf.keras.layers.MaxPooling2D()(x)
x = tf.keras.layers.Flatten()(x)
#let's add a fully-connected layer
'''x = tf.keras.layers.Dense(64,activation='relu')(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(64,activation='relu')(x)
x = tf.keras.layers.Dropout(0.2)(x)
'''


#start passing that fully connected block output to all the different model heads
y1 = tf.keras.layers.Dense(32,activation='relu')(x)
y1 = tf.keras.layers.Dropout(0.2)(y1)
y1 = tf.keras.layers.Dense(16,activation='relu')(y1)
y1 = tf.keras.layers.Dropout(0.2)(y1)

y2 = tf.keras.layers.Dense(32,activation='relu')(x)
y2 = tf.keras.layers.Dropout(0.2)(y2)
y2 = tf.keras.layers.Dense(16,activation='relu')(y2)
y2 = tf.keras.layers.Dropout(0.2)(y2)

y3 = tf.keras.layers.Dense(32,activation='sigmoid')(x)
y3 = tf.keras.layers.Dropout(0.2)(y3)
y3 = tf.keras.layers.Dense(16,activation='sigmoid')(y3)
y3 = tf.keras.layers.Dropout(0.2)(y3)


# Predictions for each task
y1 = tf.keras.layers.Dense(units=3,activation="linear",name='bmi')(y1)
y2 = tf.keras.layers.Dense(units=3,activation="linear",name='age')(y2)
y3 = tf.keras.layers.Dense(units=3,activation="sigmoid",name='sex')(y3)
 
custom_model = tf.keras.Model(inputs=model_inputs,outputs=[y1,y2,y3])

custom_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),
              loss={'bmi':'mean_squared_error','age':'mean_squared_error','sex':'hinge'},metrics=['accuracy'])


The summary of the model is:


Model: "model_1"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_4 (InputLayer)           [(None, 256, 256, 3  0           []                               
                                )]                                                                
                                                                                                  
 efficientnetb6 (Functional)    (None, 8, 8, 2304)   40960143    ['input_4[0][0]']                
                                                                                                  
 max_pooling2d_1 (MaxPooling2D)  (None, 4, 4, 2304)  0           ['efficientnetb6[0][0]']         
                                                                                                  
 flatten_1 (Flatten)            (None, 36864)        0           ['max_pooling2d_1[0][0]']        
                                                                                                  
 dense_6 (Dense)                (None, 32)           1179680     ['flatten_1[0][0]']              
                                                                                                  
 dense_8 (Dense)                (None, 32)           1179680     ['flatten_1[0][0]']              
                                                                                                  
 dense_10 (Dense)               (None, 32)           1179680     ['flatten_1[0][0]']              
                                                                                                  
 dropout_6 (Dropout)            (None, 32)           0           ['dense_6[0][0]']                
                                                                                                  
 dropout_8 (Dropout)            (None, 32)           0           ['dense_8[0][0]']                
                                                                                                  
 dropout_10 (Dropout)           (None, 32)           0           ['dense_10[0][0]']               
                                                                                                  
 dense_7 (Dense)                (None, 16)           528         ['dropout_6[0][0]']              
                                                                                                  
 dense_9 (Dense)                (None, 16)           528         ['dropout_8[0][0]']              
                                                                                                  
 dense_11 (Dense)               (None, 16)           528         ['dropout_10[0][0]']             
                                                                                                  
 dropout_7 (Dropout)            (None, 16)           0           ['dense_7[0][0]']                
                                                                                                  
 dropout_9 (Dropout)            (None, 16)           0           ['dense_9[0][0]']                
                                                                                                  
 dropout_11 (Dropout)           (None, 16)           0           ['dense_11[0][0]']               
                                                                                                  
 bmi (Dense)                    (None, 3)            51          ['dropout_7[0][0]']              
                                                                                                  
 age (Dense)                    (None, 3)            51          ['dropout_9[0][0]']              
                                                                                                  
 sex (Dense)                    (None, 3)            51          ['dropout_11[0][0]']             
                                                                                                  
==================================================================================================
Total params: 44,500,920
Trainable params: 3,540,777
Non-trainable params: 40,960,143
_____________________________________

my question is how can i train and test this new custom model with the FACE2BMI Dataset ?

0 Answers
Related