torsh-vision 0.1.2

Computer vision utilities for ToRSh deep learning 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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Self-Supervised Learning Augmentation Strategies
//!
//! This module provides augmentation pipelines specifically designed for
//! self-supervised learning methods like SimCLR, MoCo, BYOL, SwAV, etc.
//!
//! These methods learn visual representations without labels by creating
//! different augmented views of the same image and training the model to
//! recognize them as similar.

use crate::{
    ColorJitter, Compose, RandomCrop, RandomHorizontalFlip, RandomResizedCrop, Result, Transform,
};
use scirs2_core::random::{thread_rng, Random}; // SciRS2 Policy compliance
use torsh_tensor::Tensor;

// Re-export for convenience
pub(crate) type TensorF32 = Tensor<f32>;

/// SimCLR augmentation pipeline
///
/// SimCLR (Simple Framework for Contrastive Learning of Visual Representations)
/// uses strong data augmentation to create two correlated views of the same image.
///
/// Reference: Chen et al., "A Simple Framework for Contrastive Learning of
/// Visual Representations", ICML 2020
///
/// Key augmentations:
/// 1. Random cropping followed by resize back to original size
/// 2. Random color distortions
/// 3. Random Gaussian blur
/// 4. Random horizontal flip
pub struct SimCLRAugmentation {
    crop_size: usize,
    color_strength: f32,
    blur_probability: f32,
    transform: Box<dyn Transform>,
}

impl SimCLRAugmentation {
    /// Create SimCLR augmentation pipeline
    ///
    /// # Arguments
    /// * `crop_size` - Target size for cropped images
    /// * `color_strength` - Strength of color jitter (0.0 to 1.0)
    /// * `blur_probability` - Probability of applying Gaussian blur
    pub fn new(crop_size: usize, color_strength: f32, blur_probability: f32) -> Self {
        let transform = Box::new(Compose::new(vec![
            // Random cropping with resize
            Box::new(RandomResizedCrop::new((crop_size, crop_size))),
            // Random horizontal flip
            Box::new(RandomHorizontalFlip::new(0.5)),
            // Strong color jitter
            Box::new(
                ColorJitter::new()
                    .brightness(0.8 * color_strength)
                    .contrast(0.8 * color_strength)
                    .saturation(0.8 * color_strength)
                    .hue(0.2 * color_strength),
            ),
            // Random grayscale with 20% probability
            Box::new(RandomGrayscale::new(0.2)),
            // Gaussian blur (applied with specified probability)
            Box::new(GaussianBlur::new(
                (crop_size / 10).max(3) | 1,
                blur_probability,
            )),
        ]));

        Self {
            crop_size,
            color_strength,
            blur_probability,
            transform,
        }
    }

    /// Generate two augmented views of the same image
    ///
    /// # Arguments
    /// * `image` - Input image tensor
    ///
    /// # Returns
    /// Tuple of (view1, view2) - two differently augmented versions
    pub fn generate_pair(&self, image: &Tensor<f32>) -> Result<(Tensor<f32>, Tensor<f32>)> {
        let view1 = self.transform.forward(image)?;
        let view2 = self.transform.forward(image)?;
        Ok((view1, view2))
    }

    /// Generate multiple augmented views
    pub fn generate_views(
        &self,
        image: &Tensor<f32>,
        num_views: usize,
    ) -> Result<Vec<Tensor<f32>>> {
        let mut views = Vec::with_capacity(num_views);
        for _ in 0..num_views {
            views.push(self.transform.forward(image)?);
        }
        Ok(views)
    }
}

/// MoCo (Momentum Contrast) augmentation
///
/// MoCo uses similar augmentations to SimCLR but typically with slightly
/// weaker augmentations for the query and stronger for the key.
///
/// Reference: He et al., "Momentum Contrast for Unsupervised Visual
/// Representation Learning", CVPR 2020
pub struct MoCoAugmentation {
    query_transform: Box<dyn Transform>,
    key_transform: Box<dyn Transform>,
}

impl MoCoAugmentation {
    /// Create MoCo augmentation with separate query and key transforms
    pub fn new(crop_size: usize) -> Self {
        // Query: weaker augmentation
        let query_transform = Box::new(Compose::new(vec![
            Box::new(RandomResizedCrop::new((crop_size, crop_size))),
            Box::new(RandomHorizontalFlip::new(0.5)),
            Box::new(
                ColorJitter::new()
                    .brightness(0.4)
                    .contrast(0.4)
                    .saturation(0.4)
                    .hue(0.1),
            ),
        ]));

        // Key: similar augmentation (in MoCo v2)
        let key_transform = Box::new(Compose::new(vec![
            Box::new(RandomResizedCrop::new((crop_size, crop_size))),
            Box::new(RandomHorizontalFlip::new(0.5)),
            Box::new(
                ColorJitter::new()
                    .brightness(0.4)
                    .contrast(0.4)
                    .saturation(0.4)
                    .hue(0.1),
            ),
            Box::new(RandomGrayscale::new(0.2)),
        ]));

        Self {
            query_transform,
            key_transform,
        }
    }

