axonml-vision 0.4.2

Computer vision utilities for the Axonml ML framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Phantom Backbone — BlazeBlock-based Lightweight Feature Extractor
//!
//! # File
//! `crates/axonml-vision/src/models/phantom/backbone.rs`
//!
//! # Author
//! Andrew Jewell Sr - AutomataNexus
//!
//! # Updated
//! March 8, 2026
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

use axonml_autograd::Variable;
use axonml_nn::{BatchNorm2d, Conv2d, Module, Parameter, ReLU};

// =============================================================================
// BlazeBlock (Depthwise Separable with Residual)
// =============================================================================

/// Depthwise separable convolution with residual connection.
/// Based on BlazeFace architecture, optimized for mobile face detection.
struct PhantomBlazeBlock {
    dw_conv: Conv2d,
    dw_bn: BatchNorm2d,
    pw_conv: Conv2d,
    pw_bn: BatchNorm2d,
    project: Option<(Conv2d, BatchNorm2d)>,
    relu: ReLU,
    _stride: usize,
}

impl PhantomBlazeBlock {
    fn new(in_ch: usize, out_ch: usize, stride: usize) -> Self {
        let dw_conv =
            Conv2d::with_groups(in_ch, in_ch, (3, 3), (stride, stride), (1, 1), true, in_ch);
        let dw_bn = BatchNorm2d::new(in_ch);
        let pw_conv = Conv2d::with_options(in_ch, out_ch, (1, 1), (1, 1), (0, 0), true);
        let pw_bn = BatchNorm2d::new(out_ch);

        let project = if in_ch != out_ch || stride != 1 {
            Some((
                Conv2d::with_options(in_ch, out_ch, (1, 1), (stride, stride), (0, 0), true),
                BatchNorm2d::new(out_ch),
            ))
        } else {
            None
        };

        Self {
            dw_conv,
            dw_bn,
            pw_conv,
            pw_bn,
            project,
            relu: ReLU,
            _stride: stride,
        }
    }

    fn forward(&self, x: &Variable) -> Variable {
        let identity = if let Some((ref proj_conv, ref proj_bn)) = self.project {
            proj_bn.forward(&proj_conv.forward(x))
        } else {
            x.clone()
        };

        let out = self
            .relu
            .forward(&self.dw_bn.forward(&self.dw_conv.forward(x)));
        let out = self.pw_bn.forward(&self.pw_conv.forward(&out));
        self.relu.forward(&out.add_var(&identity))
    }

    fn parameters(&self) -> Vec<Parameter> {
        let mut params = Vec::new();
        params.extend(self.dw_conv.parameters());
        params.extend(self.dw_bn.parameters());
        params.extend(self.pw_conv.parameters());
        params.extend(self.pw_bn.parameters());
        if let Some((ref c, ref bn)) = self.project {
            params.extend(c.parameters());
            params.extend(bn.parameters());
        }
        params
    }

    fn train(&mut self) {
        self.dw_bn.train();
        self.pw_bn.train();
        if let Some((_, ref mut bn)) = self.project {
            bn.train();
        }
    }

    fn eval(&mut self) {
        self.dw_bn.eval();
        self.pw_bn.eval();
        if let Some((_, ref mut bn)) = self.project {
            bn.eval();
        }
    }
}

// =============================================================================
// Event Feature Extractor
// =============================================================================

/// Lightweight feature extractor for event-active patches.
///
/// Takes a 48×48 patch (RGB + motion = 4 channels) and produces
/// a compact [32]-dimensional feature vector.
pub struct EventFeatureExtractor {
    conv1: Conv2d,
    bn1: BatchNorm2d,
    conv2: Conv2d,
    bn2: BatchNorm2d,
    relu: ReLU,
}

impl Default for EventFeatureExtractor {
    fn default() -> Self {
        Self::new()
    }
}

impl EventFeatureExtractor {
    /// Create event feature extractor: 4ch input → 32-dim output.
    pub fn new() -> Self {
        // Depthwise separable blocks for efficiency
        let conv1 = Conv2d::with_options(4, 16, (3, 3), (2, 2), (1, 1), true);
        let bn1 = BatchNorm2d::new(16);
        let conv2 = Conv2d::with_options(16, 32, (3, 3), (2, 2), (1, 1), true);
        let bn2 = BatchNorm2d::new(32);

        Self {
            conv1,
            bn1,
            conv2,
            bn2,
            relu: ReLU,
        }
    }

