Offer three denoise networks and a method to choose between them
AI Denoise's Apply switch becomes Method: Bilinear, Fast, Medium, Best, default Best, so an untouched raw writes nothing and develops through the mixture. `apply` is still read and never written: 0 is Bilinear, 1 keeps a network already chosen. - Best is the mixture of a flat and an edge expert with a learned gate; Medium and Fast are students distilled from it. 2.48 s, 0.79 s and 0.57 s for a 20 MP frame on TensorRT fp16. - Each network carries its own tile border (256 for the mixture, 192 for the students) through `dr_denoise::Shipped` and `TileNet::halo`. - The file is hashed once at open and each network keys its own cached result; Bilinear keeps the result in memory for the way back. - Each has an .a16w16 sibling for the Hexagon: 0.00 dB on the 6D gate, at most 0.11 dB with the noise scaled x0.5 to x4. - APK BUNDLED 19 -> 23; the PKGBUILD installs all three.
This commit is contained in:
@@ -176,10 +176,10 @@ pub struct EditGraph {
|
||||
/// lens profile, so not in the state; it only decides whether the
|
||||
/// switch below is offered.
|
||||
denoise_available: bool,
|
||||
/// Whether the learned denoise replaces the demosaic. An edit: published
|
||||
/// as [`crate::learned_denoise`], captured, stored and undone with the
|
||||
/// rest (FR-DEV-3c).
|
||||
denoise_applied: bool,
|
||||
/// Which demosaic develops the photograph: a network, or the classical
|
||||
/// one. An edit: published as [`crate::learned_denoise`], captured,
|
||||
/// stored and undone with the rest (FR-DEV-3c).
|
||||
denoise_method: crate::learned_denoise::Method,
|
||||
/// How strongly to denoise, 0–100; what is not taken goes back as grain.
|
||||
denoise_strength: f32,
|
||||
}
|
||||
@@ -239,7 +239,7 @@ impl EditGraph {
|
||||
lens_profile: None,
|
||||
lens_profile_applied: true,
|
||||
denoise_available: false,
|
||||
denoise_applied: true,
|
||||
denoise_method: crate::learned_denoise::Method::DEFAULT,
|
||||
denoise_strength: 100.0,
|
||||
}
|
||||
}
|
||||
@@ -425,7 +425,13 @@ impl EditGraph {
|
||||
/// photograph that cannot take it is harmless and does nothing, as a
|
||||
/// lens switch with no profile does.
|
||||
pub fn denoise_applied(&self) -> bool {
|
||||
self.denoise_applied
|
||||
self.denoise_method.learned()
|
||||
}
|
||||
|
||||
/// TRACES: FR-DEV-3g
|
||||
/// Which demosaic is asked for.
|
||||
pub fn denoise_method(&self) -> crate::learned_denoise::Method {
|
||||
self.denoise_method
|
||||
}
|
||||
|
||||
/// TRACES: FR-DEV-3g
|
||||
@@ -625,7 +631,7 @@ impl EditGraph {
|
||||
OpCapability {
|
||||
id: desc.id,
|
||||
label: desc.label,
|
||||
active: self.denoise_applied,
|
||||
active: self.denoise_applied(),
|
||||
params: desc
|
||||
.params
|
||||
.iter()
|
||||
@@ -743,7 +749,7 @@ impl EditGraph {
|
||||
// Derived from the file, like the profile above.
|
||||
denoise_available: _,
|
||||
// Edits, in the state through `capabilities` like the lens switch.
|
||||
denoise_applied: _,
|
||||
denoise_method: _,
|
||||
denoise_strength: _,
|
||||
masks,
|
||||
film,
|
||||
@@ -818,7 +824,19 @@ impl EditGraph {
|
||||
pub fn set_param(&mut self, op: OpId, param: ParamId, value: f32) {
|
||||
if op == crate::learned_denoise::ID {
|
||||
match param {
|
||||
p if p == crate::learned_denoise::APPLY => self.denoise_applied = value != 0.0,
|
||||
p if p == crate::learned_denoise::METHOD => {
|
||||
self.denoise_method = crate::learned_denoise::Method::from_index(value)
|
||||
}
|
||||
// 0.21 and 0.22's switch (see `APPLY`): off is the classical
|
||||
// demosaic, on is a network — the one already chosen, if any.
|
||||
p if p == crate::learned_denoise::APPLY => {
|
||||
use crate::learned_denoise::Method;
|
||||
if value == 0.0 {
|
||||
self.denoise_method = Method::Bilinear;
|
||||
} else if !self.denoise_method.learned() {
|
||||
self.denoise_method = Method::DEFAULT;
|
||||
}
|
||||
}
|
||||
p if p == crate::learned_denoise::STRENGTH => {
|
||||
self.denoise_strength = value.clamp(0.0, 100.0)
|
||||
}
|
||||
@@ -889,8 +907,9 @@ impl EditGraph {
|
||||
pub fn param(&self, op: OpId, param: ParamId) -> Option<f32> {
|
||||
if op == crate::learned_denoise::ID {
|
||||
return match param {
|
||||
p if p == crate::learned_denoise::METHOD => Some(self.denoise_method.index()),
|
||||
p if p == crate::learned_denoise::APPLY => {
|
||||
Some(if self.denoise_applied { 1.0 } else { 0.0 })
|
||||
Some(if self.denoise_applied() { 1.0 } else { 0.0 })
|
||||
}
|
||||
p if p == crate::learned_denoise::STRENGTH => Some(self.denoise_strength),
|
||||
p if p == crate::learned_denoise::GRAIN => Some(100.0 - self.denoise_strength),
|
||||
@@ -943,9 +962,9 @@ impl EditGraph {
|
||||
// a reset does not change which lens took the photograph. What returns
|
||||
// to default is the answer to whether to use it, which is on.
|
||||
self.set_lens_profile_applied(true);
|
||||
// The learned denoise returns to off; whether it is available is the
|
||||
// file's and stays.
|
||||
self.denoise_applied = true;
|
||||
// The learned denoise returns to its default network; whether it is
|
||||
// available is the file's and stays.
|
||||
self.denoise_method = crate::learned_denoise::Method::DEFAULT;
|
||||
self.denoise_strength = 100.0;
|
||||
}
|
||||
|
||||
@@ -2106,9 +2125,14 @@ mod tests {
|
||||
.find(|c| c.id == learned_denoise::ID)
|
||||
.expect("offered");
|
||||
assert!(cap.active, "on by default");
|
||||
assert_eq!(g.denoise_method(), learned_denoise::Method::Best);
|
||||
assert_eq!(g.denoise_grain(), 0.0, "at full strength");
|
||||
assert_eq!(cap.id, g.capabilities()[0].id, "and first in the panel");
|
||||
g.set_param(learned_denoise::ID, learned_denoise::APPLY, 0.0);
|
||||
g.set_param(
|
||||
learned_denoise::ID,
|
||||
learned_denoise::METHOD,
|
||||
learned_denoise::Method::Bilinear.index(),
|
||||
);
|
||||
g.set_param(learned_denoise::ID, learned_denoise::STRENGTH, 70.0);
|
||||
assert!(!g.denoise_applied());
|
||||
assert!((g.denoise_grain() - 0.3).abs() < 1e-6);
|
||||
@@ -2117,6 +2141,58 @@ mod tests {
|
||||
assert_eq!(g.denoise_grain(), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_untouched_raw_writes_nothing_and_develops_through_the_best() {
|
||||
// TRACES: FR-DEV-3g
|
||||
use crate::learned_denoise::{self, Method};
|
||||
let mut g = EditGraph::default_chain();
|
||||
g.set_denoise_available(true);
|
||||
assert_eq!(g.denoise_method(), Method::Best);
|
||||
let stored = |g: &EditGraph| {
|
||||
crate::Preset::capture_params(g)
|
||||
.params()
|
||||
.keys()
|
||||
.any(|(op, _)| op == learned_denoise::ID.0)
|
||||
};
|
||||
assert!(!stored(&g), "the default is not written");
|
||||
g.set_param(
|
||||
learned_denoise::ID,
|
||||
learned_denoise::METHOD,
|
||||
Method::Fast.index(),
|
||||
);
|
||||
assert!(stored(&g), "a choice is");
|
||||
assert!(
|
||||
!crate::Preset::capture_params(&g).params().contains_key(&(
|
||||
learned_denoise::ID.0.into(),
|
||||
learned_denoise::APPLY.0.into()
|
||||
)),
|
||||
"and the old switch never is"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_edit_saved_with_the_switch_keeps_its_look() {
|
||||
// TRACES: FR-DEV-3g
|
||||
// 0.21 and 0.22 stored on or off; off is the classical demosaic, and
|
||||
// on keeps a network already chosen.
|
||||
use crate::learned_denoise::{self, Method};
|
||||
let mut g = EditGraph::default_chain();
|
||||
g.set_param(learned_denoise::ID, learned_denoise::APPLY, 0.0);
|
||||
assert_eq!(g.denoise_method(), Method::Bilinear);
|
||||
g.set_param(learned_denoise::ID, learned_denoise::APPLY, 1.0);
|
||||
assert_eq!(g.denoise_method(), Method::DEFAULT);
|
||||
g.set_param(
|
||||
learned_denoise::ID,
|
||||
learned_denoise::METHOD,
|
||||
Method::Fast.index(),
|
||||
);
|
||||
g.set_param(learned_denoise::ID, learned_denoise::APPLY, 1.0);
|
||||
assert_eq!(g.denoise_method(), Method::Fast);
|
||||
// A number from a newer build with more methods is the default.
|
||||
g.set_param(learned_denoise::ID, learned_denoise::METHOD, 9.0);
|
||||
assert_eq!(g.denoise_method(), Method::DEFAULT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_edit_saved_with_grain_keeps_its_look() {
|
||||
// TRACES: FR-DEV-3g
|
||||
@@ -2137,11 +2213,16 @@ mod tests {
|
||||
let mut g = EditGraph::default_chain();
|
||||
g.set_denoise_available(true);
|
||||
g.set_param(learned_denoise::ID, learned_denoise::STRENGTH, 60.0);
|
||||
g.set_param(
|
||||
learned_denoise::ID,
|
||||
learned_denoise::METHOD,
|
||||
learned_denoise::Method::Medium.index(),
|
||||
);
|
||||
let state = g.state();
|
||||
let mut h = EditGraph::default_chain();
|
||||
h.set_denoise_available(true);
|
||||
let _ = h.set_state(&state);
|
||||
assert!(h.denoise_applied());
|
||||
assert_eq!(h.denoise_method(), learned_denoise::Method::Medium);
|
||||
assert!((h.denoise_grain() - 0.4).abs() < 1e-6);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! TRACES: FR-DEV-3g
|
||||
//! The learned denoise's settings: whether to use it, and how much grain to
|
||||
//! keep.
|
||||
//! The learned denoise's settings: which network develops the photograph,
|
||||
//! if any, and how much grain to keep.
|
||||
//!
|
||||
//! Not an [`crate::operation::Operation`]: the learned stage replaces the
|
||||
//! demosaic and runs once per photograph, off the render path
|
||||
@@ -17,6 +17,12 @@ use crate::descriptor::{Attribute, LocalizedKey, OpDescriptor, ParamDescriptor,
|
||||
use crate::{OpId, ParamId};
|
||||
|
||||
pub const ID: OpId = OpId("learned_denoise");
|
||||
/// TRACES: FR-DEV-3g
|
||||
/// Which demosaic develops the photograph, a [`Method`] by index.
|
||||
pub const METHOD: ParamId = ParamId("method");
|
||||
/// What 0.21 and 0.22 stored instead of [`METHOD`]: on or off. Still read —
|
||||
/// off is [`Method::Bilinear`], on is the default network — so an edit saved
|
||||
/// by those releases keeps its look; never written, and not offered.
|
||||
pub const APPLY: ParamId = ParamId("apply");
|
||||
/// TRACES: FR-DEV-3g
|
||||
/// How strongly to denoise, 0–100: 100 is the network's result as it is, and
|
||||
@@ -27,9 +33,52 @@ pub const STRENGTH: ParamId = ParamId("strength");
|
||||
/// written, and not offered as a control.
|
||||
pub const GRAIN: ParamId = ParamId("grain");
|
||||
|
||||
/// On by default, at full strength: every Bayer raw is developed from the
|
||||
/// learned demosaic, and the switch and the slider are there to take it back
|
||||
/// or ease it off. It costs seconds per photograph the first time, while
|
||||
/// TRACES: FR-DEV-3g
|
||||
/// The demosaics a photograph can be developed with, in the order the
|
||||
/// sidecar numbers them. Three networks that trade time for quality — the
|
||||
/// same training, distilled into smaller students (docs/dev/denoise.md §13)
|
||||
/// — and the classical demosaic, which is no network at all.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Method {
|
||||
/// The classical demosaic: the noise stays.
|
||||
Bilinear,
|
||||
/// The smallest student: a quarter of the medium network's work.
|
||||
Fast,
|
||||
/// One network the size of the first release's.
|
||||
Medium,
|
||||
/// Two experts, one for flat areas and one for edges, and a gate.
|
||||
Best,
|
||||
}
|
||||
|
||||
impl Method {
|
||||
pub const ALL: [Method; 4] = [Method::Bilinear, Method::Fast, Method::Medium, Method::Best];
|
||||
pub const DEFAULT: Method = Method::Best;
|
||||
|
||||
/// The sidecar's number for it.
|
||||
pub fn index(self) -> f32 {
|
||||
Self::ALL.iter().position(|m| *m == self).unwrap_or(0) as f32
|
||||
}
|
||||
|
||||
/// The method a stored number names; out of range is the default, as
|
||||
/// from a newer build with more of them.
|
||||
pub fn from_index(value: f32) -> Method {
|
||||
let i = value.round();
|
||||
if i >= 0.0 && (i as usize) < Self::ALL.len() {
|
||||
Self::ALL[i as usize]
|
||||
} else {
|
||||
Self::DEFAULT
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a network runs at all.
|
||||
pub fn learned(self) -> bool {
|
||||
self != Method::Bilinear
|
||||
}
|
||||
}
|
||||
|
||||
/// The best network by default, at full strength: every Bayer raw is
|
||||
/// developed from the learned demosaic, and the choice and the slider are
|
||||
/// there to take it back, trade it for time, or ease it off. It costs seconds per photograph the first time, while
|
||||
/// the classical demosaic shows; the result is cached, so a photograph
|
||||
/// reopened or exported does not pay again (docs/dev/denoise.md §7).
|
||||
pub(crate) static DESCRIPTOR: LazyLock<Arc<OpDescriptor>> = LazyLock::new(|| {
|
||||
@@ -37,7 +86,17 @@ pub(crate) static DESCRIPTOR: LazyLock<Arc<OpDescriptor>> = LazyLock::new(|| {
|
||||
id: ID,
|
||||
label: LocalizedKey("op.learned_denoise"),
|
||||
params: vec![
|
||||
ParamDescriptor::switch_on("apply", "param.learned_denoise.apply"),
|
||||
ParamDescriptor::choice(
|
||||
"method",
|
||||
"param.learned_denoise.method",
|
||||
vec![
|
||||
LocalizedKey("param.learned_denoise.method.bilinear"),
|
||||
LocalizedKey("param.learned_denoise.method.fast"),
|
||||
LocalizedKey("param.learned_denoise.method.medium"),
|
||||
LocalizedKey("param.learned_denoise.method.best"),
|
||||
],
|
||||
)
|
||||
.with_default(Method::DEFAULT.index()),
|
||||
ParamDescriptor::scalar(
|
||||
"strength",
|
||||
"param.learned_denoise.strength",
|
||||
|
||||
Reference in New Issue
Block a user