/lrf

PyTorch implementation of low-rank factorization (LRF) methods for data compression

Primary LanguageJupyter NotebookApache License 2.0Apache-2.0

LRF Logo

This repository provides a PyTorch implementation of low-rank factorization (LRF) methods for data compression. Particularly, it includes the official implementation of "Quantization-free Lossy Image Compression Using Integer Matrix Factorization."

Original Original
 
JPEG JPEG
(bitrate: 0.14 bpp, PSNR: 22.66 dB)
SVD SVD
(bitrate: 0.12 bpp, PSNR: 26.90 dB)
IMF IMF
(bitrate: 0.12 bpp, PSNR: 31.63 dB)

Installation

First, ensure that you have PyTorch installed. The installation process may vary depending on your hardware (CPU, GPU, etc.).

Next, install the lrf package:

$ pip install git+https://github.com/pashtari/lrf.git

Quick Start

This guide will help you get started with the integer matrix factorization (IMF) compression method using the kodim01 image from the Kodak dataset. For a more detailed example comparing IMF against JPEG and SVD, check out this notebook. To better understand each step of the IMF compression using visualizations, refer to this notebook.

Import Packages

import torch

import lrf

Load and Visualize the Image

image = lrf.read_image("./kodim01.png")

IMF Encode the Image

imf_encoded = lrf.imf_encode(
    image,
    color_space="YCbCr",
    scale_factor=(0.5, 0.5),
    quality=7,
    patch=True,
    patch_size=(8, 8),
    bounds=(-16, 15),
    dtype=torch.int8,
    num_iters=10,
)

Decode the IMF Encoded Image

image_imf = lrf.imf_decode(imf_encoded)

Calculate Compression Metrics

cr_value = lrf.compression_ratio(image, imf_encoded)
bpp_value = lrf.bits_per_pixel(image.shape[-2:], imf_encoded)
psnr_value = lrf.psnr(image, image_imf)
ssim_value = lrf.ssim(image, image_imf)

metrics = {
    "compression ratio": cr_value,
    "bit rate (bpp)": bpp_value,
    "PSNR (dB)": psnr_value,
    "SSIM": ssim_value,
}
print(metrics)
{
    "compression ratio": 117.040,
    "bit rate (bpp)": 0.205,
    "PSNR (dB)": 21.928,
    "SSIM": 0.511
}

Visualize the Original and Compressed Images

lrf.vis_image(image, title="Original")
lrf.vis_image(
    image_imf, title=f"IMF (bit rate = {bpp_value:.2f} bpp, PSNR = {psnr_value:.2f} dB)"
)
Original Original
 
JPEG JPEG
(bitrate: 0.21 bpp, PSNR: 20.22 dB)
SVD SVD
(bitrate: 0.22 bpp, PSNR: 20.24 dB)
IMF IMF
(bitrate: 0.21 bpp, PSNR: 21.93 dB)

Contact

This repo is currently maintained by Pooya Ashtari (@pashtari) and Pourya Behmandpoor (@pourya-b). Feel free to reach out for any queries or contributions.