    /// Forward: [B, 4, 48, 48] → [B, 32].
    pub fn forward(&self, x: &Variable) -> Variable {
        let out = self.relu.forward(&self.bn1.forward(&self.conv1.forward(x)));
        let out = self
            .relu
            .forward(&self.bn2.forward(&self.conv2.forward(&out)));

        // Global average pooling → [B, 32]
        let shape = out.shape();
        let (b, c, h, w) = (shape[0], shape[1], shape[2], shape[3]);

        // Global average pooling: [B, C, H, W] -> [B, C, H*W] -> mean(dim=2) -> [B, C]
        out.reshape(&[b, c, h * w]).mean_dim(2, false)
    }

    /// Get parameters.
    pub fn parameters(&self) -> Vec<Parameter> {
        let mut p = Vec::new();
        p.extend(self.conv1.parameters());
        p.extend(self.bn1.parameters());
        p.extend(self.conv2.parameters());
        p.extend(self.bn2.parameters());
        p
    }

    /// Set to eval mode.
    pub fn eval(&mut self) {
        self.bn1.eval();
        self.bn2.eval();
    }

    /// Set to train mode.
    pub fn train(&mut self) {
        self.bn1.train();
        self.bn2.train();
    }
}

// =============================================================================
// Phantom Backbone
// =============================================================================

/// Lightweight BlazeBlock-based backbone for Phantom face detection.
///
/// Architecture:
/// - Stem: Conv(3→16, 3×3, stride=2) + BN + ReLU → [B, 16, H/2, W/2]
/// - Stage 1: 2× BlazeBlock(16→24, s=1 then 24→24, s=1) → P1
/// - Stage 2: BlazeBlock(24→32, s=2) + BlazeBlock(32→32, s=1) → P2
/// - Stage 3: BlazeBlock(32→48, s=2) + BlazeBlock(48→48, s=1) → P3
///
/// Returns multi-scale features [P1, P2, P3] for anchor-free detection.
pub struct PhantomBackbone {
    stem_conv: Conv2d,
    stem_bn: BatchNorm2d,
    stage1: Vec<PhantomBlazeBlock>,
    stage2: Vec<PhantomBlazeBlock>,
    stage3: Vec<PhantomBlazeBlock>,
    relu: ReLU,
    /// Cached features from last full backbone run.
    cached_features: Option<Vec<Variable>>,
    /// Frame counter for periodic full backbone refresh.
    frame_count: u32,
    /// How often to run full backbone (in frames).
    pub refresh_interval: u32,
}

impl PhantomBackbone {
    /// Create a new Phantom backbone.
    pub fn new() -> Self {
        let stem_conv = Conv2d::with_options(3, 16, (3, 3), (2, 2), (1, 1), true);
        let stem_bn = BatchNorm2d::new(16);

        let stage1 = vec![
            PhantomBlazeBlock::new(16, 24, 1),
            PhantomBlazeBlock::new(24, 24, 1),
        ];

        let stage2 = vec![
            PhantomBlazeBlock::new(24, 32, 2),
            PhantomBlazeBlock::new(32, 32, 1),
        ];

        let stage3 = vec![
            PhantomBlazeBlock::new(32, 48, 2),
            PhantomBlazeBlock::new(48, 48, 1),
        ];

        Self {
            stem_conv,
            stem_bn,
            stage1,
            stage2,
            stage3,
            relu: ReLU,
            cached_features: None,
            frame_count: 0,
            refresh_interval: 30,
        }
    }

    /// Run full backbone forward pass and cache features.
    ///
    /// Input: [B, 3, H, W]
    /// Returns: [P1=[B,24,H/2,W/2], P2=[B,32,H/4,W/4], P3=[B,48,H/8,W/8]]
    pub fn forward_full(&mut self, x: &Variable) -> Vec<Variable> {
        let mut out = self
            .relu
            .forward(&self.stem_bn.forward(&self.stem_conv.forward(x)));

        for block in &self.stage1 {
            out = block.forward(&out);
        }
        let p1 = out.clone();

        for block in &self.stage2 {
            out = block.forward(&out);
        }
        let p2 = out.clone();

        for block in &self.stage3 {
            out = block.forward(&out);
        }
        let p3 = out;

        let features = vec![p1, p2, p3];
        self.cached_features = Some(features.clone());
        self.frame_count = 0;
        features
    }

