PyTorchWavelets

repository·master·Indexed 43 days ago

https://github.com/tomrunia/pytorchwavelets

A high-performance implementation of Continuous Wavelet Transforms (CWT) in PyTorch. It leverages GPU-accelerated convolutions via torch.nn.Conv1d to speed up wavelet analysis for large batches of 1D signals. The library includes the WaveletTransformTorch class for GPU parallelization and a SciPy-based WaveletTransform implementation.

Tokens
904
Snippets
3
Records
4
Agent score
6%

What's inside PyTorchWavelets

  1. Compare WaveletTransform (SciPy) and WaveletTransformTorch (PyTorch)

    master

    The library provides two implementations for completeness:

    1. WaveletTransform: The original SciPy-based version.
    2. WaveletTransformTorch: The PyTorch-based version optimized for GPU parallelization.

    While WaveletTransform is suitable for individual signals, WaveletTransformTorch is significantly faster for large numbers of 1D signals when processed as a batch.

    from wavelets_pytorch.transform import WaveletTransform        # SciPy version
    from wavelets_pytorch.transform import WaveletTransformTorch   # PyTorch version
    
    dt = 0.1
    dj = 0.125
    batch = np.random.randn(32, 1000)
    
    # SciPy version
    wa_scipy = WaveletTransform(dt, dj)
    cwt_scipy = wa_scipy.cwt(batch)
    
    # PyTorch version
    wa_torch = WaveletTransformTorch(dt, dj, cuda=True)
    cwt_torch = wa_torch.cwt(batch)
  2. Install PyTorchWavelets

    master

    To install PyTorchWavelets, clone the repository, install the required dependencies, and run the setup script.

    git clone https://github.com/tomrunia/PyTorchWavelets.git
    cd PyTorchWavelets
    pip install -r requirements.txt
    python setup.py install
  3. Use WaveletTransformTorch for fast GPU-accelerated CWT

    master

    The WaveletTransformTorch class provides a PyTorch-based implementation of the Continuous Wavelet Transform (CWT) using torch.nn.Conv1d. This implementation is optimized for processing large batches of signals in parallel on a GPU.

    Key Requirements for Usage:

    • Batching: Unlike the SciPy version, the cwt and power methods in the PyTorch implementation expect signal batches of shape [num_signals, signal_length] rather than individual signals. Increasing the batch size improves performance on the GPU.
    • Initialization: Initialize with sampling frequency dt and scale distribution parameter dj.
    • Device: Use the cuda=True argument to enable GPU acceleration.
    import numpy as np
    from wavelets_pytorch.transform import WaveletTransformTorch
    
    dt = 0.1         # sampling frequency
    dj = 0.125       # scale distribution parameter
    batch_size = 32  # how many signals to process in parallel
    signal_length = 1000
    
    # Batch of signals to process: shape [num_signals, signal_length]
    batch = np.random.randn(batch_size, signal_length)
    
    # Initialize wavelet filter banks
    wa_torch = WaveletTransformTorch(dt, dj, cuda=True)
    
    # Performing wavelet transform
    cwt_torch = wa_torch.cwt(batch)