Skip to main content

A Python implementation of the K-SVD algorithm

Project description

PyKSVD

PyPI version License: MIT

PyKSVD is a Python implementation of the K-SVD algorithm based on paper K-SVD: An Algorithm for Designing Overcomplete Dictionaries for Sparse Representation (2006) by Michal Aharon, Michael Elad, and Alfred Bruckstein.

For the evaluation (Time Series 2024) :

Notebook are present in folder notebook

  • notebooks/ksvd_theory.ipynbis used to understand theory behind K-SVD
  • notebooks/ksvd_synthetic.ipynbis used to reproduce synthetic results with noising
  • notebooks/ksvd_images.ipynbis used to reproduce image denoising and pixel update (black and white only, run example in example_corrupted_image.py for colored reconstructions)

Examples

  • Signal Processing : Fit a dictionay based on input signals and then transform new signals with high level sparsity. Dictionary Learning

  • Image processing : Denoise or complete a corrupted image with missing pixels with patches. Image Reconstruction with 70% missing pixels

Installation

pip install pyksvd

Example

For signals : (example in example.py)

from pyksvd.pyksvd import KSVD
import numpy as np

# Define parameters
N = 1500 # number of training signals 
n = 20 # size of each signal
K = 50 # number of atoms in dict
T0 = 3 # number of non zero coefficients

Y = np.random.rand(n, N)
# Fit the model
KSVD_model = KSVD(K=K, T0=T0)
KSVD_model.fit(Y, verbose=True)
X, D = KSVD_model.X, KSVD_model.D

For images : (example in example_corrupted_image.py)

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from pyksvd.functions import train_ksvd_models, corrupt_image, reconstruct_image

# Define parameters
patch_size = 8
image_size = 256
K = 441  # number of atoms in dict
T0 = 10  # number of non zero coefficients
REMOVE_PIXELS_RATIO = 0.7

# Directories
train_dir = 'data/train/impressionism/'
test_image_path = 'data/test/impressionism/134.jpg'

# Train KSVD models
ksvd_models = train_ksvd_models(train_dir, patch_size, K, T0)

# Load test image
test_image = Image.open(test_image_path)
test_image = test_image.resize((image_size, image_size))
test_image_array = np.array(test_image, dtype=np.float32) / 255.0

# Corrupt image
corrupted_image, mask = corrupt_image(test_image_array, REMOVE_PIXELS_RATIO)

# Reconstruct image
reconstructed_image = reconstruct_image(corrupted_image, ksvd_models, patch_size)

References

@article{Aharon2006KSVD,
  title = {K-SVD: An Algorithm for Designing Overcomplete Dictionaries for Sparse Representation},
  author = {Aharon, Michal and Elad, Michael and Bruckstein, Alfred},
  year = {2006},
  journal = {IEEE Transactions on Signal Processing},
}

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

pyksvd-0.1.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyksvd-0.1.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file pyksvd-0.1.2.tar.gz.

File metadata

  • Download URL: pyksvd-0.1.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.4

File hashes

Hashes for pyksvd-0.1.2.tar.gz
Algorithm Hash digest
SHA256 64a55d43d63596011c7fa4830a11982b8860d3d444d0c98987e0957e8ee63a81
MD5 618935ea07755344e9527c4a0b9651cb
BLAKE2b-256 9e0940664da97395d7b902bd2dfca4f954316d111af099d860dff145a3fcaba1

See more details on using hashes here.

File details

Details for the file pyksvd-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pyksvd-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.4

File hashes

Hashes for pyksvd-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 57d7e999b8b590b3263cdd05c13539a013284ff557894443a455c6dee2389717
MD5 b5f0f84a2277a1d16bdf5d11ea9d55f2
BLAKE2b-256 370e57f87e3acc904f86fc15bb824f726c20ad5ab1b70a1a3e32e7d2aaca2967

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page