Skip to main content

🧱 Torch JPEG Blockiness Metric

The higher the blockiness metric value, the more likely it is that the image was JPEG-compressed at a low quality.

This is a implementation of blockiness algorithm from the paper "A JPEG blocking artifact detector for image forensics" (Dinesh Bhardwaj, Vinod Pankajakshan).

It is based on the gohtanii's implementation from DiverSeg dataset: "Rethinking Image Super-Resolution from Training Data Perspectives" (Go Ohtani, Ryu Tadokoro, Ryosuke Yamada, Yuki M. Asano, Iro Laina, Christian Rupprecht, Nakamasa Inoue, Rio Yokota, Hirokatsu Kataoka, and Yoshimitsu Aoki, ECCV2024).

It has the following improvements over the gohtanii's implementation:

  1. 🔥 operations are written in torch (gpu friendly)
  2. 🔥 operations are vectorized
  3. 🔥 batched input is supported, with the assumption of same image size

Usage

(option a) you can copy paste the torch_jpeg_blockiness/blockiness.py file to your project directory as it has no dependencies except torch and numpy.

(option b) you can also install the package with pip

pip install torch_jpeg_blockiness

example:

import torch
from torch_jpeg_blockiness.blockiness import calculate_image_blockiness

gray_image = torch.rand((1, 1, 100,100)) # B, 1, H, W
blockiness = calculate_image_blockiness(gray_image)
print(blockiness) # tensor([5.7635])

real example:

import torchvision
import torchvision.transforms.functional
from torch_jpeg_blockiness.blockiness import calculate_image_blockiness, rgb_to_grayscale

img = torchvision.io.read_image("example_images/unsplash.jpg")
img_gray = rgb_to_grayscale(img)
blockiness = calculate_image_blockiness(img_gray)
blockiness_float = float(blockiness)

test the code against original (gohtanii's implementation)

python3 -m test.test
.
------------------------
Ran 1 test in 11.771s

OK

Formal definitions

Definisions from the DiverSeg dataset: "Rethinking Image Super-Resolution from Training Data Perspectives" (Go Ohtani, Ryu Tadokoro, Ryosuke Yamada, Yuki M. Asano, Iro Laina, Christian Rupprecht, Nakamasa Inoue, Rio Yokota, Hirokatsu Kataoka, and Yoshimitsu Aoki, ECCV2024) paper:

These definitions are missing some crucial parts that would make them easier to understand. I therefore recommend reading the original paper "A JPEG blocking artifact detector for image forensics" with sci-hub.

Other

Motivation: Authors of the paper have demonstrated that filtering images which have JPEG compresson helps the super resolution model achieve better performance in the long run. Even more crucial, JPEG blockiness filtering prevents the dataset from containing excessively strong compression, which can be highly detrimental to the overall training process as shown by the DiverSeg dataset paper and Phillip Hoffman's BHI filtering blog post.

note: the code is tested against the original implementation. test can be found at tests/test.py

note 2: this method should be shift and scale-invariant as it computes the DCT for each fixed block rather than the entire image, but I haven't empirically tested this yet.

note 3: I was thinking about using the centarl crop of an image (most likely high entropy area) to reduce the compute time even more. The problem is that the crop might start in the middle of the JPEG block. I'm not sure if this method is robust to JPEG grid offsets. However, you could start cropping from the upper-left corner to some fixed dimension. Preferably, the bottom-right corner should pass through the center of the image.

References

Phillip Hoffman's "Filtering single image super-resolution datasets with BHI" blog post https://huggingface.co/blog/Phips/bhi-filtering


DiverSeg dataset: "Rethinking Image Super-Resolution from Training Data Perspectives" (Ohtani, Go and Tadokoro, Ryu and Yamada, Ryosuke and Asano, Yuki M and Laina, Iro and Rupprecht, Christian and Inoue, Nakamasa and Yokota, Rio and Kataoka, Hirokatsu and Aoki, Yoshimitsu)

github: https://github.com/gohtanii/DiverSeg-dataset/tree/284cc1c030424b8b0f7040020bd6435e8ed2e6d7

arxiv: https://arxiv.org/abs/2409.00768

@inproceedings{ohtani2024rethinking,
  title={Rethinking Image Super-Resolution from Training Data Perspectives},
  author={Ohtani, Go and Tadokoro, Ryu and Yamada, Ryosuke and Asano, Yuki M and Laina, Iro and Rupprecht, Christian and Inoue, Nakamasa and Yokota, Rio and Kataoka, Hirokatsu and Aoki, Yoshimitsu},
  booktitle={European Conference on Computer Vision},
  pages={19--36},
  year={2024},
  organization={Springer}
}

"A JPEG blocking artifact detector for image forensics", original paper which defined this concrete blockiness metric.

(open with sci-hub) https://www.sciencedirect.com/science/article/abs/pii/S0923596518302066

@article{bhardwaj2018jpeg,
  title={A JPEG blocking artifact detector for image forensics},
  author={Bhardwaj, Dinesh and Pankajakshan, Vinod},
  journal={Signal Processing: Image Communication},
  volume={68},
  pages={155--161},
  year={2018},
  publisher={Elsevier}
}

Release files for torch-jpeg-blockiness 1.0.4

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

Source distribution (sdist)

Source distribution for torch-jpeg-blockiness 1.0.4
File Size Uploaded
torch_jpeg_blockiness-1.0.4.tar.gz 2.8 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for torch-jpeg-blockiness 1.0.4
File Interpreter ABI Platform
torch_jpeg_blockiness-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 5.7 MB

Release files / torch_jpeg_blockiness-1.0.4.tar.gz

Download URL torch_jpeg_blockiness-1.0.4.tar.gz
Size 2.8 MB
Tags Source
SHA-256 checksum
How to use checksums
df5322bb47d251036a36f2fabb592dc7c28bba7448fab40be11e5cb09d9c6210
BLAKE2b-256 checksum
How to use checksums
4fe6a6e5c0ba1a56d4cc00328f0102c1d13a9c13e35c536d3253818d18096eaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.13

Release files / torch_jpeg_blockiness-1.0.4-py3-none-any.whl

Download URL torch_jpeg_blockiness-1.0.4-py3-none-any.whl
Size 2.8 MB
Tags Python 3
SHA-256 checksum
How to use checksums
6258985929db05b5b2adeab2dee9a1d44bdffecaeba3862c0ad2d6af7fec85a9
BLAKE2b-256 checksum
How to use checksums
08e0cb619600ddc7fe2be43a1a63bf4161b771aead8c7884f580e5b5c3adbdc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.13

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.1

2 release files

1.0.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