    /// Generate query and key views
    pub fn generate_pair(&self, image: &Tensor<f32>) -> Result<(Tensor<f32>, Tensor<f32>)> {
        let query = self.query_transform.forward(image)?;
        let key = self.key_transform.forward(image)?;
        Ok((query, key))
    }
}

/// BYOL (Bootstrap Your Own Latent) augmentation
///
/// BYOL uses asymmetric augmentation strategies where one view is augmented
/// more strongly than the other.
///
/// Reference: Grill et al., "Bootstrap Your Own Latent: A New Approach to
/// Self-Supervised Learning", NeurIPS 2020
pub struct BYOLAugmentation {
    online_transform: Box<dyn Transform>,
    target_transform: Box<dyn Transform>,
}

impl BYOLAugmentation {
    /// Create BYOL augmentation with asymmetric transforms
    pub fn new(crop_size: usize) -> Self {
        // Online network: stronger augmentation
        let online_transform = Box::new(Compose::new(vec![
            Box::new(RandomResizedCrop::new((crop_size, crop_size))),
            Box::new(RandomHorizontalFlip::new(0.5)),
            Box::new(
                ColorJitter::new()
                    .brightness(0.4)
                    .contrast(0.4)
                    .saturation(0.2)
                    .hue(0.1),
            ),
            Box::new(RandomGrayscale::new(0.2)),
            Box::new(GaussianBlur::new((crop_size / 10).max(3) | 1, 1.0)), // Always blur
            Box::new(Solarize::new(0.0, 0.0)), // Simplified solarization
        ]));

        // Target network: different augmentation
        let target_transform = Box::new(Compose::new(vec![
            Box::new(RandomResizedCrop::new((crop_size, crop_size))),
            Box::new(RandomHorizontalFlip::new(0.5)),
            Box::new(
                ColorJitter::new()
                    .brightness(0.4)
                    .contrast(0.4)
                    .saturation(0.2)
                    .hue(0.1),
            ),
            Box::new(RandomGrayscale::new(0.2)),
            Box::new(GaussianBlur::new((crop_size / 10).max(3) | 1, 0.1)), // Rarely blur
        ]));

        Self {
            online_transform,
            target_transform,
        }
    }

    /// Generate online and target views
    pub fn generate_pair(&self, image: &Tensor<f32>) -> Result<(Tensor<f32>, Tensor<f32>)> {
        let online_view = self.online_transform.forward(image)?;
        let target_view = self.target_transform.forward(image)?;
        Ok((online_view, target_view))
    }
}

/// SwAV (Swapping Assignments between Views) augmentation
///
/// SwAV uses multi-crop augmentation strategy with both global and local views.
///
/// Reference: Caron et al., "Unsupervised Learning of Visual Features by
/// Contrasting Cluster Assignments", NeurIPS 2020
#[derive(Debug)]
pub struct SwAVAugmentation {
    global_crop_size: usize,
    local_crop_size: usize,
    num_global_crops: usize,
    num_local_crops: usize,
}

impl SwAVAugmentation {
    /// Create SwAV multi-crop augmentation
    ///
    /// # Arguments
    /// * `global_crop_size` - Size for global crops (e.g., 224)
    /// * `local_crop_size` - Size for local crops (e.g., 96)
    /// * `num_global_crops` - Number of global views (typically 2)
    /// * `num_local_crops` - Number of local views (typically 6)
    pub fn new(
        global_crop_size: usize,
        local_crop_size: usize,
        num_global_crops: usize,
        num_local_crops: usize,
    ) -> Self {
        Self {
            global_crop_size,
            local_crop_size,
            num_global_crops,
            num_local_crops,
        }
    }

    /// Generate multi-crop views (global + local)
    pub fn generate_views(&self, image: &Tensor<f32>) -> Result<Vec<Tensor<f32>>> {
        let mut views = Vec::new();

        // Global crops (cover >50% of image)
        let global_transform = self.create_global_transform();
        for _ in 0..self.num_global_crops {
            views.push(global_transform.forward(image)?);
        }

        // Local crops (cover <50% of image)
        let local_transform = self.create_local_transform();
        for _ in 0..self.num_local_crops {
            views.push(local_transform.forward(image)?);
        }

        Ok(views)
    }

