Skip to main content

GPU-accelerated BM3D denoising with CuPy

Project description

BM3D with CuPy

This repository contains a GPU-accelerated implementation of BM3D using CuPy. The main script is bm3d_cupy.py, which provides a function bm3d_gpu that can be used to denoise 2D images of shape (H, W) or (H, W, C) on the GPU. The bm3d_cupy_benchmark.py script benchmarks the performance of the GPU implementation against the CPU version.

This implementation is intended to be as close as possible to the reference CPU implementation in the official BM3D implementation, which can be found here. Most of the transforms used in this implementation are exactly the same as the official implementation.

Different transforms than the default can also be used. However, you would need to change the code yourself:

# 2d transforms (along spatial dimensions)
spatial_ht, spatial_ht_inv = _transform_matrices(p, 'bior1.5')  # biorthogonal WT, (p, p)
spatial_wiener, spatial_wiener_inv = _transform_matrices(p, 'dct')  # DCT, (p, p)
# 1d transforms (along group dimension)
group_ht, group_ht_inv = _transform_matrices(ht_group_size, 'haar')  # Haar, (k_ht, k_ht)
group_wiener, group_wiener_inv = _transform_matrices(wiener_group_size, 'haar')  # Haar, (k_w, k_w)

The purpose of this implementation is to provide a fast and GPU-accelerated version of BM3D, which is beneficial for fast prototyping and, in particular, for integration as a plug-and-play prior into an iterative image reconstruction algorithm that is already implemented on the GPU (e.g., SigPy).

Installation

To install from PyPI, the distribution name is bm3d-cupy and the import name is bm3d_cupy. To use this package with CUDA 13, use:

pip install "bm3d-cupy[cuda13]"

To use this package with CUDA 12, use:

pip install "bm3d-cupy[cuda12]"

Usage

import cupy as cp
import numpy as np
from skimage.data import cat

from bm3d_cupy import bm3d_gpu


rng = np.random.default_rng(seed=0)


x = cat().astype('float32') / 255.0
sigma = 0.1
y = (x + rng.standard_normal(x.shape).astype('float32') * sigma).astype('float32')

y_gpu = cp.asarray(y)
sigma_gpu = cp.asarray(sigma, dtype=cp.float32)
z_gpu = bm3d_gpu(y_gpu, sigma_gpu)
z = cp.asnumpy(z_gpu)

Result

On an NVIDIA RTX 6000 Ada, the GPU implementation achieves a speedup of 15.91x compared to the reference CPU implementation for the cat image.

BM3D (CPU) time: 3.0155 (0.0226) s
  PSNR: 30.85 dB
  SSIM: 0.8194


BM3D (GPU) time: 0.1881 (0.0004) s
  Speedup: 16.03x
  PSNR: 30.90 dB
  SSIM: 0.8228

The argument chunk_size determines the number of groups to be processed in parallel on the GPU. A larger chunk_size increases GPU memory usage but can improve speed. However, in practice, extremely large chunk_size leads to poor speed, possibly due to overhead. The default value of chunk_size=2048 is set according to the experiment shown above run on the RTX 6000 Ada GPU. You may need to adjust it for different GPUs and images.

Acknowledgement

If you use this package, reference to the original authors (see below) is recommended.

K. Dabov, A. Foi, V. Katkovnik, and K. Egiazarian, “Image Denoising by Sparse 3-D Transform-Domain Collaborative Filtering,” IEEE Transactions on Image Processing, vol. 16, no. 8, pp. 2080–2095, Aug. 2007, doi: 10.1109/TIP.2007.901238.

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

bm3d_cupy-0.1.3.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

bm3d_cupy-0.1.3-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bm3d_cupy-0.1.3.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for bm3d_cupy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 37d319143d87bc50a0bd94aa4a549b36a4d72ef6502a3b34e4d5a105f10bdec1
MD5 74b212394684b45572e5f8d6b31960c8
BLAKE2b-256 024759438460caf2d5a4bda7d7842ea6366cfa92ce6b045009bb0b388faf1e73

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bm3d_cupy-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for bm3d_cupy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 078367cecbb71b39b07d1ac2179dfce233d8c6149b2a893baee15c9b8a62c761
MD5 a724a99d2adbec32b1bdfd4c3b13119d
BLAKE2b-256 b47435e323657567f86213c53fb2c0892473e989b30e0326ba3bff70266dd340

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