Skip to main content

Accelerated implementations of the PSNR-HVS, PSNR-HVS-M, PSNR-HA and PSNR-HMA image metrics for NumPy and PyTorch

Project description

PSNR-HVS-M, PSNR-HA and PSNR-HMA metrics for NumPy and PyTorch

cibuildwheel python_versions pypi license

Accelerated Python package for computing several image metrics based on human perception with backends in NumPy, PyTorch and C++.

This is an implementation of the PSNR-HVS, PSNR-HVS-M, PSNR-HA and PSNR-HMA metrics developed by Nikolay Ponomarenko.

The values produced by this library have been cross-checked against the results within the TID2013 dataset. (See the folder tid2013_results.) The only difference is that this library follows the common convention that PSNR for identical signals equals 100.0.

A miniscule discrepancy for PSNR-HMA (<0.01dB on average) is under investigation.

Bibliography

Installation

psnr_hvsm supports Python 3.7-3.12. Packages are distributed on PyPi. Be sure to have an up-to-date pip to be able to install the correct packages on Linux:

python -m pip install --upgrade pip
pip install psnr_hvsm

With PyTorch support:

pip install psnr_hvsm[torch]

Usage

Command line

Command line support is an extra that pulls imageio:

pip install psnr_hvsm[command_line]
python -m psnr_hvsm original.png distorted.png

Choosing a backend

The backend can be set by setting the PSNR_HVSM_BACKEND environment variable. Valid backends are:

  • numpy - pure NumPy using scipy.fft for DCT
  • cpp - C++ using FFTW3
  • torch - PyTorch; install as psnr_hvsm[torch] to install PyTorch as well
export PSNR_HVSM_BACKEND=torch
python -m psnr_hvsm original.png distorted.png

The default device for PyTorch is cuda but it can be changed by setting the PSNR_HVSM_TORCH_DEVICE environment variable.

As a library

The function psnr_hvs_hvsm accepts images as single-channel floating-point NumPy arrays. The images need to be normalised, i.e. the values need to be in the range [0,1]. This can be achieved by converting the image to float and dividing by the maximum value given the bit depth. For 8 bits per component this is 255.

The images must be padded to a multiple of 8 in each dimension.

from imageio import imread
from psnr_hvsm import psnr_hvs_hvsm, bt601ycbcr

image1 = imread('tests/baboon.png').astype(float) / 255
image2 = imread('tests/baboon_msk.png').astype(float) / 255

image1_y, *_ = bt601ycbcr(image1)
image2_y, *_ = bt601ycbcr(image2)

psnr_hvs, psnr_hvsm = psnr_hvs_hvsm(image1, image2)

print(psnr_hvs, psnr_hvsm)
34.427054505764424 51.64722121999962

If you need to measure PSNR-HVS and PSNR-HVS-M on an RGB image, you need to convert it to an YUV colorspace and pass in only the luma component.

PyTorch support

The PyTorch backend can be used for use with gradient descent algorithms and computation on GPUs. In order to use the PyTorch backend, either install the package with the torch extra:

pip install psnr_hvsm[torch]

If your PyTorch installation was manual, you need torch-dct in order to use the PyTorch backend:

pip install "torch-dct>=0.1.6"

An important distinction is that the functions that expect 3-channel input now expect (...,C,H,W) format in the PyTorch implementation. The PyTorch backend can be enabled by importing it directly from psnr_hvsm.torch:

import torch
from imageio import imread
from psnr_hvsm.torch import psnr_hvs_hvsm, bt601ycbcr

image1 = imread('tests/baboon.png').astype(float) / 255
image2 = imread('tests/baboon_msk.png').astype(float) / 255

image1 = torch.tensor(image1, device='cuda').moveaxis(-1, -3)  # convert to (N,C,H,W) format
image2 = torch.tensor(image2, device='cuda').moveaxis(-1, -3)  # convert to (N,C,H,W) format

image1_y, *_ = bt601ycbcr(image1)
image2_y, *_ = bt601ycbcr(image2)

psnr_hvs, psnr_hvsm = psnr_hvs_hvsm(image1_y, image2_y)

Alternatively, set the PSNR_HVSM_BACKEND environment variable to torch:

import os
os.environ['PSNR_HVSM_BACKEND'] = 'torch'

from psnr_hvsm import psnr_hvs_hvsm
# rest of code
# ...

Note on gradients

Some operations in the calculation of the HVS-M MSE lead to problems with the gradient and therefore a parameter called masking_epsilon (defaulting to 0.0) has been added to the PyTorch versions of hvs_hvsm_mse_tiles, hvs_hvsm_mse and psnr_hvs_hvsm. Set it to a small value (determined by your own experimentation) if you need the result of HVS-M for gradient descent.

Computing metrics for the TID2013 dataset

If you have a copy of the TID2013 dataset, you can re-verify the metrics for yourself:

python -m psnr_hvsm.tid2013_metrics D:\tid2013\ .\tid2013_results\

Other exported functions

  • hvs_hvsm_mse_tiles - compute HVS and HVS-M scores on all 8x8 tiles in the images, returns an array of numbers
  • hvs_hvsm_mse - compute average HVS and HVS-M scores

