From 9bb1b6f71291a816619cdeeb1598e13bbc2e040d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= <1671644+arrufat@users.noreply.github.com> Date: Thu, 31 Mar 2022 07:55:27 +0900 Subject: [PATCH] Fix focal loss The loss value should reflect the impact of the gamma parameter, as well. --- dlib/dnn/loss.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dlib/dnn/loss.h b/dlib/dnn/loss.h index cfb2402ea..bb017d1dc 100644 --- a/dlib/dnn/loss.h +++ b/dlib/dnn/loss.h @@ -850,14 +850,14 @@ namespace dlib { const float temp = log1pexp(-out_data[idx]); const float focus = std::pow(1 - g[idx], gamma); - loss += y * scale * temp; + loss += y * scale * temp * focus; g[idx] = y * scale * focus * (g[idx] * (gamma * temp + 1) - 1); } else { const float temp = -(-out_data[idx] - log1pexp(-out_data[idx])); const float focus = std::pow(g[idx], gamma); - loss += -y * scale * temp; + loss += -y * scale * temp * focus; g[idx] = -y * scale * focus * g[idx] * (gamma * temp + 1); } }