Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

FP2f: realness measured, not shipped. A head on a sound's CLAP and YAMNet means, labels by construction: raea's SAO foley, TokenSynth notes and Fish narration against recorded foley, instrument notes and speech under CC0 and CC BY (Clotho, NSynth, LibriSpeech). Held out by generator it scores TokenSynth 0.90, Fish 0.80 and SAO 0.57, under the 0.85 it needs on all three, so it isn't an axis

shinyobjectze65216820240
diff --git a/docs/fingerprint.feature b/docs/fingerprint.feature
index 6d97a2ff..356d7d31 100644
--- a/docs/fingerprint.feature
+++ b/docs/fingerprint.feature
@@ -203,6 +203,16 @@ Feature: Fingerprint: a person's or a team's sense of good work, learned on thei
holds its out by engine and category (its grouped CV AUC 0.92)
And it ships as an axis only at 0.85 or better held out by generator; it's a taste check ("real material only"),
never a claim that a sound is or isn't AI-made
+ And 11-12, measured, not shipped: raea-sys-fingerprint-use realness.rs, a logistic head on a sound's CLAP and
+ YAMNet means (realness_on_this_machine, ignored, RAEA_REALNESS=~/.raea/evals/realness). Made: 60 SAO foley clips
+ (15 categories, 4 seeds), 45 TokenSynth notes (9 timbres, C3-G4) and 40 Fish lines (two voices, $0.05).
+ Recorded, each clip's source and licence kept beside it: 40 LibriSpeech test-clean speakers (CC BY 4.0), 42
+ NSynth test acoustic notes in 7 families (CC BY 4.0) and 70 Clotho v2.1 evaluation clips in 14 of the 15
+ categories, CC0 or CC BY 3.0 only (the NC and Sampling+ clips dropped). TokenSynth's bass and organ and SAO's
+ typing have no recording and sit out (283 sounds measured). Held out by generator: TokenSynth 0.90, Fish
+ 0.80, SAO 0.57, so the axis doesn't ship (it needs 0.85 on all three). Held out by category it averages 0.85
+ over 22 categories (about 5 recordings each; dishes and engine 0.40, traffic 0.60). Foley is where it fails:
+ trained on notes and speech it can't tell SAO from recorded foley
@needs:FP2a @done
Scenario: FP2c. Heads train in milliseconds, on the device
diff --git a/systems/fingerprint-use/src/lib.rs b/systems/fingerprint-use/src/lib.rs
index 51ddf85c..fd39a1a1 100644
--- a/systems/fingerprint-use/src/lib.rs
+++ b/systems/fingerprint-use/src/lib.rs
@@ -15,6 +15,7 @@ pub mod geometry;
pub mod grow;
pub mod postmortem;
pub mod profile;
+pub mod realness;
pub mod score;
pub mod sheet;
pub mod things;
diff --git a/systems/fingerprint-use/src/realness.rs b/systems/fingerprint-use/src/realness.rs
new file mode 100644
index 00000000..98a9b28b
--- /dev/null
+++ b/systems/fingerprint-use/src/realness.rs
@@ -0,0 +1,153 @@
+//! Realness, measured before it ships (docs/fingerprint.feature FP2f): a head on a sound's CLAP and YAMNet means,
+//! trained from labels by construction, raea's own generators' sounds (SAO's foley, TokenSynth's notes, Fish's
+//! narration) against recordings of the same kinds of sound, and held out two ways as earshot holds its own out: by
+//! generator (trained on the other two generators and their recordings, tested on the third and its) and by category
+//! (each category left out in turn). It ships as an axis only at SHIPS or better held out by generator; it's a taste
+//! check ("real material only"), never a claim that a sound is or isn't AI-made.
+
+use raea_sys_fingerprint::head::{Logistic, Scaler, auc};
+use serde::{Deserialize, Serialize};
+use std::path::PathBuf;
+
+/// Held out by generator, the least of the three, for the axis to ship
+pub const SHIPS: f64 = 0.85;
+/// The head's C (as the axis heads')
+const C: f64 = 1.0;
+
+/// A sound with its label by construction: made by a generator (`by`), or a recording (`by` "recorded")
+#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
+pub struct Item {
+ pub file: PathBuf,
+ pub by: String,
+ /// what kind of sound: foley, instrument or speech; each generator makes one kind
+ pub kind: String,
+ pub category: String,
+}
+
+impl Item {
+ fn made(&self) -> bool {
+ self.by != "recorded"
+ }
+}
+
+/// One held-out fold's AUC (made scoring above recorded), with its counts
+#[derive(Clone, Debug, Serialize, PartialEq)]
+pub struct Held {
+ pub out: String,
+ pub auc: Option<f64>,
+ pub made: usize,
+ pub recorded: usize,
+}
+
+/// Fit on the rows `train` picks, score the rows `test` picks: the AUC of made over recorded
+fn fold(x: &[(Item, Vec<f32>)], train: impl Fn(&Item) -> bool, test: impl Fn(&Item) -> bool, out: String) -> Held {
+ let tr: Vec<&(Item, Vec<f32>)> = x.iter().filter(|r| train(&r.0)).collect();
+ let rows: Vec<&[f32]> = tr.iter().map(|r| r.1.as_slice()).collect();
+ let y: Vec<bool> = tr.iter().map(|r| r.0.made()).collect();
+ let (mut made, mut recorded) = (Vec::new(), Vec::new());
+ let both = y.iter().any(|v| *v) && y.iter().any(|v| !*v);
+ let head = both.then(|| Logistic::fit(Scaler::fit(&rows), &rows, &y, None, C, true));
+ for r in x.iter().filter(|r| test(&r.0)) {
+ let s = head.as_ref().map_or(0.0, |h| h.score(&r.1));
+ if r.0.made() { made.push(s) } else { recorded.push(s) }
+ }
+ let ok = head.is_some() && !made.is_empty() && !recorded.is_empty();
+ Held { out, auc: ok.then(|| auc(&made, &recorded)), made: made.len(), recorded: recorded.len() }
+}
+
+/// Held out by generator: each generator and the recordings of its kind left out in turn
+pub fn by_generator(x: &[(Item, Vec<f32>)]) -> Vec<Held> {
+ let mut gens: Vec<(String, String)> = x.iter().filter(|r| r.0.made()).map(|r| (r.0.by.clone(), r.0.kind.clone())).collect();
+ gens.sort();
+ gens.dedup();
+ gens.into_iter().map(|(g, k)| fold(x, |i| i.kind != k, |i| i.kind == k, g)).collect()
+}
+
+/// Held out by category: each category (rain, piano, speech...) left out in turn, made and recorded alike
+pub fn by_category(x: &[(Item, Vec<f32>)]) -> Vec<Held> {
+ let mut cats: Vec<String> = x.iter().map(|r| r.0.category.clone()).collect();
+ cats.sort();
+ cats.dedup();
+ cats.into_iter().map(|c| fold(x, |i| i.category != c, |i| i.category == c, c.clone())).collect()
+}
+
+/// Whether the axis ships: every generator held out at SHIPS or better
+pub fn ships(held: &[Held]) -> bool {
+ !held.is_empty() && held.iter().all(|h| h.auc.is_some_and(|a| a >= SHIPS))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn item(by: &str, kind: &str, category: &str) -> Item {
+ Item { file: PathBuf::new(), by: by.into(), kind: kind.into(), category: category.into() }
+ }
+
+ #[test]
+ fn held_out_by_generator_trains_on_the_others_only() {
+ // made sound sits high on the first feature, recorded low, in every kind: a head trained on two kinds finds
+ // the third's; a feature only one kind uses (the second) says nothing held out
+ let mut x = Vec::new();
+ for (g, k) in [("sao", "foley"), ("tokensynth", "instrument"), ("fish", "speech")] {
+ for i in 0..12 {
+ let j = i as f32 * 0.01;
+ x.push((item(g, k, k), vec![1.0 + j, if k == "speech" { 5.0 } else { 0.0 }]));
+ x.push((item("recorded", k, k), vec![-1.0 - j, if k == "speech" { 5.0 } else { 0.0 }]));
+ }
+ }
+ let g = by_generator(&x);
+ assert_eq!(g.iter().map(|h| (h.out.as_str(), h.made, h.recorded)).collect::<Vec<_>>(), [("fish", 12, 12), ("sao", 12, 12), ("tokensynth", 12, 12)]);
+ assert!(ships(&g), "{g:?}");
+ // a generator whose sound is no different from its recordings doesn't ship
+ for r in x.iter_mut().filter(|r| r.0.kind == "speech") {
+ r.1[0] = 0.0;
+ }
+ assert!(!ships(&by_generator(&x)));
+ assert_eq!(by_category(&x).len(), 3);
+ }
+}
+
+#[cfg(test)]
+mod real {
+ use super::*;
+ use raea_sys_columns::window::{Modality, features};
+
+ /// `RAEA_REALNESS=DIR cargo test -p raea-sys-fingerprint-use realness_on -- --ignored --nocapture`: DIR/made/made.jsonl
+ /// and DIR/real/real.jsonl (each a file, its generator or source and category), measured; DIR/report.json keeps it
+ #[test]
+ #[ignore]
+ fn realness_on_this_machine() {
+ let dir = PathBuf::from(std::env::var("RAEA_REALNESS").expect("RAEA_REALNESS"));
+ let kind = |c: &str| match c {
+ "speech" => "speech",
+ "brass" | "flute" | "guitar" | "keyboard" | "mallet" | "reed" | "string" | "bass" | "organ" => "instrument",
+ _ => "foley",
+ };
+ let mut items = Vec::new();
+ for (f, made) in [("made/made.jsonl", true), ("real/real.jsonl", false)] {
+ for l in std::fs::read_to_string(dir.join(f)).unwrap().lines() {
+ let v: serde_json::Value = serde_json::from_str(l).unwrap();
+ let c = v["category"].as_str().unwrap().to_string();
+ let by = if made { v["by"].as_str().unwrap().to_string() } else { "recorded".into() };
+ items.push(Item { file: v["file"].as_str().unwrap().into(), by, kind: kind(&c).into(), category: c });
+ }
+ }
+ // a category with no recordings (TokenSynth's bass and organ) can't be held out against anything
+ let recorded: Vec<String> = items.iter().filter(|i| !i.made()).map(|i| i.category.clone()).collect();
+ items.retain(|i| recorded.contains(&i.category));
+ let mut enc = raea_sys_columns::Encoders::load().unwrap();
+ let x: Vec<(Item, Vec<f32>)> = items
+ .into_iter()
+ .map(|i| {
+ let c = enc.columns(&i.file).unwrap();
+ let f = [Modality::Sound, Modality::Events].iter().flat_map(|m| features(&c, 0.0, c.dur, *m)).collect();
+ (i, f)
+ })
+ .collect();
+ let (g, c) = (by_generator(&x), by_category(&x));
+ let r = serde_json::json!({"by_generator": g, "by_category": c, "ships": ships(&g), "bar": SHIPS, "items": x.len()});
+ std::fs::write(dir.join("report.json"), serde_json::to_vec_pretty(&r).unwrap()).unwrap();
+ eprintln!("{}", serde_json::to_string_pretty(&r).unwrap());
+ }
+}