torsh-data 0.1.2

Data loading and preprocessing utilities for ToRSh
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
use crate::transforms::Transform;
#[cfg(feature = "image-support")]
use image::{DynamicImage, GenericImageView, ImageBuffer};
use scirs2_core::RngExt;
use torsh_core::error::{Result, TorshError};
use torsh_tensor::Tensor;

/// Transform to convert image to tensor
pub struct ImageToTensor;

impl Transform<DynamicImage> for ImageToTensor {
    type Output = Tensor<f32>;

    fn transform(&self, input: DynamicImage) -> Result<Self::Output> {
        #[cfg(feature = "image-support")]
        {
            let rgb_image = input.to_rgb8();
            let (width, height) = rgb_image.dimensions();

            // Convert to CHW format (channels, height, width)
            let mut data = Vec::with_capacity((width * height * 3) as usize);

            // Extract channels separately
            for channel in 0..3 {
                for y in 0..height {
                    for x in 0..width {
                        let pixel = rgb_image.get_pixel(x, y);
                        let value = pixel[channel] as f32 / 255.0;
                        data.push(value);
                    }
                }
            }

            Tensor::from_data(
                data,
                vec![3, height as usize, width as usize],
                torsh_core::device::DeviceType::Cpu,
            )
        }

        #[cfg(not(feature = "image-support"))]
        {
            Err(TorshError::UnsupportedOperation {
                op: "image to tensor conversion".to_string(),
                dtype: "DynamicImage".to_string(),
            })
        }
    }
}

/// Transform to convert tensor to image
pub struct TensorToImage;

impl Transform<Tensor<f32>> for TensorToImage {
    type Output = DynamicImage;

    fn transform(&self, input: Tensor<f32>) -> Result<Self::Output> {
        #[cfg(feature = "image-support")]
        {
            let shape = input.shape();
            if shape.ndim() != 3 {
                return Err(TorshError::InvalidShape(
                    "Expected 3D tensor (C, H, W)".to_string(),
                ));
            }

            let dims = shape.dims();
            let (channels, height, width) = (dims[0], dims[1], dims[2]);

            if channels != 3 {
                return Err(TorshError::InvalidShape(
                    "Expected 3 channels for RGB image".to_string(),
                ));
            }

            let data = input.to_vec()?;
            let mut img_data = Vec::with_capacity(width * height * 3);

            // Convert from CHW to HWC format
            for y in 0..height {
                for x in 0..width {
                    for c in 0..3 {
                        let idx = c * height * width + y * width + x;
                        let value = (data[idx] * 255.0).clamp(0.0, 255.0) as u8;
                        img_data.push(value);
                    }
                }
            }

            let img_buffer = ImageBuffer::from_raw(width as u32, height as u32, img_data)
                .ok_or_else(|| {
                    TorshError::InvalidArgument("Failed to create image buffer".to_string())
                })?;

            Ok(DynamicImage::ImageRgb8(img_buffer))
        }

        #[cfg(not(feature = "image-support"))]
        {
            Err(TorshError::UnsupportedOperation {
                op: "tensor to image conversion".to_string(),
                dtype: "Tensor<f32>".to_string(),
            })
        }
    }
}

/// Compose multiple transforms
pub struct Compose<T> {
    transforms: Vec<Box<dyn Transform<T, Output = T>>>,
}

impl<T: 'static> Compose<T> {
    pub fn new() -> Self {
        Self {
            transforms: Vec::new(),
        }
    }

    pub fn add_transform<F>(mut self, transform: F) -> Self
    where
        F: Transform<T, Output = T> + 'static,
    {
        self.transforms.push(Box::new(transform));
        self
    }

    pub fn add_boxed(mut self, transform: Box<dyn Transform<T, Output = T>>) -> Self {
        self.transforms.push(transform);
        self
    }
}

impl<T> Transform<T> for Compose<T> {
    type Output = T;

    fn transform(&self, mut input: T) -> Result<Self::Output> {
        for transform in &self.transforms {
            input = transform.transform(input)?;
        }
        Ok(input)
    }
}

impl<T: 'static> Default for Compose<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Random horizontal flip
pub struct RandomHorizontalFlip {
    prob: f32,
}

impl RandomHorizontalFlip {
    pub fn new(prob: f32) -> Self {
        Self { prob }
    }
}

impl Transform<DynamicImage> for RandomHorizontalFlip {
    type Output = DynamicImage;

    fn transform(&self, input: DynamicImage) -> Result<Self::Output> {
        #[cfg(feature = "image-support")]
        {
            // ✅ SciRS2 Policy Compliant - Using scirs2_core::random instead of direct rand
            #[allow(unused_imports)] // Rng trait needed for random() method
            use scirs2_core::random::{Random, Rng};
            let mut rng = Random::seed(0);
            if rng.random::<f32>() < self.prob {
                Ok(input.fliph())
            } else {
                Ok(input)
            }
        }

        #[cfg(not(feature = "image-support"))]
        {
            Err(TorshError::UnsupportedOperation {
                op: "random horizontal flip".to_string(),
                dtype: "DynamicImage".to_string(),
            })
        }
    }
}

/// Random vertical flip
pub struct RandomVerticalFlip {
    prob: f32,
}

impl RandomVerticalFlip {
    pub fn new(prob: f32) -> Self {
        Self { prob }
    }
}

impl Transform<DynamicImage> for RandomVerticalFlip {
    type Output = DynamicImage;

    fn transform(&self, input: DynamicImage) -> Result<Self::Output> {
        #[cfg(feature = "image-support")]
        {
            // ✅ SciRS2 Policy Compliant - Using scirs2_core::random instead of direct rand
            #[allow(unused_imports)] // Rng trait needed for random() method
            use scirs2_core::random::{Random, Rng};
            let mut rng = Random::seed(0);
            if rng.random::<f32>() < self.prob {
                Ok(input.flipv())
            } else {
                Ok(input)
            }
        }

        #[cfg(not(feature = "image-support"))]
        {
            Err(TorshError::UnsupportedOperation {
                op: "random vertical flip".to_string(),
                dtype: "DynamicImage".to_string(),
            })
        }
    }
}

/// Random rotation
pub struct RandomRotation {
    degrees: f32,
}

impl RandomRotation {
    pub fn new(degrees: f32) -> Self {
        Self { degrees }
    }
}

impl Transform<DynamicImage> for RandomRotation {
    type Output = DynamicImage;

    fn transform(&self, input: DynamicImage) -> Result<Self::Output> {
        #[cfg(all(feature = "image-support", feature = "imageproc"))]
        {
            // ✅ SciRS2 Policy Compliant - Using scirs2_core::random instead of direct rand
            #[allow(unused_imports)] // Rng trait needed for random() method
            use scirs2_core::random::{Random, Rng};
            let mut rng = Random::seed(0);
            let angle_deg = rng.gen_range(-self.degrees..=self.degrees);
            let angle_rad = angle_deg.to_radians();

            // Convert to RGB8 for processing
            let rgb_image = input.to_rgb8();

            // Perform rotation using imageproc
            // Use imageproc's rotation function
            let rotated = imageproc::geometric_transformations::rotate_about_center(
                &rgb_image,
                angle_rad,
                imageproc::geometric_transformations::Interpolation::Bilinear,
                image::Rgb([0u8, 0u8, 0u8]), // Black background
            );

            Ok(DynamicImage::ImageRgb8(rotated))
        }

        #[cfg(all(feature = "image-support", not(feature = "imageproc")))]
        {
            // ✅ SciRS2 Policy Compliant - Using scirs2_core::random instead of direct rand
            #[allow(unused_imports)] // Rng trait needed for random() method
            use scirs2_core::random::{Random, Rng};
            let mut rng = Random::seed(0);
            let _angle = rng.gen_range(-self.degrees..=self.degrees);
            // imageproc not available, return input unchanged
            // In production, you might want to log a warning here
            Ok(input)
        }

        #[cfg(not(feature = "image-support"))]
        {
            Err(TorshError::UnsupportedOperation {
                op: "random rotation".to_string(),
                dtype: "DynamicImage".to_string(),
            })
        }
    }
}

/// Common vision transforms
pub mod transforms {
    use super::*;
    use crate::transforms::Transform;

    /// Resize image
    pub struct Resize {
        size: (u32, u32),
    }

    impl Resize {
        pub fn new(size: (u32, u32)) -> Self {
            Self { size }
        }
    }

    impl Transform<DynamicImage> for Resize {
        type Output = DynamicImage;

        fn transform(&self, input: DynamicImage) -> Result<Self::Output> {
            #[cfg(feature = "image-support")]
            {
                Ok(input.resize_exact(
                    self.size.0,
                    self.size.1,
                    image::imageops::FilterType::Lanczos3,
                ))
            }

            #[cfg(not(feature = "image-support"))]
            {
                Err(TorshError::UnsupportedOperation {
                    op: "image resize".to_string(),
                    dtype: "DynamicImage".to_string(),
                })
            }
        }
    }

    /// Center crop image
    pub struct CenterCrop {
        size: (u32, u32),
    }

    impl CenterCrop {
        pub fn new(size: (u32, u32)) -> Self {
            Self { size }
        }
    }

    impl Transform<DynamicImage> for CenterCrop {
        type Output = DynamicImage;

        fn transform(&self, input: DynamicImage) -> Result<Self::Output> {
            #[cfg(feature = "image-support")]
            {
                let (width, height) = input.dimensions();
                let (crop_width, crop_height) = self.size;

                if crop_width > width || crop_height > height {
                    return Err(TorshError::InvalidArgument(
                        "Crop size cannot be larger than image size".to_string(),
                    ));
                }

                let x = (width - crop_width) / 2;
                let y = (height - crop_height) / 2;

                Ok(input.crop_imm(x, y, crop_width, crop_height))
            }

            #[cfg(not(feature = "image-support"))]
            {
                Err(TorshError::UnsupportedOperation {
                    op: "image center crop".to_string(),
                    dtype: "DynamicImage".to_string(),
                })
            }
        }
    }

    /// Normalize image values
    pub struct Normalize {
        mean: [f32; 3],
        std: [f32; 3],
    }

    impl Normalize {
        pub fn new(mean: [f32; 3], std: [f32; 3]) -> Self {
            Self { mean, std }
        }

        /// ImageNet normalization
        pub fn imagenet() -> Self {
            Self::new([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        }
    }

    impl Transform<Tensor<f32>> for Normalize {
        type Output = Tensor<f32>;

        fn transform(&self, input: Tensor<f32>) -> Result<Self::Output> {
            // Apply ImageNet-style normalization per channel
            // Assumes input tensor is in CHW format (channels, height, width)
            let shape_ref = input.shape();
            let shape = shape_ref.dims();

            if shape.len() != 3 {
                return Err(TorshError::InvalidShape(format!(
                    "Expected 3D tensor (C, H, W), got shape {shape:?}"
                )));
            }

            let channels = shape[0];
            if channels != 3 {
                return Err(TorshError::InvalidShape(format!(
                    "Expected 3 channels for RGB image, got {channels}"
                )));
            }

            // Get tensor data and apply normalization
            let mut data = input.to_vec()?;
            let height = shape[1];
            let width = shape[2];
            let channel_size = height * width;

            // Apply per-channel normalization: (pixel - mean) / std
            for c in 0..3 {
                let channel_start = c * channel_size;
                let channel_end = channel_start + channel_size;
                let mean = self.mean[c];
                let std = self.std[c];

                for pixel in &mut data[channel_start..channel_end] {
                    *pixel = (*pixel - mean) / std;
                }
            }

            // Create normalized tensor with same shape and device
            Tensor::from_data(data, shape.to_vec(), input.device())
        }
    }
}