rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! Image transformations for computer vision
//! コンピュータビジョン用画像変換
//!
//! This module provides image transformation functions similar to torchvision.transforms,
//! including basic transformations, data augmentation, and composition utilities.
//!
//! このモジュールはtorchvision.transformsと同様の画像変換機能を提供し、
//! 基本変換、データ拡張、合成ユーティリティを含みます。

use crate::error::{RusTorchError, RusTorchResult};
use crate::tensor::Tensor;
use crate::vision::{Image, ImageFormat};
use num_traits::Float;
use rand::Rng;

/// Trait for image transformations
/// 画像変換のトレイト
pub trait Transform<T: Float>: std::fmt::Debug {
    /// Apply transformation to an image
    /// 画像に変換を適用
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>>;
}

/// Resize transformation
/// リサイズ変換
#[derive(Debug, Clone)]
pub struct Resize {
    /// Target size (height, width)
    /// 目標サイズ (高さ, 幅)
    pub size: (usize, usize),
    /// Interpolation mode
    /// 補間モード
    pub interpolation: InterpolationMode,
}

/// Interpolation modes for resizing
/// リサイズ用補間モード
#[derive(Debug, Clone, Copy)]
pub enum InterpolationMode {
    /// Nearest neighbor interpolation
    /// 最近傍補間
    Nearest,
    /// Bilinear interpolation
    /// バイリニア補間
    Bilinear,
    /// Bicubic interpolation
    /// バイキュービック補間
    Bicubic,
}

impl Resize {
    /// Create new resize transformation
    /// 新しいリサイズ変換を作成
    pub fn new(size: (usize, usize)) -> Self {
        Self {
            size,
            interpolation: InterpolationMode::Bilinear,
        }
    }

    /// Set interpolation mode
    /// 補間モードを設定
    pub fn with_interpolation(mut self, mode: InterpolationMode) -> Self {
        self.interpolation = mode;
        self
    }
}

impl<T: Float + From<f32> + 'static + std::fmt::Debug> Transform<T> for Resize {
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>> {
        let (target_height, target_width) = self.size;

        // Simple implementation: create new tensor with target size
        // 簡単な実装: 目標サイズの新しいテンソルを作成
        let new_shape = match image.format {
            ImageFormat::CHW => vec![image.channels, target_height, target_width],
            ImageFormat::HWC => vec![target_height, target_width, image.channels],
        };

        // For now, create a tensor filled with zeros - actual implementation would perform interpolation
        // 現在はゼロで埋められたテンソルを作成 - 実際の実装では補間を実行
        let resized_data = Tensor::zeros(&new_shape);

        Image::new(resized_data, image.format).map_err(|e| e.into())
    }
}

/// Center crop transformation
/// 中央クロップ変換
#[derive(Debug, Clone)]
pub struct CenterCrop {
    /// Crop size (height, width)
    /// クロップサイズ (高さ, 幅)
    pub size: (usize, usize),
}

impl CenterCrop {
    /// Create new center crop transformation
    /// 新しい中央クロップ変換を作成
    pub fn new(size: (usize, usize)) -> Self {
        Self { size }
    }
}

impl<T: Float + From<f32> + 'static + std::fmt::Debug> Transform<T> for CenterCrop {
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>> {
        let (crop_height, crop_width) = self.size;

        if crop_height > image.height || crop_width > image.width {
            return Err(RusTorchError::InvalidTransformParams(format!(
                "Crop size ({}, {}) larger than image size ({}, {})",
                crop_height, crop_width, image.height, image.width
            ))
            .into());
        }

        // Calculate crop coordinates
        // クロップ座標を計算
        let _start_y = (image.height - crop_height) / 2;
        let _start_x = (image.width - crop_width) / 2;

        // Create new tensor with cropped data
        // クロップされたデータで新しいテンソルを作成
        let new_shape = match image.format {
            ImageFormat::CHW => vec![image.channels, crop_height, crop_width],
            ImageFormat::HWC => vec![crop_height, crop_width, image.channels],
        };

        let cropped_data = Tensor::zeros(&new_shape);

        Image::new(cropped_data, image.format).map_err(|e| e.into())
    }
}

/// Random crop transformation
/// ランダムクロップ変換
#[derive(Debug, Clone)]
pub struct RandomCrop {
    /// Crop size (height, width)
    /// クロップサイズ (高さ, 幅)
    pub size: (usize, usize),
    /// Padding size
    /// パディングサイズ
    pub padding: Option<(usize, usize)>,
}

