Felipehonorato commited on
Commit
d200671
·
1 Parent(s): 3328595

ERR initial version

Browse files
Files changed (1) hide show
  1. eer.py +26 -44
eer.py CHANGED
@@ -11,11 +11,13 @@
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 = """\
@@ -26,70 +28,50 @@ year={2020}
26
  }
27
  """
28
 
29
- # TODO: Add description of the module here
30
  _DESCRIPTION = """\
31
- This new module is designed to solve this great ML task and is crafted with a lot of care.
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: list of predictions to score. Each predictions
40
- should be a string with tokens separated by spaces.
41
- references: list of reference for each prediction. Each
42
- reference should be a string with tokens separated by spaces.
43
- Returns:
44
- accuracy: description of the first score,
45
- another_score: description of the second score,
46
- Examples:
47
- Examples should be written in doctest format, and should illustrate how
48
- to use the function.
49
-
50
- >>> my_new_module = evaluate.load("my_new_module")
51
- >>> results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
52
- >>> print(results)
53
- {'accuracy': 1.0}
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 EER(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
  'predictions': datasets.Value('int64'),
75
  'references': datasets.Value('int64'),
 
76
  }),
77
- # Homepage of the module for documentation
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 _download_and_prepare(self, dl_manager):
85
- """Optional: download external resources useful to compute the scores"""
86
- # TODO: Download external resources if needed
87
- pass
 
 
 
 
 
 
 
 
 
88
 
89
- def _compute(self, predictions, references):
90
- """Returns the scores"""
91
- # TODO: Compute the different scores of the module
92
- accuracy = sum(i == j for i, j in zip(predictions, references)) / len(predictions)
93
- return {
94
- "accuracy": accuracy,
95
- }
 
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
+ """EER compute script"""
15
 
16
  import evaluate
17
  import datasets
18
 
19
+ import numpy as np
20
+ import sklearn.metrics
21
 
22
  # TODO: Add BibTeX citation
23
  _CITATION = """\
 
28
  }
29
  """
30
 
 
31
  _DESCRIPTION = """\
32
+ This module is designed to compute Equal Error Rate metric, which is used a lot in the Automatic Speaker Verification task.
33
  """
34
 
35
 
 
36
  _KWARGS_DESCRIPTION = """
37
+ The EER is the location on a ROC or DET curve where the false acceptance rate and false rejection rate are equal. In general, the lower the equal error rate value, the higher the accuracy of the biometric system.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  """
39
 
 
 
 
40
 
41
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
42
  class EER(evaluate.Metric):
43
+ """Compute Equal error rate metrics"""
44
 
45
  def _info(self):
 
46
  return evaluate.MetricInfo(
 
47
  module_type="metric",
48
  description=_DESCRIPTION,
49
  citation=_CITATION,
50
  inputs_description=_KWARGS_DESCRIPTION,
 
51
  features=datasets.Features({
52
  'predictions': datasets.Value('int64'),
53
  'references': datasets.Value('int64'),
54
+ 'pos_label': datasets.Value('int64')
55
  }),
56
+ reference_urls=["https://github.com/YuanGongND/python-compute-eer"]
 
 
 
 
57
  )
58
 
59
+ def _compute(self, predictions, references, pos_label=1):
60
+ """Returns EER the scores"""
61
+ eer_score_list = []
62
+
63
+ fpr, tpr, threshold = sklearn.metrics.roc_curve(references, predictions, pos_label=pos_label)
64
+ fnr = 1 - tpr
65
+
66
+ # the threshold of fnr == fpr
67
+ eer_threshold = threshold[np.nanargmin(np.absolute((fnr - fpr)))]
68
+
69
+ # theoretically eer from fpr and eer from fnr should be identical but they can be slightly differ in reality
70
+ eer_1 = fpr[np.nanargmin(np.absolute((fnr - fpr)))]
71
+ eer_2 = fnr[np.nanargmin(np.absolute((fnr - fpr)))]
72
 
73
+ # return the mean of eer from fpr and from fnr
74
+ eer = (eer_1 + eer_2) / 2
75
+
76
+
77
+ return {"eer": eer}