    /// Get cached features, or run full backbone if cache is stale.
    ///
    /// Returns (features, was_full_run).
    pub fn get_features(&mut self, frame: &Variable) -> (Vec<Variable>, bool) {
        self.frame_count += 1;

        if self.cached_features.is_none() || self.frame_count >= self.refresh_interval {
            let features = self.forward_full(frame);
            (features, true)
        } else {
            (self.cached_features.clone().unwrap(), false)
        }
    }

    /// Force a cache refresh on next call.
    pub fn invalidate_cache(&mut self) {
        self.cached_features = None;
    }

    /// Check if cached features are available.
    pub fn has_cache(&self) -> bool {
        self.cached_features.is_some()
    }

    /// Get parameters for optimization.
    pub fn parameters(&self) -> Vec<Parameter> {
        let mut params = Vec::new();
        params.extend(self.stem_conv.parameters());
        params.extend(self.stem_bn.parameters());
        for block in &self.stage1 {
            params.extend(block.parameters());
        }
        for block in &self.stage2 {
            params.extend(block.parameters());
        }
        for block in &self.stage3 {
            params.extend(block.parameters());
        }
        params
    }

    /// Set to eval mode.
    pub fn eval(&mut self) {
        self.stem_bn.eval();
        for block in &mut self.stage1 {
            block.eval();
        }
        for block in &mut self.stage2 {
            block.eval();
        }
        for block in &mut self.stage3 {
            block.eval();
        }
    }

    /// Set to train mode.
    pub fn train(&mut self) {
        self.stem_bn.train();
        for block in &mut self.stage1 {
            block.train();
        }
        for block in &mut self.stage2 {
            block.train();
        }
        for block in &mut self.stage3 {
            block.train();
        }
    }
}

impl Default for PhantomBackbone {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use axonml_tensor::Tensor;

    #[test]
    fn test_blaze_block_same_channels() {
        let block = PhantomBlazeBlock::new(24, 24, 1);
        let x = Variable::new(
            Tensor::from_vec(vec![0.1; 24 * 16 * 16], &[1, 24, 16, 16]).unwrap(),
            false,
        );
        let out = block.forward(&x);
        assert_eq!(out.shape(), vec![1, 24, 16, 16]);
    }

    #[test]
    fn test_blaze_block_downsample() {
        let block = PhantomBlazeBlock::new(24, 32, 2);
        let x = Variable::new(
            Tensor::from_vec(vec![0.1; 24 * 16 * 16], &[1, 24, 16, 16]).unwrap(),
            false,
        );
        let out = block.forward(&x);
        assert_eq!(out.shape(), vec![1, 32, 8, 8]);
    }

    #[test]
    fn test_event_feature_extractor() {
        let efe = EventFeatureExtractor::new();
        let x = Variable::new(
            Tensor::from_vec(vec![0.1; 4 * 48 * 48], &[1, 4, 48, 48]).unwrap(),
            false,
        );
        let out = efe.forward(&x);
        assert_eq!(out.shape(), vec![1, 32]);
    }

    #[test]
    fn test_phantom_backbone_forward() {
        let mut backbone = PhantomBackbone::new();
        let x = Variable::new(
            Tensor::from_vec(vec![0.1; 3 * 128 * 128], &[1, 3, 128, 128]).unwrap(),
            false,
        );
        let features = backbone.forward_full(&x);

        assert_eq!(features.len(), 3);
        assert_eq!(features[0].shape(), vec![1, 24, 64, 64]); // P1
        assert_eq!(features[1].shape(), vec![1, 32, 32, 32]); // P2
        assert_eq!(features[2].shape(), vec![1, 48, 16, 16]); // P3
    }

    #[test]
    fn test_phantom_backbone_caching() {
        let mut backbone = PhantomBackbone::new();
        backbone.refresh_interval = 5;

        let x = Variable::new(
            Tensor::from_vec(vec![0.1; 3 * 64 * 64], &[1, 3, 64, 64]).unwrap(),
            false,
        );

        // First call: full run
        let (_, was_full) = backbone.get_features(&x);
        assert!(was_full);

        // Second call: cached
        let (_, was_full) = backbone.get_features(&x);
        assert!(!was_full);

        assert!(backbone.has_cache());
    }

    #[test]
    fn test_phantom_backbone_param_count() {
        let backbone = PhantomBackbone::new();
        let total: usize = backbone.parameters().iter().map(|p| p.numel()).sum();
        // Should be lightweight (~20-30K params for backbone alone)
        assert!(total < 100_000, "Backbone too large: {total} params");
        assert!(total > 5_000, "Backbone too small: {total} params");
    }
}