Spaces:
Runtime error
Runtime error
Update content with metric specific features
Browse files- peak_signal_to_noise_ratio.py +55 -67
- requirements.txt +2 -1
peak_signal_to_noise_ratio.py
CHANGED
@@ -1,95 +1,83 @@
|
|
1 |
-
|
2 |
-
#
|
3 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
-
# you may not use this file except in compliance with the License.
|
5 |
-
# You may obtain a copy of the License at
|
6 |
-
#
|
7 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
-
#
|
9 |
-
# Unless required by applicable law or agreed to in writing, software
|
10 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
-
# See the License for the specific language governing permissions and
|
13 |
-
# limitations under the License.
|
14 |
-
"""TODO: Add a description here."""
|
15 |
|
16 |
-
import evaluate
|
17 |
import datasets
|
|
|
18 |
|
|
|
|
|
|
|
19 |
|
20 |
-
# TODO: Add BibTeX citation
|
21 |
-
_CITATION = """\
|
22 |
-
@InProceedings{huggingface:module,
|
23 |
-
title = {A great new module},
|
24 |
-
authors={huggingface, Inc.},
|
25 |
-
year={2020}
|
26 |
-
}
|
27 |
-
"""
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
"""
|
33 |
|
34 |
|
35 |
-
# TODO: Add description of the arguments of the module here
|
36 |
_KWARGS_DESCRIPTION = """
|
37 |
-
Calculates how good are predictions given some references, using certain scores
|
38 |
Args:
|
39 |
-
predictions
|
40 |
-
|
41 |
-
|
42 |
-
reference should be a string with tokens separated by spaces.
|
43 |
Returns:
|
44 |
-
|
45 |
-
|
|
|
46 |
Examples:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
"""
|
55 |
|
56 |
-
# TODO: Define external resources urls if needed
|
57 |
-
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
61 |
-
class PeakSignaltoNoiseRatio(evaluate.Metric):
|
62 |
-
"""TODO: Short description of my evaluation module."""
|
63 |
|
|
|
|
|
64 |
def _info(self):
|
65 |
-
# TODO: Specifies the evaluate.EvaluationModuleInfo object
|
66 |
return evaluate.MetricInfo(
|
67 |
-
# This is the description that will appear on the modules page.
|
68 |
-
module_type="metric",
|
69 |
description=_DESCRIPTION,
|
70 |
citation=_CITATION,
|
71 |
inputs_description=_KWARGS_DESCRIPTION,
|
72 |
-
# This defines the format of each prediction and reference
|
73 |
features=datasets.Features({
|
74 |
-
|
75 |
-
|
76 |
}),
|
77 |
-
|
78 |
-
homepage="http://module.homepage",
|
79 |
-
# Additional links to the codebase or references
|
80 |
-
codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
|
81 |
-
reference_urls=["http://path.to.reference.url/new_module"]
|
82 |
)
|
83 |
|
84 |
-
def
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
accuracy = sum(i == j for i, j in zip(predictions, references)) / len(predictions)
|
93 |
return {
|
94 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
}
|
|
|
1 |
+
"""Accuracy metric."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
3 |
import datasets
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
from skimage.metrics import peak_signal_noise_ratio
|
7 |
+
from typing import Dict, Optional
|
8 |
+
import evaluate
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
_DESCRIPTION = """
|
12 |
+
Compute the Peak Signal-to-Noise Ratio (PSNR) for an image.
|
13 |
+
|
14 |
+
Please pay attention to the `data_range` parameter with floating-point images.
|
15 |
"""
|
16 |
|
17 |
|
|
|
18 |
_KWARGS_DESCRIPTION = """
|
|
|
19 |
Args:
|
20 |
+
predictions (`list` of `np.array`): Predicted labels.
|
21 |
+
references (`list` of `np.array`): Ground truth labels.
|
22 |
+
sample_weight (`list` of `float`): Sample weights Defaults to None.
|
|
|
23 |
Returns:
|
24 |
+
psnr (`float`):Peak Signal-to-Noise Ratio. The SSIM values are positive. Typical
|
25 |
+
values for the PSNR in lossy image and video compression are between 30 and 50 dB,
|
26 |
+
provided the bit depth is 8 bits, where higher is better.
|
27 |
Examples:
|
28 |
+
Example 1-A simple example
|
29 |
+
>>> psnr = evaluate.load("jpxkqx/peak_signal_to_noise_ratio")
|
30 |
+
>>> results = psnr.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0])
|
31 |
+
>>> print(results)
|
32 |
+
{'psnr': 0.5}
|
33 |
+
Example 2-The same as Example 1, except with `sample_weight` set.
|
34 |
+
>>> psnr = evaluate.load("jpxkqx/peak_signal_to_noise_ratio")
|
35 |
+
>>> 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])
|
36 |
+
>>> print(results)
|
37 |
+
{'psnr': 0.8778625954198473}
|
38 |
"""
|
39 |
|
|
|
|
|
40 |
|
41 |
+
_CITATION = """
|
42 |
+
@article{boulogne2014scikit,
|
43 |
+
title={Scikit-image: Image processing in Python},
|
44 |
+
author={Boulogne, Fran{\c{c}}ois and Warner, Joshua D and Neil Yager, Emmanuelle},
|
45 |
+
journal={J. PeerJ},
|
46 |
+
volume={2},
|
47 |
+
pages={453},
|
48 |
+
year={2014}
|
49 |
+
}
|
50 |
+
"""
|
51 |
|
|
|
|
|
|
|
52 |
|
53 |
+
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
54 |
+
class StructuralSimilarityIndexMeasure(evaluate.Metric):
|
55 |
def _info(self):
|
|
|
56 |
return evaluate.MetricInfo(
|
|
|
|
|
57 |
description=_DESCRIPTION,
|
58 |
citation=_CITATION,
|
59 |
inputs_description=_KWARGS_DESCRIPTION,
|
|
|
60 |
features=datasets.Features({
|
61 |
+
"predictions": datasets.Sequence(datasets.Array2D("float32")),
|
62 |
+
"references": datasets.Sequence(datasets.Array2D("float32")),
|
63 |
}),
|
64 |
+
reference_urls=["https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio"],
|
|
|
|
|
|
|
|
|
65 |
)
|
66 |
|
67 |
+
def _compute(
|
68 |
+
self,
|
69 |
+
predictions,
|
70 |
+
references,
|
71 |
+
data_range: Optional[float] = None,
|
72 |
+
sample_weight=None,
|
73 |
+
) -> Dict[str, float]:
|
74 |
+
samples = zip(predictions, references)
|
|
|
75 |
return {
|
76 |
+
"psnr": np.average(
|
77 |
+
list(map(
|
78 |
+
lambda args: peak_signal_noise_ratio(*args, data_range),
|
79 |
+
samples
|
80 |
+
)),
|
81 |
+
weights=sample_weight
|
82 |
+
)
|
83 |
}
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
git+https://github.com/huggingface/evaluate@main
|
|
|
|
1 |
+
git+https://github.com/huggingface/evaluate@main
|
2 |
+
scikit-image>=0.19
|