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.3.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.3-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyksvd-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 248fc01bf0c87d801440db1668afb4e433de49012172c0e9010b525461f6f4ae
MD5 6684ff2512a604fa93d972d62a551df8
BLAKE2b-256 e3c1e211549e68e161759baf90d90f73616ad16167262537d220f40e260bc951

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyksvd-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c20f55eddc555de02981bfc82f8e15d142f524d7e7078ffec4fbd92f668be4f4
MD5 1f487fff360926a1040f306e93b48cbb
BLAKE2b-256 4260f8964195adf5b8516e07a3407bbe630e057aac76830116eb1ff07390931f

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