Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ml-convolution

Package Overview
Dependencies
Maintainers
6
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-convolution

Convolution using the FFT or standard algorithm

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created
Source

convolution

NPM version build status npm download

Convolution using the FFT or direct algorithm.

Installation

npm install ml-convolution

Usage

One execution

import { directConvolution, fftConvolution } from 'ml-convolution';

const input = [0, 1, 2, 3];
const kernel = [-1, 1, -1];

const outputDirect = directConvolution(input, kernel); // [-1, -1, -2, 1]
const outputFFT = fftConvolution(input, kernel); // [-1, -1, -2, 1]

The functions both take an optional third argument to determine the way borders are processed. The default value, CONSTANT, will consider that the values out of the bounds are all 0. If it is set to CUT, borders will be ignored and the result will be smaller than te input by kernel.length - 1:

const outputDirect = directConvolution(input, kernel, 'CUT'); // [-1, -2]

Optimized, multiple executions

If you need to execute the convolution many times with the same kernel and input length, you should consider instead to use the class-based API:

import { DirectConvolution, FFTConvolution } from 'ml-convolution';

// const input = [0, 255, 255, 255, 255, 0, 0, 0];
const kernel = [0.1, 0.2, 0.3];

// First parameter is the size of the inputs and allows to pre-allocate an array with the correct size
const direct = new DirectConvolution(8, kernel, 'CUT');

// The convolve function mutates the same array at each execution
direct.convolve([0, 255, 255, 255, 255, 0, 0, 0]); // [ 127.5, 153, 153, 76.5, 25.5, 0 ]
direct.convolve([255, 0, 0, 255, 255, 255, 0, 0]); // [ 25.5, 76.5, 127.5, 153, 76.5, 25.5 ]

const fft = new FFTConvolution(8, kernel, 'CONSTANT');
fft.convolve([0, 255, 255, 255, 255, 0, 0, 0]); // [ 76.5, 127.5, 153, 153, 76.5, 25.5, 0, 0 ]
fft.convolve([255, 0, 0, 255, 255, 255, 0, 0]); // [ 51, 25.5, 76.5, 127.5, 153, 76.5, 25.5, 0 ]

Benchmark

With small kernels, direct convolution is usually faster:
Current results suggest that from a kernel size around 64, FFT convolution should be used.

Data x Kernelfft [ops/s]direct [ops/s]
128 x 597889569110
128 x 1199403280271
128 x 1797686181608
128 x 339463393847
128 x 659658549320
128 x 1299718925346
128 x 513217716469
512 x 520712144025
512 x 112113473189
512 x 172120144320
512 x 332103723591
512 x 652139812405
512 x 129215146358
512 x 513214941618
2048 x 5474636360
2048 x 11474018422
2048 x 17473511248
2048 x 3346895927
2048 x 6547403100
2048 x 12947411591
2048 x 5134753405
4096 x 5206818201
4096 x 1120629241
4096 x 1720715629
4096 x 3320692976
4096 x 6520791551
4096 x 1292074797
4096 x 5132079203
16384 x 53704036
16384 x 113712295
16384 x 173771390
16384 x 33374748
16384 x 65370389
16384 x 129375199
16384 x 51337651
65536 x 570991
65536 x 1170541
65536 x 1770351
65536 x 3369186
65536 x 657197
65536 x 1297150
65536 x 5137013
262144 x 510247
262144 x 1110135
262144 x 171088
262144 x 331047
262144 x 651024
262144 x 1291012
262144 x 513103
1048576 x 5260
1048576 x 11232
1048576 x 17222
1048576 x 33212
1048576 x 6526
1048576 x 12923
1048576 x 51321

License

MIT

FAQs

Package last updated on 15 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc