-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add error rate calculation script. #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
kuke
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost LGTM
deep_speech_2/error_rate.py
Outdated
| :param hypophysis: The hypophysis sentence. | ||
| :type reference: str | ||
| :param squeeze: If set true, consecutive space character | ||
| will be squeezed to one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to add and indent in line 114? The same below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| :param squeeze: If set true, consecutive space character | ||
| will be squeezed to one | ||
| :type squeezed: bool | ||
| :param ignore_case: Whether ignoring character case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whether case-sensitive or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| def wer(reference, hypophysis, delimiter=' ', filter_none=True): | ||
| """ | ||
| Calculate word error rate (WER). WER is a popular evaluation metric used | ||
| in speech recognition. It compares a reference to an hypophysis and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compare to --> compare with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| return ref_len | ||
|
|
||
| distance = np.zeros((ref_len + 1) * (hyp_len + 1), dtype=np.int64) | ||
| distance = distance.reshape((ref_len + 1, hyp_len + 1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Above two lines can be merged into one line
distance = np.zeros((ref_len + 1, hyp_len + 1), dtype=np.int64)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| Iw is the number of words inserted, | ||
| Nw is the number of words in the reference | ||
|
|
||
| We can use levenshtein distance to calculate WER. Take an attention that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems there is no "take an attention", but pay/draw ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
比较通用的代码可以放到paddle的repo里吧~ |
deep_speech_2/error_rate.py
Outdated
| return distance[ref_len][hyp_len] | ||
|
|
||
|
|
||
| def wer(reference, hypophysis, delimiter=' ', filter_none=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- "hypophysis" or "hypothesis"?
- Why not provide a ignore_case argument, just as CER does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| reference and hypophysis sentences before calculating WER. | ||
|
|
||
| :param reference: The reference sentence. | ||
| :type reference: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
str --> basestring. The same below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| :return: WER | ||
| :rtype: float | ||
| """ | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the blank line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| :type delimiter: char | ||
| :param filter_none: Whether to remove None value when splitting sentence. | ||
| :type filter_none: bool | ||
| :return: WER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"WER" --> "Word error rate."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| .. code-block:: text | ||
|
|
||
| Sc is the number of character substituted, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
character --> characters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| def wer(reference, hypophysis, delimiter=' ', filter_none=True): | ||
| """ | ||
| Calculate word error rate (WER). WER is a popular evaluation metric used | ||
| in speech recognition. It compares a reference with an hypophysis and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- an hypothesis --> a hypothesis.
- Please keep this description similar to cer function doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| def cer(reference, hypophysis, squeeze=True, ignore_case=False, strip_char=''): | ||
| """ | ||
| Calculate charactor error rate (CER). CER will compare reference text and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will compare --> compares
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| if hyp_len == 0: | ||
| return ref_len | ||
|
|
||
| distance = np.zeros((ref_len + 1, hyp_len + 1), dtype=np.int64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why int64?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int32 is enough here. Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| distance = np.zeros((ref_len + 1, hyp_len + 1), dtype=np.int64) | ||
|
|
||
| # initialization distance matrix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initialization --> initialize
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| @@ -0,0 +1,137 @@ | |||
| # -- * -- coding: utf-8 -- * -- | |||
| import numpy as np | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a simple module doc.
|
@qingqing01 目前因为ctc_beam_search暂时实现在计算图外,所以相应的WER/CER也只能临时放到计算图外,不合适放入paddle layer/evaluator. |
pkuyym
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary parameters to keep the processing logic correct and simple.
deep_speech_2/error_rate.py
Outdated
| if hyp_len == 0: | ||
| return ref_len | ||
|
|
||
| distance = np.zeros((ref_len + 1, hyp_len + 1), dtype=np.int64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int32 is enough here. Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| distance = np.zeros((ref_len + 1, hyp_len + 1), dtype=np.int64) | ||
|
|
||
| # initialization distance matrix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| :type delimiter: char | ||
| :param filter_none: Whether to remove None value when splitting sentence. | ||
| :type filter_none: bool | ||
| :return: WER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| reference and hypophysis sentences before calculating WER. | ||
|
|
||
| :param reference: The reference sentence. | ||
| :type reference: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| :return: WER | ||
| :rtype: float | ||
| """ | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| def wer(reference, hypophysis, delimiter=' ', filter_none=True): | ||
| """ | ||
| Calculate word error rate (WER). WER is a popular evaluation metric used | ||
| in speech recognition. It compares a reference with an hypophysis and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| raise ValueError("Reference's word number should be greater than 0.") | ||
|
|
||
| if filter_none == True: | ||
| ref_words = filter(None, reference.strip(delimiter).split(delimiter)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| return wer | ||
|
|
||
|
|
||
| def cer(reference, hypophysis, squeeze=True, ignore_case=False, strip_char=''): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| .. code-block:: text | ||
|
|
||
| Sc is the number of character substituted, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| hypophysis = hypophysis.strip(strip_char) | ||
| if squeeze == True: | ||
| reference = ' '.join(filter(None, reference.split(' '))) | ||
| hypophysis = ' '.join(filter(None, hypophysis.split(' '))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
xinghai-sun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a unit test (with both English and Mandarin test case)?
deep_speech_2/error_rate.py
Outdated
| @@ -0,0 +1,133 @@ | |||
| # -*- coding: utf-8 -*- | |||
| """ | |||
| This module provides functions to calculate error rate in different level. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-->"""This .....
Let's keep consistent across all files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| def wer(reference, hypothesis, ignore_case=False, delimiter=' '): | ||
| """ | ||
| Calculate word error rate (WER). WER compares reference text and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--> """ Calculate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| def cer(reference, hypothesis, ignore_case=False): | ||
| """ | ||
| Calculate charactor error rate (CER). CER compares reference text and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--> """Calculate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| :param ignore_case: Whether case-sensitive or not. | ||
| :type ignore_case: bool | ||
| :return: Character error rate. | ||
| :rtype: float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add :raises ValueError: If reference length is zero.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| :param delimiter: Delimiter of input sentences. | ||
| :type delimiter: char | ||
| :return: Word error rate. | ||
| :rtype: float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add :raises ValueError: If there is zero reference words.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| @@ -0,0 +1,29 @@ | |||
| # -*- coding: utf-8 -*- | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add:
""" Test error rate."""
from future import absolute_import
from future import division
from future import print_function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| """ | ||
| This module provides functions to calculate error rate in different level. | ||
| e.g. wer for word-level, cer for char-level. | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add:
from __future__ import __absolute_import__
from __future__ import __division__
from __future__ import __print_function__
Lets keep consistent across DS2 project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| # -*- coding: utf-8 -*- | ||
| import unittest | ||
| import sys | ||
| sys.path.append('..') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we avoid using sys.path.append?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| def test_wer(self): | ||
| ref = 'i UM the PHONE IS i LEFT THE portable PHONE UPSTAIRS last night' | ||
| hyp = 'i GOT IT TO the FULLEST i LOVE TO portable FROM OF STORES last night' | ||
| word_error_rate = error_rate.wer(ref, hyp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add more test cases?
e.g.
self.assertTrue(error_rate.wer(ref, ref) == 0)
test if ValueError is raised if len(ref) == 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
1. First install dependencies;
2. Surpport abosulate import.
pkuyym
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extend ci and fix unittest following comments.
deep_speech_2/error_rate.py
Outdated
| @@ -0,0 +1,133 @@ | |||
| # -*- coding: utf-8 -*- | |||
| """ | |||
| This module provides functions to calculate error rate in different level. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| """ | ||
| This module provides functions to calculate error rate in different level. | ||
| e.g. wer for word-level, cer for char-level. | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| def wer(reference, hypothesis, ignore_case=False, delimiter=' '): | ||
| """ | ||
| Calculate word error rate (WER). WER compares reference text and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| :param delimiter: Delimiter of input sentences. | ||
| :type delimiter: char | ||
| :return: Word error rate. | ||
| :rtype: float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| :param ignore_case: Whether case-sensitive or not. | ||
| :type ignore_case: bool | ||
| :return: Character error rate. | ||
| :rtype: float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
|
|
||
| def cer(reference, hypothesis, ignore_case=False): | ||
| """ | ||
| Calculate charactor error rate (CER). CER compares reference text and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| @@ -0,0 +1,29 @@ | |||
| # -*- coding: utf-8 -*- | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| # -*- coding: utf-8 -*- | ||
| import unittest | ||
| import sys | ||
| sys.path.append('..') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| def test_wer(self): | ||
| ref = 'i UM the PHONE IS i LEFT THE portable PHONE UPSTAIRS last night' | ||
| hyp = 'i GOT IT TO the FULLEST i LOVE TO portable FROM OF STORES last night' | ||
| word_error_rate = error_rate.wer(ref, hyp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
xinghai-sun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! Almost LGTM.
deep_speech_2/error_rate.py
Outdated
| """This module provides functions to calculate error rate in different level. | ||
| e.g. wer for word-level, cer for char-level. | ||
| """ | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove Line 5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a blank line below Line 8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| try: | ||
| word_error_rate = error_rate.wer(ref, hyp) | ||
| except Exception as e: | ||
| self.assertTrue(isinstance(e, ValueError)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.assertRaises?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| try: | ||
| char_error_rate = error_rate.cer(ref, hyp) | ||
| except Exception as e: | ||
| self.assertTrue(isinstance(e, ValueError)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use assertRaises ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| :type ignore_case: bool | ||
| :return: Character error rate. | ||
| :rtype: float | ||
| :raises ValueError: If reference length is zero. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reference length --> the reference length ?
The same in the cer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| import numpy as np | ||
|
|
||
|
|
||
| def levenshtein_distance(ref, hyp): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- rename it to
_levenshtein_distance? - Add a simple description or reference for levenshtein_distance?
pkuyym
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow comments.
deep_speech_2/error_rate.py
Outdated
| """This module provides functions to calculate error rate in different level. | ||
| e.g. wer for word-level, cer for char-level. | ||
| """ | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
deep_speech_2/error_rate.py
Outdated
| :type ignore_case: bool | ||
| :return: Character error rate. | ||
| :rtype: float | ||
| :raises ValueError: If reference length is zero. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| try: | ||
| word_error_rate = error_rate.wer(ref, hyp) | ||
| except Exception as e: | ||
| self.assertTrue(isinstance(e, ValueError)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| try: | ||
| char_error_rate = error_rate.cer(ref, hyp) | ||
| except Exception as e: | ||
| self.assertTrue(isinstance(e, ValueError)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
resolves #81