Keras中基于自定义指标的提前停止和学习率计划 [英] Early stopping and learning rate schedule based on custom metric in Keras

查看:692
本文介绍了Keras中基于自定义指标的提前停止和学习率计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Keras中有一个对象检测模型,并希望基于在验证集上计算出的平均平均精度(mAP)来监视和控制我的训练.

I have an object detection model in Keras and want to monitor and control my training based on mean average precision (mAP) calculated on the validation set.

我已将代码从 tensorflow-models 移植到我的脚本中,该脚本使用模型和提供的数据.不过,它不是作为Keras度量实现的,而是作为一个独立的类实现的:

I have ported code from tensorflow-models into my scripts that runs the evaluation using the model and data provided. It is not implemented as a Keras metric though, but as a standalone class:

evaluation = SSDEvaluation(model, data, data_size)
mAP = evaluation.evaluate()

拥有这样的东西我完全可以.确实,我不希望为训练批次计算它,因为它会减慢训练速度.

I am completely fine with having it like this. Indeed, I do not want it to be calculated for training batches, as it will slow down the training.

我的问题是:如何根据在每个时期后计算出的此指标重复使用ReduceLROnPlateauEarlyStopping回调?

My question is: How to re-use ReduceLROnPlateau and EarlyStopping callbacks based on this metric being calculated after each epoch?

推荐答案

您可以使用 LambdaCallback来实现会更新您的logs对象:

You can do this with using a LambdaCallback that updates your logs object:

假设您的evaluation.evaluate()返回的字典类似于{'val/mAP': value},则可以执行以下操作:

Assuming that your evaluation.evaluate() returns a dictionary like {'val/mAP': value}, you can do like this:

eval_callback = LambdaCallback(
     on_epoch_end=lambda epoch, logs: logs.update(evaluation.evaluate())
) 

这里的窍门是logs将被进一步传递给其他回调,因此它们可以直接访问该值:

The trick here is that the logs will be passed further to other callbacks, so they can directly access the value:

early_stopping = EarlyStopping(monitor='val/mAP', min_delta=0.0, patience=10, verbose=1, mode='max') 

它将自动出现在CSVLogger和任何其他回调中.但是请注意,eval_callback必须使用回调列表中的值在任何回调之前:

It will automagically appear in the CSVLogger and any other callback. But note that eval_callback must be prior to any callback using the value in the callbacks list:

callbacks = [eval_callback, early_stopping]

这篇关于Keras中基于自定义指标的提前停止和学习率计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