Building

Dependencies

psnr_hvsm has several dependencies:

FFTW3 is automatically resolved by CMake and the rest can be installed by creating a conda environment using the provided YAML file:

conda env create -f psnr_hvsm-dev.yml

Development mode

To install in development mode:

pip install --upgrade -r requirements.txt

Creating Python wheel

pip install --upgrade -r requirements-build.txt
python setup.py bdist_wheel

Running tests on different versions of Python using tox

pip install --upgrade -r requirements-tox.txt
tox --parallel auto

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

psnr_hvsm-0.2.4-cp312-cp312-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.12 Windows x86-64

psnr_hvsm-0.2.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

psnr_hvsm-0.2.4-cp311-cp311-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.11 Windows x86-64

psnr_hvsm-0.2.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

psnr_hvsm-0.2.4-cp310-cp310-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.10 Windows x86-64

psnr_hvsm-0.2.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

psnr_hvsm-0.2.4-cp39-cp39-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.9 Windows x86-64

psnr_hvsm-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

psnr_hvsm-0.2.4-cp38-cp38-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.8 Windows x86-64

psnr_hvsm-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

psnr_hvsm-0.2.4-cp37-cp37m-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.7m Windows x86-64

psnr_hvsm-0.2.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

File details

Details for the file psnr_hvsm-0.2.4-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 09a686fd2ef452340624451273ad5c211eb80d23888b63554b40781a26c441b6
MD5 a6a702d4c3601abac12b478456f41928
BLAKE2b-256 5da7e828d5ee7f309681ac0b91aa7a3763aeeebc603c04e626f964dc02e90b9a

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 63e9b478a796e8484de9039d9abd500e3885180498ac5bc47848f9d10b620a3f
MD5 f38e13f0eb77a4378262858ee45c2899
BLAKE2b-256 23ba17d96d3a35e87127d6ac2af68889356515f6661ff1d695b53c2369a8d5e1

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bc68ad39bf2f159fba17fecb824fda3a3ed7fd570a2fb2abf8525591bae646b4
MD5 e43bfb8242030f619e5a67b444666582
BLAKE2b-256 ec7f2053425b960f4a8538c065a99f9e29b8214d66e2a27071f30b0276992a00

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 65992a881c414633c442cecb4c99a120453efab210c1d0876dd1e2702a2119fc
MD5 fa5afa07774c83e632f44db4d9469bb5
BLAKE2b-256 6a6ee10ee109d79d895721569bdbabf10f8b777f15a5bdf34cf6cab28635dd5c

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0d1a411cc77fd7171ade6781c2240aa5433838451452ca9023b8770461ddb897
MD5 e5252bcf1f2316cf6a462b10a7fe7ef3
BLAKE2b-256 4fd111fcdcbbb34f2eb2050569950610fdcbd3a6ef99052f53909ab8985d23e1

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4cef163c5553fc6a2ce92f1bd4be198243059b6817fc322f1a6cac982976dfc8
MD5 b45154978ea2dec62157423be33b5b8e
BLAKE2b-256 cc0b7937a9442b54319287a9e015e699d5537100aa669307629315bff227f2e4

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: psnr_hvsm-0.2.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for psnr_hvsm-0.2.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 e4a91ee6ab72c26805d693d5562378049c4b5df5ee0d26f1e9fc2d3ca258ba9c
MD5 694019f9918fc423362863be78a533c3
BLAKE2b-256 ff2444bfe99588b148cb1bbb5244db4e6e6cbb7e300dbd94f74770377020db53

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8780f941b0fca8af150321e4337cbe258f5e022e246fa0f5ac014877e5baa504
MD5 7e5e2f30794c395f6200e34840dce34d
BLAKE2b-256 61964b2792a07f1b8735cf554fb86915d477b30410483ea13a67ca1fa88b4a93

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: psnr_hvsm-0.2.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for psnr_hvsm-0.2.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 be25baf7e23c3dad6350f6af694780eb013366035241617ee5bd013668448fe6
MD5 8d11769d5ea9a94535a11f64b4fe8eea
BLAKE2b-256 c2923b75b58d96e3a5a21c1af1d60ceeabd84460e9de60ce64d39e77969531d0

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 99582fb362aa893940ebb65014a0ddd05885096ae1cfd7527dd03aff60e6cda1
MD5 6ed72f6d1ab5888b467f3970354673bf
BLAKE2b-256 1ebde70feb0d82f74efd5bbe297fbde5aab02deb10811dc4c16e9c51665290e5

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 9cb3728955e2c384769cc0481140bc30056911cefbb34f0cd44c7926e62834d1
MD5 099139bd3325972174257a14599ada57
BLAKE2b-256 c489f6d4d586192afded56f053e83b662240bb193d8f0af0f9cd7674c2e091df

See more details on using hashes here.

File details

Details for the file psnr_hvsm-0.2.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psnr_hvsm-0.2.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a25f97cce647f6900782058ed448820ef12dc56cebb38e8d17ce008d8b918573
MD5 1594f76f309803b517bddb8cd153cf57
BLAKE2b-256 cb7177383430ae9a67f8911aec45683d5f75c22fa27272948ec90d89d4b42272

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page