0

I am training a CNN on face images and I want it to perform classification and regression tasks at the same time. I figured out how to train the CNN as below:

resnet = tf.keras.applications.ResNet50( 
include_top=False ,
weights='imagenet' ,
input_shape=(96, 96, 3) ,
pooling="avg"
)

for layer in resnet.layers:
   layer.trainable = True

inputs = Input(shape=(96, 96, 3), name='main_input')
main_branch = resnet(inputs)
main_branch = Flatten()(main_branch)
expr_branch = Dense(8, activation='softmax', name='expr_output')(main_branch)
va_branch = Dense(2, name='va_output')(main_branch)
model = Model(inputs = inputs,
  outputs = [expr_branch, va_branch])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
          loss={'expr_output': 'sparse_categorical_crossentropy', 'va_output': 
'mean_squared_error'})

I want after every epoch to prin the accuracy and the mse metrics for each task. So far I have written the below:

checkpoint = tf.keras.callbacks.ModelCheckpoint(
    model_path,
    save_weights_only=True,
    verbose=1
)

history = model.fit_generator(
                   train_generator,
                   epochs=2,
                   steps_per_epoch=STEP_SIZE_TRAIN_resnet,
                   validation_data=test_generator,
                   validation_steps=STEP_SIZE_TEST_resnet,
                   max_queue_size=1,
                   shuffle=True,
                   callbacks=[checkpoint],
                   verbose=1
                  )

When I had only the classification task I would write

checkpoint = tf.keras.callbacks.ModelCheckpoint(
    model_path,
    monitor='val_accuracy',
    save_best_only=True,
    mode='max',
    verbose=1
)

which printed the val_accuracy at every epoch and saved the weights. How can I do the same (print mse and accuracy and save the weights after every epoch) at a multitask problem?

0 Answers0