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
// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use crate::{Result, VisionError};
// use half; // Commented out - half crate not available
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use torsh_core::device::{CpuDevice, Device, DeviceType};
use torsh_core::dtype::DType;
use torsh_tensor::Tensor;

/// Unified transform trait that supports both CPU and GPU operations
pub trait UnifiedTransform: Send + Sync + fmt::Debug {
    /// Apply the transform to an input tensor using the best available device
    fn apply(&self, input: &Tensor<f32>) -> Result<Tensor<f32>>;

    /// Apply the transform using GPU acceleration if available
    fn apply_gpu(&self, input: &Tensor<f32>) -> Result<Tensor<f32>> {
        // Default fallback to CPU implementation
        self.apply(input)
    }

    /// Apply the transform using mixed precision (f16) on GPU
    fn apply_gpu_f16(&self, input: &Tensor<f32>) -> Result<Tensor<f32>> {
        // Default implementation: convert to f32, apply, convert back
        let input_f32 = input.to_dtype(DType::F32)?;
        let output_f32 = self.apply_gpu(&input_f32)?;
        Ok(output_f32.to_dtype(DType::F16)?)
    }

    /// Get the canonical name of this transform
    fn name(&self) -> &'static str;

    /// Get transform parameters for introspection and serialization
    fn parameters(&self) -> HashMap<String, TransformParameter>;

    /// Check if this transform supports GPU acceleration
    fn supports_gpu(&self) -> bool {
        false
    }

    /// Check if this transform supports mixed precision
    fn supports_mixed_precision(&self) -> bool {
        false
    }

    /// Check if this transform modifies the input in-place (for optimization)
    fn is_inplace(&self) -> bool {
        false
    }

    /// Get the output shape given an input shape
    fn output_shape(&self, input_shape: &[usize]) -> Result<Vec<usize>> {
        // Default: output shape same as input shape
        Ok(input_shape.to_vec())
    }

    /// Clone the transform
    fn clone_transform(&self) -> Box<dyn UnifiedTransform>;

    /// Get device affinity for this transform
    fn preferred_device(&self) -> Option<&dyn Device> {
        None
    }
}

/// Parameter type for transform introspection
#[derive(Debug, Clone)]
pub enum TransformParameter {
    Float(f32),
    Int(i32),
    Usize(usize),
    Bool(bool),
    String(String),
    FloatVec(Vec<f32>),
    IntVec(Vec<i32>),
    UsizeVec(Vec<usize>),
    Tuple2Usize((usize, usize)),
    Tuple2Float((f32, f32)),
}

impl fmt::Display for TransformParameter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TransformParameter::Float(v) => write!(f, "{:.3}", v),
            TransformParameter::Int(v) => write!(f, "{}", v),
            TransformParameter::Usize(v) => write!(f, "{}", v),
            TransformParameter::Bool(v) => write!(f, "{}", v),
            TransformParameter::String(v) => write!(f, "{}", v),
            TransformParameter::FloatVec(v) => write!(f, "{:?}", v),
            TransformParameter::IntVec(v) => write!(f, "{:?}", v),
            TransformParameter::UsizeVec(v) => write!(f, "{:?}", v),
            TransformParameter::Tuple2Usize((a, b)) => write!(f, "({}, {})", a, b),
            TransformParameter::Tuple2Float((a, b)) => write!(f, "({:.3}, {:.3})", a, b),
        }
    }
}

/// Transform execution context for hardware-aware execution
#[derive(Clone)]
pub struct TransformContext {
    pub device: Arc<dyn Device>,
    pub use_mixed_precision: bool,
    pub batch_size: Option<usize>,
    pub enable_optimizations: bool,
}

impl std::fmt::Debug for TransformContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TransformContext")
            .field("device", &"Arc<dyn Device>")
            .field("use_mixed_precision", &self.use_mixed_precision)
            .field("batch_size", &self.batch_size)
            .field("enable_optimizations", &self.enable_optimizations)
            .finish()
    }
}

impl Default for TransformContext {
    fn default() -> Self {
        Self {
            device: Arc::new(CpuDevice::new()),
            use_mixed_precision: false,
            batch_size: None,
            enable_optimizations: true,
        }
    }
}

