Skip to main content

Lightweight Learned Image Compression (LLIC)

Installation

  1. Follow the installation instructions for torch and compressai
  2. Install LLIC via pip: pip install LLIC

Pre-trained checkpoints

An imagenet-trained checkpoint for RGB images is available on huggingface: LLIC_rgb_v0.0.1.pth

Request access to other checkpoints (grayscale, hyperspectral, microscopy, etc)

Usage example

import torch
import zlib
import numpy as np
import compressai
from io import BytesIO
from IPython.display import display
from PIL import Image
from LLIC import LLIC
from torchvision.transforms import ToPILImage, PILToTensor

Load the model

checkpoint = torch.load("LLIC_rgb_v0.0.1.pth",map_location="cpu")
codec = LLIC.RateDistortionAutoEncoder()
codec.load_state_dict(checkpoint['model_state_dict'])
<All keys matched successfully>

Download example image

!wget https://r0k.us/graphics/kodak/kodak/kodim05.png
original_image = Image.open("kodim05.png")
original_image

png

The analysis and synthesis transforms expect dimensions to be multiples of of 16. Zero padding can be applied otherwise.

def pad(x, p=2**5):
    h, w = x.size(2), x.size(3)
    pad, _ = compressai.ops.compute_padding(h, w, min_div=p)
    return torch.nn.functional.pad(x, pad, mode="constant", value=0)

def preprocess(pil_image):
    tensor = PILToTensor()(pil_image)
    tensor = tensor.unsqueeze(0)
    tensor = tensor.to(torch.float)
    tensor = tensor/255
    tensor = tensor - 0.5
    return pad(tensor)

Compress the image and save file

padded_image = preprocess(original_image)
original_size = padded_image.shape
compressed_image, compressed_shape = LLIC.compress(padded_image, codec)
with open("kodim05.llic", 'wb') as f:
    f.write(compressed_image)

Decompress and view the image

def crop(x, size):
    H, W = x.size(2), x.size(3)
    h, w = size
    _, unpad = compressai.ops.compute_padding(h, w, out_h=H, out_w=W)
    return torch.nn.functional.pad(x, unpad, mode="constant", value=0)

def postprocess(tensor):
    tensor = tensor[0] + 0.5
    tensor = 255*tensor
    tensor = tensor.clamp(0,255)
    tensor = tensor.to(torch.uint8)
    pil_image = ToPILImage()(tensor)
    return pil_image
with open("kodim05.llic", 'rb') as f:
    compressed_image = f.read()
    tensor = LLIC.decompress(compressed_image, compressed_shape, codec)
    recovered_image = postprocess(crop(tensor, (512,768)))
recovered_image

png

!jupyter nbconvert --to markdown README.ipynb
[NbConvertApp] Converting notebook README.ipynb to markdown
[NbConvertApp] Support files will be in README_files/
[NbConvertApp] Making directory README_files
[NbConvertApp] Writing 2873 bytes to README.md

Metadata

Release files for LLIC 0.1.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for LLIC 0.1.13
File Size Uploaded
llic-0.1.13.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for LLIC 0.1.13
File Interpreter ABI Platform
LLIC-0.1.13-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / llic-0.1.13.tar.gz

Download URL llic-0.1.13.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
65536ba6d4fcb29fa301f7530ff1018c2fa8db5995cd77fc0b038d0404096b36
BLAKE2b-256 checksum
How to use checksums
38fa8a7e06bdbdd91351721a41f61bf5b886be8f99e5684e5835544bf3b813d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / LLIC-0.1.13-py3-none-any.whl

Download URL LLIC-0.1.13-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d473043c72dfe5f80b3b7686ff375e6dca1052311c174d1938a498274796fbd7
BLAKE2b-256 checksum
How to use checksums
1a823c3a9a6c16a51e8cf38ce93aa8c88886a5b9b6506a3a60c27f87ac4f7cc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.13 This release

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page