Spaces:
Runtime error
Runtime error
Include SSIM from scikit-image
Browse files- app.py +5 -0
- requirements.txt +2 -0
- structural_similarity_index_measure.py +92 -0
app.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import evaluate
|
2 |
+
from evaluate.utils import launch_gradio_widget
|
3 |
+
|
4 |
+
module = evaluate.load("structural_similarity_index_measure")
|
5 |
+
launch_gradio_widget(module)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
scikit-image>=0.19
|
structural_similarity_index_measure.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Accuracy metric."""
|
2 |
+
|
3 |
+
import datasets
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
from skimage.metrics import structural_similarity
|
7 |
+
from typing import Dict, Optional
|
8 |
+
import evaluate
|
9 |
+
|
10 |
+
|
11 |
+
_DESCRIPTION = """
|
12 |
+
Accuracy is the proportion of correct predictions among the total number of cases processed. It can be computed with:
|
13 |
+
Accuracy = (TP + TN) / (TP + TN + FP + FN)
|
14 |
+
Where:
|
15 |
+
TP: True positive
|
16 |
+
TN: True negative
|
17 |
+
FP: False positive
|
18 |
+
FN: False negative
|
19 |
+
"""
|
20 |
+
|
21 |
+
|
22 |
+
_KWARGS_DESCRIPTION = """
|
23 |
+
Args:
|
24 |
+
predictions (`list` of `np.array`): Predicted labels.
|
25 |
+
references (`list` of `np.array`): Ground truth labels.
|
26 |
+
sample_weight (`list` of `float`): Sample weights Defaults to None.
|
27 |
+
Returns:
|
28 |
+
ssim (`float`): Structural Similarity Index Measure. The SSIM values are in range (-1, 1], when pixels are non-negative.
|
29 |
+
Examples:
|
30 |
+
Example 1-A simple example
|
31 |
+
>>> accuracy_metric = evaluate.load("accuracy")
|
32 |
+
>>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0])
|
33 |
+
>>> print(results)
|
34 |
+
{'accuracy': 0.5}
|
35 |
+
Example 2-The same as Example 1, except with `sample_weight` set.
|
36 |
+
>>> accuracy_metric = evaluate.load("accuracy")
|
37 |
+
>>> results = accuracy_metric.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])
|
38 |
+
>>> print(results)
|
39 |
+
{'accuracy': 0.8778625954198473}
|
40 |
+
"""
|
41 |
+
|
42 |
+
|
43 |
+
_CITATION = """
|
44 |
+
@article{boulogne2014scikit,
|
45 |
+
title={Scikit-image: Image processing in Python},
|
46 |
+
author={Boulogne, Fran{\c{c}}ois and Warner, Joshua D and Neil Yager, Emmanuelle},
|
47 |
+
journal={J. PeerJ},
|
48 |
+
volume={2},
|
49 |
+
pages={453},
|
50 |
+
year={2014}
|
51 |
+
}
|
52 |
+
"""
|
53 |
+
|
54 |
+
|
55 |
+
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
56 |
+
class StructuralSimilarityIndexMeasure(evaluate.Metric):
|
57 |
+
def _info(self):
|
58 |
+
return evaluate.MetricInfo(
|
59 |
+
description=_DESCRIPTION,
|
60 |
+
citation=_CITATION,
|
61 |
+
inputs_description=_KWARGS_DESCRIPTION,
|
62 |
+
features=datasets.Features({
|
63 |
+
"predictions": datasets.Sequence(datasets.Array2D("float32")),
|
64 |
+
"references": datasets.Sequence(datasets.Array2D("float32")),
|
65 |
+
}),
|
66 |
+
reference_urls=["https://scikit-image.org/docs/dev/auto_examples/transform/plot_ssim.html"],
|
67 |
+
)
|
68 |
+
|
69 |
+
def _compute(
|
70 |
+
self,
|
71 |
+
predictions,
|
72 |
+
references,
|
73 |
+
win_size: Optional[int] = None,
|
74 |
+
gaussian_weights: Optional[bool] = False,
|
75 |
+
data_range: Optional[float] = None,
|
76 |
+
multichannel: Optional[bool] = False,
|
77 |
+
sample_weight=None,
|
78 |
+
**kwargs
|
79 |
+
) -> Dict[str, float]:
|
80 |
+
def func_ssim(x, y):
|
81 |
+
return structural_similarity(
|
82 |
+
x,
|
83 |
+
y,
|
84 |
+
win_size=win_size,
|
85 |
+
gaussian_weights=gaussian_weights,
|
86 |
+
data_range=data_range,
|
87 |
+
multichannel=multichannel
|
88 |
+
)
|
89 |
+
|
90 |
+
return {
|
91 |
+
"ssim": np.average(list(map()), weights=sample_weight))
|
92 |
+
}
|