FR-CULL-8 said detection runs against the thumbnail or proxy tier and never a full decode, and faces.md §5 said the aligned crop is sampled from that same proxy. Both are wrong in the same place: they treat detection and cropping as one resolution problem when they are two, with opposite answers. Detection does not care. §4.1 fixes the graph's input at 640x640 and letterboxes whatever arrives, so a face filling 2% of the frame reaches the model at 12px whether the buffer handed over is 1024px or 6000px. Every pixel above the detector's own input is discarded before inference. The crop cares about nothing else. §5's warp produces the fixed 112x112 ArcFace sees, so source resolution converts directly into whether those 112 pixels were photographed or interpolated. Reading crop_px across the 18,671 faces the proxy-tier implementation stored: 47.3% were upsampled to reach the embedder, 314 of them by more than 2x, the smallest from 34 source pixels. An upsampled crop does not fail loudly -- it yields a confident embedding of detail that was never there, and the damage appears three stages later as clusters that will not separate. So FR-CULL-8 now specifies four stages with the resolutions named separately: render native through FR-EXP-9's pipeline, downscale for the detector, map boxes and landmarks back to native, crop and align from the native render. The affordability the old rule bought is met instead by when the pass runs -- background, preempted, resumable -- and the requirement says plainly what it now costs on a remote library: the original rather than FR-NC-3's byte range, 412 GB across the reference library's 19,107 images, so a whole-library pass is a transfer under FR-NC-6 rather than something that may start on its own. MIN_CROP_EDGE replaces the MIN_DETECT_EDGE this branch briefly had. Same number, guarding the quantity that turned out to matter. faces.md §7b records both measurements, and marks the second as unexplained rather than dressing it as a finding. Grouped by the buffer detection ran against, faces per image was 0.078 at 1024 or below and 1.82 at 2048 or better, controlled for file type and size. That gap is real and reproducible and I cannot account for it, because the letterbox above says detector input should not matter. M4 is where it gets settled. The crop measurement does not depend on it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
525 lines
18 KiB
Rust
525 lines
18 KiB
Rust
//! The face-indexing batch job, off the GUI.
|
|
//!
|
|
//! Checks every library image for a face-detection run marker, and optionally
|
|
//! indexes whatever is missing one.
|
|
//!
|
|
//! cargo run -p dr-ui --example face_index -- CATALOG.db THUMBS_DIR [--run DET.onnx EMB.onnx]
|
|
//! cargo run -p dr-ui --example face_index -- CATALOG.db THUMBS_DIR --cluster
|
|
//!
|
|
//! # Why this exists beside the button in the Identity screen
|
|
//!
|
|
//! Indexing a real library is hours of work (docs/faces.md §12.2), and the
|
|
//! cases where that is worth starting — an overnight pass, a fresh import, a
|
|
//! machine left running — are exactly the ones where holding a window open is
|
|
//! the wrong shape. The check half is useful on its own: it is cheap, it
|
|
//! answers "has face recognition been over all of this", and it distinguishes
|
|
//! *not yet indexed* from *waiting on a proxy*, which are different problems
|
|
//! with different fixes.
|
|
//!
|
|
//! The models must have had their input dims frozen first; see
|
|
//! `tools/fix-face-model-shapes.sh`.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use dr_catalog::Catalog;
|
|
use dr_thumbs::ThumbStore;
|
|
use dr_ui::faces::{self, FaceSweepMessage};
|
|
|
|
const MODEL_ID: &str = "w600k_mbf";
|
|
|
|
fn main() {
|
|
env_logger::init();
|
|
|
|
let args: Vec<String> = std::env::args().skip(1).collect();
|
|
if args.len() < 2 {
|
|
eprintln!(
|
|
"usage: face_index CATALOG.db THUMBS_DIR [--run DETECTOR.onnx EMBEDDER.onnx]\n\
|
|
\n\
|
|
With no --run this only reports; nothing is written.\n\
|
|
--cluster groups what is indexed; --tune compares thresholds without writing.\n\
|
|
--quality DET.onnx EMB.onnx reports face size and sharpness, also without writing."
|
|
);
|
|
std::process::exit(2);
|
|
}
|
|
|
|
let catalog_path = PathBuf::from(&args[0]);
|
|
let store_dir = PathBuf::from(&args[1]);
|
|
|
|
let catalog = match Catalog::open(&catalog_path) {
|
|
Ok(c) => c,
|
|
Err(e) => {
|
|
eprintln!("cannot open catalog {}: {e}", catalog_path.display());
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
let store = match ThumbStore::open(&store_dir) {
|
|
Ok(s) => s,
|
|
Err(e) => {
|
|
eprintln!("cannot open thumbnail store {}: {e}", store_dir.display());
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
|
|
let audit = match faces::audit(&catalog, &store, MODEL_ID) {
|
|
Ok(a) => a,
|
|
Err(e) => {
|
|
eprintln!("coverage check failed: {e}");
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
|
|
println!("model {MODEL_ID}");
|
|
println!("images {}", audit.coverage.images);
|
|
println!(
|
|
"indexed {} ({:.1}%)",
|
|
audit.coverage.indexed,
|
|
audit.coverage.fraction() * 100.0
|
|
);
|
|
println!("faces {}", audit.coverage.faces);
|
|
println!(
|
|
"no faces {} (indexed, nothing found — the common case)",
|
|
audit.coverage.without_faces
|
|
);
|
|
println!("outstanding {}", audit.coverage.outstanding());
|
|
println!(" ready to index {}", audit.ready);
|
|
println!(" awaiting proxy {}", audit.awaiting_proxy);
|
|
|
|
// Grouping is a separate step from indexing on purpose: it is a
|
|
// whole-library operation over the embeddings detection produced, and it is
|
|
// worth running *after* a sweep rather than during one (catalog.md §10.2).
|
|
if args.iter().any(|a| a == "--cluster") {
|
|
// The engine's own defaults, not this device's settings file: a batch
|
|
// job run over a library on a server has no business inheriting the
|
|
// dials somebody moved on their laptop.
|
|
let grouping = dr_types::settings::FaceSettings::default();
|
|
match dr_ui::faces::recluster(&catalog, MODEL_ID, &grouping) {
|
|
Ok((suggested, created)) => {
|
|
println!("\nclustering: {suggested} suggestion(s), {created} new group(s)");
|
|
report_people(&catalog);
|
|
}
|
|
Err(e) => {
|
|
eprintln!("clustering failed: {e}");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Tuning, and deliberately read-only: it answers "what would this
|
|
// threshold do to my library" without writing a single suggestion, which
|
|
// is the only way to compare several without each one polluting the next.
|
|
if args.iter().any(|a| a == "--tune") {
|
|
tune_thresholds(&catalog);
|
|
return;
|
|
}
|
|
|
|
// Quality tuning, and read-only like `--tune`. Runs the real detector over
|
|
// real proxies with **both gates disabled**, so the distribution it prints
|
|
// is of everything the detector finds rather than of what survives the
|
|
// current settings — which is the only way to see what a threshold would
|
|
// actually remove.
|
|
if let Some(i) = args.iter().position(|a| a == "--quality") {
|
|
let (Some(detector), Some(embedder)) = (args.get(i + 1), args.get(i + 2)) else {
|
|
eprintln!("--quality needs both a detector and an embedder");
|
|
std::process::exit(2);
|
|
};
|
|
report_quality(
|
|
&catalog,
|
|
&store,
|
|
std::path::Path::new(detector),
|
|
std::path::Path::new(embedder),
|
|
);
|
|
return;
|
|
}
|
|
|
|
let run = args.iter().position(|a| a == "--run");
|
|
let Some(i) = run else {
|
|
if audit.coverage.is_complete() {
|
|
println!("\nnothing outstanding.");
|
|
} else {
|
|
println!("\npass --run DETECTOR.onnx EMBEDDER.onnx to index the outstanding images.");
|
|
}
|
|
return;
|
|
};
|
|
|
|
let (Some(detector), Some(embedder)) = (args.get(i + 1), args.get(i + 2)) else {
|
|
eprintln!("--run needs both a detector and an embedder");
|
|
std::process::exit(2);
|
|
};
|
|
|
|
if audit.ready == 0 {
|
|
println!("\nnothing ready to index.");
|
|
if audit.awaiting_proxy > 0 {
|
|
// Worth saying plainly: running this again will not help, because
|
|
// the blocker is in the thumbnail store rather than here.
|
|
println!(
|
|
"{} image(s) are waiting on a proxy — run the thumbnail sweep first.",
|
|
audit.awaiting_proxy
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Say it here rather than letting the pass return an empty result and
|
|
// print "done: 0 image(s)". That is the shape of report this whole change
|
|
// exists to stop producing.
|
|
if faces::FACE_TIER.edge() < dr_face::MIN_DETECT_EDGE {
|
|
println!(
|
|
"\n--run cannot index from the local store any more.\n\
|
|
\n\
|
|
Detection needs a proxy longer than {}px and the store's largest\n\
|
|
tier is {}px, so every image here would be refused. The measurement\n\
|
|
behind that floor is docs/faces.md §7c.\n\
|
|
\n\
|
|
Index from the app's Identity screen instead: that pass fetches the\n\
|
|
embedded preview at full size, which is the resolution detection\n\
|
|
needs.",
|
|
dr_face::MIN_DETECT_EDGE - 1,
|
|
faces::FACE_TIER.edge(),
|
|
);
|
|
return;
|
|
}
|
|
|
|
println!("\nindexing {} image(s)…", audit.ready);
|
|
let rx = faces::spawn_store_face_sweep(
|
|
catalog_path,
|
|
store_dir,
|
|
PathBuf::from(detector),
|
|
PathBuf::from(embedder),
|
|
MODEL_ID.to_string(),
|
|
dr_face::DetectOptions::default(),
|
|
);
|
|
|
|
let mut seen = 0usize;
|
|
let mut total = 0usize;
|
|
let start = std::time::Instant::now();
|
|
for msg in rx {
|
|
match msg {
|
|
FaceSweepMessage::Total(n) => total = n,
|
|
FaceSweepMessage::Indexed { faces, .. } => {
|
|
seen += 1;
|
|
// One line per image would be thousands of lines; one per
|
|
// twenty-five is enough to see it moving and to estimate.
|
|
if seen.is_multiple_of(25) || faces > 0 {
|
|
let rate = seen as f64 / start.elapsed().as_secs_f64().max(1e-6);
|
|
println!(
|
|
" {seen}/{total} {:.2} img/s ~{:.0} min left",
|
|
rate,
|
|
(total.saturating_sub(seen)) as f64 / rate.max(1e-6) / 60.0
|
|
);
|
|
}
|
|
}
|
|
FaceSweepMessage::Failed { images } => seen += images,
|
|
FaceSweepMessage::Finished {
|
|
images,
|
|
faces,
|
|
failed,
|
|
too_small,
|
|
} => {
|
|
println!(
|
|
"\ndone: {images} image(s), {faces} face(s), {failed} failed, \
|
|
{too_small} with no proxy big enough, in {:.0}s",
|
|
start.elapsed().as_secs_f64()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Ok(a) = faces::audit(&catalog, &store, MODEL_ID) {
|
|
println!("{}", a.summary());
|
|
}
|
|
println!("\nrun again with --cluster to group these faces into people.");
|
|
}
|
|
|
|
/// What the clustering proposed, largest group first.
|
|
fn report_people(catalog: &Catalog) {
|
|
let Ok(people) = dr_catalog::faces::people(catalog.connection()) else {
|
|
return;
|
|
};
|
|
if people.is_empty() {
|
|
println!("no groups — too few faces, or none similar enough to group.");
|
|
return;
|
|
}
|
|
println!("\n{} group(s):", people.len());
|
|
for p in people.iter().take(30) {
|
|
let name = if p.name.is_empty() {
|
|
"(unnamed)".to_string()
|
|
} else {
|
|
p.name.clone()
|
|
};
|
|
println!(
|
|
" {name:<24} {} confirmed, {} suggested",
|
|
p.confirmed_faces, p.suggested_faces
|
|
);
|
|
}
|
|
if people.len() > 30 {
|
|
println!(" … and {} more", people.len() - 30);
|
|
}
|
|
}
|
|
|
|
/// What several merge thresholds would each do to this library.
|
|
///
|
|
/// The default 0.9 is a *probability*, and the cosine it lands on depends on
|
|
/// the calibration — so "is 0.9 too tight" is not a question anyone can answer
|
|
/// from the number alone. This runs the real clusterer over the real
|
|
/// embeddings at a range of thresholds and prints what each one produces, which
|
|
/// is the only honest way to choose.
|
|
///
|
|
/// Nothing is written. Run it, read the table, then pass the number you want.
|
|
fn tune_thresholds(catalog: &Catalog) {
|
|
use dr_catalog::faces;
|
|
|
|
let conn = catalog.connection();
|
|
let cal = match faces::calibration(conn, MODEL_ID) {
|
|
Ok(Some((c, _))) => c,
|
|
_ => dr_face::Calibration::default(),
|
|
};
|
|
|
|
let stored = match faces::embeddings(conn, MODEL_ID) {
|
|
Ok(s) => s,
|
|
Err(e) => {
|
|
eprintln!("cannot read embeddings: {e}");
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
if stored.is_empty() {
|
|
println!("no faces indexed yet — nothing to tune.");
|
|
return;
|
|
}
|
|
|
|
let model = dr_face::ModelId::new(MODEL_ID.to_string());
|
|
let mut candidates = Vec::with_capacity(stored.len());
|
|
for (face_id, image_id, blob, crop_px) in stored {
|
|
let Some(emb) = dr_face::Embedding::from_f16_bytes(model.clone(), &blob) else {
|
|
continue;
|
|
};
|
|
candidates.push(dr_face::Candidate {
|
|
face: face_id.0,
|
|
image: image_id.0,
|
|
embedding: emb.v.to_vec(),
|
|
crop_px,
|
|
confirmed_person: None,
|
|
});
|
|
}
|
|
|
|
println!(
|
|
"\n{} face(s), calibration valid: {}",
|
|
candidates.len(),
|
|
cal.valid
|
|
);
|
|
println!(
|
|
"\n{:>6} {:>7} {:>7} {:>7} {:>7} {:>8} {:>7}",
|
|
"P", "cosine", "groups", "grouped", "largest", "in groups", "time"
|
|
);
|
|
println!("{}", "-".repeat(60));
|
|
|
|
for p in [0.99_f32, 0.97, 0.95, 0.9, 0.85, 0.8, 0.75, 0.7, 0.6, 0.5] {
|
|
let start = std::time::Instant::now();
|
|
let clusters = dr_face::cluster(&candidates, &cal, p);
|
|
let elapsed = start.elapsed();
|
|
|
|
// A group of one is not a person, and `recluster` discards those, so
|
|
// the interesting figures count only the real groups.
|
|
let real: Vec<_> = clusters.iter().filter(|c| c.members.len() >= 2).collect();
|
|
let grouped: usize = real.iter().map(|c| c.members.len()).sum();
|
|
let largest = real.first().map(|c| c.members.len()).unwrap_or(0);
|
|
|
|
println!(
|
|
"{p:>6.2} {:>7.3} {:>7} {:>7} {:>7} {:>7.0}% {:>6.2}s",
|
|
cal.boundary_at(p, 150.0, 0.0),
|
|
real.len(),
|
|
grouped,
|
|
largest,
|
|
100.0 * grouped as f64 / candidates.len() as f64,
|
|
elapsed.as_secs_f64(),
|
|
);
|
|
}
|
|
|
|
println!(
|
|
"\nA looser threshold makes bigger groups and merges people who are not\n\
|
|
the same; a tighter one splits one person across several. The largest\n\
|
|
group is the tell: when it starts growing much faster than the rest,\n\
|
|
identities are being welded together."
|
|
);
|
|
}
|
|
|
|
/// How many images to sample for the quality report.
|
|
///
|
|
/// Enough for the distribution to settle, few enough to finish while the user
|
|
/// is watching: detection is ~100ms an image, so this is a couple of minutes.
|
|
const QUALITY_SAMPLE: usize = 600;
|
|
|
|
/// What the detector finds, before either quality gate is applied.
|
|
///
|
|
/// The two floors — face size and sharpness — are not independent: a face
|
|
/// smaller than the embedder's 112-pixel input was upsampled to reach it, and
|
|
/// upsampling invents no edges, so small faces score low on sharpness even when
|
|
/// the original was crisp. Choosing either number without seeing the other is
|
|
/// how you end up with one gate doing nothing and the other doing too much.
|
|
///
|
|
/// So this prints them together, over the real library, with nothing filtered.
|
|
fn report_quality(
|
|
catalog: &Catalog,
|
|
store: &ThumbStore,
|
|
detector: &std::path::Path,
|
|
embedder: &std::path::Path,
|
|
) {
|
|
let mut det = match dr_face::Detector::from_path(detector) {
|
|
Ok(d) => d,
|
|
Err(e) => {
|
|
eprintln!("cannot load the detector: {e}");
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
// Loaded but unused: the point is to fail here, before a two-minute scan,
|
|
// if the pair the user passed is not the pair indexing would use.
|
|
if let Err(e) =
|
|
dr_face::Embedder::from_path(embedder, dr_face::ModelId::new(MODEL_ID.to_string()))
|
|
{
|
|
eprintln!("cannot load the embedder: {e}");
|
|
std::process::exit(1);
|
|
}
|
|
|
|
// Everything the detector can find: no size floor, no sharpness floor.
|
|
let options = dr_face::DetectOptions {
|
|
min_face_px: 0.0,
|
|
min_source_px: 0.0,
|
|
min_sharpness: 0.0,
|
|
..Default::default()
|
|
};
|
|
|
|
let mut stmt = match catalog.connection().prepare(
|
|
"SELECT r.file_id FROM remote r
|
|
JOIN images i ON i.id = r.image_id
|
|
WHERE r.file_id IS NOT NULL AND i.trashed_at IS NULL
|
|
ORDER BY i.id",
|
|
) {
|
|
Ok(s) => s,
|
|
Err(e) => {
|
|
eprintln!("cannot list images: {e}");
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
let file_ids: Vec<u64> = stmt
|
|
.query_map([], |r| r.get::<_, i64>(0))
|
|
.into_iter()
|
|
.flatten()
|
|
.filter_map(Result::ok)
|
|
.map(|v| v as u64)
|
|
.filter(|id| store.contains(*id, faces::FACE_TIER))
|
|
.take(QUALITY_SAMPLE)
|
|
.collect();
|
|
|
|
if file_ids.is_empty() {
|
|
println!("no proxies on disk to measure — browse the library first.");
|
|
return;
|
|
}
|
|
println!("\nmeasuring {} image(s)…", file_ids.len());
|
|
|
|
// (source_px, sharpness) per detected face.
|
|
let mut found: Vec<(f32, f32)> = Vec::new();
|
|
let mut images = 0usize;
|
|
for id in &file_ids {
|
|
let Ok(Some(thumb)) = store.get(*id, faces::FACE_TIER) else {
|
|
continue;
|
|
};
|
|
let Ok((w, h, rgba)) = dr_thumbs::codec::decode_rgba(&thumb.bytes) else {
|
|
continue;
|
|
};
|
|
let rgb: Vec<f32> = rgba
|
|
.chunks_exact(4)
|
|
.flat_map(|p| {
|
|
[
|
|
p[0] as f32 / 255.0,
|
|
p[1] as f32 / 255.0,
|
|
p[2] as f32 / 255.0,
|
|
]
|
|
})
|
|
.collect();
|
|
|
|
let Ok(dets) = det.detect(&rgb, w as usize, h as usize, &options) else {
|
|
continue;
|
|
};
|
|
images += 1;
|
|
for d in &dets {
|
|
if let Some(a) = dr_face::warp(&rgb, w as usize, h as usize, &d.landmarks) {
|
|
found.push((a.source_px(), a.sharpness()));
|
|
}
|
|
}
|
|
if images.is_multiple_of(50) {
|
|
println!(
|
|
" {images}/{} images, {} face(s)",
|
|
file_ids.len(),
|
|
found.len()
|
|
);
|
|
}
|
|
}
|
|
|
|
if found.is_empty() {
|
|
println!("no faces found in the sample.");
|
|
return;
|
|
}
|
|
|
|
let pct = |v: &mut Vec<f32>, p: f64| -> f32 {
|
|
v.sort_by(|a, b| a.total_cmp(b));
|
|
v[(((v.len() - 1) as f64) * p) as usize]
|
|
};
|
|
let mut sizes: Vec<f32> = found.iter().map(|f| f.0).collect();
|
|
let mut sharps: Vec<f32> = found.iter().map(|f| f.1).collect();
|
|
|
|
println!("\n{} face(s) in {images} image(s)\n", found.len());
|
|
println!(
|
|
"{:>12} {:>8} {:>10}",
|
|
"percentile", "size px", "sharpness"
|
|
);
|
|
println!("{}", "-".repeat(34));
|
|
for p in [0.01, 0.05, 0.10, 0.25, 0.50, 0.75, 0.90, 0.99] {
|
|
println!(
|
|
"{:>11.0}% {:>8.0} {:>10.4}",
|
|
p * 100.0,
|
|
pct(&mut sizes, p),
|
|
pct(&mut sharps, p)
|
|
);
|
|
}
|
|
|
|
// What each candidate pair would remove. Cumulative, because the gates are
|
|
// applied together and their overlap is the whole question.
|
|
println!(
|
|
"\n{:>8} {:>10} {:>9} {:>9} {:>9}",
|
|
"min crop", "min sharp", "size cut", "blur cut", "kept"
|
|
);
|
|
println!("{}", "-".repeat(52));
|
|
for (min_px, min_sharp) in [
|
|
(0.0_f32, 0.0_f32),
|
|
(32.0, 0.0),
|
|
(32.0, 0.002),
|
|
(32.0, 0.005),
|
|
(32.0, 0.010),
|
|
(32.0, 0.020),
|
|
(48.0, 0.005),
|
|
(64.0, 0.010),
|
|
(64.0, 0.020),
|
|
] {
|
|
let by_size = found.iter().filter(|f| f.0 < min_px).count();
|
|
let by_blur = found
|
|
.iter()
|
|
.filter(|f| f.0 >= min_px && f.1 < min_sharp)
|
|
.count();
|
|
let kept = found.len() - by_size - by_blur;
|
|
println!(
|
|
"{min_px:>8.0} {min_sharp:>10.3} {:>8.0}% {:>8.0}% {:>8.0}%",
|
|
100.0 * by_size as f64 / found.len() as f64,
|
|
100.0 * by_blur as f64 / found.len() as f64,
|
|
100.0 * kept as f64 / found.len() as f64,
|
|
);
|
|
}
|
|
|
|
println!(
|
|
"\n`size cut` is what the size floor removes; `blur cut` is what the\n\
|
|
sharpness floor removes *of what the size floor left*, so the two\n\
|
|
columns do not double-count. A sharpness floor that cuts almost\n\
|
|
nothing once the size floor is in place is a floor that is not\n\
|
|
earning its place."
|
|
);
|
|
}
|