shunk031 commited on
Commit
bae937d
1 Parent(s): 266f9fb

deploy: fc0c10e734116107123b6dce81a6df2cbbf84dfe

Browse files
Files changed (3) hide show
  1. README.md +0 -1
  2. layout-underlay-effectiveness.py +194 -0
  3. requirements.txt +89 -0
README.md CHANGED
@@ -8,5 +8,4 @@ sdk_version: 4.36.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
-
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
8
  app_file: app.py
9
  pinned: false
10
  ---
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
layout-underlay-effectiveness.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Union
2
+
3
+ import datasets as ds
4
+ import evaluate
5
+ import numpy as np
6
+ import numpy.typing as npt
7
+
8
+ _DESCRIPTION = r"""\
9
+ Computes the non-flatness of regions that text elements are solely put on, referring to CGL-GAN.
10
+
11
+ Computes the ratio of valid underlay elements to total underlay elements used in PosterLayout. Intuitively, underlay should be placed under other non-underlay elements.
12
+ - strict: scoring the underlay as:
13
+ - 1: there is a non-underlay element completely inside
14
+ - 0: otherwise
15
+ - loose: Calcurate (ai/a2).
16
+ """
17
+
18
+ _KWARGS_DESCRIPTION = """\
19
+ FIXME
20
+ """
21
+
22
+ _CITATION = """\
23
+ @inproceedings{hsu2023posterlayout,
24
+ title={Posterlayout: A new benchmark and approach for content-aware visual-textual presentation layout},
25
+ author={Hsu, Hsiao Yuan and He, Xiangteng and Peng, Yuxin and Kong, Hao and Zhang, Qing},
26
+ booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
27
+ pages={6018--6026},
28
+ year={2023}
29
+ }
30
+ """
31
+
32
+
33
+ class LayoutUnderlayEffectiveness(evaluate.Metric):
34
+ def __init__(
35
+ self,
36
+ canvas_width: int,
37
+ canvas_height: int,
38
+ text_label_index: int = 1,
39
+ decoration_label_index: int = 3,
40
+ **kwargs,
41
+ ) -> None:
42
+ super().__init__(**kwargs)
43
+ self.canvas_width = canvas_width
44
+ self.canvas_height = canvas_height
45
+
46
+ self.text_label_index = text_label_index
47
+ self.decoration_label_index = decoration_label_index
48
+
49
+ def _info(self) -> evaluate.EvaluationModuleInfo:
50
+ return evaluate.MetricInfo(
51
+ description=_DESCRIPTION,
52
+ citation=_CITATION,
53
+ inputs_description=_KWARGS_DESCRIPTION,
54
+ features=ds.Features(
55
+ {
56
+ "predictions": ds.Sequence(ds.Sequence(ds.Value("float64"))),
57
+ "gold_labels": ds.Sequence(ds.Sequence(ds.Value("int64"))),
58
+ }
59
+ ),
60
+ codebase_urls=[
61
+ "https://github.com/PKU-ICST-MIPL/PosterLayout-CVPR2023/blob/main/eval.py#L224-L252",
62
+ "https://github.com/PKU-ICST-MIPL/PosterLayout-CVPR2023/blob/main/eval.py#L265-L292",
63
+ ],
64
+ )
65
+
66
+ def get_rid_of_invalid(
67
+ self, predictions: npt.NDArray[np.float64], gold_labels: npt.NDArray[np.int64]
68
+ ) -> npt.NDArray[np.int64]:
69
+ assert len(predictions) == len(gold_labels)
70
+
71
+ w = self.canvas_width / 100
72
+ h = self.canvas_height / 100
73
+
74
+ for i, prediction in enumerate(predictions):
75
+ for j, b in enumerate(prediction):
76
+ xl, yl, xr, yr = b
77
+ xl = max(0, xl)
78
+ yl = max(0, yl)
79
+ xr = min(self.canvas_width, xr)
80
+ yr = min(self.canvas_height, yr)
81
+ if abs((xr - xl) * (yr - yl)) < w * h * 10:
82
+ if gold_labels[i, j]:
83
+ gold_labels[i, j] = 0
84
+ return gold_labels
85
+
86
+ def metrics_inter_oneside(self, bb1, bb2):
87
+ xl_1, yl_1, xr_1, yr_1 = bb1
88
+ xl_2, yl_2, xr_2, yr_2 = bb2
89
+
90
+ # w_1 = xr_1 - xl_1
91
+ w_2 = xr_2 - xl_2
92
+ # h_1 = yr_1 - yl_1
93
+ h_2 = yr_2 - yl_2
94
+
95
+ w_inter = min(xr_1, xr_2) - max(xl_1, xl_2)
96
+ h_inter = min(yr_1, yr_2) - max(yl_1, yl_2)
97
+
98
+ # a_1 = w_1 * h_1
99
+ a_2 = w_2 * h_2
100
+ a_inter = w_inter * h_inter
101
+ if w_inter <= 0 or h_inter <= 0:
102
+ a_inter = 0
103
+
104
+ return a_inter / a_2
105
+
106
+ def _compute_und_l(
107
+ self, predictions: npt.NDArray[np.float64], gold_labels: npt.NDArray[np.int64]
108
+ ) -> float:
109
+ metrics, avali = 0.0, 0
110
+
111
+ for gold_label, prediction in zip(gold_labels, predictions):
112
+ und = 0
113
+ mask_deco = (gold_label == 3).reshape(-1)
114
+ mask_other = (gold_label > 0).reshape(-1) & (gold_label != 3).reshape(-1)
115
+ box_deco = prediction[mask_deco]
116
+ box_other = prediction[mask_other]
117
+ n1, n2 = len(box_deco), len(box_other)
118
+ if not n1:
119
+ continue
120
+
121
+ avali += 1
122
+ for i in range(n1):
123
+ max_ios = 0
124
+ bb1 = box_deco[i]
125
+ for j in range(n2):
126
+ bb2 = box_other[j]
127
+ ios = self.metrics_inter_oneside(bb1, bb2)
128
+ max_ios = max(max_ios, ios)
129
+ und += max_ios
130
+ metrics += und / n1
131
+
132
+ return metrics / avali if avali > 0 else 0.0
133
+
134
+ def _compute_und_s(
135
+ self, predictions: npt.NDArray[np.float64], gold_labels: npt.NDArray[np.int64]
136
+ ) -> float:
137
+ def is_contain(bb1, bb2):
138
+ xl_1, yl_1, xr_1, yr_1 = bb1
139
+ xl_2, yl_2, xr_2, yr_2 = bb2
140
+
141
+ c1 = xl_1 <= xl_2
142
+ c2 = yl_1 <= yl_2
143
+ c3 = xr_2 >= xr_2
144
+ c4 = yr_1 >= yr_2
145
+
146
+ return c1 and c2 and c3 and c4
147
+
148
+ metrics, avali = 0.0, 0
149
+
150
+ for gold_label, prediction in zip(gold_labels, predictions):
151
+ und = 0
152
+ mask_deco = (gold_label == 3).reshape(-1)
153
+ mask_other = (gold_label > 0).reshape(-1) & (gold_label != 3).reshape(-1)
154
+ box_deco = prediction[mask_deco]
155
+ box_other = prediction[mask_other]
156
+ n1, n2 = len(box_deco), len(box_other)
157
+ if not n1:
158
+ continue
159
+
160
+ avali += 1
161
+ for i in range(n1):
162
+ bb1 = box_deco[i]
163
+ for j in range(n2):
164
+ bb2 = box_other[j]
165
+ if is_contain(bb1, bb2):
166
+ und += 1
167
+ break
168
+ metrics += und / n1
169
+
170
+ return metrics / avali if avali > 0 else 0.0
171
+
172
+ def _compute(
173
+ self,
174
+ *,
175
+ predictions: Union[npt.NDArray[np.float64], List[List[float]]],
176
+ gold_labels: Union[npt.NDArray[np.int64], List[int]],
177
+ ) -> Dict[str, float]:
178
+ predictions = np.array(predictions)
179
+ gold_labels = np.array(gold_labels)
180
+
181
+ predictions[:, :, ::2] *= self.canvas_width
182
+ predictions[:, :, 1::2] *= self.canvas_height
183
+
184
+ gold_labels = self.get_rid_of_invalid(
185
+ predictions=predictions, gold_labels=gold_labels
186
+ )
187
+ return {
188
+ "und_l": self._compute_und_l(
189
+ predictions=predictions, gold_labels=gold_labels
190
+ ),
191
+ "und_s": self._compute_und_s(
192
+ predictions=predictions, gold_labels=gold_labels
193
+ ),
194
+ }
requirements.txt ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1 ; python_version >= "3.9" and python_version < "4.0"
2
+ aiohttp==3.9.3 ; python_version >= "3.9" and python_version < "4.0"
3
+ aiosignal==1.3.1 ; python_version >= "3.9" and python_version < "4.0"
4
+ altair==5.2.0 ; python_version >= "3.9" and python_version < "4.0"
5
+ annotated-types==0.6.0 ; python_version >= "3.9" and python_version < "4.0"
6
+ anyio==4.2.0 ; python_version >= "3.9" and python_version < "4.0"
7
+ arrow==1.3.0 ; python_version >= "3.9" and python_version < "4.0"
8
+ async-timeout==4.0.3 ; python_version >= "3.9" and python_version < "3.11"
9
+ attrs==23.2.0 ; python_version >= "3.9" and python_version < "4.0"
10
+ binaryornot==0.4.4 ; python_version >= "3.9" and python_version < "4.0"
11
+ certifi==2024.2.2 ; python_version >= "3.9" and python_version < "4.0"
12
+ chardet==5.2.0 ; python_version >= "3.9" and python_version < "4.0"
13
+ charset-normalizer==3.3.2 ; python_version >= "3.9" and python_version < "4.0"
14
+ click==8.1.7 ; python_version >= "3.9" and python_version < "4.0"
15
+ colorama==0.4.6 ; python_version >= "3.9" and python_version < "4.0"
16
+ contourpy==1.2.0 ; python_version >= "3.9" and python_version < "4.0"
17
+ cookiecutter==2.5.0 ; python_version >= "3.9" and python_version < "4.0"
18
+ cycler==0.12.1 ; python_version >= "3.9" and python_version < "4.0"
19
+ datasets==2.17.0 ; python_version >= "3.9" and python_version < "4.0"
20
+ dill==0.3.8 ; python_version >= "3.9" and python_version < "4.0"
21
+ evaluate[template]==0.4.1 ; python_version >= "3.9" and python_version < "4.0"
22
+ exceptiongroup==1.2.0 ; python_version >= "3.9" and python_version < "3.11"
23
+ fastapi==0.109.2 ; python_version >= "3.9" and python_version < "4.0"
24
+ ffmpy==0.3.1 ; python_version >= "3.9" and python_version < "4.0"
25
+ filelock==3.13.1 ; python_version >= "3.9" and python_version < "4.0"
26
+ fonttools==4.48.1 ; python_version >= "3.9" and python_version < "4.0"
27
+ frozenlist==1.4.1 ; python_version >= "3.9" and python_version < "4.0"
28
+ fsspec==2023.10.0 ; python_version >= "3.9" and python_version < "4.0"
29
+ fsspec[http]==2023.10.0 ; python_version >= "3.9" and python_version < "4.0"
30
+ gradio-client==0.10.0 ; python_version >= "3.9" and python_version < "4.0"
31
+ gradio==4.18.0 ; python_version >= "3.9" and python_version < "4.0"
32
+ h11==0.14.0 ; python_version >= "3.9" and python_version < "4.0"
33
+ httpcore==1.0.2 ; python_version >= "3.9" and python_version < "4.0"
34
+ httpx==0.26.0 ; python_version >= "3.9" and python_version < "4.0"
35
+ huggingface-hub==0.20.3 ; python_version >= "3.9" and python_version < "4.0"
36
+ idna==3.6 ; python_version >= "3.9" and python_version < "4.0"
37
+ importlib-resources==6.1.1 ; python_version >= "3.9" and python_version < "4.0"
38
+ jinja2==3.1.3 ; python_version >= "3.9" and python_version < "4.0"
39
+ jsonschema-specifications==2023.12.1 ; python_version >= "3.9" and python_version < "4.0"
40
+ jsonschema==4.21.1 ; python_version >= "3.9" and python_version < "4.0"
41
+ kiwisolver==1.4.5 ; python_version >= "3.9" and python_version < "4.0"
42
+ markdown-it-py==3.0.0 ; python_version >= "3.9" and python_version < "4.0"
43
+ markupsafe==2.1.5 ; python_version >= "3.9" and python_version < "4.0"
44
+ matplotlib==3.8.2 ; python_version >= "3.9" and python_version < "4.0"
45
+ mdurl==0.1.2 ; python_version >= "3.9" and python_version < "4.0"
46
+ multidict==6.0.5 ; python_version >= "3.9" and python_version < "4.0"
47
+ multiprocess==0.70.16 ; python_version >= "3.9" and python_version < "4.0"
48
+ numpy==1.26.4 ; python_version >= "3.9" and python_version < "4.0"
49
+ orjson==3.9.13 ; python_version >= "3.9" and python_version < "4.0"
50
+ packaging==23.2 ; python_version >= "3.9" and python_version < "4.0"
51
+ pandas==2.2.0 ; python_version >= "3.9" and python_version < "4.0"
52
+ pillow==10.2.0 ; python_version >= "3.9" and python_version < "4.0"
53
+ pyarrow-hotfix==0.6 ; python_version >= "3.9" and python_version < "4.0"
54
+ pyarrow==15.0.0 ; python_version >= "3.9" and python_version < "4.0"
55
+ pydantic-core==2.16.2 ; python_version >= "3.9" and python_version < "4.0"
56
+ pydantic==2.6.1 ; python_version >= "3.9" and python_version < "4.0"
57
+ pydub==0.25.1 ; python_version >= "3.9" and python_version < "4.0"
58
+ pygments==2.17.2 ; python_version >= "3.9" and python_version < "4.0"
59
+ pyparsing==3.1.1 ; python_version >= "3.9" and python_version < "4.0"
60
+ python-dateutil==2.8.2 ; python_version >= "3.9" and python_version < "4.0"
61
+ python-multipart==0.0.9 ; python_version >= "3.9" and python_version < "4.0"
62
+ python-slugify==8.0.4 ; python_version >= "3.9" and python_version < "4.0"
63
+ pytz==2024.1 ; python_version >= "3.9" and python_version < "4.0"
64
+ pyyaml==6.0.1 ; python_version >= "3.9" and python_version < "4.0"
65
+ referencing==0.33.0 ; python_version >= "3.9" and python_version < "4.0"
66
+ requests==2.31.0 ; python_version >= "3.9" and python_version < "4.0"
67
+ responses==0.18.0 ; python_version >= "3.9" and python_version < "4.0"
68
+ rich==13.7.0 ; python_version >= "3.9" and python_version < "4.0"
69
+ rpds-py==0.17.1 ; python_version >= "3.9" and python_version < "4.0"
70
+ ruff==0.2.1 ; python_version >= "3.9" and python_version < "4.0"
71
+ semantic-version==2.10.0 ; python_version >= "3.9" and python_version < "4.0"
72
+ shellingham==1.5.4 ; python_version >= "3.9" and python_version < "4.0"
73
+ six==1.16.0 ; python_version >= "3.9" and python_version < "4.0"
74
+ sniffio==1.3.0 ; python_version >= "3.9" and python_version < "4.0"
75
+ starlette==0.36.3 ; python_version >= "3.9" and python_version < "4.0"
76
+ text-unidecode==1.3 ; python_version >= "3.9" and python_version < "4.0"
77
+ tomlkit==0.12.0 ; python_version >= "3.9" and python_version < "4.0"
78
+ toolz==0.12.1 ; python_version >= "3.9" and python_version < "4.0"
79
+ tqdm==4.66.2 ; python_version >= "3.9" and python_version < "4.0"
80
+ typer[all]==0.9.0 ; python_version >= "3.9" and python_version < "4.0"
81
+ types-python-dateutil==2.8.19.20240106 ; python_version >= "3.9" and python_version < "4.0"
82
+ typing-extensions==4.9.0 ; python_version >= "3.9" and python_version < "4.0"
83
+ tzdata==2024.1 ; python_version >= "3.9" and python_version < "4.0"
84
+ urllib3==2.2.0 ; python_version >= "3.9" and python_version < "4.0"
85
+ uvicorn==0.27.1 ; python_version >= "3.9" and python_version < "4.0"
86
+ websockets==11.0.3 ; python_version >= "3.9" and python_version < "4.0"
87
+ xxhash==3.4.1 ; python_version >= "3.9" and python_version < "4.0"
88
+ yarl==1.9.4 ; python_version >= "3.9" and python_version < "4.0"
89
+ zipp==3.17.0 ; python_version >= "3.9" and python_version < "3.10"