Spaces:
Runtime error
Runtime error
| """Accuracy metric.""" | |
| import datasets | |
| import numpy as np | |
| from skimage.metrics import peak_signal_noise_ratio | |
| from typing import Dict, Optional | |
| import evaluate | |
| _DESCRIPTION = """ | |
| Compute the Peak Signal-to-Noise Ratio (PSNR) for an image. | |
| Please pay attention to the `data_range` parameter with floating-point images. | |
| """ | |
| _KWARGS_DESCRIPTION = """ | |
| Args: | |
| predictions (`list` of `np.array`): Predicted labels. | |
| references (`list` of `np.array`): Ground truth labels. | |
| sample_weight (`list` of `float`): Sample weights Defaults to None. | |
| Returns: | |
| psnr (`float`):Peak Signal-to-Noise Ratio. The SSIM values are positive. Typical | |
| values for the PSNR in lossy image and video compression are between 30 and 50 dB, | |
| provided the bit depth is 8 bits, where higher is better. | |
| Examples: | |
| Example 1-A simple example | |
| >>> psnr = evaluate.load("jpxkqx/peak_signal_to_noise_ratio") | |
| >>> results = psnr.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0]) | |
| >>> print(results) | |
| {'psnr': 0.5} | |
| Example 2-The same as Example 1, except with `sample_weight` set. | |
| >>> psnr = evaluate.load("jpxkqx/peak_signal_to_noise_ratio") | |
| >>> results = psnr.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], sample_weight=[0.5, 2, 0.7, 0.5, 9, 0.4]) | |
| >>> print(results) | |
| {'psnr': 0.8778625954198473} | |
| """ | |
| _CITATION = """ | |
| @article{boulogne2014scikit, | |
| title={Scikit-image: Image processing in Python}, | |
| author={Boulogne, Fran{\c{c}}ois and Warner, Joshua D and Neil Yager, Emmanuelle}, | |
| journal={J. PeerJ}, | |
| volume={2}, | |
| pages={453}, | |
| year={2014} | |
| } | |
| """ | |
| class StructuralSimilarityIndexMeasure(evaluate.Metric): | |
| def _info(self): | |
| return evaluate.MetricInfo( | |
| description=_DESCRIPTION, | |
| citation=_CITATION, | |
| inputs_description=_KWARGS_DESCRIPTION, | |
| features=datasets.Features({ | |
| "predictions": datasets.Sequence(datasets.Array2D(dtype="float32")), | |
| "references": datasets.Sequence(datasets.Array2D(dtype="float32")), | |
| }), | |
| reference_urls=["https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio"], | |
| ) | |
| def _compute( | |
| self, | |
| predictions, | |
| references, | |
| data_range: Optional[float] = None, | |
| sample_weight=None, | |
| ) -> Dict[str, float]: | |
| samples = zip(predictions, references) | |
| return { | |
| "psnr": np.average( | |
| list(map( | |
| lambda args: peak_signal_noise_ratio(*args, data_range), | |
| samples | |
| )), | |
| weights=sample_weight | |
| ) | |
| } |