Skip to content
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

[Fix] fix np.arange() output not stable error #1749

Open
wants to merge 4 commits into
base: dev-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/en/basic_concepts/evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ In MMOCR, the calculation of `HmeanIOUMetric` can be summarized as the following
It is worth noting that `pred_score_thrs` will **automatically search** for the **best threshold** within a certain range by default, and users can also customize the search range by manually modifying the configuration file:

```python
# By default, HmeanIOUMetric searches the best threshold within the range [0.3, 0.9] with a step size of 0.1
val_evaluator = dict(type='HmeanIOUMetric', pred_score_thrs=dict(start=0.3, stop=0.9, step=0.1))
# By default, HmeanIOUMetric searches the best threshold within the range [0.3, 1) with a step size of 0.1
val_evaluator = dict(type='HmeanIOUMetric', pred_score_thrs=dict(start=0.3, stop=1, step=0.1))
```

2. Calculate the IoU matrix
Expand Down
4 changes: 2 additions & 2 deletions docs/zh_cn/basic_concepts/evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ F_1=(1+\beta^2)\cdot\frac{PR}{\beta^2\cdot P+R} = \frac{2PR}{P+R}
值得注意的是,`pred_score_thrs` 默认将**自动搜索**一定范围内的**最佳阈值**,用户也可以通过手动修改配置文件来自定义搜索范围:

```python
# HmeanIOUMetric 默认以 0.1 为步长搜索 [0.3, 0.9] 范围内的最佳得分阈值
val_evaluator = dict(type='HmeanIOUMetric', pred_score_thrs=dict(start=0.3, stop=0.9, step=0.1))
# HmeanIOUMetric 默认以 0.1 为步长搜索 [0.3, 1) 范围内的最佳得分阈值
val_evaluator = dict(type='HmeanIOUMetric', pred_score_thrs=dict(start=0.3, stop=1, step=0.1))
```

2. 计算 IoU 矩阵
Expand Down
12 changes: 9 additions & 3 deletions mmocr/evaluation/metrics/hmean_iou_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class HmeanIOUMetric(BaseMetric):
ignore_precision_thr (float): Precision threshold when prediction and\
gt ignored polygons are matched. Defaults to 0.5.
pred_score_thrs (dict): Best prediction score threshold searching
space. Defaults to dict(start=0.3, stop=0.9, step=0.1).
space. Defaults to dict(start=0.3, stop=1 step=0.1).
strategy (str): Polygon matching strategy. Options are 'max_matching'
and 'vanilla'. 'max_matching' refers to the optimum strategy that
maximizes the number of matches. Vanilla strategy matches gt and
Expand All @@ -63,14 +63,20 @@ class HmeanIOUMetric(BaseMetric):
def __init__(self,
match_iou_thr: float = 0.5,
ignore_precision_thr: float = 0.5,
pred_score_thrs: Dict = dict(start=0.3, stop=0.9, step=0.1),
pred_score_thrs: Dict = dict(start=0.3, stop=1, step=0.1),
strategy: str = 'vanilla',
collect_device: str = 'cpu',
prefix: Optional[str] = None) -> None:
super().__init__(collect_device=collect_device, prefix=prefix)
self.match_iou_thr = match_iou_thr
self.ignore_precision_thr = ignore_precision_thr
self.pred_score_thrs = np.arange(**pred_score_thrs)
self.pred_score_thrs = np.linspace(
pred_score_thrs['start'],
pred_score_thrs['stop'],
int(
np.round((pred_score_thrs['stop'] - pred_score_thrs['start']) /
pred_score_thrs['step'])),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about int((pred_score_thrs['stop'] - pred_score_thrs['start']) // pred_score_thrs['step'])

Copy link
Contributor Author

@KevinNuNu KevinNuNu Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still can not include "stop" when using "start=0.3, stop=0.9, step=0.1".
Using np.linspace(start, stop, int(np.round((stop - start) / step)) + 1, endpoint=True) can fix it.

endpoint=False)
assert strategy in ['max_matching', 'vanilla']
self.strategy = strategy

Expand Down
12 changes: 9 additions & 3 deletions projects/ABCNet/abcnet/metric/e2e_hmean_iou_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class E2EHmeanIOUMetric(BaseMetric):
ignore_precision_thr (float): Precision threshold when prediction and\
gt ignored polygons are matched. Defaults to 0.5.
pred_score_thrs (dict): Best prediction score threshold searching
space. Defaults to dict(start=0.3, stop=0.9, step=0.1).
space. Defaults to dict(start=0.3, stop=1, step=0.1).
strategy (str): Polygon matching strategy. Options are 'max_matching'
and 'vanilla'. 'max_matching' refers to the optimum strategy that
maximizes the number of matches. Vanilla strategy matches gt and
Expand All @@ -64,7 +64,7 @@ class E2EHmeanIOUMetric(BaseMetric):
def __init__(self,
match_iou_thr: float = 0.5,
ignore_precision_thr: float = 0.5,
pred_score_thrs: Dict = dict(start=0.3, stop=0.9, step=0.1),
pred_score_thrs: Dict = dict(start=0.3, stop=1, step=0.1),
lexicon_path: Optional[str] = None,
word_spotting: bool = False,
min_length_case_word: int = 3,
Expand All @@ -75,7 +75,13 @@ def __init__(self,
super().__init__(collect_device=collect_device, prefix=prefix)
self.match_iou_thr = match_iou_thr
self.ignore_precision_thr = ignore_precision_thr
self.pred_score_thrs = np.arange(**pred_score_thrs)
self.pred_score_thrs = np.linspace(
pred_score_thrs['start'],
pred_score_thrs['stop'],
int(
np.round((pred_score_thrs['stop'] - pred_score_thrs['start']) /
pred_score_thrs['step'])),
endpoint=False)
self.word_spotting = word_spotting
self.min_length_case_word = min_length_case_word
self.special_characters = special_characters
Expand Down
8 changes: 7 additions & 1 deletion projects/SPTS/spts/metric/e2e_point_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ def __init__(self,
collect_device: str = 'cpu',
prefix: Optional[str] = None) -> None:
super().__init__(collect_device=collect_device, prefix=prefix)
self.text_score_thrs = np.arange(**text_score_thrs)
self.text_score_thrs = np.linspace(
text_score_thrs['start'],
text_score_thrs['stop'],
int(
np.round((text_score_thrs['stop'] - text_score_thrs['start']) /
text_score_thrs['step'])),
endpoint=False)
self.word_spotting = word_spotting

def poly_center(self, poly_pts):
Expand Down