shunk031 commited on
Commit
0dee3bc
·
verified ·
1 Parent(s): 47f7742

Delete layout_overlap.py

Browse files
Files changed (1) hide show
  1. layout_overlap.py +0 -184
layout_overlap.py DELETED
@@ -1,184 +0,0 @@
1
- from typing import Dict, List, Tuple, TypedDict, Union
2
-
3
- import datasets as ds
4
- import evaluate
5
- import numpy as np
6
- import numpy.typing as npt
7
-
8
- _DESCRIPTION = """\
9
- Some overlap metrics that are different to each other in previous works.
10
- """
11
-
12
- _CITATION = """\
13
- @inproceedings{li2018layoutgan,
14
- title={LayoutGAN: Generating Graphic Layouts with Wireframe Discriminators},
15
- author={Li, Jianan and Yang, Jimei and Hertzmann, Aaron and Zhang, Jianming and Xu, Tingfa},
16
- booktitle={International Conference on Learning Representations},
17
- year={2019}
18
- }
19
-
20
- @article{li2020attribute,
21
- title={Attribute-conditioned layout gan for automatic graphic design},
22
- author={Li, Jianan and Yang, Jimei and Zhang, Jianming and Liu, Chang and Wang, Christina and Xu, Tingfa},
23
- journal={IEEE Transactions on Visualization and Computer Graphics},
24
- volume={27},
25
- number={10},
26
- pages={4039--4048},
27
- year={2020},
28
- publisher={IEEE}
29
- }
30
-
31
- @inproceedings{kikuchi2021constrained,
32
- title={Constrained graphic layout generation via latent optimization},
33
- author={Kikuchi, Kotaro and Simo-Serra, Edgar and Otani, Mayu and Yamaguchi, Kota},
34
- booktitle={Proceedings of the 29th ACM International Conference on Multimedia},
35
- pages={88--96},
36
- year={2021}
37
- }
38
- """
39
-
40
-
41
- def convert_xywh_to_ltrb(
42
- batch_bbox: npt.NDArray[np.float64],
43
- ) -> Tuple[
44
- npt.NDArray[np.float64],
45
- npt.NDArray[np.float64],
46
- npt.NDArray[np.float64],
47
- npt.NDArray[np.float64],
48
- ]:
49
- xc, yc, w, h = batch_bbox
50
- x1 = xc - w / 2
51
- y1 = yc - h / 2
52
- x2 = xc + w / 2
53
- y2 = yc + h / 2
54
- return (x1, y1, x2, y2)
55
-
56
-
57
- class A(TypedDict):
58
- a1: npt.NDArray[np.float64]
59
- ai: npt.NDArray[np.float64]
60
-
61
-
62
- class LayoutOverlap(evaluate.Metric):
63
- def _info(self) -> evaluate.EvaluationModuleInfo:
64
- return evaluate.MetricInfo(
65
- description=_DESCRIPTION,
66
- citation=_CITATION,
67
- features=ds.Features(
68
- {
69
- "bbox": ds.Sequence(ds.Sequence(ds.Value("float64"))),
70
- "mask": ds.Sequence(ds.Value("bool")),
71
- }
72
- ),
73
- codebase_urls=[
74
- "https://github.com/ktrk115/const_layout/blob/master/metric.py#L138-L164",
75
- "https://github.com/CyberAgentAILab/layout-dm/blob/main/src/trainer/trainer/helpers/metric.py#L150-L203",
76
- ],
77
- )
78
-
79
- def __calculate_a1_ai(self, batch_bbox: npt.NDArray[np.float64]) -> A:
80
-
81
- l1, t1, r1, b1 = convert_xywh_to_ltrb(batch_bbox[:, :, :, None])
82
- l2, t2, r2, b2 = convert_xywh_to_ltrb(batch_bbox[:, :, None, :])
83
- a1 = (r1 - l1) * (b1 - t1)
84
-
85
- # shape: (B, S, S)
86
- l_max = np.maximum(l1, l2)
87
- r_min = np.minimum(r1, r2)
88
- t_max = np.maximum(t1, t2)
89
- b_min = np.minimum(b1, b2)
90
- cond = (l_max < r_min) & (t_max < b_min)
91
- ai = np.where(cond, (r_min - l_max) * (b_min - t_max), 0.0)
92
-
93
- return {"a1": a1, "ai": ai}
94
-
95
- def _compute_ac_layout_gan(
96
- self,
97
- S: int,
98
- ai: npt.NDArray[np.float64],
99
- a1: npt.NDArray[np.float64],
100
- batch_mask: npt.NDArray[np.bool_],
101
- ) -> npt.NDArray[np.float64]:
102
-
103
- # shape: (B, S) -> (B, S, S)
104
- batch_mask = ~batch_mask[:, None, :] | ~batch_mask[:, :, None]
105
- indices = np.arange(S)
106
- batch_mask[:, indices, indices] = True
107
- ai[batch_mask] = 0.0
108
-
109
- # shape: (B, S, S)
110
- ar = np.nan_to_num(ai / a1)
111
- score = ar.sum(axis=(1, 2))
112
-
113
- return score
114
-
115
- def _compute_layout_gan_pp(
116
- self,
117
- score_ac_layout_gan: npt.NDArray[np.float64],
118
- batch_mask: npt.NDArray[np.bool_],
119
- ) -> npt.NDArray[np.float64]:
120
-
121
- # shape: (B, S) -> (B,)
122
- batch_mask = batch_mask.sum(axis=1)
123
-
124
- # shape: (B,)
125
- score_normalized = score_ac_layout_gan / batch_mask
126
- score_normalized[np.isnan(score_normalized)] = 0.0
127
-
128
- return score_normalized
129
-
130
- def _compute_layout_gan(
131
- self, S: int, B: int, ai: npt.NDArray[np.float64]
132
- ) -> npt.NDArray[np.float64]:
133
-
134
- indices = np.arange(S)
135
- ii, jj = np.meshgrid(indices, indices, indexing="ij")
136
-
137
- # shape: ii (S, S) -> (1, S, S), jj (S, S) -> (1, S, S)
138
- # shape: (1, S, S) -> (B, S, S)
139
- ai[np.repeat((ii[None, :] >= jj[None, :]), axis=0, repeats=B)] = 0.0
140
-
141
- # shape: (B, S, S) -> (B,)
142
- score = ai.sum(axis=(1, 2))
143
-
144
- return score
145
-
146
- def _compute(
147
- self,
148
- *,
149
- bbox: Union[npt.NDArray[np.float64], List[List[int]]],
150
- mask: Union[npt.NDArray[np.bool_], List[List[bool]]],
151
- ) -> Dict[str, npt.NDArray[np.float64]]:
152
-
153
- # shape: (B, model_max_length, C)
154
- bbox = np.array(bbox)
155
- # shape: (B, model_max_length)
156
- mask = np.array(mask)
157
-
158
- assert bbox.ndim == 3
159
- assert mask.ndim == 2
160
-
161
- # S: model_max_length
162
- B, S, C = bbox.shape
163
-
164
- # shape: batch_bbox (B, S, C), batch_mask (B, S) -> (B, S, 1) -> (B, S, C)
165
- bbox[np.repeat(~mask[:, :, None], axis=2, repeats=C)] = 0.0
166
- # shape: (C, B, S)
167
- bbox = bbox.transpose(2, 0, 1)
168
-
169
- A = self.__calculate_a1_ai(bbox)
170
-
171
- # shape: (B,)
172
- score_ac_layout_gan = self._compute_ac_layout_gan(S=S, batch_mask=mask, **A)
173
- # shape: (B,)
174
- score_layout_gan_pp = self._compute_layout_gan_pp(
175
- score_ac_layout_gan=score_ac_layout_gan, batch_mask=mask
176
- )
177
- # shape: (B,)
178
- score_layout_gan = self._compute_layout_gan(B=B, S=S, ai=A["ai"])
179
-
180
- return {
181
- "overlap-ACLayoutGAN": score_ac_layout_gan,
182
- "overlap-LayoutGAN++": score_layout_gan_pp,
183
- "overlap-LayoutGAN": score_layout_gan,
184
- }