deploy: fb8481effdf5a0b23ff86fad414906046d7620bd
Browse files- layout-underlay-effectiveness.py +63 -14
layout-underlay-effectiveness.py
CHANGED
|
@@ -16,14 +16,32 @@ Computes the ratio of valid underlay elements to total underlay elements used in
|
|
| 16 |
|
| 17 |
_KWARGS_DESCRIPTION = """\
|
| 18 |
Args:
|
| 19 |
-
predictions (`list` of `
|
| 20 |
-
gold_labels (`list` of `
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
Returns:
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
Examples:
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
"""
|
| 28 |
|
| 29 |
_CITATION = """\
|
|
@@ -41,8 +59,8 @@ _CITATION = """\
|
|
| 41 |
class LayoutUnderlayEffectiveness(evaluate.Metric):
|
| 42 |
def __init__(
|
| 43 |
self,
|
| 44 |
-
canvas_width: int,
|
| 45 |
-
canvas_height: int,
|
| 46 |
text_label_index: int = 1,
|
| 47 |
decoration_label_index: int = 3,
|
| 48 |
**kwargs,
|
|
@@ -72,20 +90,24 @@ class LayoutUnderlayEffectiveness(evaluate.Metric):
|
|
| 72 |
)
|
| 73 |
|
| 74 |
def get_rid_of_invalid(
|
| 75 |
-
self,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
) -> npt.NDArray[np.int64]:
|
| 77 |
assert len(predictions) == len(gold_labels)
|
| 78 |
|
| 79 |
-
w =
|
| 80 |
-
h =
|
| 81 |
|
| 82 |
for i, prediction in enumerate(predictions):
|
| 83 |
for j, b in enumerate(prediction):
|
| 84 |
xl, yl, xr, yr = b
|
| 85 |
xl = max(0, xl)
|
| 86 |
yl = max(0, yl)
|
| 87 |
-
xr = min(
|
| 88 |
-
yr = min(
|
| 89 |
if abs((xr - xl) * (yr - yl)) < w * h * 10:
|
| 90 |
if gold_labels[i, j]:
|
| 91 |
gold_labels[i, j] = 0
|
|
@@ -191,15 +213,42 @@ class LayoutUnderlayEffectiveness(evaluate.Metric):
|
|
| 191 |
*,
|
| 192 |
predictions: Union[npt.NDArray[np.float64], List[List[float]]],
|
| 193 |
gold_labels: Union[npt.NDArray[np.int64], List[int]],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
) -> Dict[str, float]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
predictions = np.array(predictions)
|
| 196 |
gold_labels = np.array(gold_labels)
|
| 197 |
|
| 198 |
-
predictions[:, :, ::2] *=
|
| 199 |
-
predictions[:, :, 1::2] *=
|
| 200 |
|
| 201 |
gold_labels = self.get_rid_of_invalid(
|
| 202 |
-
predictions=predictions,
|
|
|
|
|
|
|
|
|
|
| 203 |
)
|
| 204 |
return {
|
| 205 |
"und_l": self._compute_und_l(
|
|
|
|
| 16 |
|
| 17 |
_KWARGS_DESCRIPTION = """\
|
| 18 |
Args:
|
| 19 |
+
predictions (`list` of `list` of `float`): A list of lists of floats representing normalized `ltrb`-format bounding boxes.
|
| 20 |
+
gold_labels (`list` of `list` of `int`): A list of lists of integers representing class labels.
|
| 21 |
+
canvas_width (`int`, *optional*): Width of the canvas in pixels. Can be provided at initialization or during computation.
|
| 22 |
+
canvas_height (`int`, *optional*): Height of the canvas in pixels. Can be provided at initialization or during computation.
|
| 23 |
+
text_label_index (`int`, *optional*, defaults to 1): The label index for text elements.
|
| 24 |
+
decoration_label_index (`int`, *optional*, defaults to 3): The label index for decoration (underlay) elements.
|
| 25 |
|
| 26 |
Returns:
|
| 27 |
+
dict: A dictionary containing two underlay effectiveness metrics:
|
| 28 |
+
- `und_l` (loose): The average ratio of intersection area between underlay and other elements to underlay area. Higher values indicate better underlay effectiveness.
|
| 29 |
+
- `und_s` (strict): The ratio of underlay elements that completely contain at least one non-underlay element. Higher values indicate better underlay effectiveness.
|
| 30 |
|
| 31 |
Examples:
|
| 32 |
+
>>> import evaluate
|
| 33 |
+
>>> metric = evaluate.load("creative-graphic-design/layout-underlay-effectiveness")
|
| 34 |
+
>>> # Underlay box with text box on top
|
| 35 |
+
>>> predictions = [[[0.1, 0.1, 0.5, 0.5], [0.2, 0.2, 0.4, 0.4]]]
|
| 36 |
+
>>> gold_labels = [[3, 1]] # 3 is decoration (underlay), 1 is text
|
| 37 |
+
>>> result = metric.compute(
|
| 38 |
+
... predictions=predictions,
|
| 39 |
+
... gold_labels=gold_labels,
|
| 40 |
+
... canvas_width=512,
|
| 41 |
+
... canvas_height=512
|
| 42 |
+
... )
|
| 43 |
+
>>> print(f"Loose underlay effectiveness: {result['und_l']:.4f}")
|
| 44 |
+
>>> print(f"Strict underlay effectiveness: {result['und_s']:.4f}")
|
| 45 |
"""
|
| 46 |
|
| 47 |
_CITATION = """\
|
|
|
|
| 59 |
class LayoutUnderlayEffectiveness(evaluate.Metric):
|
| 60 |
def __init__(
|
| 61 |
self,
|
| 62 |
+
canvas_width: int | None = None,
|
| 63 |
+
canvas_height: int | None = None,
|
| 64 |
text_label_index: int = 1,
|
| 65 |
decoration_label_index: int = 3,
|
| 66 |
**kwargs,
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
def get_rid_of_invalid(
|
| 93 |
+
self,
|
| 94 |
+
predictions: npt.NDArray[np.float64],
|
| 95 |
+
gold_labels: npt.NDArray[np.int64],
|
| 96 |
+
canvas_width: int,
|
| 97 |
+
canvas_height: int,
|
| 98 |
) -> npt.NDArray[np.int64]:
|
| 99 |
assert len(predictions) == len(gold_labels)
|
| 100 |
|
| 101 |
+
w = canvas_width / 100
|
| 102 |
+
h = canvas_height / 100
|
| 103 |
|
| 104 |
for i, prediction in enumerate(predictions):
|
| 105 |
for j, b in enumerate(prediction):
|
| 106 |
xl, yl, xr, yr = b
|
| 107 |
xl = max(0, xl)
|
| 108 |
yl = max(0, yl)
|
| 109 |
+
xr = min(canvas_width, xr)
|
| 110 |
+
yr = min(canvas_height, yr)
|
| 111 |
if abs((xr - xl) * (yr - yl)) < w * h * 10:
|
| 112 |
if gold_labels[i, j]:
|
| 113 |
gold_labels[i, j] = 0
|
|
|
|
| 213 |
*,
|
| 214 |
predictions: Union[npt.NDArray[np.float64], List[List[float]]],
|
| 215 |
gold_labels: Union[npt.NDArray[np.int64], List[int]],
|
| 216 |
+
canvas_width: int | None = None,
|
| 217 |
+
canvas_height: int | None = None,
|
| 218 |
+
text_label_index: int | None = None,
|
| 219 |
+
decoration_label_index: int | None = None,
|
| 220 |
) -> Dict[str, float]:
|
| 221 |
+
# パラメータの優先順位処理
|
| 222 |
+
canvas_width = canvas_width if canvas_width is not None else self.canvas_width
|
| 223 |
+
canvas_height = (
|
| 224 |
+
canvas_height if canvas_height is not None else self.canvas_height
|
| 225 |
+
)
|
| 226 |
+
text_label_index = (
|
| 227 |
+
text_label_index if text_label_index is not None else self.text_label_index
|
| 228 |
+
)
|
| 229 |
+
decoration_label_index = (
|
| 230 |
+
decoration_label_index
|
| 231 |
+
if decoration_label_index is not None
|
| 232 |
+
else self.decoration_label_index
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
if canvas_width is None or canvas_height is None:
|
| 236 |
+
raise ValueError(
|
| 237 |
+
"canvas_width and canvas_height must be provided either "
|
| 238 |
+
"at initialization or during computation"
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
predictions = np.array(predictions)
|
| 242 |
gold_labels = np.array(gold_labels)
|
| 243 |
|
| 244 |
+
predictions[:, :, ::2] *= canvas_width
|
| 245 |
+
predictions[:, :, 1::2] *= canvas_height
|
| 246 |
|
| 247 |
gold_labels = self.get_rid_of_invalid(
|
| 248 |
+
predictions=predictions,
|
| 249 |
+
gold_labels=gold_labels,
|
| 250 |
+
canvas_width=canvas_width,
|
| 251 |
+
canvas_height=canvas_height,
|
| 252 |
)
|
| 253 |
return {
|
| 254 |
"und_l": self._compute_und_l(
|