Sparse convolution in python using Toeplitz convolution matrix multiplication.
Project description
sparse_convolution
Sparse convolution in python.
Uses Toeplitz convolutional matrix multiplication to perform sparse convolution.
This allows for extremely fast convolution when:
- The kernel is small (<= 100x100)
- The input array is sparse (<= 1% density)
- The input array is small (<= 1000x1000)
- Many arrays are convolved with the same kernel (large batch size >= 1000)
Install:
The package is available on PyPI.
pip install sparse_convolution
Alternatively, you can install from source.
git clone https://github.com/RichieHakim/sparse_convolution
cd sparse_convolution
pip install -e .
Basic usage:
Convolve a single sparse 2D array with a 2D kernel.
import sparse_convolution as sc
import numpy as np
import scipy.sparse
# Create a single sparse matrix
A = scipy.sparse.rand(100, 100, density=0.1)
# Create a dense kernel
B = np.random.rand(3, 3)
# Prepare class
conv = sc.Toeplitz_convolution2d(
x_shape=A.shape,
k=B,
mode='same',
dtype=np.float32,
)
# Convolve
C = conv(
x=A,
batching=False,
).toarray()
Batching usage:
Convolve multiple sparse 2D arrays with a 2D kernel.
The input arrays must be reshaped into flattened vectors and stacked into a single sparse array of shape: (n_arrays, height * width)
.
import sparse_convolution as sc
import numpy as np
import scipy.sparse
# Create multiple sparse matrices
# note that the shape of A will be (3, 100**2)
A = scipy.sparse.vstack([
scipy.sparse.rand(100, 100, density=0.1).reshape(1, -1),
scipy.sparse.rand(100, 100, density=0.1).reshape(1, -1),
scipy.sparse.rand(100, 100, density=0.1).reshape(1, -1),
]).tocsr()
# Create a dense kernel
B = np.random.rand(3, 3)
# Prepare class
conv = sc.Toeplitz_convolution2d(
x_shape=(100, 100), # note that the input shape here is (100, 100)
k=B,
mode='same',
dtype=np.float32,
)
# Convolve
C = conv(
x=A,
batching=True,
)
# Reshape the output back to (3, 100, 100)
C_reshaped = np.stack([c.reshape(100, 100).toarray() for c in C], axis=0)
References
- See: https://stackoverflow.com/a/51865516 and https://github.com/alisaaalehi/convolution_as_multiplication for a nice illustration.
- See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.convolution_matrix.html for 1D version.
- See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.matmul_toeplitz.html#scipy.linalg.matmul_toeplitz for potential ways to make this implementation faster.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sparse_convolution-0.1.4.tar.gz
.
File metadata
- Download URL: sparse_convolution-0.1.4.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 437d118c8dfc4e49680abd0f356e7baa9eb56a0581fd9297984f029703491700 |
|
MD5 | 26f2b91b5134611acb6bf0a62a18490f |
|
BLAKE2b-256 | c614c541fb8e158ff28c89c4b48d97c2fe16eba3fae72b8f4374585335c109f8 |
Provenance
The following attestation bundles were made for sparse_convolution-0.1.4.tar.gz
:
Publisher:
pypi_release.yml
on RichieHakim/sparse_convolution
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
sparse_convolution-0.1.4.tar.gz
- Subject digest:
437d118c8dfc4e49680abd0f356e7baa9eb56a0581fd9297984f029703491700
- Sigstore transparency entry: 148472160
- Sigstore integration time:
- Predicate type:
File details
Details for the file sparse_convolution-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: sparse_convolution-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f637184ed2faf1b20e45ccf9a2e8bbdf51bb6b3f9dea50dbf2ef2603008037eb |
|
MD5 | c89fd4b1716c714d2084b6605332d971 |
|
BLAKE2b-256 | 200222f8de78d623b22ebd03180048dea3606cfaa9aa37db7396fa86da5361ac |
Provenance
The following attestation bundles were made for sparse_convolution-0.1.4-py3-none-any.whl
:
Publisher:
pypi_release.yml
on RichieHakim/sparse_convolution
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
sparse_convolution-0.1.4-py3-none-any.whl
- Subject digest:
f637184ed2faf1b20e45ccf9a2e8bbdf51bb6b3f9dea50dbf2ef2603008037eb
- Sigstore transparency entry: 148472162
- Sigstore integration time:
- Predicate type: