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 : The task involves processing a list of audio file paths, aiming to enhance speed and accuracy using multiprocessing in PyTorch. The solution should give users complete control over various preprocessing steps:
a) Load the audio files with user-specified parameters such as sample rate, channels, and duration. Apply normalization, all configurable by the user.
b) Leverage available APIs from torch and any compatible audio augmentation libraries (e.g., torchaudio or librosa). The user should have the flexibility to select and control all augmentation options offered by these libraries, allowing easy integration of predefined or custom transformations.
Finally, the processed audio files should be returned as batch-wise tensors, with the batch size determined by the user. The multiprocessing approach will ensure efficient parallelization, speeding up the audio preprocessing pipeline while preserving accuracy by utilizing high-quality augmentations and normalization techniques.
- Assistant
import os from typing import List, Optional, Callable, Union import torch from torch import Tensor from torch.utils.data import Dataset, DataLoader import torchaudio from torchaudio.transforms import Resample from torchaudio.functional import amplitude_to_DB # Additional imports for augmentations from torchvision.transforms import Compose class AudioDataset(Dataset): """ A PyTorch Dataset for loading and preprocessing audio files with configurable parameters. """ def __init__( self, file_paths: List[str], sample_rate: int = 16000, num_channels: int = 1, duration: Optional[float] = None, normalize: bool = True, transforms: Optional[Callable[[Tensor], Tensor]] = None, ) -> None: """ Initialize the dataset with file paths and preprocessing parameters. Args: file_paths (List[str]): List of paths to audio files. sample_rate (int): Desired sample rate for the audio files. num_channels (int): Desired number of audio channels. duration (Optional[float]): Desired duration (in seconds) of audio clips. normalize (bool): Whether to normalize the audio waveform. transforms (Optional[Callable[[Tensor], Tensor]]): A sequence of transformations to apply. Raises: FileNotFoundError: If any audio file in file_paths does not exist. """ self.file_paths = file_paths self.sample_rate = sample_rate self.num_channels = num_channels self.duration = duration self.normalize = normalize self.transforms = transforms # Verify that all files exist for path in self.file_paths: if not os.path.isfile(path): raise FileNotFoundError(f"Audio file not found: {path}") def __len__(self) -> int: return len(self.file_paths) def __getitem__(self, idx: int) -> Tensor: file_path = self.file_paths[idx] try: waveform, sr = torchaudio.load(file_path) # Resample if needed if sr != self.sample_rate: waveform = self._resample(waveform, sr) # Adjust number of channels waveform = self._mix_channels(waveform) # Adjust duration if self.duration is not None: waveform = self._fix_duration(waveform) # Normalize if self.normalize: waveform = self._normalize(waveform) # Apply user-specified transforms if self.transforms: waveform = self.transforms(waveform) return waveform except Exception as e: error_msg = f"Error processing '{file_path}': {e}" print(error_msg) raise RuntimeError(error_msg) from e def _resample(self, waveform: Tensor, orig_sr: int) -> Tensor: resampler = Resample(orig_freq=orig_sr, new_freq=self.sample_rate) return resampler(waveform) def _mix_channels(self, waveform: Tensor) -> Tensor: if waveform.size(0) == self.num_channels: return waveform elif self.num_channels == 1: # Mix down to mono return waveform.mean(dim=0, keepdim=True) else: # Duplicate channels return waveform.expand(self.num_channels, -1) def _fix_duration(self, waveform: Tensor) -> Tensor: num_expected_samples = int(self.sample_rate * self.duration) num_actual_samples = waveform.size(1) if num_actual_samples > num_expected_samples: # Truncate to desired duration waveform = waveform[:, :num_expected_samples] else: # Pad with zeros to desired duration padding = num_expected_samples - num_actual_samples waveform = torch.nn.functional.pad(waveform, (0, padding)) return waveform def _normalize(self, waveform: Tensor) -> Tensor: mean = waveform.mean() std = waveform.std() if std > 0: waveform = (waveform - mean) / std else: waveform = waveform - mean return waveform def create_dataloader( file_paths: List[str], batch_size: int = 16, num_workers: int = 4, sample_rate: int = 16000, num_channels: int = 1, duration: Optional[float] = None, normalize: bool = True, transforms: Optional[Callable[[Tensor], Tensor]] = None, shuffle: bool = False, pin_memory: bool = True, ) -> DataLoader: """ Create a DataLoader for batch-wise loading of audio data. Args: file_paths (List[str]): List of paths to audio files. batch_size (int): Number of samples per batch. num_workers (int): Number of subprocesses to use for data loading. sample_rate (int): Desired sample rate for the audio files. num_channels (int): Desired number of audio channels. duration (Optional[float]): Desired duration (in seconds) of audio clips. normalize (bool): Whether to normalize the audio waveform. transforms (Optional[Callable[[Tensor], Tensor]]): A sequence of transformations to apply. shuffle (bool): Whether to shuffle the data at every epoch. pin_memory (bool): Whether to copy Tensors into CUDA pinned memory. Returns: DataLoader: A PyTorch DataLoader instance. """ dataset = AudioDataset( file_paths=file_paths, sample_rate=sample_rate, num_channels=num_channels, duration=duration, normalize=normalize, transforms=transforms, ) dataloader = DataLoader( dataset=dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers, pin_memory=pin_memory, ) return dataloader # Example usage if __name__ == "__main__": # User-specified parameters file_paths = ["audio1.wav", "audio2.wav", "audio3.wav"] # Replace with actual file paths sample_rate = 22050 num_channels = 1 duration = 5.0 # seconds normalize = True batch_size = 8 num_workers = 2 # Define custom transforms transforms = Compose([ torchaudio.transforms.MelSpectrogram(), lambda x: amplitude_to_DB(x, multiplier=10, amin=1e-10, db_multiplier=0), ]) try: dataloader = create_dataloader( file_paths=file_paths, batch_size=batch_size, num_workers=num_workers, sample_rate=sample_rate, num_channels=num_channels, duration=duration, normalize=normalize, transforms=transforms, shuffle=True, pin_memory=True, ) for batch in dataloader: # batch is a tensor of shape (batch_size, num_channels, num_samples) print(batch.shape) except Exception as e: print(f"An error occurred during data loading: {e}")Explanation
The provided code defines an
AudioDatasetclass and acreate_dataloaderfunction to process audio files efficiently using multiprocessing and PyTorch'sDataLoader. Here's how the code addresses the task requirements:User-Specified Parameters and Preprocessing Control
- Sample Rate, Channels, Duration, Normalization: Users can specify
sample_rate,num_channels,duration, andnormalizewhen creating the dataset or dataloader. - Transforms and Augmentations: Users can pass any transformations or augmentations via the
transformsparameter. This can be a single transform or a composition of multiple transforms from libraries liketorchaudioorlibrosa.
Leveraging PyTorch and Compatible Libraries
- Audio Loading and Resampling: Uses
torchaudio.loadto read audio files andtorchaudio.transforms.Resamplefor resampling to the desired sample rate. - Augmentations: The code accepts any callable transform, allowing users to integrate predefined or custom transformations from supported libraries.
Batch-wise Tensors and Multiprocessing
- DataLoader: Returns data as batch-wise tensors with user-defined
batch_size. - Multiprocessing: Employs multiple worker subprocesses via the
num_workersparameter inDataLoaderfor efficient parallel data loading.
Comprehensive Error Handling
- File Verification: Checks if all provided audio file paths exist before processing.
- Exception Handling: Catches and reports errors during audio processing, providing meaningful messages to help users diagnose issues.
Code Structure and Maintainability
- PEP-8 Compliance: The code follows PEP-8 standards for readability and consistency.
- Type Hints: Uses Python's
typingmodule for type hints, enhancing code clarity and aiding in static analysis. - Modularity: Breaks down processing steps into helper methods (
_resample,_mix_channels,_fix_duration,_normalize) within theAudioDatasetclass for better organization and maintainability.
Usage Instructions
-
Replace File Paths: Update the
file_pathslist with the actual paths to your audio files. -
Customize Parameters: Modify parameters such as
sample_rate,num_channels,duration,normalize,batch_size, andnum_workersas needed. -
Define Transformations: Create a sequence of transformations using
torchaudioor other compatible libraries and pass them to thetransformsparameter.Example:
transforms = Compose([ torchaudio.transforms.MelSpectrogram(sample_rate=sample_rate), torchaudio.transforms.AmplitudeToDB(), ]) -
Run the Script: Execute the script to process the audio files. The processed batches will be printed or can be integrated into your training pipeline.
Notes on Scalability and Performance
-
Efficient Data Loading: By setting
num_workersgreater than zero, theDataLoaderloads data in parallel subprocesses, significantly speeding up data preparation, especially with a large number of files. -
Memory Management: Setting
pin_memory=Truecan improve performance on GPU-equipped systems by enabling faster data transfer to CUDA-enabled GPUs. -
Batch Processing: Handling data in batches reduces the overhead per sample and is more efficient for large datasets.
Error Handling
-
Graceful Degradation: If an error occurs while processing a specific file, the error is reported with the file path and exception details, and the exception is re-raised for the calling code to handle appropriately.
-
File Existence Check: Immediately checks for file existence during initialization, preventing unnecessary computations later on.
Advanced Customization
-
Custom Augmentations: Users can integrate custom augmentation functions as long as they accept and return a
torch.Tensor. -
Dynamic Transformations: Transforms can be randomized or conditional, offering flexibility for data augmentation strategies in training machine learning models.
Conclusion
This code provides a robust, scalable solution for processing and batching audio data with extensive user configurability and efficient parallelization, adhering to best practices in Python coding and leveraging the capabilities of PyTorch and its ecosystem.
- Sample Rate, Channels, Duration, Normalization: Users can specify