impl RandomCrop {
    /// Create new random crop transformation
    /// 新しいランダムクロップ変換を作成
    pub fn new(size: (usize, usize)) -> Self {
        Self {
            size,
            padding: None,
        }
    }

    /// Set padding
    /// パディングを設定
    pub fn with_padding(mut self, padding: (usize, usize)) -> Self {
        self.padding = Some(padding);
        self
    }
}

impl<T: Float + From<f32> + 'static + std::fmt::Debug> Transform<T> for RandomCrop {
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>> {
        let (crop_height, crop_width) = self.size;
        let mut rng = rand::thread_rng();

        // Apply padding if specified
        // 指定されている場合はパディングを適用
        let working_image = if let Some((_pad_h, _pad_w)) = self.padding {
            // Simplified: return original image for now
            // 簡略化: 現在は元の画像を返す
            image.clone()
        } else {
            image.clone()
        };

        if crop_height > working_image.height || crop_width > working_image.width {
            return Err(RusTorchError::InvalidTransformParams(format!(
                "Crop size ({}, {}) larger than image size ({}, {})",
                crop_height, crop_width, working_image.height, working_image.width
            ))
            .into());
        }

        // Random crop coordinates
        // ランダムクロップ座標
        let max_y = working_image.height - crop_height;
        let max_x = working_image.width - crop_width;
        let _start_y = rng.gen_range(0..=max_y);
        let _start_x = rng.gen_range(0..=max_x);

        let new_shape = match image.format {
            ImageFormat::CHW => vec![image.channels, crop_height, crop_width],
            ImageFormat::HWC => vec![crop_height, crop_width, image.channels],
        };

        let cropped_data = Tensor::zeros(&new_shape);

        Image::new(cropped_data, image.format).map_err(|e| e.into())
    }
}

/// Random horizontal flip transformation
/// ランダム水平反転変換
#[derive(Debug, Clone)]
pub struct RandomHorizontalFlip {
    /// Probability of flipping
    /// 反転の確率
    pub probability: f64,
}

impl RandomHorizontalFlip {
    /// Create new random horizontal flip transformation
    /// 新しいランダム水平反転変換を作成
    pub fn new(probability: f64) -> Self {
        Self { probability }
    }
}

impl<T: Float + From<f32> + 'static + std::fmt::Debug> Transform<T> for RandomHorizontalFlip {
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>> {
        let mut rng = rand::thread_rng();

        if rng.gen::<f64>() < self.probability {
            // Apply horizontal flip
            // 水平反転を適用
            // For now, return cloned image - actual implementation would flip the tensor
            // 現在はクローン画像を返す - 実際の実装ではテンソルを反転
            Ok(image.clone())
        } else {
            Ok(image.clone())
        }
    }
}

/// Random rotation transformation
/// ランダム回転変換
#[derive(Debug, Clone)]
pub struct RandomRotation {
    /// Rotation angle range in degrees
    /// 回転角度範囲(度)
    pub degrees: (f64, f64),
    /// Fill value for empty pixels
    /// 空ピクセルの填込値
    pub fill: Option<f64>,
}

impl RandomRotation {
    /// Create new random rotation transformation
    /// 新しいランダム回転変換を作成
    pub fn new(degrees: (f64, f64)) -> Self {
        Self {
            degrees,
            fill: None,
        }
    }

    /// Set fill value
    /// 填込値を設定
    pub fn with_fill(mut self, fill: f64) -> Self {
        self.fill = Some(fill);
        self
    }
}

impl<T: Float + From<f32> + From<f64> + 'static + std::fmt::Debug> Transform<T> for RandomRotation {
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>> {
        let mut rng = rand::thread_rng();
        let _angle = rng.gen_range(self.degrees.0..=self.degrees.1);

        // For now, return cloned image - actual implementation would rotate the tensor
        // 現在はクローン画像を返す - 実際の実装ではテンソルを回転
        Ok(image.clone())
    }
}

/// Normalize transformation
/// 正規化変換
#[derive(Debug, Clone)]
pub struct Normalize<T: Float> {
    /// Mean values for each channel
    /// 各チャンネルの平均値
    pub mean: Vec<T>,
    /// Standard deviation values for each channel
    /// 各チャンネルの標準偏差値
    pub std: Vec<T>,
}