    fn create_global_transform(&self) -> Box<dyn Transform> {
        Box::new(Compose::new(vec![
            Box::new(RandomResizedCrop::new((
                self.global_crop_size,
                self.global_crop_size,
            ))),
            Box::new(RandomHorizontalFlip::new(0.5)),
            Box::new(
                ColorJitter::new()
                    .brightness(0.4)
                    .contrast(0.4)
                    .saturation(0.2)
                    .hue(0.1),
            ),
            Box::new(RandomGrayscale::new(0.2)),
            Box::new(GaussianBlur::new(
                (self.global_crop_size / 10).max(3) | 1,
                0.5,
            )),
        ]))
    }

    fn create_local_transform(&self) -> Box<dyn Transform> {
        Box::new(Compose::new(vec![
            Box::new(RandomResizedCrop::new((
                self.local_crop_size,
                self.local_crop_size,
            ))),
            Box::new(RandomHorizontalFlip::new(0.5)),
            Box::new(
                ColorJitter::new()
                    .brightness(0.4)
                    .contrast(0.4)
                    .saturation(0.2)
                    .hue(0.1),
            ),
            Box::new(RandomGrayscale::new(0.2)),
            Box::new(GaussianBlur::new(
                (self.local_crop_size / 10).max(3) | 1,
                0.5,
            )),
        ]))
    }
}

/// DINO (Self-Distillation with No Labels) augmentation
///
/// DINO combines multi-crop strategy with strong augmentations for student
/// and weaker augmentations for teacher.
///
/// Reference: Caron et al., "Emerging Properties in Self-Supervised Vision
/// Transformers", ICCV 2021
#[derive(Debug)]
pub struct DINOAugmentation {
    global_crop_size: usize,
    local_crop_size: usize,
}

impl DINOAugmentation {
    /// Create DINO augmentation strategy
    pub fn new(global_crop_size: usize, local_crop_size: usize) -> Self {
        Self {
            global_crop_size,
            local_crop_size,
        }
    }

    /// Generate views for DINO (2 global + multiple local crops)
    pub fn generate_views(
        &self,
        image: &Tensor<f32>,
        num_local_crops: usize,
    ) -> Result<Vec<Tensor<f32>>> {
        let mut views = Vec::new();

        // 2 global crops (teacher and student)
        let global_transform = self.create_global_transform();
        views.push(global_transform.forward(image)?);
        views.push(global_transform.forward(image)?);

        // Multiple local crops (only for student)
        let local_transform = self.create_local_transform();
        for _ in 0..num_local_crops {
            views.push(local_transform.forward(image)?);
        }

        Ok(views)
    }

    fn create_global_transform(&self) -> Box<dyn Transform> {
        Box::new(Compose::new(vec![
            Box::new(RandomResizedCrop::new((
                self.global_crop_size,
                self.global_crop_size,
            ))),
            Box::new(RandomHorizontalFlip::new(0.5)),
            Box::new(
                ColorJitter::new()
                    .brightness(0.4)
                    .contrast(0.4)
                    .saturation(0.2)
                    .hue(0.1),
            ),
            Box::new(RandomGrayscale::new(0.2)),
            Box::new(GaussianBlur::new(
                (self.global_crop_size / 10).max(3) | 1,
                1.0,
            )),
            Box::new(Solarize::new(0.0, 0.2)), // Solarize with 20% probability
        ]))
    }

    fn create_local_transform(&self) -> Box<dyn Transform> {
        Box::new(Compose::new(vec![
            Box::new(RandomResizedCrop::new((
                self.local_crop_size,
                self.local_crop_size,
            ))),
            Box::new(RandomHorizontalFlip::new(0.5)),
            Box::new(
                ColorJitter::new()
                    .brightness(0.4)
                    .contrast(0.4)
                    .saturation(0.2)
                    .hue(0.1),
            ),
            Box::new(RandomGrayscale::new(0.2)),
            Box::new(GaussianBlur::new(
                (self.local_crop_size / 10).max(3) | 1,
                0.5,
            )),
        ]))
    }
}

/// Helper transforms for self-supervised learning
impl RandomGrayscale {
    /// Apply random grayscale conversion
    pub fn new(probability: f32) -> Self {
        Self { probability }
    }
}

/// Random grayscale transform
#[derive(Debug)]
pub struct RandomGrayscale {
    probability: f32,
}

impl Transform for RandomGrayscale {
    fn forward(&self, input: &Tensor<f32>) -> Result<Tensor<f32>> {
        let mut rng = thread_rng();
        if rng.random::<f32>() < self.probability {
            // Convert to grayscale and replicate to 3 channels
            let gray = crate::rgb_to_grayscale(input)?;
            // Replicate across channels by concatenating
            Ok(Tensor::cat(&[&gray, &gray, &gray], 0)?)
        } else {
            Ok(input.clone())
        }
    }

