Following this instruction : 1. Strictly adhere to PEP-8 standards, ensuring code is…
- User
Following this instruction :
- Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
- Use appropriate modules like
typingfor type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively. - Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
- Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
- Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.
TASK :
we have tensor with dim (batch_size , channel, height, weight) , patches (height,weight)Gabor Filter Patch Augmentation Description: Apply Gabor filters with varying orientations and scales to each patch, augmenting the patch with filtered versions as additional channels. Purpose: Encode multi-scale and multi-orientation texture features within each patch for robust feature learning.
with eps of 5%
return tensor(b,c,H,W)
- Assistant
import math from typing import Tuple, List import torch import torch.nn.functional as F from torch import Tensor def generate_gabor_kernel( kernel_size: int, sigma: float, theta: float, lambda_: float, gamma: float, psi: float = 0, ) -> Tensor: """ Generates a 2D Gabor kernel. Args: kernel_size (int): Size of the Gabor kernel (must be odd). sigma (float): Standard deviation of the Gaussian envelope. theta (float): Orientation of the Gabor filter in radians. lambda_ (float): Wavelength of the sinusoidal factor. gamma (float): Spatial aspect ratio. psi (float, optional): Phase offset. Defaults to 0. Returns: Tensor: Gabor kernel of shape (1, 1, kernel_size, kernel_size). """ if kernel_size % 2 == 0 or kernel_size <= 0: raise ValueError("kernel_size must be a positive odd integer.") half_size = kernel_size // 2 y, x = torch.meshgrid( torch.arange(-half_size, half_size + 1), torch.arange(-half_size, half_size + 1), ) x = x.float() y = y.float() # Rotation x_theta = x * math.cos(theta) + y * math.sin(theta) y_theta = -x * math.sin(theta) + y * math.cos(theta) # Gabor kernel formula gb = torch.exp( -0.5 * (x_theta ** 2 + (gamma ** 2) * y_theta ** 2) / (sigma ** 2) ) * torch.cos(2 * math.pi * x_theta / lambda_ + psi) # Normalize the kernel gb -= gb.mean() norm = torch.sqrt(torch.sum(gb ** 2)) gb /= norm return gb.unsqueeze(0).unsqueeze(0) # Shape: (1, 1, K, K) def create_gabor_filters( kernel_size: int = 31, sigmas: List[float] = [4.0, 8.0, 16.0], thetas: List[float] = [0, math.pi / 4, math.pi / 2, 3 * math.pi / 4], lambd: float = 10.0, gamma: float = 0.5, psi: float = 0, ) -> Tensor: """ Creates a set of Gabor filters with specified parameters. Args: kernel_size (int, optional): Size of each Gabor kernel. Defaults to 31. sigmas (List[float], optional): List of sigma values for different scales. Defaults to [4.0, 8.0, 16.0]. thetas (List[float], optional): List of orientations in radians. Defaults to [0, π/4, π/2, 3π/4]. lambd (float, optional): Wavelength of the sinusoidal factor. Defaults to 10.0. gamma (float, optional): Spatial aspect ratio. Defaults to 0.5. psi (float, optional): Phase offset. Defaults to 0. Returns: Tensor: Gabor filters of shape (num_filters, 1, K, K). """ filters = [] for sigma in sigmas: for theta in thetas: kernel = generate_gabor_kernel( kernel_size=kernel_size, sigma=sigma, theta=theta, lambda_=lambd, gamma=gamma, psi=psi, ) filters.append(kernel) return torch.cat(filters, dim=0) # Shape: (num_filters, 1, K, K) def gabor_filter_patch_augmentation( input_tensor: Tensor, patch_size: Tuple[int, int], eps: float = 0.05, ) -> Tensor: """ Applies Gabor filter patch augmentation to the input tensor. Args: input_tensor (Tensor): Input tensor of shape (B, C, H, W). patch_size (Tuple[int, int]): Size of each patch as (height, width). eps (float, optional): Epsilon value for numerical stability. Defaults to 0.05. Returns: Tensor: Augmented tensor with Gabor filtered channels, shape (B, C + C*num_filters, H, W). Raises: TypeError: If input types are incorrect. ValueError: If input dimensions are incorrect. """ if not isinstance(input_tensor, Tensor): raise TypeError("input_tensor must be a torch.Tensor.") if not isinstance(patch_size, tuple) or len(patch_size) != 2: raise TypeError("patch_size must be a tuple of two integers.") if not isinstance(eps, float) and not isinstance(eps, int): raise TypeError("eps must be a float.") if input_tensor.dim() != 4: raise ValueError("input_tensor must have 4 dimensions (B, C, H, W).") batch_size, channels, height, width = input_tensor.shape patch_height, patch_width = patch_size if patch_height > height or patch_width > width: raise ValueError("patch_size must be smaller than or equal to input tensor dimensions.") # Define Gabor filter parameters kernel_size = 31 sigmas = [4.0, 8.0, 16.0] thetas = [0, math.pi / 4, math.pi / 2, 3 * math.pi / 4] lambd = 10.0 gamma = 0.5 psi = 0 # Generate Gabor filters gabor_filters = create_gabor_filters( kernel_size=kernel_size, sigmas=sigmas, thetas=thetas, lambd=lambd, gamma=gamma, psi=psi, ) # Shape: (num_filters, 1, K, K) num_filters = gabor_filters.shape[0] # Adjust filters for input channels gabor_filters = gabor_filters.repeat(channels, 1, 1, 1) # Shape: (C*num_filters, 1, K, K) # Apply padding to maintain spatial dimensions padding = kernel_size // 2 # Perform convolution augmented = F.conv2d( input_tensor, weight=gabor_filters, bias=None, stride=1, padding=padding, groups=channels, ) # Shape: (B, C*num_filters, H, W) # Normalize augmented features with epsilon to avoid division by zero augmented = augmented / (torch.norm(augmented, dim=1, keepdim=True) + eps) # Concatenate the augmented features to the original tensor output = torch.cat([input_tensor, augmented], dim=1) # Shape: (B, C + C*num_filters, H, W) return outputUsage Example
import torch # Example input tensor with batch_size=2, channels=3, height=128, width=128 input_tensor = torch.randn(2, 3, 128, 128) # Define patch size patch_size = (32, 32) # Apply Gabor filter patch augmentation augmented_tensor = gabor_filter_patch_augmentation(input_tensor, patch_size, eps=0.05) print(augmented_tensor.shape) # Expected output shape: (2, 3 + 3*num_filters, 128, 128) # Where num_filters = len(sigmas) * len(thetas) = 3 * 4 = 12 # So, shape should be (2, 39, 128, 128)Explanation
-
Gabor Kernel Generation:
- The
generate_gabor_kernelfunction creates a single Gabor kernel based on the provided parameters. - The kernel is normalized to ensure that its magnitude does not distort the input features during convolution.
- The
-
Gabor Filter Bank Creation:
- The
create_gabor_filtersfunction generates a bank of Gabor filters by varying thesigmaandthetaparameters. - This results in multiple filters capturing different scales and orientations, essential for encoding diverse texture features.
- The
-
Augmentation Function:
- The
gabor_filter_patch_augmentationfunction is the core augmentation utility. - Validation: It first validates the input types and dimensions, ensuring robustness.
- Filter Preparation: Gabor filters are generated and repeated across input channels to match the convolution requirements.
- Convolution: Applies the Gabor filters to the input tensor using depth-wise convolution (
groups=channels), maintaining efficiency and scalability. - Normalization: The augmented features are normalized with an epsilon value to ensure numerical stability.
- Concatenation: The augmented features are concatenated to the original tensor, effectively increasing the channel dimension to include the Gabor-filtered features.
- The
-
Performance and Scalability:
- Utilizes PyTorch’s optimized convolution operations to handle large datasets efficiently.
- The use of depth-wise convolution (
groups=channels) ensures that the operation scales linearly with the number of channels, maintaining performance even as the number of input channels increases.
-
Error Handling:
- Comprehensive checks are in place to validate input types and dimensions.
- Raises informative errors to aid debugging and ensure that the function behaves predictably under unexpected conditions.
-
PEP-8 Compliance:
- The code adheres to Python’s PEP-8 style guidelines, ensuring readability and maintainability.
- Proper naming conventions, indentation, and documentation are maintained throughout.
-
Type Hints and Documentation:
- Utilizes Python’s
typingmodule to provide clear type hints, enhancing code clarity. - Each function includes docstrings that explain the purpose, arguments, and return values, facilitating easier understanding and usage.
- Utilizes Python’s
This implementation ensures that the Gabor filter patch augmentation is performed efficiently, robustly, and in a manner that is easy to maintain and integrate into larger codebases.
-