impl<T: Float + From<f32> + Copy> Normalize<T> {
    /// Create new normalize transformation
    /// 新しい正規化変換を作成
    pub fn new(mean: Vec<T>, std: Vec<T>) -> RusTorchResult<Self> {
        if mean.len() != std.len() {
            return Err(RusTorchError::InvalidTransformParams(
                "Mean and std must have same length".to_string(),
            )
            .into());
        }

        Ok(Self { mean, std })
    }

    /// ImageNet normalization
    /// ImageNet正規化
    pub fn imagenet() -> Self {
        Self {
            mean: vec![
                <T as From<f32>>::from(0.485),
                <T as From<f32>>::from(0.456),
                <T as From<f32>>::from(0.406),
            ],
            std: vec![
                <T as From<f32>>::from(0.229),
                <T as From<f32>>::from(0.224),
                <T as From<f32>>::from(0.225),
            ],
        }
    }
}

impl<T: Float + From<f32> + Copy + 'static + std::fmt::Debug> Transform<T> for Normalize<T> {
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>> {
        if self.mean.len() != image.channels {
            return Err(RusTorchError::InvalidTransformParams(format!(
                "Mean length {} doesn't match image channels {}",
                self.mean.len(),
                image.channels
            ))
            .into());
        }

        // For now, return cloned image - actual implementation would normalize the tensor
        // 現在はクローン画像を返す - 実際の実装ではテンソルを正規化
        Ok(image.clone())
    }
}

/// ToTensor transformation - converts PIL Image or numpy array to tensor
/// ToTensor変換 - PIL画像またはnumpy配列をテンソルに変換
#[derive(Debug, Clone)]
pub struct ToTensor {
    /// Target format for output tensor
    /// 出力テンソルの目標形式
    pub format: ImageFormat,
}

impl ToTensor {
    /// Create new ToTensor transformation
    /// 新しいToTensor変換を作成
    pub fn new() -> Self {
        Self {
            format: ImageFormat::CHW,
        }
    }

    /// Set output format
    /// 出力形式を設定
    pub fn with_format(mut self, format: ImageFormat) -> Self {
        self.format = format;
        self
    }
}

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

impl<T: Float + From<f32> + 'static + std::fmt::Debug> Transform<T> for ToTensor {
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>> {
        // Convert to target format
        // 目標形式に変換
        image.to_format(self.format).map_err(|e| e.into())
    }
}

/// Compose multiple transformations
/// 複数の変換を合成
#[derive(Debug)]
pub struct Compose<T: Float> {
    /// List of transformations to apply
    /// 適用する変換のリスト
    pub transforms: Vec<Box<dyn Transform<T>>>,
}

impl<T: Float> Compose<T> {
    /// Create new compose transformation
    /// 新しい合成変換を作成
    pub fn new(transforms: Vec<Box<dyn Transform<T>>>) -> Self {
        Self { transforms }
    }
}

impl<T: Float + 'static + std::fmt::Debug> Transform<T> for Compose<T> {
    fn apply(&self, image: &Image<T>) -> RusTorchResult<Image<T>> {
        let mut result = image.clone();

        for transform in &self.transforms {
            result = transform.apply(&result)?;
        }

        Ok(result)
    }
}

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

    #[test]
    fn test_resize_creation() {
        let resize = Resize::new((224, 224));
        assert_eq!(resize.size, (224, 224));
    }

    #[test]
    fn test_center_crop_creation() {
        let crop = CenterCrop::new((224, 224));
        assert_eq!(crop.size, (224, 224));
    }

    #[test]
    fn test_random_crop_creation() {
        let crop = RandomCrop::new((224, 224)).with_padding((4, 4));
        assert_eq!(crop.size, (224, 224));
        assert_eq!(crop.padding, Some((4, 4)));
    }

    #[test]
    fn test_normalize_creation() {
        let normalize = Normalize::new(vec![0.5f32], vec![0.5f32]).unwrap();
        assert_eq!(normalize.mean, vec![0.5f32]);
        assert_eq!(normalize.std, vec![0.5f32]);
    }

    #[test]
    fn test_normalize_imagenet() {
        let normalize: Normalize<f32> = Normalize::imagenet();
        assert_eq!(normalize.mean.len(), 3);
        assert_eq!(normalize.std.len(), 3);
    }

    #[test]
    fn test_to_tensor_creation() {
        let to_tensor = ToTensor::new();
        assert_eq!(to_tensor.format, ImageFormat::CHW);
    }
}