    fn clone_transform(&self) -> Box<dyn Transform> {
        Box::new(Self {
            probability: self.probability,
        })
    }
}

/// Gaussian blur transform
#[derive(Debug)]
pub struct GaussianBlur {
    kernel_size: usize,
    probability: f32,
}

impl GaussianBlur {
    pub fn new(kernel_size: usize, probability: f32) -> Self {
        Self {
            kernel_size,
            probability,
        }
    }
}

impl Transform for GaussianBlur {
    fn forward(&self, input: &Tensor<f32>) -> Result<Tensor<f32>> {
        let mut rng = thread_rng();
        if rng.random::<f32>() < self.probability {
            // Use appropriate sigma value
            let sigma = (self.kernel_size as f32) * 0.3;
            crate::gaussian_blur(input, sigma)
        } else {
            Ok(input.clone())
        }
    }

    fn clone_transform(&self) -> Box<dyn Transform> {
        Box::new(Self {
            kernel_size: self.kernel_size,
            probability: self.probability,
        })
    }
}

/// Solarization transform
#[derive(Debug)]
pub struct Solarize {
    threshold: f32,
    probability: f32,
}

impl Solarize {
    pub fn new(threshold: f32, probability: f32) -> Self {
        Self {
            threshold,
            probability,
        }
    }
}

impl Transform for Solarize {
    fn forward(&self, input: &Tensor<f32>) -> Result<Tensor<f32>> {
        let mut rng = thread_rng();
        if rng.random::<f32>() < self.probability {
            // Simplified solarization: invert pixel values
            let inverted = input.mul_scalar(-1.0)?.add_scalar(1.0)?;
            Ok(inverted)
        } else {
            Ok(input.clone())
        }
    }

    fn clone_transform(&self) -> Box<dyn Transform> {
        Box::new(Self {
            threshold: self.threshold,
            probability: self.probability,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use torsh_tensor::creation::{self, randn};

    #[test]
    fn test_simclr_augmentation() {
        let aug = SimCLRAugmentation::new(224, 1.0, 0.5);
        assert_eq!(aug.crop_size, 224);
        assert_eq!(aug.color_strength, 1.0);
        assert_eq!(aug.blur_probability, 0.5);
    }

    #[test]
    fn test_simclr_generate_pair() {
        let aug = SimCLRAugmentation::new(224, 1.0, 0.5);
        let image = randn::<f32>(&[3, 256, 256]).unwrap();

        let result = aug.generate_pair(&image);
        assert!(result.is_ok());

        let (view1, view2) = result.unwrap();
        // Verify we got valid augmented views
        assert!(view1.numel() > 0);
        assert!(view2.numel() > 0);
    }

    #[test]
    fn test_moco_augmentation() {
        let aug = MoCoAugmentation::new(224);
        let image = randn::<f32>(&[3, 256, 256]).unwrap();

        let result = aug.generate_pair(&image);
        assert!(result.is_ok());
    }

    #[test]
    fn test_byol_augmentation() {
        let aug = BYOLAugmentation::new(224);
        let image = randn::<f32>(&[3, 256, 256]).unwrap();

        let result = aug.generate_pair(&image);
        assert!(result.is_ok());
    }

    #[test]
    fn test_swav_augmentation() {
        let aug = SwAVAugmentation::new(224, 96, 2, 6);
        let image = randn::<f32>(&[3, 256, 256]).unwrap();

        let result = aug.generate_views(&image);
        assert!(result.is_ok());

        let views = result.unwrap();
        assert_eq!(views.len(), 8); // 2 global + 6 local
    }

    #[test]
    fn test_dino_augmentation() {
        let aug = DINOAugmentation::new(224, 96);
        let image = randn::<f32>(&[3, 256, 256]).unwrap();

        let result = aug.generate_views(&image, 4);
        assert!(result.is_ok());

        let views = result.unwrap();
        assert_eq!(views.len(), 6); // 2 global + 4 local
    }

    #[test]
    fn test_random_grayscale() {
        let transform = RandomGrayscale::new(1.0); // Always apply
        let image = randn::<f32>(&[3, 64, 64]).unwrap();

        let result = transform.forward(&image);
        assert!(result.is_ok());
    }

    #[test]
    fn test_gaussian_blur() {
        let transform = GaussianBlur::new(5, 1.0);
        let image = randn::<f32>(&[3, 64, 64]).unwrap();

        let result = transform.forward(&image);
        assert!(result.is_ok());
    }

    #[test]
    fn test_solarize() {
        let transform = Solarize::new(0.5, 1.0);
        let image = randn::<f32>(&[3, 64, 64]).unwrap();

        let result = transform.forward(&image);
        assert!(result.is_ok());
    }
}