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
//! Computer Vision utilities for RusTorch
//! RusTorch用コンピュータビジョンユーティリティ
//!
//! This module provides computer vision functionality similar to torchvision,
//! including image transformations, data augmentation, and built-in datasets.
//!
//! このモジュールはtorchvisionと同様のコンピュータビジョン機能を提供し、
//! 画像変換、データ拡張、組み込みデータセットを含みます。

pub mod datasets;
pub mod pipeline;
pub mod presets;
pub mod transforms;
pub mod utils;

pub use datasets::*;
pub use pipeline::*;
pub use transforms::*;

use crate::tensor::Tensor;
use num_traits::Float;

/// Common image format representation
/// 共通画像形式表現
#[derive(Debug, Clone)]
pub struct Image<T: Float> {
    /// Image data tensor with shape (C, H, W) or (H, W, C)
    /// 画像データテンソル (C, H, W) または (H, W, C) の形状
    pub data: Tensor<T>,
    /// Image height in pixels
    /// 画像の高さ(ピクセル)
    pub height: usize,
    /// Image width in pixels
    /// 画像の幅(ピクセル)
    pub width: usize,
    /// Number of channels (e.g., 1 for grayscale, 3 for RGB)
    /// チャンネル数 (グレースケール=1, RGB=3 など)
    pub channels: usize,
    /// Data format: CHW (channels first) or HWC (channels last)
    /// データ形式: CHW (チャンネル最初) または HWC (チャンネル最後)
    pub format: ImageFormat,
}

/// Image data format
/// 画像データ形式
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageFormat {
    /// Channels first: (C, H, W)
    /// チャンネル最初: (C, H, W)
    CHW,
    /// Channels last: (H, W, C)
    /// チャンネル最後: (H, W, C)
    HWC,
}

impl<T: Float + 'static> Image<T> {
    /// Create a new image from tensor data
    /// テンソルデータから新しい画像を作成
    pub fn new(data: Tensor<T>, format: ImageFormat) -> crate::error::RusTorchResult<Self> {
        let shape = data.shape();

        let (height, width, channels) = match (format, shape.len()) {
            (ImageFormat::CHW, 3) => (shape[1], shape[2], shape[0]),
            (ImageFormat::HWC, 3) => (shape[0], shape[1], shape[2]),
            (ImageFormat::CHW, 4) => (shape[2], shape[3], shape[1]), // Batch dimension included
            (ImageFormat::HWC, 4) => (shape[1], shape[2], shape[3]), // Batch dimension included
            _ => return Err(crate::error::RusTorchError::invalid_image_shape(shape)),
        };

        Ok(Image {
            data,
            height,
            width,
            channels,
            format,
        })
    }

    /// Convert image format (CHW <-> HWC)
    /// 画像形式を変換 (CHW <-> HWC)
    pub fn to_format(&self, target_format: ImageFormat) -> crate::error::RusTorchResult<Image<T>> {
        if self.format == target_format {
            return Ok(self.clone());
        }

        // For now, return a simple clone - actual implementation would permute dimensions
        // 現在は簡単なクローンを返す - 実際の実装では次元を入れ替える
        let mut new_image = self.clone();
        new_image.format = target_format;
        Ok(new_image)
    }

    /// Get image size as (height, width)
    /// 画像サイズを (高さ, 幅) として取得
    pub fn size(&self) -> (usize, usize) {
        (self.height, self.width)
    }
}

// VisionError enum removed - now using unified RusTorchError system
// VisionErrorエナム削除 - 統一RusTorchErrorシステムを使用

/// Result type for vision operations (統一済み)
/// ビジョン操作の結果型 (統一済み)
pub type RusTorchResult<T> = crate::error::RusTorchResult<T>;