Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fkodom/fft-conv-pytorch
Implementation of 1D, 2D, and 3D FFT convolutions in PyTorch. Much faster than direct convolutions for large kernel sizes.
https://github.com/fkodom/fft-conv-pytorch
convolution image-processing neural-networks python3 pytorch
Last synced: 14 days ago
JSON representation
Implementation of 1D, 2D, and 3D FFT convolutions in PyTorch. Much faster than direct convolutions for large kernel sizes.
- Host: GitHub
- URL: https://github.com/fkodom/fft-conv-pytorch
- Owner: fkodom
- License: mit
- Created: 2019-07-30T14:05:34.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-09-28T16:05:33.000Z (about 1 year ago)
- Last Synced: 2024-10-22T19:44:23.497Z (19 days ago)
- Topics: convolution, image-processing, neural-networks, python3, pytorch
- Language: Python
- Homepage:
- Size: 146 KB
- Stars: 472
- Watchers: 8
- Forks: 58
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
# fft-conv-pytorch
Implementation of 1D, 2D, and 3D FFT convolutions in PyTorch.
* Faster than direct convolution for large kernels.
* **Much slower** than direct convolution for small kernels.
* In my local tests, FFT convolution is faster when the kernel has >100 or so elements.
* Dependent on machine and PyTorch version.
* Also see benchmarks below.## Install
Using `pip`:
```bash
pip install fft-conv-pytorch
```From source:
```bash
git clone https://github.com/fkodom/fft-conv-pytorch.git
cd fft-conv-pytorch
pip install .
```## Example Usage
```python
import torch
from fft_conv_pytorch import fft_conv, FFTConv1d# Create dummy data.
# Data shape: (batch, channels, length)
# Kernel shape: (out_channels, in_channels, kernel_size)
# Bias shape: (out channels, )
# For ordinary 1D convolution, simply set batch=1.
signal = torch.randn(3, 3, 1024 * 1024)
kernel = torch.randn(2, 3, 128)
bias = torch.randn(2)# Functional execution. (Easiest for generic use cases.)
out = fft_conv(signal, kernel, bias=bias)# Object-oriented execution. (Requires some extra work, since the
# defined classes were designed for use in neural networks.)
fft_conv = FFTConv1d(3, 2, 128, bias=True)
fft_conv.weight = torch.nn.Parameter(kernel)
fft_conv.bias = torch.nn.Parameter(bias)
out = fft_conv(signal)
```## Benchmarks
Benchmarking FFT convolution against the direct convolution from PyTorch in 1D, 2D,
and 3D. The exact times are heavily dependent on your local machine, but relative
scaling with kernel size is always the same.Dimensions | Input Size | Input Channels | Output Channels | Bias | Padding | Stride | Dilation
-----------|--------------|----------------|-----------------|------|---------|--------|---------
1 | (4096) | 4 | 4 | True | 0 | 1 | 1
2 | (512, 512) | 4 | 4 | True | 0 | 1 | 1
3 | (64, 64, 64) | 4 | 4 | True | 0 | 1 | 1![Benchmark Plot](doc/benchmark.png)