shunk031 commited on
Commit
c6b4e85
·
1 Parent(s): b21f772

deploy: fb8481effdf5a0b23ff86fad414906046d7620bd

Browse files
Files changed (1) hide show
  1. layout-validity.py +41 -9
layout-validity.py CHANGED
@@ -11,7 +11,25 @@ Computes the ratio of valid elements to all elements in the layout, where the ar
11
  """
12
 
13
  _KWARGS_DESCRIPTION = """\
14
- FIXME
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  """
16
 
17
  _CITATION = """\
@@ -29,8 +47,8 @@ _CITATION = """\
29
  class LayoutValidity(evaluate.Metric):
30
  def __init__(
31
  self,
32
- canvas_width: int,
33
- canvas_height: int,
34
  **kwargs,
35
  ) -> None:
36
  super().__init__(**kwargs)
@@ -58,17 +76,31 @@ class LayoutValidity(evaluate.Metric):
58
  *,
59
  predictions: Union[npt.NDArray[np.float64], List[List[float]]],
60
  gold_labels: Union[npt.NDArray[np.int64], List[int]],
 
 
61
  ) -> float:
 
 
 
 
 
 
 
 
 
 
 
 
62
  predictions = np.array(predictions)
63
  gold_labels = np.array(gold_labels)
64
 
65
- predictions[:, :, ::2] *= self.canvas_width
66
- predictions[:, :, 1::2] *= self.canvas_height
67
 
68
  total_elements, empty_elements = 0, 0
69
 
70
- w = self.canvas_width / 100
71
- h = self.canvas_height / 100
72
 
73
  assert len(predictions) == len(gold_labels)
74
 
@@ -80,8 +112,8 @@ class LayoutValidity(evaluate.Metric):
80
  xl, yl, xr, yr = mp
81
  xl = max(0, xl)
82
  yl = max(0, yl)
83
- xr = min(self.canvas_width, xr)
84
- yr = min(self.canvas_height, yr)
85
 
86
  if abs((xr - xl) * (yr - yl)) < w * h * 10:
87
  empty_elements += 1
 
11
  """
12
 
13
  _KWARGS_DESCRIPTION = """\
14
+ Args:
15
+ predictions (`list` of `list` of `float`): A list of lists of floats representing normalized `ltrb`-format bounding boxes.
16
+ gold_labels (`list` of `list` of `int`): A list of lists of integers representing class labels.
17
+ canvas_width (`int`, *optional*): Width of the canvas in pixels. Can be provided at initialization or during computation.
18
+ canvas_height (`int`, *optional*): Height of the canvas in pixels. Can be provided at initialization or during computation.
19
+
20
+ Returns:
21
+ float: The ratio of valid elements to all elements (0.0 to 1.0). An element is considered valid if its area within the canvas is greater than 0.1% of the canvas area.
22
+
23
+ Examples:
24
+ >>> import evaluate
25
+ >>> import numpy as np
26
+ >>> metric = evaluate.load("creative-graphic-design/layout-validity")
27
+ >>> # Normalized bounding boxes (left, top, right, bottom)
28
+ >>> predictions = [[[0.1, 0.1, 0.5, 0.5], [0.6, 0.6, 0.9, 0.9]]]
29
+ >>> gold_labels = [[1, 2]] # Non-zero labels indicate valid elements
30
+ >>> result = metric.compute(predictions=predictions, gold_labels=gold_labels, canvas_width=512, canvas_height=512)
31
+ >>> print(result)
32
+ 1.0
33
  """
34
 
35
  _CITATION = """\
 
47
  class LayoutValidity(evaluate.Metric):
48
  def __init__(
49
  self,
50
+ canvas_width: int | None = None,
51
+ canvas_height: int | None = None,
52
  **kwargs,
53
  ) -> None:
54
  super().__init__(**kwargs)
 
76
  *,
77
  predictions: Union[npt.NDArray[np.float64], List[List[float]]],
78
  gold_labels: Union[npt.NDArray[np.int64], List[int]],
79
+ canvas_width: int | None = None,
80
+ canvas_height: int | None = None,
81
  ) -> float:
82
+ # パラメータの優先順位処理
83
+ canvas_width = canvas_width if canvas_width is not None else self.canvas_width
84
+ canvas_height = (
85
+ canvas_height if canvas_height is not None else self.canvas_height
86
+ )
87
+
88
+ if canvas_width is None or canvas_height is None:
89
+ raise ValueError(
90
+ "canvas_width and canvas_height must be provided either "
91
+ "at initialization or during computation"
92
+ )
93
+
94
  predictions = np.array(predictions)
95
  gold_labels = np.array(gold_labels)
96
 
97
+ predictions[:, :, ::2] *= canvas_width
98
+ predictions[:, :, 1::2] *= canvas_height
99
 
100
  total_elements, empty_elements = 0, 0
101
 
102
+ w = canvas_width / 100
103
+ h = canvas_height / 100
104
 
105
  assert len(predictions) == len(gold_labels)
106
 
 
112
  xl, yl, xr, yr = mp
113
  xl = max(0, xl)
114
  yl = max(0, yl)
115
+ xr = min(canvas_width, xr)
116
+ yr = min(canvas_height, yr)
117
 
118
  if abs((xr - xl) * (yr - yl)) < w * h * 10:
119
  empty_elements += 1