impl TransformContext {
    pub fn new(device: Arc<dyn Device>) -> Self {
        Self {
            device,
            ..Default::default()
        }
    }

    pub fn with_mixed_precision(mut self, enabled: bool) -> Self {
        self.use_mixed_precision = enabled;
        self
    }

    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = Some(batch_size);
        self
    }

    pub fn auto_detect() -> Result<Self> {
        // For now, default to CPU device since CUDA detection is not fully implemented
        let _device_type = DeviceType::Cpu;

        let device = Arc::new(CpuDevice::new()) as Arc<dyn Device>;
        let use_mixed_precision = false; // Set to false for CPU devices

        Ok(Self {
            device,
            use_mixed_precision,
            batch_size: None,
            enable_optimizations: true,
        })
    }
}

/// Unified transform chain for composing multiple transforms
#[derive(Debug)]
pub struct UnifiedCompose {
    transforms: Vec<Box<dyn UnifiedTransform>>,
    context: TransformContext,
}

impl UnifiedCompose {
    pub fn new(transforms: Vec<Box<dyn UnifiedTransform>>) -> Self {
        Self {
            transforms,
            context: TransformContext::default(),
        }
    }

    pub fn with_context(mut self, context: TransformContext) -> Self {
        self.context = context;
        self
    }

    pub fn add_transform(&mut self, transform: Box<dyn UnifiedTransform>) {
        self.transforms.push(transform);
    }

    pub fn len(&self) -> usize {
        self.transforms.len()
    }

    pub fn is_empty(&self) -> bool {
        self.transforms.is_empty()
    }
}

impl UnifiedTransform for UnifiedCompose {
    fn apply(&self, input: &Tensor<f32>) -> Result<Tensor<f32>> {
        let mut output = input.clone();
        for transform in &self.transforms {
            output = if self.context.use_mixed_precision && transform.supports_mixed_precision() {
                let output_f16 = output.to_dtype(DType::F16)?;
                let result_f16 = transform.apply_gpu_f16(&output_f16)?;
                result_f16.to_dtype(DType::F32)?
            } else if matches!(self.context.device.device_type(), DeviceType::Cuda(_))
                && transform.supports_gpu()
            {
                transform.apply_gpu(&output)?
            } else {
                transform.apply(&output)?
            };
        }
        Ok(output)
    }

    fn apply_gpu(&self, input: &Tensor<f32>) -> Result<Tensor<f32>> {
        let mut output = input.clone();
        for transform in &self.transforms {
            output = transform.apply_gpu(&output)?;
        }
        Ok(output)
    }

    fn apply_gpu_f16(&self, input: &Tensor<f32>) -> Result<Tensor<f32>> {
        let mut output = input.clone();
        for transform in &self.transforms {
            output = transform.apply_gpu_f16(&output)?;
        }
        Ok(output)
    }

    fn name(&self) -> &'static str {
        "UnifiedCompose"
    }

    fn parameters(&self) -> HashMap<String, TransformParameter> {
        let mut params = HashMap::new();
        params.insert(
            "num_transforms".to_string(),
            TransformParameter::Usize(self.transforms.len()),
        );

        // Add individual transform info
        for (i, transform) in self.transforms.iter().enumerate() {
            params.insert(
                format!("transform_{}", i),
                TransformParameter::String(transform.name().to_string()),
            );
        }

        params
    }

    fn supports_gpu(&self) -> bool {
        self.transforms.iter().any(|t| t.supports_gpu())
    }

    fn supports_mixed_precision(&self) -> bool {
        self.transforms.iter().any(|t| t.supports_mixed_precision())
    }

    fn output_shape(&self, input_shape: &[usize]) -> Result<Vec<usize>> {
        let mut shape = input_shape.to_vec();
        for transform in &self.transforms {
            shape = transform.output_shape(&shape)?;
        }
        Ok(shape)
    }

    fn clone_transform(&self) -> Box<dyn UnifiedTransform> {
        let cloned_transforms = self
            .transforms
            .iter()
            .map(|t| t.clone_transform())
            .collect();
        Box::new(UnifiedCompose::new(cloned_transforms).with_context(self.context.clone()))
    }
}

