GithubHelp home page GithubHelp logo

fkodom / fft-conv-pytorch Goto Github PK

View Code? Open in Web Editor NEW
445.0 8.0 53.0 149 KB

Implementation of 1D, 2D, and 3D FFT convolutions in PyTorch. Much faster than direct convolutions for large kernel sizes.

License: MIT License

Python 100.00%
pytorch python3 convolution image-processing neural-networks

fft-conv-pytorch's Introduction

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:

pip install fft-conv-pytorch

From source:

git clone https://github.com/fkodom/fft-conv-pytorch.git
cd fft-conv-pytorch
pip install .

Example Usage

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.