Skip to content
/ lrf Public

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

License

Notifications You must be signed in to change notification settings

pashtari/lrf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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-aware Matrix Factorization for Low Bit Rate Image Compression."

Original Original
 
JPEG JPEG
(bitrate: 0.14 bpp, PSNR: 22.66 dB)
SVD SVD
(bitrate: 0.12 bpp, PSNR: 26.90 dB)
QMF QMF
(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 quantization-aware matrix factorization (QMF) compression method using the kodim01 image from the Kodak dataset. For a more detailed example comparing QMF against JPEG and SVD, check out this notebook. To better understand each step of the QMF compression using visualizations, refer to this notebook.

Import Packages

import torch

import lrf

Load Image

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

QMF Encode Image

qmf_encoded = lrf.qmf_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 QMF-Encoded Image

image_qmf = lrf.qmf_decode(qmf_encoded)

Calculate Compression Metrics

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

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 Original and Compressed Images

lrf.vis_image(image, title="Original")
lrf.vis_image(
    image_qmf, title=f"QMF (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)
QMF QMF
(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.