/// Transform builder for fluent API construction
pub struct TransformBuilder {
    transforms: Vec<Box<dyn UnifiedTransform>>,
    context: TransformContext,
}

impl TransformBuilder {
    pub fn new() -> Self {
        Self {
            transforms: Vec::new(),
            context: TransformContext::default(),
        }
    }

    pub fn with_context(mut self, context: TransformContext) -> Self {
        self.context = context;
        self
    }

    pub fn add<T: UnifiedTransform + 'static>(mut self, transform: T) -> Self {
        self.transforms.push(Box::new(transform));
        self
    }

    pub fn resize(self, size: (usize, usize)) -> Self {
        self.add(crate::transforms::unified::UnifiedResize::new(size))
    }

    pub fn center_crop(self, size: (usize, usize)) -> Self {
        self.add(crate::transforms::unified::UnifiedCenterCrop::new(size))
    }

    pub fn random_horizontal_flip(self, p: f32) -> Self {
        self.add(crate::transforms::unified::UnifiedRandomHorizontalFlip::new(p))
    }

    pub fn normalize(self, mean: Vec<f32>, std: Vec<f32>) -> Self {
        self.add(crate::transforms::unified::UnifiedNormalize::new(mean, std))
    }

    pub fn build(self) -> UnifiedCompose {
        UnifiedCompose::new(self.transforms).with_context(self.context)
    }
}

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

/// Macro for easy transform composition
#[macro_export]
macro_rules! compose_transforms {
    ($($transform:expr),+ $(,)?) => {
        $crate::unified_transforms::UnifiedCompose::new(vec![
            $(Box::new($transform),)+
        ])
    };
}

/// Convenience functions for common transform combinations
pub mod presets {
    use super::*;

    /// Standard ImageNet preprocessing
    pub fn imagenet_preprocessing(size: (usize, usize)) -> UnifiedCompose {
        TransformBuilder::new()
            .resize(size)
            .center_crop(size)
            .normalize(vec![0.485, 0.456, 0.406], vec![0.229, 0.224, 0.225])
            .build()
    }

    /// Data augmentation for training
    pub fn training_augmentation(size: (usize, usize)) -> UnifiedCompose {
        TransformBuilder::new()
            .resize((size.0 + 32, size.1 + 32))
            .random_horizontal_flip(0.5)
            .center_crop(size)
            .normalize(vec![0.485, 0.456, 0.406], vec![0.229, 0.224, 0.225])
            .build()
    }

    /// GPU-optimized transforms with mixed precision
    pub fn gpu_optimized_transforms(size: (usize, usize)) -> Result<UnifiedCompose> {
        let context = TransformContext::auto_detect()?;
        Ok(TransformBuilder::new()
            .with_context(context)
            .resize(size)
            .normalize(vec![0.485, 0.456, 0.406], vec![0.229, 0.224, 0.225])
            .build())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use torsh_tensor::creation::zeros;

    #[test]
    fn test_transform_builder() {
        let transform = TransformBuilder::new()
            .resize((224, 224))
            .center_crop((224, 224))
            .normalize(vec![0.5], vec![0.5])
            .build();

        assert_eq!(transform.len(), 3);
        assert_eq!(transform.name(), "UnifiedCompose");
    }

    #[test]
    fn test_compose_macro() {
        let resize = crate::transforms::unified::UnifiedResize::new((224, 224));
        let normalize = crate::transforms::unified::UnifiedNormalize::new(vec![0.5], vec![0.5]);

        let _transform = compose_transforms![resize, normalize];
    }

    #[test]
    fn test_transform_parameters() {
        let param = TransformParameter::Tuple2Usize((224, 224));
        assert_eq!(param.to_string(), "(224, 224)");

        let param = TransformParameter::FloatVec(vec![0.485, 0.456, 0.406]);
        assert!(param.to_string().contains("0.485"));
    }

    #[test]
    fn test_context_auto_detect() {
        let context = TransformContext::auto_detect();
        assert!(context.is_ok());
    }
}