shunk031 commited on
Commit
37a848c
Β·
1 Parent(s): edeb60c

deploy: fc0c10e734116107123b6dce81a6df2cbbf84dfe

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. layout-non-alignment.py +152 -0
  3. requirements.txt +89 -0
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Layout Non Alignment
3
  emoji: 🌍
4
  colorFrom: blue
5
  colorTo: green
 
1
  ---
2
+ title: Layout Non-Alignment
3
  emoji: 🌍
4
  colorFrom: blue
5
  colorTo: green
layout-non-alignment.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import copy
2
+ import math
3
+ from typing import List, Union
4
+
5
+ import datasets as ds
6
+ import evaluate
7
+ import numpy as np
8
+ import numpy.typing as npt
9
+
10
+ _DESCRIPTION = r"""\
11
+ Computes the extent of spatial non-alignment between elements.
12
+ """
13
+
14
+ _KWARGS_DESCRIPTION = """\
15
+ FIXME
16
+ """
17
+
18
+ _CITATION = """\
19
+ @inproceedings{hsu2023posterlayout,
20
+ title={Posterlayout: A new benchmark and approach for content-aware visual-textual presentation layout},
21
+ author={Hsu, Hsiao Yuan and He, Xiangteng and Peng, Yuxin and Kong, Hao and Zhang, Qing},
22
+ booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
23
+ pages={6018--6026},
24
+ year={2023}
25
+ }
26
+
27
+ @article{li2020attribute,
28
+ title={Attribute-conditioned layout gan for automatic graphic design},
29
+ author={Li, Jianan and Yang, Jimei and Zhang, Jianming and Liu, Chang and Wang, Christina and Xu, Tingfa},
30
+ journal={IEEE Transactions on Visualization and Computer Graphics},
31
+ volume={27},
32
+ number={10},
33
+ pages={4039--4048},
34
+ year={2020},
35
+ publisher={IEEE}
36
+ }
37
+ """
38
+
39
+
40
+ class LayoutNonAlignment(evaluate.Metric):
41
+ def __init__(
42
+ self,
43
+ canvas_width: int,
44
+ canvas_height: int,
45
+ **kwargs,
46
+ ) -> None:
47
+ super().__init__(**kwargs)
48
+ self.canvas_width = canvas_width
49
+ self.canvas_height = canvas_height
50
+
51
+ def _info(self) -> evaluate.EvaluationModuleInfo:
52
+ return evaluate.MetricInfo(
53
+ description=_DESCRIPTION,
54
+ citation=_CITATION,
55
+ inputs_description=_KWARGS_DESCRIPTION,
56
+ features=ds.Features(
57
+ {
58
+ "predictions": ds.Sequence(ds.Sequence(ds.Value("float64"))),
59
+ "gold_labels": ds.Sequence(ds.Sequence(ds.Value("int64"))),
60
+ }
61
+ ),
62
+ codebase_urls=[
63
+ "https://github.com/PKU-ICST-MIPL/PosterLayout-CVPR2023/blob/main/eval.py#L306-L339"
64
+ ],
65
+ )
66
+
67
+ def ali_delta(self, xs: npt.NDArray[np.float64]) -> float:
68
+ n = len(xs)
69
+ min_delta = np.inf
70
+ for i in range(n):
71
+ for j in range(i + 1, n):
72
+ delta = abs(xs[i] - xs[j])
73
+ min_delta = min(min_delta, delta)
74
+ return min_delta
75
+
76
+ def ali_g(self, x: float) -> float:
77
+ return -math.log(1 - x, 10)
78
+
79
+ def get_rid_of_invalid(
80
+ self, predictions: npt.NDArray[np.float64], gold_labels: npt.NDArray[np.int64]
81
+ ) -> npt.NDArray[np.int64]:
82
+ assert len(predictions) == len(gold_labels)
83
+
84
+ w = self.canvas_width / 100
85
+ h = self.canvas_height / 100
86
+
87
+ for i, prediction in enumerate(predictions):
88
+ for j, b in enumerate(prediction):
89
+ xl, yl, xr, yr = b
90
+ xl = max(0, xl)
91
+ yl = max(0, yl)
92
+ xr = min(self.canvas_width, xr)
93
+ yr = min(self.canvas_height, yr)
94
+ if abs((xr - xl) * (yr - yl)) < w * h * 10:
95
+ if gold_labels[i, j]:
96
+ gold_labels[i, j] = 0
97
+ return gold_labels
98
+
99
+ def _compute(
100
+ self,
101
+ *,
102
+ predictions: Union[npt.NDArray[np.float64], List[List[float]]],
103
+ gold_labels: Union[npt.NDArray[np.int64], List[int]],
104
+ ) -> float:
105
+ predictions = np.array(predictions)
106
+ gold_labels = np.array(gold_labels)
107
+
108
+ predictions[:, :, ::2] *= self.canvas_width
109
+ predictions[:, :, 1::2] *= self.canvas_height
110
+
111
+ gold_labels = self.get_rid_of_invalid(
112
+ predictions=predictions, gold_labels=gold_labels
113
+ )
114
+
115
+ metrics: float = 0.0
116
+ for gold_label, prediction in zip(gold_labels, predictions):
117
+ ali = 0.0
118
+ mask = (gold_label > 0).reshape(-1)
119
+ mask_box = prediction[mask]
120
+
121
+ theda = []
122
+ for mb in mask_box:
123
+ pos = copy.deepcopy(mb)
124
+ pos[0] /= self.canvas_width
125
+ pos[2] /= self.canvas_width
126
+ pos[1] /= self.canvas_height
127
+ pos[3] /= self.canvas_height
128
+ theda.append(
129
+ [
130
+ pos[0],
131
+ pos[1],
132
+ (pos[0] + pos[2]) / 2,
133
+ (pos[1] + pos[3]) / 2,
134
+ pos[2],
135
+ pos[3],
136
+ ]
137
+ )
138
+ theda_arr = np.array(theda)
139
+ if theda_arr.shape[0] <= 1:
140
+ continue
141
+
142
+ n = len(mask_box)
143
+ for _ in range(n):
144
+ g_val = []
145
+ for j in range(6):
146
+ xys = theda_arr[:, j]
147
+ delta = self.ali_delta(xys)
148
+ g_val.append(self.ali_g(delta))
149
+ ali += min(g_val)
150
+ metrics += ali
151
+
152
+ return metrics / len(gold_labels)
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"