Skip to main content

A Python implementation of the K-SVD algorithm

Project description

PyKSVD

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 ksvd.ksvd 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.0.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.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyksvd-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 d641514876c6f2fc19c4ec170f8d29276969b828411dae9bf881e677fae8fda2
MD5 364a3f68c729876c7b51922d95955fbc
BLAKE2b-256 6d9385cc41ce965c4a284a87f871b5e69082b9d61c313ef6f8cfae65b03d53d8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyksvd-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a7ab1c39852073cf334b02e9cadcf1433312856837a8e2335c84bc7e702062c
MD5 d660003c5bf4ca32a253268d5773b8bf
BLAKE2b-256 e6c36bd1a7aaa317c78bcfab8d7c9e4384ada46cb12a12d583bca45939b87246

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