Layout fid
Public API for the layout FID evaluator package.
LayoutFIDArchitecture ¶
Bases: StrEnum
Closed set of supported layout FID encoder architectures.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
19 20 21 22 23 | |
LayoutFIDConfig ¶
Bases: PretrainedConfig
Configuration saved with layout FID checkpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Canonical layout dataset name. |
required |
id2label
|
Mapping[int, str] | Mapping[str, str] | None
|
Dataset-local id-to-label metadata. |
None
|
architecture
|
LayoutFIDArchitecture | str
|
Encoder architecture selected by the checkpoint. |
required |
source
|
LayoutFIDSource | str
|
Released artifact family selected by the checkpoint. |
required |
num_public_labels
|
int
|
Number of public dataset labels. |
required |
num_label_embeddings
|
int
|
Number of model label embeddings. |
required |
max_length
|
int
|
Maximum element count accepted by the checkpoint. |
required |
d_model
|
int
|
Transformer hidden dimension. |
256
|
nhead
|
int
|
Number of attention heads. |
4
|
num_layers
|
int
|
Number of transformer encoder layers. |
4
|
bbox_format_for_model
|
BoxFormat | str
|
Internal bbox format consumed by the encoder. |
'ltrb'
|
label_id_offset
|
int
|
Offset applied before model label embedding lookup. |
0
|
pad_label_id
|
int
|
Label id used only in padded model tensor positions. |
0
|
reference_stats
|
dict[str, str] | None
|
Relative reference-statistics paths by split. |
None
|
kwargs
|
LayoutFIDConfigValue
|
Extra Hugging Face config fields. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If label counts or enum values are invalid. |
Examples:
>>> cfg = LayoutFIDConfig(
... dataset_name="publaynet",
... architecture="layoutnet",
... source="layoutflow",
... num_public_labels=5,
... num_label_embeddings=6,
... max_length=20,
... )
>>> cfg.reference_stats["test"]
'reference_stats/test.npz'
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
__init__ ¶
__init__(
*,
dataset_name: DatasetName | str,
id2label: Mapping[int, str]
| Mapping[str, str]
| None = None,
architecture: LayoutFIDArchitecture | str,
source: LayoutFIDSource | str,
num_public_labels: int,
num_label_embeddings: int,
max_length: int,
d_model: int = 256,
nhead: int = 4,
num_layers: int = 4,
bbox_format_for_model: BoxFormat | str = "ltrb",
label_id_offset: int = 0,
pad_label_id: int = 0,
reference_stats: dict[str, str] | None = None,
**kwargs: LayoutFIDConfigValue,
) -> None
Initialize a layout FID checkpoint configuration.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
LayoutFIDSource ¶
Bases: StrEnum
Closed set of supported released artifact families.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
26 27 28 29 30 | |
LayoutFIDStatsSplit ¶
Bases: StrEnum
Closed set of bundled reference-statistics splits.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
33 34 35 36 37 | |
LayoutFIDStatistics
dataclass
¶
Feature distribution statistics used by layout FID.
Source code in models/layout-fid/src/layout_fid/evaluation.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
from_mapping
classmethod
¶
from_mapping(
values: Mapping[
str,
Float[ndarray, "..."]
| list[float]
| list[list[float]]
| str
| int
| None,
],
) -> "LayoutFIDStatistics"
Create statistics from a mapping.
Source code in models/layout-fid/src/layout_fid/evaluation.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
LayoutFIDModel ¶
Bases: PreTrainedModel
Feature encoder used for layout FID evaluation.
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
__init__ ¶
__init__(config: LayoutFIDConfig) -> None
Create a layout FID encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LayoutFIDConfig
|
Explicit layout FID configuration. |
required |
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
extract_features ¶
extract_features(
*,
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
padding_mask: Bool[Tensor, "batch elements"],
) -> Float[torch.Tensor, "batch channels"]
Extract batch-level feature vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4']
|
Model-ready boxes. |
required |
labels
|
Int[Tensor, 'batch elements']
|
Model-ready label ids. |
required |
padding_mask
|
Bool[Tensor, 'batch elements']
|
Boolean mask where |
required |
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'batch channels']
|
Feature tensor shaped |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input shapes are inconsistent. |
Examples:
>>> from layout_fid import LayoutFIDConfig, LayoutFIDModel
>>> cfg = LayoutFIDConfig(
... dataset_name="publaynet", architecture="layoutnet",
... source="layoutflow", num_public_labels=5,
... num_label_embeddings=6, max_length=2,
... )
>>> model = LayoutFIDModel(cfg)
>>> out = model.extract_features(
... bbox=torch.zeros(1, 2, 4),
... labels=torch.zeros(1, 2, dtype=torch.long),
... padding_mask=torch.zeros(1, 2, dtype=torch.bool),
... )
>>> tuple(out.shape)
(1, 256)
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
forward ¶
forward(
*,
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
padding_mask: Bool[Tensor, "batch elements"],
output_reconstruction: bool = False,
return_dict: bool = True,
) -> (
LayoutFIDOutput
| tuple[Shaped[torch.Tensor, "..."], ...]
)
Run feature extraction and optional reconstruction heads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4']
|
Model-ready boxes. |
required |
labels
|
Int[Tensor, 'batch elements']
|
Model-ready label ids. |
required |
padding_mask
|
Bool[Tensor, 'batch elements']
|
Boolean mask where |
required |
output_reconstruction
|
bool
|
Whether to return class and bbox predictions. |
False
|
return_dict
|
bool
|
Whether to return |
True
|
Returns:
| Type | Description |
|---|---|
LayoutFIDOutput | tuple[Shaped[Tensor, '...'], ...]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If input shapes are inconsistent. |
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
LayoutFIDOutput
dataclass
¶
Bases: ModelOutput
Output returned by LayoutFIDModel.forward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Float[Tensor, 'batch channels']
|
Batch-level layout feature vectors. |
required |
discriminator_logits
|
Float[Tensor, 'batch'] | None
|
Optional discriminator logits. |
None
|
class_logits
|
Float[Tensor, '... labels'] | None
|
Optional per-element or valid-element class logits. |
None
|
bbox_pred
|
Float[Tensor, '... 4'] | None
|
Optional reconstructed boxes. |
None
|
intermediates
|
dict[str, object] | None
|
Optional diagnostic tensors. |
None
|
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
LayoutFIDEvaluator ¶
Compose a layout FID model, processor, and reference statistics.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
__init__ ¶
__init__(
*,
model: LayoutFIDModel,
processor: LayoutFIDProcessor,
reference_statistics: Mapping[str, LayoutFIDStatistics]
| None = None,
device: device | str | None = None,
) -> None
Create an evaluator.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
*,
device: device | str | None = None,
**kwargs: LayoutFIDLoadKwarg,
) -> LayoutFIDEvaluator
Load evaluator components from a local directory or Hub id.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
extract_features ¶
extract_features(
*,
layouts: LayoutGenerationOutput
| Mapping[
str,
Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Mapping[int, str]
| Mapping[str, str]
| None,
]
| None = None,
bbox: Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Sequence[ArrayLikeInput]
| None = None,
labels: Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
mask: Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
id2label: Mapping[int, str]
| Mapping[str, str]
| None = None,
box_format: str = "xywh",
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
batch_size: int = 512,
) -> Float[torch.Tensor, "batch channels"]
Extract features from public layout tensors.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
compute_statistics ¶
compute_statistics(
*,
layouts: LayoutGenerationOutput
| Mapping[
str,
Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Mapping[int, str]
| Mapping[str, str]
| None,
]
| None = None,
features: Float[Tensor, "batch channels"]
| Float[ndarray, "batch channels"]
| None = None,
**layout_kwargs: LayoutFIDLayoutKwarg,
) -> LayoutFIDStatistics
Compute candidate feature statistics.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
compute_fid ¶
compute_fid(
*,
layouts: LayoutGenerationOutput
| Mapping[
str,
Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Mapping[int, str]
| Mapping[str, str]
| None,
]
| None = None,
features: Float[Tensor, "batch channels"]
| Float[ndarray, "batch channels"]
| None = None,
statistics: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, ...]
| list[float]
| list[list[float]]
| str
| int
| None,
]
| None = None,
reference_statistics: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, ...]
| list[float]
| list[list[float]]
| str
| int
| None,
]
| None = None,
reference_split: LayoutFIDStatsSplit | str = "test",
**layout_kwargs: LayoutFIDLayoutKwarg,
) -> float
Compute layout FID against bundled or supplied reference statistics.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
LayoutFIDBatch
dataclass
¶
Model-ready layout FID batch.
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
24 25 26 27 28 29 30 31 32 | |
LayoutFIDProcessor ¶
Bases: ProcessorMixin
Convert public layout tensors into layout FID model inputs.
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
__init__ ¶
__init__(config: LayoutFIDConfig) -> None
Create a processor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LayoutFIDConfig
|
Explicit layout FID configuration. |
required |
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
40 41 42 43 44 45 46 47 | |
__call__ ¶
__call__(
*,
bbox: Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Sequence[ArrayLikeInput],
labels: Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Sequence[ArrayLikeInput],
mask: Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
id2label: Mapping[int, str]
| Mapping[str, str]
| None = None,
box_format: BoxFormat | str = "xywh",
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
label_id_offset: int | None = None,
max_length: int | None = None,
pad_label_id: int | None = None,
device: device | str | None = None,
) -> LayoutFIDBatch
Prepare model inputs from the repository public layout schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4'] | Float[ndarray, 'batch elements 4'] | Sequence[ArrayLikeInput]
|
Public layout boxes. |
required |
labels
|
Int[Tensor, 'batch elements'] | Int[ndarray, 'batch elements'] | Sequence[ArrayLikeInput]
|
Public dataset-local label ids. |
required |
mask
|
Bool[Tensor, 'batch elements'] | Bool[ndarray, 'batch elements'] | Sequence[ArrayLikeInput] | None
|
Optional public valid-element mask. |
None
|
id2label
|
Mapping[int, str] | Mapping[str, str] | None
|
Optional public id-to-label metadata. |
None
|
box_format
|
BoxFormat | str
|
Public input box format. |
'xywh'
|
normalized
|
bool
|
Whether boxes are normalized to |
True
|
canvas_size
|
tuple[int, int] | None
|
Pixel canvas size required when |
None
|
label_id_offset
|
int | None
|
Optional parity/debug label-offset override. |
None
|
max_length
|
int | None
|
Optional maximum sequence length override. |
None
|
pad_label_id
|
int | None
|
Optional padded-position model label id. |
None
|
device
|
device | str | None
|
Target torch device. |
None
|
Returns:
| Type | Description |
|---|---|
LayoutFIDBatch
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If metadata or tensor shapes are inconsistent. |
Examples:
>>> from layout_fid import LayoutFIDConfig, LayoutFIDProcessor
>>> cfg = LayoutFIDConfig(
... dataset_name="publaynet", architecture="layoutnet",
... source="layoutflow", num_public_labels=5,
... num_label_embeddings=6, max_length=2,
... )
>>> batch = LayoutFIDProcessor(cfg)(
... bbox=[[[0.5, 0.5, 0.2, 0.2]]], labels=[[0]]
... )
>>> batch.padding_mask.tolist()
[[False, True]]
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
save_pretrained ¶
save_pretrained(
save_directory: str | PathLike[str],
) -> tuple[str]
Save processor metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_directory
|
str | PathLike[str]
|
Directory receiving |
required |
Returns:
| Type | Description |
|---|---|
tuple[str]
|
Tuple containing the saved config path. |
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
**kwargs: LayoutFIDConfigValue,
) -> "LayoutFIDProcessor"
Load a processor from a saved model directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pretrained_model_name_or_path
|
str | PathLike[str]
|
Local path or Hub id. |
required |
kwargs
|
LayoutFIDConfigValue
|
Extra config-loading keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
'LayoutFIDProcessor'
|
Loaded processor bound to the model config. |
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
normalize_architecture ¶
normalize_architecture(
architecture: LayoutFIDArchitecture | str,
) -> LayoutFIDArchitecture
Normalize a public architecture value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
architecture
|
LayoutFIDArchitecture | str
|
Architecture enum or string value. |
required |
Returns:
| Type | Description |
|---|---|
LayoutFIDArchitecture
|
Normalized architecture enum. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the architecture is unsupported. |
Examples:
>>> str(normalize_architecture("layoutnet"))
'layoutnet'
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
normalize_source ¶
normalize_source(
source: LayoutFIDSource | str,
) -> LayoutFIDSource
Normalize a public artifact-source value.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
77 78 79 80 81 82 83 84 | |
normalize_stats_split ¶
normalize_stats_split(
split: LayoutFIDStatsSplit | str,
) -> LayoutFIDStatsSplit
Normalize a public reference-statistics split value.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
87 88 89 90 91 92 93 94 | |
calculate_frechet_distance ¶
calculate_frechet_distance(
mu1: Float[ndarray, "channels"],
sigma1: Float[ndarray, "channels channels"],
mu2: Float[ndarray, "channels"],
sigma2: Float[ndarray, "channels channels"],
*,
eps: float = 1e-06,
) -> float
Compute the Frechet distance between two Gaussian distributions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu1
|
Float[ndarray, 'channels']
|
First mean vector. |
required |
sigma1
|
Float[ndarray, 'channels channels']
|
First covariance matrix. |
required |
mu2
|
Float[ndarray, 'channels']
|
Second mean vector. |
required |
sigma2
|
Float[ndarray, 'channels channels']
|
Second covariance matrix. |
required |
eps
|
float
|
Diagonal offset used when covariance products are nearly singular. |
1e-06
|
Returns:
| Type | Description |
|---|---|
float
|
Frechet distance as a Python float. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dimensions are inconsistent. |
Examples:
>>> mu = np.zeros(2)
>>> sigma = np.eye(2)
>>> calculate_frechet_distance(mu, sigma, mu, sigma)
0.0
Source code in models/layout-fid/src/layout_fid/evaluation.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
compute_feature_statistics ¶
compute_feature_statistics(
features: Float[Tensor, "batch channels"]
| Float[ndarray, "batch channels"],
*,
split: str = "candidate",
dataset_name: str = "",
source: str = "",
) -> LayoutFIDStatistics
Compute float64 mean and covariance from feature vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Float[Tensor, 'batch channels'] | Float[ndarray, 'batch channels']
|
Feature matrix shaped |
required |
split
|
str
|
Split label stored in the returned metadata. |
'candidate'
|
dataset_name
|
str
|
Dataset metadata. |
''
|
source
|
str
|
Source-family metadata. |
''
|
Returns:
| Type | Description |
|---|---|
LayoutFIDStatistics
|
Feature statistics with NumPy |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two feature vectors are provided. |
Examples:
>>> stats = compute_feature_statistics(np.eye(3, dtype=np.float32))
>>> stats.sigma.shape
(3, 3)
Source code in models/layout-fid/src/layout_fid/evaluation.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
compute_layout_fid ¶
compute_layout_fid(
model: "LayoutFIDModel",
processor: "LayoutFIDProcessor",
*,
reference_statistics: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, "..."]
| list[float]
| list[list[float]]
| str
| int
| None,
],
batch_size: int = 512,
**layout_kwargs: Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| Mapping[int, str]
| Mapping[str, str]
| str
| bool
| tuple[int, int]
| int
| device
| None,
) -> float
Compute layout FID directly from model, processor, and layout tensors.
Source code in models/layout-fid/src/layout_fid/evaluation.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
compute_layout_fid_from_statistics ¶
compute_layout_fid_from_statistics(
candidate: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, "..."]
| list[float]
| list[list[float]]
| str
| int
| None,
],
reference: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, "..."]
| list[float]
| list[list[float]]
| str
| int
| None,
],
) -> float
Compute layout FID from two statistics objects.
Source code in models/layout-fid/src/layout_fid/evaluation.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
load_reference_statistics ¶
load_reference_statistics(
path: str | PathLike[str],
) -> LayoutFIDStatistics
Load reference_stats/{split}.npz statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Statistics file path. |
required |
Returns:
| Type | Description |
|---|---|
LayoutFIDStatistics
|
Loaded layout FID statistics. |
Source code in models/layout-fid/src/layout_fid/evaluation.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
save_reference_statistics ¶
save_reference_statistics(
path: str | PathLike[str], stats: LayoutFIDStatistics
) -> None
Save reference statistics in package-local .npz format.
Source code in models/layout-fid/src/layout_fid/evaluation.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
configuration_layout_fid ¶
Configuration for layout FID feature encoders.
LayoutFIDArchitecture ¶
Bases: StrEnum
Closed set of supported layout FID encoder architectures.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
19 20 21 22 23 | |
LayoutFIDSource ¶
Bases: StrEnum
Closed set of supported released artifact families.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
26 27 28 29 30 | |
LayoutFIDStatsSplit ¶
Bases: StrEnum
Closed set of bundled reference-statistics splits.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
33 34 35 36 37 | |
LayoutFIDConfig ¶
Bases: PretrainedConfig
Configuration saved with layout FID checkpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Canonical layout dataset name. |
required |
id2label
|
Mapping[int, str] | Mapping[str, str] | None
|
Dataset-local id-to-label metadata. |
None
|
architecture
|
LayoutFIDArchitecture | str
|
Encoder architecture selected by the checkpoint. |
required |
source
|
LayoutFIDSource | str
|
Released artifact family selected by the checkpoint. |
required |
num_public_labels
|
int
|
Number of public dataset labels. |
required |
num_label_embeddings
|
int
|
Number of model label embeddings. |
required |
max_length
|
int
|
Maximum element count accepted by the checkpoint. |
required |
d_model
|
int
|
Transformer hidden dimension. |
256
|
nhead
|
int
|
Number of attention heads. |
4
|
num_layers
|
int
|
Number of transformer encoder layers. |
4
|
bbox_format_for_model
|
BoxFormat | str
|
Internal bbox format consumed by the encoder. |
'ltrb'
|
label_id_offset
|
int
|
Offset applied before model label embedding lookup. |
0
|
pad_label_id
|
int
|
Label id used only in padded model tensor positions. |
0
|
reference_stats
|
dict[str, str] | None
|
Relative reference-statistics paths by split. |
None
|
kwargs
|
LayoutFIDConfigValue
|
Extra Hugging Face config fields. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If label counts or enum values are invalid. |
Examples:
>>> cfg = LayoutFIDConfig(
... dataset_name="publaynet",
... architecture="layoutnet",
... source="layoutflow",
... num_public_labels=5,
... num_label_embeddings=6,
... max_length=20,
... )
>>> cfg.reference_stats["test"]
'reference_stats/test.npz'
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
__init__ ¶
__init__(
*,
dataset_name: DatasetName | str,
id2label: Mapping[int, str]
| Mapping[str, str]
| None = None,
architecture: LayoutFIDArchitecture | str,
source: LayoutFIDSource | str,
num_public_labels: int,
num_label_embeddings: int,
max_length: int,
d_model: int = 256,
nhead: int = 4,
num_layers: int = 4,
bbox_format_for_model: BoxFormat | str = "ltrb",
label_id_offset: int = 0,
pad_label_id: int = 0,
reference_stats: dict[str, str] | None = None,
**kwargs: LayoutFIDConfigValue,
) -> None
Initialize a layout FID checkpoint configuration.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
normalize_architecture ¶
normalize_architecture(
architecture: LayoutFIDArchitecture | str,
) -> LayoutFIDArchitecture
Normalize a public architecture value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
architecture
|
LayoutFIDArchitecture | str
|
Architecture enum or string value. |
required |
Returns:
| Type | Description |
|---|---|
LayoutFIDArchitecture
|
Normalized architecture enum. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the architecture is unsupported. |
Examples:
>>> str(normalize_architecture("layoutnet"))
'layoutnet'
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
normalize_source ¶
normalize_source(
source: LayoutFIDSource | str,
) -> LayoutFIDSource
Normalize a public artifact-source value.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
77 78 79 80 81 82 83 84 | |
normalize_stats_split ¶
normalize_stats_split(
split: LayoutFIDStatsSplit | str,
) -> LayoutFIDStatsSplit
Normalize a public reference-statistics split value.
Source code in models/layout-fid/src/layout_fid/configuration_layout_fid.py
87 88 89 90 91 92 93 94 | |
conversion ¶
Conversion helpers for layout FID checkpoints and statistics.
LayoutFlowDatasetSpec ¶
Bases: TypedDict
Conversion metadata for one LayoutFlow dataset.
Source code in models/layout-fid/src/layout_fid/conversion.py
22 23 24 25 26 27 28 | |
convert_layoutflow_checkpoint ¶
convert_layoutflow_checkpoint(
*,
checkpoint_path: str | PathLike[str],
output_dir: str | PathLike[str],
dataset_name: str,
stats_paths: Mapping[str, str | PathLike[str]]
| None = None,
) -> LayoutFIDConfig
Convert a LayoutFlow-style LayoutNet checkpoint directory.
Source code in models/layout-fid/src/layout_fid/conversion.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
convert_layoutdm_fidnet_v3_checkpoint ¶
convert_layoutdm_fidnet_v3_checkpoint(
*,
checkpoint_path: str | PathLike[str],
output_dir: str | PathLike[str],
dataset_name: str,
num_public_labels: int,
max_length: int,
) -> LayoutFIDConfig
Convert a LayoutDM FIDNetV3 checkpoint when assets are available.
Source code in models/layout-fid/src/layout_fid/conversion.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
load_musig_statistics ¶
load_musig_statistics(
path: str | PathLike[str],
*,
split: str,
dataset_name: str,
source: str,
) -> LayoutFIDStatistics
Convert a stacked [mu; sigma] tensor into typed statistics.
Source code in models/layout-fid/src/layout_fid/conversion.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
load_checkpoint_state_dict ¶
load_checkpoint_state_dict(
path: str | PathLike[str],
*,
state_dict_key: str | None = None,
) -> dict[str, Shaped[torch.Tensor, "..."]]
Load a torch checkpoint state dict.
Source code in models/layout-fid/src/layout_fid/conversion.py
142 143 144 145 146 147 148 149 150 151 | |
strip_module_prefix ¶
strip_module_prefix(
state_dict: Mapping[str, Shaped[Tensor, "..."]],
) -> dict[str, Shaped[torch.Tensor, "..."]]
Strip optional module. prefixes from checkpoint keys.
Source code in models/layout-fid/src/layout_fid/conversion.py
154 155 156 157 158 | |
validate_state_dict_shapes ¶
validate_state_dict_shapes(
state_dict: Mapping[str, Shaped[Tensor, "..."]],
config: LayoutFIDConfig,
) -> None
Validate checkpoint tensor shapes before writing artifacts.
Source code in models/layout-fid/src/layout_fid/conversion.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
evaluation ¶
Statistics and Frechet-distance helpers for layout FID.
LayoutFIDStatistics
dataclass
¶
Feature distribution statistics used by layout FID.
Source code in models/layout-fid/src/layout_fid/evaluation.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
from_mapping
classmethod
¶
from_mapping(
values: Mapping[
str,
Float[ndarray, "..."]
| list[float]
| list[list[float]]
| str
| int
| None,
],
) -> "LayoutFIDStatistics"
Create statistics from a mapping.
Source code in models/layout-fid/src/layout_fid/evaluation.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
compute_feature_statistics ¶
compute_feature_statistics(
features: Float[Tensor, "batch channels"]
| Float[ndarray, "batch channels"],
*,
split: str = "candidate",
dataset_name: str = "",
source: str = "",
) -> LayoutFIDStatistics
Compute float64 mean and covariance from feature vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Float[Tensor, 'batch channels'] | Float[ndarray, 'batch channels']
|
Feature matrix shaped |
required |
split
|
str
|
Split label stored in the returned metadata. |
'candidate'
|
dataset_name
|
str
|
Dataset metadata. |
''
|
source
|
str
|
Source-family metadata. |
''
|
Returns:
| Type | Description |
|---|---|
LayoutFIDStatistics
|
Feature statistics with NumPy |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two feature vectors are provided. |
Examples:
>>> stats = compute_feature_statistics(np.eye(3, dtype=np.float32))
>>> stats.sigma.shape
(3, 3)
Source code in models/layout-fid/src/layout_fid/evaluation.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
calculate_frechet_distance ¶
calculate_frechet_distance(
mu1: Float[ndarray, "channels"],
sigma1: Float[ndarray, "channels channels"],
mu2: Float[ndarray, "channels"],
sigma2: Float[ndarray, "channels channels"],
*,
eps: float = 1e-06,
) -> float
Compute the Frechet distance between two Gaussian distributions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu1
|
Float[ndarray, 'channels']
|
First mean vector. |
required |
sigma1
|
Float[ndarray, 'channels channels']
|
First covariance matrix. |
required |
mu2
|
Float[ndarray, 'channels']
|
Second mean vector. |
required |
sigma2
|
Float[ndarray, 'channels channels']
|
Second covariance matrix. |
required |
eps
|
float
|
Diagonal offset used when covariance products are nearly singular. |
1e-06
|
Returns:
| Type | Description |
|---|---|
float
|
Frechet distance as a Python float. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dimensions are inconsistent. |
Examples:
>>> mu = np.zeros(2)
>>> sigma = np.eye(2)
>>> calculate_frechet_distance(mu, sigma, mu, sigma)
0.0
Source code in models/layout-fid/src/layout_fid/evaluation.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
compute_layout_fid_from_statistics ¶
compute_layout_fid_from_statistics(
candidate: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, "..."]
| list[float]
| list[list[float]]
| str
| int
| None,
],
reference: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, "..."]
| list[float]
| list[list[float]]
| str
| int
| None,
],
) -> float
Compute layout FID from two statistics objects.
Source code in models/layout-fid/src/layout_fid/evaluation.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
load_reference_statistics ¶
load_reference_statistics(
path: str | PathLike[str],
) -> LayoutFIDStatistics
Load reference_stats/{split}.npz statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Statistics file path. |
required |
Returns:
| Type | Description |
|---|---|
LayoutFIDStatistics
|
Loaded layout FID statistics. |
Source code in models/layout-fid/src/layout_fid/evaluation.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
save_reference_statistics ¶
save_reference_statistics(
path: str | PathLike[str], stats: LayoutFIDStatistics
) -> None
Save reference statistics in package-local .npz format.
Source code in models/layout-fid/src/layout_fid/evaluation.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
compute_layout_fid ¶
compute_layout_fid(
model: "LayoutFIDModel",
processor: "LayoutFIDProcessor",
*,
reference_statistics: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, "..."]
| list[float]
| list[list[float]]
| str
| int
| None,
],
batch_size: int = 512,
**layout_kwargs: Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| Mapping[int, str]
| Mapping[str, str]
| str
| bool
| tuple[int, int]
| int
| device
| None,
) -> float
Compute layout FID directly from model, processor, and layout tensors.
Source code in models/layout-fid/src/layout_fid/evaluation.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
metrics ¶
Layout metric helpers matching the layout-dm reference definitions.
compute_overlap ¶
compute_overlap(
bbox: Float[Tensor, "... elements 4"],
mask: Bool[Tensor, "... elements"] | None = None,
) -> dict[str, Shaped[torch.Tensor, "..."]]
Compute LayoutDM-compatible overlap metrics for normalized xywh boxes.
Source code in models/layout-fid/src/layout_fid/metrics.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
compute_alignment ¶
compute_alignment(
bbox: Float[Tensor, "... elements 4"],
mask: Bool[Tensor, "... elements"] | None = None,
) -> dict[str, Shaped[torch.Tensor, "..."]]
Compute LayoutDM-compatible alignment metrics for normalized xywh boxes.
Source code in models/layout-fid/src/layout_fid/metrics.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
compute_average_iou ¶
compute_average_iou(
bbox: Float[Tensor, "... elements 4"]
| Float[ndarray, "... elements 4"]
| Sequence[
tuple[
Float[ndarray, "elements 4"],
Int[ndarray, "elements"],
]
],
mask: Bool[Tensor, "... elements"]
| Bool[ndarray, "... elements"]
| None = None,
) -> dict[str, float]
Compute LayoutDM-compatible average IoU metrics.
bbox may be a batched normalized center xywh tensor with a public
valid-element mask, or an unpadded sequence of (bbox, labels) layouts.
Labels are ignored by this metric and are accepted for LayoutDM call-shape
compatibility.
Source code in models/layout-fid/src/layout_fid/metrics.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
compute_maximum_iou ¶
compute_maximum_iou(
candidate_bbox: Float[Tensor, "batch elements 4"],
reference_bbox: Float[Tensor, "batch elements 4"],
candidate_mask: Bool[Tensor, "batch elements"]
| None = None,
reference_mask: Bool[Tensor, "batch elements"]
| None = None,
) -> Float[torch.Tensor, ""]
Compute maximum pairwise IoU between two layout batches.
Source code in models/layout-fid/src/layout_fid/metrics.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
model_card ¶
Model-card helpers for layout FID evaluator checkpoints.
model_card_metadata ¶
model_card_metadata(
config: LayoutFIDConfig, *, hub_id: str
) -> dict[str, str | list[str]]
Return model-card metadata for a converted layout FID checkpoint.
Source code in models/layout-fid/src/layout_fid/model_card.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
modeling_layout_fid ¶
PyTorch modules for layout FID feature extraction.
TransformerWithToken ¶
Bases: Module
Transformer encoder with a learned summary token.
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
__init__ ¶
__init__(
*,
d_model: int,
nhead: int,
dim_feedforward: int,
num_layers: int,
) -> None
Initialize the token encoder.
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
forward ¶
forward(
x: Float[Tensor, "elements batch channels"],
src_key_padding_mask: Bool[Tensor, "batch elements"],
) -> Float[
torch.Tensor, "elements_plus_token batch channels"
]
Encode element features with a prepended summary token.
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
40 41 42 43 44 45 46 47 48 49 50 51 | |
LayoutFIDOutput
dataclass
¶
Bases: ModelOutput
Output returned by LayoutFIDModel.forward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Float[Tensor, 'batch channels']
|
Batch-level layout feature vectors. |
required |
discriminator_logits
|
Float[Tensor, 'batch'] | None
|
Optional discriminator logits. |
None
|
class_logits
|
Float[Tensor, '... labels'] | None
|
Optional per-element or valid-element class logits. |
None
|
bbox_pred
|
Float[Tensor, '... 4'] | None
|
Optional reconstructed boxes. |
None
|
intermediates
|
dict[str, object] | None
|
Optional diagnostic tensors. |
None
|
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
LayoutFIDModel ¶
Bases: PreTrainedModel
Feature encoder used for layout FID evaluation.
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
__init__ ¶
__init__(config: LayoutFIDConfig) -> None
Create a layout FID encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LayoutFIDConfig
|
Explicit layout FID configuration. |
required |
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
extract_features ¶
extract_features(
*,
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
padding_mask: Bool[Tensor, "batch elements"],
) -> Float[torch.Tensor, "batch channels"]
Extract batch-level feature vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4']
|
Model-ready boxes. |
required |
labels
|
Int[Tensor, 'batch elements']
|
Model-ready label ids. |
required |
padding_mask
|
Bool[Tensor, 'batch elements']
|
Boolean mask where |
required |
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'batch channels']
|
Feature tensor shaped |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input shapes are inconsistent. |
Examples:
>>> from layout_fid import LayoutFIDConfig, LayoutFIDModel
>>> cfg = LayoutFIDConfig(
... dataset_name="publaynet", architecture="layoutnet",
... source="layoutflow", num_public_labels=5,
... num_label_embeddings=6, max_length=2,
... )
>>> model = LayoutFIDModel(cfg)
>>> out = model.extract_features(
... bbox=torch.zeros(1, 2, 4),
... labels=torch.zeros(1, 2, dtype=torch.long),
... padding_mask=torch.zeros(1, 2, dtype=torch.bool),
... )
>>> tuple(out.shape)
(1, 256)
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
forward ¶
forward(
*,
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
padding_mask: Bool[Tensor, "batch elements"],
output_reconstruction: bool = False,
return_dict: bool = True,
) -> (
LayoutFIDOutput
| tuple[Shaped[torch.Tensor, "..."], ...]
)
Run feature extraction and optional reconstruction heads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4']
|
Model-ready boxes. |
required |
labels
|
Int[Tensor, 'batch elements']
|
Model-ready label ids. |
required |
padding_mask
|
Bool[Tensor, 'batch elements']
|
Boolean mask where |
required |
output_reconstruction
|
bool
|
Whether to return class and bbox predictions. |
False
|
return_dict
|
bool
|
Whether to return |
True
|
Returns:
| Type | Description |
|---|---|
LayoutFIDOutput | tuple[Shaped[Tensor, '...'], ...]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If input shapes are inconsistent. |
Source code in models/layout-fid/src/layout_fid/modeling_layout_fid.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
pipeline_layout_fid ¶
High-level layout FID evaluator.
LayoutFIDEvaluator ¶
Compose a layout FID model, processor, and reference statistics.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
__init__ ¶
__init__(
*,
model: LayoutFIDModel,
processor: LayoutFIDProcessor,
reference_statistics: Mapping[str, LayoutFIDStatistics]
| None = None,
device: device | str | None = None,
) -> None
Create an evaluator.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
*,
device: device | str | None = None,
**kwargs: LayoutFIDLoadKwarg,
) -> LayoutFIDEvaluator
Load evaluator components from a local directory or Hub id.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
extract_features ¶
extract_features(
*,
layouts: LayoutGenerationOutput
| Mapping[
str,
Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Mapping[int, str]
| Mapping[str, str]
| None,
]
| None = None,
bbox: Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Sequence[ArrayLikeInput]
| None = None,
labels: Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
mask: Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
id2label: Mapping[int, str]
| Mapping[str, str]
| None = None,
box_format: str = "xywh",
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
batch_size: int = 512,
) -> Float[torch.Tensor, "batch channels"]
Extract features from public layout tensors.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
compute_statistics ¶
compute_statistics(
*,
layouts: LayoutGenerationOutput
| Mapping[
str,
Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Mapping[int, str]
| Mapping[str, str]
| None,
]
| None = None,
features: Float[Tensor, "batch channels"]
| Float[ndarray, "batch channels"]
| None = None,
**layout_kwargs: LayoutFIDLayoutKwarg,
) -> LayoutFIDStatistics
Compute candidate feature statistics.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
compute_fid ¶
compute_fid(
*,
layouts: LayoutGenerationOutput
| Mapping[
str,
Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Mapping[int, str]
| Mapping[str, str]
| None,
]
| None = None,
features: Float[Tensor, "batch channels"]
| Float[ndarray, "batch channels"]
| None = None,
statistics: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, ...]
| list[float]
| list[list[float]]
| str
| int
| None,
]
| None = None,
reference_statistics: LayoutFIDStatistics
| Mapping[
str,
Float[ndarray, ...]
| list[float]
| list[list[float]]
| str
| int
| None,
]
| None = None,
reference_split: LayoutFIDStatsSplit | str = "test",
**layout_kwargs: LayoutFIDLayoutKwarg,
) -> float
Compute layout FID against bundled or supplied reference statistics.
Source code in models/layout-fid/src/layout_fid/pipeline_layout_fid.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
processing_layout_fid ¶
Input processing for layout FID evaluators.
LayoutFIDBatch
dataclass
¶
Model-ready layout FID batch.
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
24 25 26 27 28 29 30 31 32 | |
LayoutFIDProcessor ¶
Bases: ProcessorMixin
Convert public layout tensors into layout FID model inputs.
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
__init__ ¶
__init__(config: LayoutFIDConfig) -> None
Create a processor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LayoutFIDConfig
|
Explicit layout FID configuration. |
required |
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
40 41 42 43 44 45 46 47 | |
__call__ ¶
__call__(
*,
bbox: Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Sequence[ArrayLikeInput],
labels: Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Sequence[ArrayLikeInput],
mask: Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
id2label: Mapping[int, str]
| Mapping[str, str]
| None = None,
box_format: BoxFormat | str = "xywh",
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
label_id_offset: int | None = None,
max_length: int | None = None,
pad_label_id: int | None = None,
device: device | str | None = None,
) -> LayoutFIDBatch
Prepare model inputs from the repository public layout schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4'] | Float[ndarray, 'batch elements 4'] | Sequence[ArrayLikeInput]
|
Public layout boxes. |
required |
labels
|
Int[Tensor, 'batch elements'] | Int[ndarray, 'batch elements'] | Sequence[ArrayLikeInput]
|
Public dataset-local label ids. |
required |
mask
|
Bool[Tensor, 'batch elements'] | Bool[ndarray, 'batch elements'] | Sequence[ArrayLikeInput] | None
|
Optional public valid-element mask. |
None
|
id2label
|
Mapping[int, str] | Mapping[str, str] | None
|
Optional public id-to-label metadata. |
None
|
box_format
|
BoxFormat | str
|
Public input box format. |
'xywh'
|
normalized
|
bool
|
Whether boxes are normalized to |
True
|
canvas_size
|
tuple[int, int] | None
|
Pixel canvas size required when |
None
|
label_id_offset
|
int | None
|
Optional parity/debug label-offset override. |
None
|
max_length
|
int | None
|
Optional maximum sequence length override. |
None
|
pad_label_id
|
int | None
|
Optional padded-position model label id. |
None
|
device
|
device | str | None
|
Target torch device. |
None
|
Returns:
| Type | Description |
|---|---|
LayoutFIDBatch
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If metadata or tensor shapes are inconsistent. |
Examples:
>>> from layout_fid import LayoutFIDConfig, LayoutFIDProcessor
>>> cfg = LayoutFIDConfig(
... dataset_name="publaynet", architecture="layoutnet",
... source="layoutflow", num_public_labels=5,
... num_label_embeddings=6, max_length=2,
... )
>>> batch = LayoutFIDProcessor(cfg)(
... bbox=[[[0.5, 0.5, 0.2, 0.2]]], labels=[[0]]
... )
>>> batch.padding_mask.tolist()
[[False, True]]
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
save_pretrained ¶
save_pretrained(
save_directory: str | PathLike[str],
) -> tuple[str]
Save processor metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_directory
|
str | PathLike[str]
|
Directory receiving |
required |
Returns:
| Type | Description |
|---|---|
tuple[str]
|
Tuple containing the saved config path. |
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
**kwargs: LayoutFIDConfigValue,
) -> "LayoutFIDProcessor"
Load a processor from a saved model directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pretrained_model_name_or_path
|
str | PathLike[str]
|
Local path or Hub id. |
required |
kwargs
|
LayoutFIDConfigValue
|
Extra config-loading keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
'LayoutFIDProcessor'
|
Loaded processor bound to the model config. |
Source code in models/layout-fid/src/layout_fid/processing_layout_fid.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
testing ¶
Test helpers for layout FID packages.
assert_feature_close ¶
assert_feature_close(
actual: Float[Tensor, "batch channels"],
expected: Float[Tensor, "batch channels"],
*,
atol: float = 1e-06,
rtol: float = 1e-05,
) -> None
Assert layout FID feature parity.
Source code in models/layout-fid/src/layout_fid/testing.py
10 11 12 13 14 15 16 17 18 | |
assert_statistics_shape ¶
assert_statistics_shape(
mu: Float[ndarray, "channels"],
sigma: Float[ndarray, "channels channels"],
) -> None
Assert reference statistics have compatible shapes.
Source code in models/layout-fid/src/layout_fid/testing.py
21 22 23 24 25 26 27 28 29 30 | |