use std::collections::HashMap;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use super::hkm::{HierarchicalVocabulary, HkmBuildOptions};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ImageScore {
pub image_id: usize,
pub score: f32,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct QueryWorkStats {
pub leaf_distance_computations: usize,
pub entries_visited: usize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VocabTreeOptions {
pub embedding_dim: usize,
pub hamming_sigma: f32,
pub index_num_neighbors: usize,
pub query_num_neighbors: usize,
pub min_he_entries: usize,
pub projection_seed: u64,
}
impl Default for VocabTreeOptions {
fn default() -> Self {
Self {
embedding_dim: 64,
hamming_sigma: 16.0,
index_num_neighbors: 1,
query_num_neighbors: 5,
min_he_entries: 5,
projection_seed: 1,
}
}
}
#[derive(Debug, Clone)]
struct HammingWeightLut {
lut: Vec<f32>,
max_distance: usize,
}
impl HammingWeightLut {
fn new(embedding_dim: usize, sigma: f32) -> Self {
let max_distance = (1.5 * sigma) as usize;
let sigma_sq = sigma * sigma;
let lut = (0..=embedding_dim)
.map(|h| {
if h <= max_distance {
(-(h as f32) * (h as f32) / sigma_sq).exp()
} else {
0.0
}
})
.collect();
Self { lut, max_distance }
}
fn weight(&self, h: usize) -> f32 {
self.lut.get(h).copied().unwrap_or(0.0)
}
}
#[derive(Debug, Clone)]
struct InvertedFileEntry {
image_id: usize,
binary: Vec<u64>,
}
fn binarize(projected: &[f32], thresholds: &[f32]) -> Vec<u64> {
let words = thresholds.len().div_ceil(64);
let mut bits = vec![0u64; words.max(1)];
for (i, (&p, &t)) in projected.iter().zip(thresholds).enumerate() {
if p > t {
bits[i / 64] |= 1u64 << (i % 64);
}
}
bits
}
fn hamming_distance(a: &[u64], b: &[u64]) -> usize {
a.iter()
.zip(b)
.map(|(x, y)| (x ^ y).count_ones() as usize)
.sum()
}
fn median(sorted: &[f32]) -> f32 {
let n = sorted.len();
if n == 0 {
return 0.0;
}
if n % 2 == 1 {
sorted[n / 2]
} else {
0.5 * (sorted[n / 2 - 1] + sorted[n / 2])
}
}
#[derive(Debug, Clone)]
struct InvertedFile {
entries: Vec<InvertedFileEntry>,
thresholds: Vec<f32>,
squared_idf_weight: f32,
sorted: bool,
}
impl InvertedFile {
fn new(embedding_dim: usize) -> Self {
Self {
entries: Vec::new(),
thresholds: vec![0.0; embedding_dim],
squared_idf_weight: 0.0,
sorted: false,
}
}
fn compute_hamming_embedding(&mut self, projected_samples: &[Vec<f32>]) {
let dim = self.thresholds.len();
for d in 0..dim {
let mut vals: Vec<f32> = projected_samples.iter().map(|p| p[d]).collect();
vals.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
self.thresholds[d] = median(&vals);
}
}
fn add_entry(&mut self, image_id: usize, projected: &[f32]) {
let binary = binarize(projected, &self.thresholds);
self.entries.push(InvertedFileEntry { image_id, binary });
self.sorted = false;
}
fn sort_entries(&mut self) {
self.entries.sort_by_key(|e| e.image_id);
self.sorted = true;
}
fn compute_idf_weight(&mut self, num_total_images: usize) {
if self.entries.is_empty() || num_total_images == 0 {
self.squared_idf_weight = 0.0;
return;
}
let unique: std::collections::HashSet<usize> =
self.entries.iter().map(|e| e.image_id).collect();
let idf = (num_total_images as f64 / unique.len() as f64).ln();
self.squared_idf_weight = (idf * idf) as f32;
}
fn accumulate_self_similarities(&self, acc: &mut HashMap<usize, f64>) {
for e in &self.entries {
*acc.entry(e.image_id).or_insert(0.0) += self.squared_idf_weight as f64;
}
}
fn score_feature(&self, query_projected: &[f32], lut: &HammingWeightLut) -> Vec<(usize, f32)> {
debug_assert!(
self.sorted,
"score_feature requires sorted entries (call finalize() first)"
);
if self.entries.is_empty() {
return Vec::new();
}
let query_bits = binarize(query_projected, &self.thresholds);
let mut out = Vec::new();
let mut cur_image = self.entries[0].image_id;
let mut cur_score = 0.0f32;
let mut cur_votes = 0usize;
let flush = |image_id: usize, score: f32, votes: usize, out: &mut Vec<(usize, f32)>| {
if votes > 0 {
let s = (score / (votes as f32).sqrt()) * self.squared_idf_weight;
out.push((image_id, s));
}
};
for e in &self.entries {
if e.image_id != cur_image {
flush(cur_image, cur_score, cur_votes, &mut out);
cur_image = e.image_id;
cur_score = 0.0;
cur_votes = 0;
}
let hd = hamming_distance(&query_bits, &e.binary);
if hd <= lut.max_distance {
cur_score += lut.weight(hd);
cur_votes += 1;
}
}
flush(cur_image, cur_score, cur_votes, &mut out);
out
}
}
fn generate_orthonormal_projection(dim: usize, embedding_dim: usize, seed: u64) -> Vec<Vec<f32>> {
assert!(
embedding_dim <= dim,
"embedding_dim ({embedding_dim}) must not exceed the descriptor dimension ({dim})"
);
let mut rng = SmallRng::seed_from_u64(seed);
let mut rows: Vec<Vec<f32>> = Vec::with_capacity(embedding_dim);
while rows.len() < embedding_dim {
let mut v = gaussian_vector(dim, &mut rng);
for r in &rows {
let dot: f32 = v.iter().zip(r.iter()).map(|(a, b)| a * b).sum();
for (vi, ri) in v.iter_mut().zip(r.iter()) {
*vi -= dot * ri;
}
}
let norm = v.iter().map(|x| x * x).sum::<f32>().sqrt();
if norm > 1e-6 {
for x in v.iter_mut() {
*x /= norm;
}
rows.push(v);
}
}
rows
}
fn gaussian_vector(dim: usize, rng: &mut SmallRng) -> Vec<f32> {
(0..dim)
.map(|_| {
let u1: f64 = rng.gen_range(1e-12..1.0);
let u2: f64 = rng.gen_range(0.0..1.0);
((-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos()) as f32
})
.collect()
}
fn project(proj_matrix: &[Vec<f32>], descriptor: &[f32]) -> Vec<f32> {
proj_matrix
.iter()
.map(|row| row.iter().zip(descriptor.iter()).map(|(a, b)| a * b).sum())
.collect()
}
pub struct VocabTree {
vocab: HierarchicalVocabulary,
proj_matrix: Vec<Vec<f32>>,
files: Vec<InvertedFile>,
normalization_constants: HashMap<usize, f32>,
weight_lut: HammingWeightLut,
options: VocabTreeOptions,
image_ids: std::collections::HashSet<usize>,
finalized: bool,
}
impl VocabTree {
pub fn build(
training_descriptors: &[&[f32]],
hkm_options: &HkmBuildOptions,
options: &VocabTreeOptions,
) -> Option<VocabTree> {
let vocab = HierarchicalVocabulary::build(training_descriptors, hkm_options)?;
if options.embedding_dim == 0 || options.embedding_dim > vocab.dim() {
return None;
}
let proj_matrix = generate_orthonormal_projection(
vocab.dim(),
options.embedding_dim,
options.projection_seed,
);
let mut files: Vec<InvertedFile> = (0..vocab.num_words())
.map(|_| InvertedFile::new(options.embedding_dim))
.collect();
let mut per_word_projected: Vec<Vec<Vec<f32>>> = vec![Vec::new(); vocab.num_words()];
for &d in training_descriptors {
let word = vocab.nearest_words(d, 1)[0];
per_word_projected[word].push(project(&proj_matrix, d));
}
for (i, samples) in per_word_projected.into_iter().enumerate() {
if samples.len() >= options.min_he_entries {
files[i].compute_hamming_embedding(&samples);
}
}
Some(VocabTree {
vocab,
proj_matrix,
files,
normalization_constants: HashMap::new(),
weight_lut: HammingWeightLut::new(options.embedding_dim, options.hamming_sigma),
options: *options,
image_ids: std::collections::HashSet::new(),
finalized: false,
})
}
pub fn num_words(&self) -> usize {
self.vocab.num_words()
}
pub fn num_images(&self) -> usize {
self.image_ids.len()
}
pub fn is_image_indexed(&self, image_id: usize) -> bool {
self.image_ids.contains(&image_id)
}
pub fn add_image(&mut self, image_id: usize, descriptors: &[Vec<f32>]) {
if self.is_image_indexed(image_id) {
return;
}
self.image_ids.insert(image_id);
self.finalized = false;
for d in descriptors {
if d.len() != self.vocab.dim() {
continue;
}
let proj = project(&self.proj_matrix, d);
for word in self
.vocab
.nearest_words(d, self.options.index_num_neighbors)
{
self.files[word].add_entry(image_id, &proj);
}
}
}
pub fn finalize(&mut self) {
for f in &mut self.files {
f.sort_entries();
}
let num_total_images = self.image_ids.len();
for f in &mut self.files {
f.compute_idf_weight(num_total_images);
}
let mut self_sim: HashMap<usize, f64> = HashMap::new();
for f in &self.files {
f.accumulate_self_similarities(&mut self_sim);
}
self.normalization_constants = self_sim
.into_iter()
.map(|(id, s)| {
(
id,
if s > 0.0 {
(1.0 / s.sqrt()) as f32
} else {
0.0
},
)
})
.collect();
self.finalized = true;
}
pub fn query(
&self,
descriptors: &[Vec<f32>],
max_num_images: Option<usize>,
) -> Vec<ImageScore> {
self.query_with_work(descriptors, max_num_images).0
}
pub fn query_with_work(
&self,
descriptors: &[Vec<f32>],
max_num_images: Option<usize>,
) -> (Vec<ImageScore>, QueryWorkStats) {
let mut stats = QueryWorkStats::default();
debug_assert!(self.finalized, "query() requires finalize() to have run");
if descriptors.is_empty() {
return (Vec::new(), stats);
}
let mut self_similarity = 0.0f64;
let mut assignments: Vec<Vec<usize>> = Vec::with_capacity(descriptors.len());
for d in descriptors {
let words = if d.len() == self.vocab.dim() {
self.vocab
.nearest_words(d, self.options.query_num_neighbors)
} else {
Vec::new()
};
stats.leaf_distance_computations += self.vocab.num_words();
for &w in &words {
self_similarity += self.files[w].squared_idf_weight as f64;
}
assignments.push(words);
}
let normalization_weight = if self_similarity > 0.0 {
(1.0 / self_similarity.sqrt()) as f32
} else {
1.0
};
let mut scores: HashMap<usize, f32> = HashMap::new();
for (d, words) in descriptors.iter().zip(assignments.iter()) {
if words.is_empty() {
continue;
}
let proj = project(&self.proj_matrix, d);
for &w in words {
stats.entries_visited += self.files[w].entries.len();
for (image_id, s) in self.files[w].score_feature(&proj, &self.weight_lut) {
*scores.entry(image_id).or_insert(0.0) += s;
}
}
}
let mut out: Vec<ImageScore> = scores
.into_iter()
.map(|(image_id, score)| {
let norm_const = self
.normalization_constants
.get(&image_id)
.copied()
.unwrap_or(0.0);
ImageScore {
image_id,
score: score * normalization_weight * norm_const,
}
})
.collect();
out.sort_by(|a, b| {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
.then(a.image_id.cmp(&b.image_id))
});
if let Some(n) = max_num_images {
out.truncate(n);
}
(out, stats)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn word_desc(dim: usize, i: usize) -> Vec<f32> {
let mut w = vec![0.0f32; dim];
w[i] = 1.0;
w
}
struct Lcg(u64);
impl Lcg {
fn next(&mut self) -> f32 {
self.0 = self
.0
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
((self.0 >> 11) as f64 / (1u64 << 53) as f64) as f32
}
}
fn cluster(center: &[f32], n: usize, jitter: f32, seed: u64) -> Vec<Vec<f32>> {
let mut rng = Lcg(seed);
(0..n)
.map(|_| {
center
.iter()
.map(|&c| c + (rng.next() - 0.5) * 2.0 * jitter)
.collect()
})
.collect()
}
fn build_test_tree(training: &[&[f32]], embedding_dim: usize) -> VocabTree {
let hkm = HkmBuildOptions {
branching_factor: 3,
depth: 2,
iterations: 15,
seed: 11,
};
let opts = VocabTreeOptions {
embedding_dim,
min_he_entries: 3,
..VocabTreeOptions::default()
};
VocabTree::build(training, &hkm, &opts).expect("tree should build on well-formed input")
}
#[test]
fn image_retrieves_itself_first() {
let dim = 12;
let places: Vec<Vec<Vec<f32>>> = (0..3)
.map(|p| {
let mut img = cluster(&word_desc(dim, p), 20, 0.03, 1000 + p as u64);
img.extend(cluster(&word_desc(dim, p + 3), 20, 0.03, 2000 + p as u64));
img
})
.collect();
let training: Vec<&[f32]> = places.iter().flatten().map(|v| v.as_slice()).collect();
let mut tree = build_test_tree(&training, 8);
for (i, img) in places.iter().enumerate() {
tree.add_image(i, img);
}
tree.finalize();
for (i, img) in places.iter().enumerate() {
let scores = tree.query(img, None);
let top = scores.first().expect("at least one scored image");
assert_eq!(
top.image_id, i,
"image {i} should retrieve itself first; got {scores:?}"
);
}
}
#[test]
fn near_duplicate_ranks_above_unrelated_image() {
let dim = 12;
let content_a = {
let mut img = cluster(&word_desc(dim, 0), 25, 0.02, 10);
img.extend(cluster(&word_desc(dim, 1), 25, 0.02, 20));
img
};
let content_a_dup = {
let mut img = cluster(&word_desc(dim, 0), 25, 0.02, 30);
img.extend(cluster(&word_desc(dim, 1), 25, 0.02, 40));
img
};
let content_b = {
let mut img = cluster(&word_desc(dim, 4), 25, 0.02, 50);
img.extend(cluster(&word_desc(dim, 5), 25, 0.02, 60));
img
};
let all: Vec<Vec<Vec<f32>>> = vec![content_a.clone(), content_a_dup, content_b];
let training: Vec<&[f32]> = all.iter().flatten().map(|v| v.as_slice()).collect();
let mut tree = build_test_tree(&training, 8);
for (i, img) in all.iter().enumerate() {
tree.add_image(i, img);
}
tree.finalize();
let scores = tree.query(&content_a, None);
let score_of = |id: usize| {
scores
.iter()
.find(|s| s.image_id == id)
.map_or(0.0, |s| s.score)
};
let (s_dup, s_b) = (score_of(1), score_of(2));
assert!(
s_dup > s_b,
"near-duplicate (image 1, score {s_dup}) should outrank the unrelated image (image 2, score {s_b})"
);
}
#[test]
fn hamming_weight_lut_matches_colmap_formula() {
let lut = HammingWeightLut::new(64, 16.0);
assert_eq!(lut.max_distance, 24); assert!((lut.weight(0) - 1.0).abs() < 1e-6);
let expected_8 = (-64.0f32 / 256.0).exp(); assert!((lut.weight(8) - expected_8).abs() < 1e-6);
assert_eq!(lut.weight(25), 0.0); }
#[test]
fn projection_rows_are_orthonormal() {
let rows = generate_orthonormal_projection(20, 6, 123);
assert_eq!(rows.len(), 6);
for r in &rows {
let norm = r.iter().map(|x| x * x).sum::<f32>().sqrt();
assert!((norm - 1.0).abs() < 1e-4, "row not unit-norm: {norm}");
}
for i in 0..rows.len() {
for j in (i + 1)..rows.len() {
let dot: f32 = rows[i].iter().zip(rows[j].iter()).map(|(a, b)| a * b).sum();
assert!(dot.abs() < 1e-3, "rows {i},{j} not orthogonal (dot={dot})");
}
}
}
#[test]
fn query_work_grows_linearly_while_flat_pairwise_is_quadratic() {
let dim = 256;
let per_image = 8;
let places = 24usize;
let mut rng = Lcg(4242);
let centroids: Vec<Vec<f32>> = (0..places)
.map(|_| (0..dim).map(|_| rng.next() * 2.0 - 1.0).collect())
.collect();
let mut descriptors_for = |image: usize| -> Vec<Vec<f32>> {
let c = ¢roids[image % places];
(0..per_image)
.map(|_| {
c.iter()
.map(|&v| v + (rng.next() - 0.5) * 0.2)
.collect::<Vec<f32>>()
})
.collect()
};
let mut build_and_query_work = |num_images: usize| -> usize {
let mut training: Vec<Vec<Vec<f32>>> = Vec::new();
let mut corpus: Vec<Vec<Vec<f32>>> = Vec::new();
for image in 0..num_images {
let d = descriptors_for(image);
if image % 32 == 0 {
training.push(d.clone());
}
corpus.push(d);
}
let flat_training: Vec<&[f32]> = training
.iter()
.flat_map(|d| d.iter().map(|v| v.as_slice()))
.collect();
let hkm = HkmBuildOptions {
branching_factor: 4,
depth: 2,
iterations: 3,
seed: 7,
};
let options = VocabTreeOptions::default();
let mut tree = VocabTree::build(&flat_training, &hkm, &options).expect("tree builds");
for (image, d) in corpus.iter().enumerate() {
tree.add_image(image, d);
}
tree.finalize();
let queries: Vec<Vec<f32>> = corpus[num_images / 2].clone();
let (_, stats) = tree.query_with_work(&queries, None);
stats.entries_visited
};
let work_small = build_and_query_work(256);
let work_large = build_and_query_work(2048);
let ratio = work_large as f64 / work_small as f64;
assert!(
ratio < 10.0,
"entries_visited grew {ratio:.2}x for an 8x corpus — not near-linear"
);
let flat_ratio = ((2048f64) * 2047.0) / ((256f64) * 255.0);
assert!(flat_ratio > 60.0, "sanity: flat scan is ~64x for 8x corpus");
}
}