From patchwork Thu Jan 23 11:41:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22632 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 9DA89C3226 for ; Thu, 23 Jan 2025 11:43:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 36A7368580; Thu, 23 Jan 2025 12:43:04 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ZKdaoF8t"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3929168579 for ; Thu, 23 Jan 2025 12:43:02 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:c0a:33cd:b453:5d3f]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B752A1189; Thu, 23 Jan 2025 12:41:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1737632518; bh=sXB+CAsjYxy3i+h3ne6gPXoBMck5zTv5VhZL2vG13Js=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZKdaoF8tjaLtd46fHt4dV4lblXGn0cS/QKd9HWap6s3h+BXbAOWP8P9rbMkTo/lTY 0v7yuIthLH1NKcckDXTaEOb4aBRxHzCTbgUGGxgdwJyDUeQ1rnm1yzJ0P825te5aPE hK/BX4ZDtY52MerFn3QMQsZIY7G0vaDl9bhq4CeA= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug Subject: [PATCH v2 17/17] libipa: awb_bayes: Change the probabilities from log space into linear space Date: Thu, 23 Jan 2025 12:41:07 +0100 Message-ID: <20250123114204.79321-18-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250123114204.79321-1-stefan.klug@ideasonboard.com> References: <20250123114204.79321-1-stefan.klug@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The original code used to specify the probabilities in log space and scaled for the RaspberryPi hardware with 192 AWB measurement points. This is reasonable as the whole algorithm makes use of unitless numbers to prefer some colour temperatures based on a lux level. These numbers are then hand tuned with the specific device in mind. This has two shortcomings: 1. The linear interpolation of PWLs in log space is mathematically incorrect. The outcome might still be ok, as both spaces (log and linear) are monotonic, but it is still not "right". 2. Having unitless numbers gets more error prone when we try to harmonize the behavior over multiple platforms. Change the algorithm to interpret the numbers as being in linear space. This makes the interpolation mathematically correct at the expense of a few log operations. To account for that change, update the numbers in the tuning example file with the linear counterparts scaled to one AWB zone measurement. Signed-off-by: Stefan Klug Reviewed-by: Paul Elder Reviewed-by: Daniel Scally --- Changes in v2: - Added this commit --- src/ipa/libipa/awb.cpp | 5 +++-- src/ipa/libipa/awb_bayes.cpp | 8 ++++++-- utils/tuning/config-example.yaml | 12 ++++++------ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/ipa/libipa/awb.cpp b/src/ipa/libipa/awb.cpp index 62b69dd96238..6157bd436183 100644 --- a/src/ipa/libipa/awb.cpp +++ b/src/ipa/libipa/awb.cpp @@ -57,8 +57,9 @@ namespace ipa { * applied. To keep the actual implementations computationally inexpensive, * the squared colour error shall be returned. * - * If the awb statistics provide multiple zones, the sum over all zones needs to - * calculated. + * If the awb statistics provide multiple zones, the average of the individual + * squared errors shall be returned. Averaging/normalizing is necessary so that + * the numeric dimensions are the same on all hardware platforms. * * \return The computed error value */ diff --git a/src/ipa/libipa/awb_bayes.cpp b/src/ipa/libipa/awb_bayes.cpp index 6b88aebeffb5..5f43421e14c7 100644 --- a/src/ipa/libipa/awb_bayes.cpp +++ b/src/ipa/libipa/awb_bayes.cpp @@ -235,6 +235,10 @@ int AwbBayes::readPriors(const YamlObject &tuningData) auto &pwl = priors[lux]; for (const auto &[ct, prob] : ctToProbability) { + if (prob < 1e-6) { + LOG(Awb, Error) << "Prior probability must be larger than 1e-6"; + return -EINVAL; + } pwl.append(ct, prob); } } @@ -324,7 +328,7 @@ double AwbBayes::coarseSearch(const ipa::Pwl &prior, const AwbStats &stats) cons double b = ctB_.eval(t, &spanB); RGB gains({ 1 / r, 1.0, 1 / b }); double delta2Sum = stats.computeColourError(gains); - double priorLogLikelihood = prior.eval(prior.domain().clamp(t)); + double priorLogLikelihood = log(prior.eval(prior.domain().clamp(t))); double finalLogLikelihood = delta2Sum - priorLogLikelihood; errorLimits.record(delta2Sum); @@ -407,7 +411,7 @@ void AwbBayes::fineSearch(double &t, double &r, double &b, ipa::Pwl const &prior for (int i = -nsteps; i <= nsteps; i++) { double tTest = t + i * step; double priorLogLikelihood = - prior.eval(prior.domain().clamp(tTest)); + log(prior.eval(prior.domain().clamp(tTest))); priorLogLikelihoodLimits.record(priorLogLikelihood); Pwl::Point rbStart{ { ctR_.eval(tTest, &spanR), ctB_.eval(tTest, &spanB) } }; diff --git a/utils/tuning/config-example.yaml b/utils/tuning/config-example.yaml index 1bbb275778dc..5593eaef809e 100644 --- a/utils/tuning/config-example.yaml +++ b/utils/tuning/config-example.yaml @@ -7,21 +7,21 @@ general: awb: # Algorithm can either be 'grey' or 'bayes' algorithm: bayes - # Priors is only used for the bayes algorithm. They are defined in - # logarithmic space. A good staring point is: + # Priors is only used for the bayes algorithm. They are defined in linear + # space. A good staring point is: # - lux: 0 # ct: [ 2000, 3000, 13000 ] - # probability: [ 1.0, 0.0, 0.0 ] + # probability: [ 1.005, 1.0, 1.0 ] # - lux: 800 # ct: [ 2000, 6000, 13000 ] - # probability: [ 0.0, 2.0, 2.0 ] + # probability: [ 1.0, 1.01, 1.01 ] # - lux: 1500 # ct: [ 2000, 4000, 6000, 6500, 7000, 13000 ] - # probability: [ 0.0, 1.0, 6.0, 7.0, 1.0, 1.0 ] + # probability: [ 1.0, 1.005, 1.032, 1.037, 1.01, 1.01 ] priors: - lux: 0 ct: [ 2000, 13000 ] - probability: [ 0.0, 0.0 ] + probability: [ 1.0, 1.0 ] AwbMode: AwbAuto: lo: 2500