help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
pytorch-neuralnetworks
/
data
RSK World
pytorch-neuralnetworks
Neural networks with PyTorch
data
  • .gitkeep162 B
  • __init__.py499 B
  • augmentation.py3.5 KB
  • datasets.py3.1 KB
augmentation.py
data/augmentation.py
Raw Download
Find: Go to:
"""
Data Augmentation Utilities - PyTorch Neural Networks
Project: PyTorch Neural Networks
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Description: Data augmentation utilities for images and sequences
"""

import torch
import torchvision.transforms as transforms
import numpy as np


def get_image_augmentation(train=True):
    """
    Get image augmentation transforms
    
    Args:
        train: Whether to apply training augmentations
        
    Returns:
        Transform composition
    """
    if train:
        return transforms.Compose([
            transforms.RandomHorizontalFlip(p=0.5),
            transforms.RandomRotation(degrees=15),
            transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
            transforms.RandomAffine(degrees=0, translate=(0.1, 0.1), scale=(0.9, 1.1)),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
    else:
        return transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])


def add_noise(tensor, noise_factor=0.1):
    """
    Add Gaussian noise to tensor
    
    Args:
        tensor: Input tensor
        noise_factor: Noise intensity
        
    Returns:
        Tensor with added noise
    """
    noise = torch.randn_like(tensor) * noise_factor
    return tensor + noise


def random_crop(tensor, crop_size):
    """
    Random crop for tensor
    
    Args:
        tensor: Input tensor (C, H, W)
        crop_size: Size of crop (height, width)
        
    Returns:
        Cropped tensor
    """
    _, h, w = tensor.shape
    th, tw = crop_size
    
    if h < th or w < tw:
        return tensor
    
    i = torch.randint(0, h - th + 1, (1,)).item()
    j = torch.randint(0, w - tw + 1, (1,)).item()
    
    return tensor[:, i:i+th, j:j+tw]


def sequence_augmentation(sequence, noise_factor=0.05, dropout_prob=0.1):
    """
    Augment sequence data
    
    Args:
        sequence: Input sequence tensor (seq_len, features)
        noise_factor: Noise intensity
        dropout_prob: Probability of dropping features
        
    Returns:
        Augmented sequence
    """
    # Add noise
    if noise_factor > 0:
        noise = torch.randn_like(sequence) * noise_factor
        sequence = sequence + noise
    
    # Random feature dropout
    if dropout_prob > 0:
        mask = torch.rand(sequence.shape) > dropout_prob
        sequence = sequence * mask.float()
    
    return sequence


class MixUp:
    """
    MixUp data augmentation
    
    Project: PyTorch Neural Networks
    Author: RSK World
    Website: https://rskworld.in
    """
    
    def __init__(self, alpha=0.2):
        """
        Initialize MixUp
        
        Args:
            alpha: Beta distribution parameter
        """
        self.alpha = alpha
    
    def __call__(self, x1, y1, x2, y2):
        """
        Apply MixUp
        
        Args:
            x1, y1: First sample and label
            x2, y2: Second sample and label
            
        Returns:
            Mixed sample and labels
        """
        lam = np.random.beta(self.alpha, self.alpha)
        mixed_x = lam * x1 + (1 - lam) * x2
        y_a, y_b = y1, y2
        return mixed_x, y_a, y_b, lam

139 lines•3.5 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer