Layoutvae
LayoutVAE Transformers-style package.
LayoutVAEConfig ¶
Bases: PretrainedConfig
Configuration for LayoutVAE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. The first release supports PubLayNet. |
publaynet
|
num_labels
|
int
|
Public label vocabulary size. |
5
|
internal_num_labels
|
int
|
Internal label-set size including the empty label. |
6
|
max_position_embeddings
|
int | None
|
Maximum number of layout elements. |
None
|
count_latent_dim
|
int
|
Latent dimension for the count module. |
32
|
bbox_latent_dim
|
int
|
Latent dimension for the box module. |
32
|
bbox_format
|
BoxFormat | str
|
Internal box format. |
ltwh
|
bbox_normalized
|
bool
|
Whether internal boxes are normalized. |
True
|
id2label
|
Id2LabelMapping | None
|
Optional public ID-to-label mapping. |
None
|
label2id
|
dict[str, int] | None
|
Optional public label-to-ID mapping. |
None
|
**kwargs
|
str | int | float | bool | None
|
Extra |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> LayoutVAEConfig().model_type
'layoutvae'
Source code in models/layoutvae/src/layoutvae/configuration_layoutvae.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 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 | |
__init__ ¶
__init__(
dataset_name: DatasetName | str = DatasetName.publaynet,
num_labels: int = 5,
internal_num_labels: int = 6,
max_position_embeddings: int | None = None,
count_latent_dim: int = 32,
bbox_latent_dim: int = 32,
bbox_format: BoxFormat | str = BoxFormat.ltwh,
bbox_normalized: bool = True,
id2label: Id2LabelMapping | None = None,
label2id: dict[str, int] | None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize a LayoutVAE config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. The first release supports PubLayNet. |
publaynet
|
num_labels
|
int
|
Public label vocabulary size. |
5
|
internal_num_labels
|
int
|
Internal label-set size including the empty label. |
6
|
max_position_embeddings
|
int | None
|
Maximum number of generated elements. |
None
|
count_latent_dim
|
int
|
Latent dimension for the count module. |
32
|
bbox_latent_dim
|
int
|
Latent dimension for the box module. |
32
|
bbox_format
|
BoxFormat | str
|
Internal box format. |
ltwh
|
bbox_normalized
|
bool
|
Whether internal boxes are normalized. |
True
|
id2label
|
Id2LabelMapping | None
|
Optional public ID-to-label mapping. |
None
|
label2id
|
dict[str, int] | None
|
Optional public label-to-ID mapping. |
None
|
**kwargs
|
str | int | float | bool | None
|
Extra |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> LayoutVAEConfig(dataset_name="publaynet").num_labels
5
Source code in models/layoutvae/src/layoutvae/configuration_layoutvae.py
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 | |
LayoutVAEModel ¶
Bases: PreTrainedModel
Transformers-compatible LayoutVAE model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LayoutVAEConfig
|
LayoutVAE configuration. |
required |
Examples:
>>> model = LayoutVAEModel(LayoutVAEConfig())
>>> model.config.model_type
'layoutvae'
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
__init__ ¶
__init__(config: LayoutVAEConfig) -> None
Initialize LayoutVAE submodules.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
410 411 412 413 414 415 416 417 418 419 420 421 422 | |
forward ¶
forward(
label_set: Float[Tensor, "batch internal_labels"],
*,
count_latents: Float[
Tensor, "batch internal_labels latent"
]
| None = None,
bbox_latents: Float[Tensor, "batch elements latent"]
| None = None,
bbox_noise: Float[Tensor, "batch elements 4"]
| None = None,
class_counts: Float[Tensor, "batch internal_labels"]
| None = None,
count_samples: Float[Tensor, "batch internal_labels"]
| None = None,
generator: Generator | None = None,
return_dict: bool = True,
) -> (
LayoutVAEModelOutput
| tuple[
Float[torch.Tensor, "batch elements 4"],
Float[torch.Tensor, "batch elements 4"],
Int[torch.Tensor, "batch elements"],
Bool[torch.Tensor, "batch elements"],
Float[torch.Tensor, "batch internal_labels"],
Int[torch.Tensor, "batch elements"],
]
)
Run label-conditioned layout generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label_set
|
Float[Tensor, 'batch internal_labels']
|
Six-way label-set tensor. |
required |
count_latents
|
Float[Tensor, 'batch internal_labels latent'] | None
|
Optional fixed count latents. |
None
|
bbox_latents
|
Float[Tensor, 'batch elements latent'] | None
|
Optional fixed box latents. |
None
|
bbox_noise
|
Float[Tensor, 'batch elements 4'] | None
|
Optional fixed output noise. |
None
|
class_counts
|
Float[Tensor, 'batch internal_labels'] | None
|
Optional fixed six-way class counts. |
None
|
count_samples
|
Float[Tensor, 'batch internal_labels'] | None
|
Optional fixed count samples. |
None
|
generator
|
Generator | None
|
Optional PyTorch random generator. |
None
|
return_dict
|
bool
|
Whether to return a dataclass. |
True
|
Returns:
| Type | Description |
|---|---|
LayoutVAEModelOutput | tuple[Float[Tensor, 'batch elements 4'], Float[Tensor, 'batch elements 4'], Int[Tensor, 'batch elements'], Bool[Tensor, 'batch elements'], Float[Tensor, 'batch internal_labels'], Int[Tensor, 'batch elements']]
|
Model output dataclass or tuple. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes are invalid. |
Examples:
>>> model = LayoutVAEModel(LayoutVAEConfig())
>>> label_set = torch.tensor([[0, 1, 0, 0, 0, 1]], dtype=torch.float32)
>>> out = model(label_set, class_counts=torch.tensor([[7, 1, 0, 0, 0, 1.]]))
>>> tuple(out.bbox.shape)
(1, 9, 4)
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
LayoutVAEModelOutput
dataclass
¶
Bases: ModelOutput
Raw LayoutVAE model output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_ltwh
|
Float[Tensor, 'batch elements 4']
|
Internal normalized left-top-width-height boxes. |
required |
bbox
|
Float[Tensor, 'batch elements 4']
|
Public normalized center |
cast(Float[Tensor, 'batch elements 4'], None)
|
labels
|
Int[Tensor, 'batch elements']
|
Public label IDs. |
cast(Int[Tensor, 'batch elements'], None)
|
mask
|
Bool[Tensor, 'batch elements']
|
Valid-element mask. |
cast(Bool[Tensor, 'batch elements'], None)
|
class_counts
|
Float[Tensor, 'batch internal_labels']
|
Six-way class-count tensor. |
cast(Float[Tensor, 'batch internal_labels'], None)
|
label_set
|
Float[Tensor, 'batch internal_labels'] | None
|
Optional label-set input. |
None
|
internal_labels
|
Int[Tensor, 'batch elements'] | None
|
Optional six-way generated label IDs. |
None
|
Examples:
>>> output = LayoutVAEModelOutput(
... raw_ltwh=torch.zeros(1, 1, 4),
... bbox=torch.zeros(1, 1, 4),
... labels=torch.zeros(1, 1, dtype=torch.long),
... mask=torch.ones(1, 1, dtype=torch.bool),
... class_counts=torch.ones(1, 6),
... )
>>> tuple(output.bbox.shape)
(1, 1, 4)
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 | |
LayoutVAEPipeline ¶
Bases: LayoutGenerationPipeline
Transformers pipeline for LayoutVAE label-conditioned generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
LayoutVAEModel
|
LayoutVAE model instance. |
required |
processor
|
LayoutVAEProcessor | None
|
Optional processor for label-set encoding. |
None
|
config
|
LayoutVAEConfig | None
|
Optional root pipeline config. Defaults to |
None
|
device
|
int | device | None
|
Optional torch device. |
None
|
binary_output
|
bool
|
Reserved compatibility flag. |
False
|
Examples:
>>> model = LayoutVAEModel(LayoutVAEConfig())
>>> pipe = LayoutVAEPipeline(model=model)
>>> pipe.config.model_type
'layoutvae'
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
__init__ ¶
__init__(
model: LayoutVAEModel,
processor: LayoutVAEProcessor | None = None,
config: LayoutVAEConfig | None = None,
device: int | device | None = None,
binary_output: bool = False,
) -> None
Initialize a LayoutVAE pipeline.
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
preprocess ¶
preprocess(
input_: list[list[str | int]]
| list[str | int]
| Int[Tensor, ...]
| None = None,
**preprocess_parameters: LayoutVAEParam,
) -> BatchEncoding
Encode labels into model inputs.
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
postprocess ¶
postprocess(
model_outputs: LayoutGenerationOutput
| LayoutVAEOutputDict,
**kwargs: str | int | float | bool | None,
) -> LayoutGenerationOutput | LayoutVAEOutputDict
Return generated layouts unchanged.
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
310 311 312 313 314 315 316 317 | |
__call__ ¶
__call__(
labels: list[list[str | int]]
| list[str | int]
| Int[Tensor, ...]
| None = None,
*,
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.label,
bbox: Float[Tensor, "batch elements 4"] | None = None,
mask: Bool[Tensor, "batch elements"] | None = None,
num_elements: int
| list[int]
| Int[Tensor, "batch"]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: OutputType | str = OutputType.dataclass,
return_intermediates: bool = False,
count_latents: Float[
Tensor, "batch internal_labels latent"
]
| None = None,
bbox_latents: Float[Tensor, "batch elements latent"]
| None = None,
bbox_noise: Float[Tensor, "batch elements 4"]
| None = None,
class_counts: Float[Tensor, "batch internal_labels"]
| None = None,
) -> LayoutGenerationOutput | LayoutVAEOutputDict
Generate PubLayNet layouts from label conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list[list[str | int]] | list[str | int] | Int[Tensor, ...] | None
|
Public PubLayNet label strings or IDs. |
None
|
batch_size
|
int
|
Used when |
1
|
seed
|
int | None
|
Optional random seed used when |
None
|
generator
|
Generator | None
|
Optional PyTorch random generator. Takes precedence. |
None
|
condition_type
|
ConditionType | str
|
Condition type or alias. Only |
label
|
bbox
|
Float[Tensor, 'batch elements 4'] | None
|
Reserved compatibility argument. |
None
|
mask
|
Bool[Tensor, 'batch elements'] | None
|
Reserved compatibility argument. |
None
|
num_elements
|
int | list[int] | Int[Tensor, 'batch'] | None
|
Reserved compatibility argument. |
None
|
box_format
|
BoxFormat | str
|
Reserved compatibility argument. |
xywh
|
normalized
|
bool
|
Reserved compatibility argument. |
True
|
canvas_size
|
tuple[int, int] | None
|
Reserved compatibility argument. |
None
|
num_inference_steps
|
int | None
|
Reserved compatibility argument. |
None
|
output_type
|
OutputType | str
|
Return format. |
dataclass
|
return_intermediates
|
bool
|
Whether to include raw generation tensors. |
False
|
count_latents
|
Float[Tensor, 'batch internal_labels latent'] | None
|
Optional fixed count latents. |
None
|
bbox_latents
|
Float[Tensor, 'batch elements latent'] | None
|
Optional fixed box latents. |
None
|
bbox_noise
|
Float[Tensor, 'batch elements 4'] | None
|
Optional fixed box output noise. |
None
|
class_counts
|
Float[Tensor, 'batch internal_labels'] | None
|
Optional fixed class counts. |
None
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | LayoutVAEOutputDict
|
Layout generation output. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If labels are missing or condition options are unsupported. |
Examples:
>>> pipe = LayoutVAEPipeline(LayoutVAEModel(LayoutVAEConfig()))
>>> out = pipe(labels=["text"], class_counts=torch.tensor([[8, 1, 0, 0, 0, 0.]]))
>>> tuple(out.bbox.shape)
(1, 9, 4)
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
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 | |
LayoutVAEProcessor ¶
Bases: ProcessorMixin
Encode PubLayNet labels into LayoutVAE label sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. The first release supports PubLayNet. |
publaynet
|
id2label
|
Id2LabelMapping | None
|
Optional public ID-to-label mapping. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset is unsupported. |
Examples:
>>> processor = LayoutVAEProcessor()
>>> processor.label2id["text"]
0
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
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 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 | |
__init__ ¶
__init__(
dataset_name: DatasetName | str = DatasetName.publaynet,
id2label: Id2LabelMapping | None = None,
) -> None
Initialize the processor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. The first release supports PubLayNet. |
publaynet
|
id2label
|
Id2LabelMapping | None
|
Optional public ID-to-label mapping. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset is unsupported. |
Examples:
>>> LayoutVAEProcessor("publaynet").id2label[4]
'figure'
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
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 | |
__call__ ¶
__call__(
labels: list[list[str | int]]
| list[str | int]
| Int[Tensor, "..."],
*,
return_tensors: Literal["pt"] = "pt",
) -> BatchEncoding
Encode public labels as a six-way label-set tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list[list[str | int]] | list[str | int] | Int[Tensor, '...']
|
Public label names or IDs. A flat list is treated as one row. |
required |
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
Returns:
| Type | Description |
|---|---|
BatchEncoding
|
Batch encoding with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If labels are empty, unknown, or tensors are unsupported. |
Examples:
>>> encoded = LayoutVAEProcessor()(["text", "figure"])
>>> encoded["label_set"].tolist()
[[0.0, 1.0, 0.0, 0.0, 0.0, 1.0]]
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
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 | |
public_from_internal ¶
public_from_internal(
internal_labels: Int[Tensor, "batch elements"],
) -> tuple[
Int[torch.Tensor, "batch elements"],
Bool[torch.Tensor, "batch elements"],
]
Map six-way labels to public labels and validity masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
internal_labels
|
Int[Tensor, 'batch elements']
|
Internal label IDs where zero marks empty slots. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Int[Tensor, 'batch elements'], Bool[Tensor, 'batch elements']]
|
Public label IDs and mask tensors. |
Examples:
>>> processor = LayoutVAEProcessor()
>>> labels, mask = processor.public_from_internal(torch.tensor([[0, 1, 5]]))
>>> labels.tolist(), mask.tolist()
([[0, 0, 4]], [[False, True, True]])
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
batch_decode ¶
batch_decode(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"] | None = None,
) -> list[list[DecodedLayoutRecord]]
Decode layout tensors into records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4']
|
Public normalized center |
required |
labels
|
Int[Tensor, 'batch elements']
|
Public label IDs. |
required |
mask
|
Bool[Tensor, 'batch elements'] | None
|
Optional valid-element mask. |
None
|
Returns:
| Type | Description |
|---|---|
list[list[DecodedLayoutRecord]]
|
Nested records with label text, label ID, and box coordinates. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a public label ID is unknown. |
Examples:
>>> records = LayoutVAEProcessor().batch_decode(
... torch.zeros(1, 1, 4), torch.tensor([[0]])
... )
>>> records[0][0]["label"]
'text'
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
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 | |
configuration_layoutvae ¶
Configuration objects for LayoutVAE checkpoints.
LayoutVAEConfig ¶
Bases: PretrainedConfig
Configuration for LayoutVAE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. The first release supports PubLayNet. |
publaynet
|
num_labels
|
int
|
Public label vocabulary size. |
5
|
internal_num_labels
|
int
|
Internal label-set size including the empty label. |
6
|
max_position_embeddings
|
int | None
|
Maximum number of layout elements. |
None
|
count_latent_dim
|
int
|
Latent dimension for the count module. |
32
|
bbox_latent_dim
|
int
|
Latent dimension for the box module. |
32
|
bbox_format
|
BoxFormat | str
|
Internal box format. |
ltwh
|
bbox_normalized
|
bool
|
Whether internal boxes are normalized. |
True
|
id2label
|
Id2LabelMapping | None
|
Optional public ID-to-label mapping. |
None
|
label2id
|
dict[str, int] | None
|
Optional public label-to-ID mapping. |
None
|
**kwargs
|
str | int | float | bool | None
|
Extra |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> LayoutVAEConfig().model_type
'layoutvae'
Source code in models/layoutvae/src/layoutvae/configuration_layoutvae.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 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 | |
__init__ ¶
__init__(
dataset_name: DatasetName | str = DatasetName.publaynet,
num_labels: int = 5,
internal_num_labels: int = 6,
max_position_embeddings: int | None = None,
count_latent_dim: int = 32,
bbox_latent_dim: int = 32,
bbox_format: BoxFormat | str = BoxFormat.ltwh,
bbox_normalized: bool = True,
id2label: Id2LabelMapping | None = None,
label2id: dict[str, int] | None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize a LayoutVAE config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. The first release supports PubLayNet. |
publaynet
|
num_labels
|
int
|
Public label vocabulary size. |
5
|
internal_num_labels
|
int
|
Internal label-set size including the empty label. |
6
|
max_position_embeddings
|
int | None
|
Maximum number of generated elements. |
None
|
count_latent_dim
|
int
|
Latent dimension for the count module. |
32
|
bbox_latent_dim
|
int
|
Latent dimension for the box module. |
32
|
bbox_format
|
BoxFormat | str
|
Internal box format. |
ltwh
|
bbox_normalized
|
bool
|
Whether internal boxes are normalized. |
True
|
id2label
|
Id2LabelMapping | None
|
Optional public ID-to-label mapping. |
None
|
label2id
|
dict[str, int] | None
|
Optional public label-to-ID mapping. |
None
|
**kwargs
|
str | int | float | bool | None
|
Extra |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> LayoutVAEConfig(dataset_name="publaynet").num_labels
5
Source code in models/layoutvae/src/layoutvae/configuration_layoutvae.py
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 | |
conversion ¶
Conversion helpers for LayoutVAE checkpoint artifacts.
load_original_state_dicts ¶
load_original_state_dicts(
source_root: str | Path,
) -> tuple[
dict[str, Shaped[torch.Tensor, "..."]],
dict[str, Shaped[torch.Tensor, "..."]],
]
Load original checkpoint state dictionaries through a pickle shim.
Source code in models/layoutvae/src/layoutvae/conversion.py
23 24 25 26 27 28 29 30 31 | |
load_original_modules ¶
load_original_modules(
source_root: str | Path,
) -> tuple[torch.nn.Module, torch.nn.Module]
Load original checkpoint modules through a pickle shim.
Source code in models/layoutvae/src/layoutvae/conversion.py
34 35 36 37 38 39 40 41 42 43 | |
build_default_config ¶
build_default_config() -> LayoutVAEConfig
Return the fixed PubLayNet LayoutVAE configuration.
Returns:
| Type | Description |
|---|---|
LayoutVAEConfig
|
PubLayNet LayoutVAE configuration. |
Examples:
>>> build_default_config().dataset_name
'publaynet'
Source code in models/layoutvae/src/layoutvae/conversion.py
86 87 88 89 90 91 92 93 94 95 96 | |
convert_state_dicts ¶
convert_state_dicts(
*,
count_state_dict: dict[str, Shaped[Tensor, "..."]],
bbox_state_dict: dict[str, Shaped[Tensor, "..."]],
output_dir: str | Path,
) -> Path
Convert count and box state dictionaries into HF files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count_state_dict
|
dict[str, Shaped[Tensor, '...']]
|
State dictionary for |
required |
bbox_state_dict
|
dict[str, Shaped[Tensor, '...']]
|
State dictionary for |
required |
output_dir
|
str | Path
|
Directory where converted files are written. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The output directory. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a state dictionary is incompatible. |
ValueError
|
If output filenames would violate the HF artifact contract. |
Source code in models/layoutvae/src/layoutvae/conversion.py
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 | |
model_card ¶
Model-card helpers for LayoutVAE.
write_layoutvae_model_card ¶
write_layoutvae_model_card(output_dir: str | Path) -> Path
Write a minimal Hub README for converted LayoutVAE artifacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str | Path
|
Target checkpoint directory. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the written README. |
Examples:
>>> import tempfile
>>> path = write_layoutvae_model_card(tempfile.mkdtemp())
>>> path.name
'README.md'
Source code in models/layoutvae/src/layoutvae/model_card.py
8 9 10 11 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 | |
modeling_layoutvae ¶
PyTorch model classes for LayoutVAE.
OutputType ¶
Bases: StrEnum
Supported generation output formats.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
23 24 25 26 27 | |
LayoutVAEModelOutput
dataclass
¶
Bases: ModelOutput
Raw LayoutVAE model output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_ltwh
|
Float[Tensor, 'batch elements 4']
|
Internal normalized left-top-width-height boxes. |
required |
bbox
|
Float[Tensor, 'batch elements 4']
|
Public normalized center |
cast(Float[Tensor, 'batch elements 4'], None)
|
labels
|
Int[Tensor, 'batch elements']
|
Public label IDs. |
cast(Int[Tensor, 'batch elements'], None)
|
mask
|
Bool[Tensor, 'batch elements']
|
Valid-element mask. |
cast(Bool[Tensor, 'batch elements'], None)
|
class_counts
|
Float[Tensor, 'batch internal_labels']
|
Six-way class-count tensor. |
cast(Float[Tensor, 'batch internal_labels'], None)
|
label_set
|
Float[Tensor, 'batch internal_labels'] | None
|
Optional label-set input. |
None
|
internal_labels
|
Int[Tensor, 'batch elements'] | None
|
Optional six-way generated label IDs. |
None
|
Examples:
>>> output = LayoutVAEModelOutput(
... raw_ltwh=torch.zeros(1, 1, 4),
... bbox=torch.zeros(1, 1, 4),
... labels=torch.zeros(1, 1, dtype=torch.long),
... mask=torch.ones(1, 1, dtype=torch.bool),
... class_counts=torch.ones(1, 6),
... )
>>> tuple(output.bbox.shape)
(1, 1, 4)
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 | |
FCBlock ¶
Bases: Module
Two-layer fully connected block used by the encoders.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
__init__ ¶
__init__(n_class: int) -> None
Initialize the block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_class
|
int
|
Input dimension. |
required |
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
98 99 100 101 102 103 104 105 106 107 108 109 110 | |
forward ¶
forward(
inputs: Float[Tensor, "batch features"],
) -> Float[torch.Tensor, "batch 128"]
Run the block.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
112 113 114 115 116 | |
Embeder ¶
Bases: Module
Count module embedding network.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
__init__ ¶
__init__(n_class: int) -> None
Initialize the embedding network.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
122 123 124 125 126 127 128 | |
forward ¶
forward(
inputs: tuple[
Float[Tensor, "batch internal_labels"],
Float[Tensor, "batch internal_labels"],
Float[Tensor, "batch internal_labels"],
],
) -> Float[torch.Tensor, "batch 128"]
Embed label-set, current-label, and previous-count tensors.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
130 131 132 133 134 135 136 137 138 139 140 | |
Encoder ¶
Bases: Module
Gaussian posterior encoder.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 | |
__init__ ¶
__init__(in_dim: int = 1, latent_dim: int = 32) -> None
Initialize the encoder.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
146 147 148 149 150 151 152 153 154 | |
forward ¶
forward(
inputs: tuple[
Float[Tensor, "batch input_features"],
Float[Tensor, "batch 128"],
],
) -> tuple[
Float[torch.Tensor, "batch latent"],
Float[torch.Tensor, "batch latent"],
]
Encode a target tensor and conditional embedding.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
Prior ¶
Bases: Module
Conditional Gaussian prior network.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
__init__ ¶
__init__(latent_dim: int = 32) -> None
Initialize the prior.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
177 178 179 180 181 182 183 | |
forward ¶
forward(
inputs: Float[Tensor, "batch 128"],
) -> tuple[
Float[torch.Tensor, "batch latent"],
Float[torch.Tensor, "batch latent"],
]
Predict latent mean and log variance.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
185 186 187 188 189 190 191 192 193 | |
Decoder ¶
Bases: Module
Conditional decoder network.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
__init__ ¶
__init__(output_dim: int, latent_dim: int = 32) -> None
Initialize the decoder.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
199 200 201 202 203 204 205 | |
forward ¶
forward(
inputs: tuple[
Float[Tensor, "batch 128"],
Float[Tensor, "batch latent"],
],
) -> Float[torch.Tensor, "batch output"]
Decode from an embedding and latent tensor.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
207 208 209 210 211 212 213 214 215 216 217 218 | |
EmbedBbox ¶
Bases: Module
Autoregressive box embedding network.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 | |
__init__ ¶
__init__(n_class: int) -> None
Initialize the box embedding network.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
224 225 226 227 228 229 230 231 232 | |
forward ¶
forward(
inputs: tuple[
Float[Tensor, "batch internal_labels"],
Float[Tensor, "batch internal_labels"],
Float[Tensor, "history batch features"],
],
) -> Float[torch.Tensor, "batch 128"]
Embed counts, current label, and previous elements.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
234 235 236 237 238 239 240 241 242 243 244 245 246 | |
CountVAEModel ¶
Bases: Module
Autoregressive count module.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 | |
__init__ ¶
__init__(n_class: int, latent_dim: int = 32) -> None
Initialize the count module.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
265 266 267 268 269 270 271 272 | |
forward ¶
forward(
label_set: Float[Tensor, "batch internal_labels"],
*,
latents: Float[Tensor, "batch internal_labels latent"]
| None = None,
count_samples: Float[Tensor, "batch internal_labels"]
| None = None,
generator: Generator | None = None,
) -> Float[torch.Tensor, "batch internal_labels"]
Generate class counts from a label set.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 | |
BboxVAEModel ¶
Bases: Module
Autoregressive bounding-box module.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 | |
__init__ ¶
__init__(
n_class: int,
n_dim: int,
max_box: int,
latent_dim: int = 32,
) -> None
Initialize the box module.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
forward ¶
forward(
box_counts: Float[Tensor, "batch internal_labels"],
box_label: Float[
Tensor, "batch elements internal_labels"
],
*,
latents: Float[Tensor, "batch elements latent"]
| None = None,
output_noise: Float[Tensor, "batch elements 4"]
| None = None,
generator: Generator | None = None,
) -> Float[torch.Tensor, "batch elements 4"]
Generate normalized left-top-width-height boxes.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 | |
LayoutVAEModel ¶
Bases: PreTrainedModel
Transformers-compatible LayoutVAE model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LayoutVAEConfig
|
LayoutVAE configuration. |
required |
Examples:
>>> model = LayoutVAEModel(LayoutVAEConfig())
>>> model.config.model_type
'layoutvae'
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
__init__ ¶
__init__(config: LayoutVAEConfig) -> None
Initialize LayoutVAE submodules.
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
410 411 412 413 414 415 416 417 418 419 420 421 422 | |
forward ¶
forward(
label_set: Float[Tensor, "batch internal_labels"],
*,
count_latents: Float[
Tensor, "batch internal_labels latent"
]
| None = None,
bbox_latents: Float[Tensor, "batch elements latent"]
| None = None,
bbox_noise: Float[Tensor, "batch elements 4"]
| None = None,
class_counts: Float[Tensor, "batch internal_labels"]
| None = None,
count_samples: Float[Tensor, "batch internal_labels"]
| None = None,
generator: Generator | None = None,
return_dict: bool = True,
) -> (
LayoutVAEModelOutput
| tuple[
Float[torch.Tensor, "batch elements 4"],
Float[torch.Tensor, "batch elements 4"],
Int[torch.Tensor, "batch elements"],
Bool[torch.Tensor, "batch elements"],
Float[torch.Tensor, "batch internal_labels"],
Int[torch.Tensor, "batch elements"],
]
)
Run label-conditioned layout generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label_set
|
Float[Tensor, 'batch internal_labels']
|
Six-way label-set tensor. |
required |
count_latents
|
Float[Tensor, 'batch internal_labels latent'] | None
|
Optional fixed count latents. |
None
|
bbox_latents
|
Float[Tensor, 'batch elements latent'] | None
|
Optional fixed box latents. |
None
|
bbox_noise
|
Float[Tensor, 'batch elements 4'] | None
|
Optional fixed output noise. |
None
|
class_counts
|
Float[Tensor, 'batch internal_labels'] | None
|
Optional fixed six-way class counts. |
None
|
count_samples
|
Float[Tensor, 'batch internal_labels'] | None
|
Optional fixed count samples. |
None
|
generator
|
Generator | None
|
Optional PyTorch random generator. |
None
|
return_dict
|
bool
|
Whether to return a dataclass. |
True
|
Returns:
| Type | Description |
|---|---|
LayoutVAEModelOutput | tuple[Float[Tensor, 'batch elements 4'], Float[Tensor, 'batch elements 4'], Int[Tensor, 'batch elements'], Bool[Tensor, 'batch elements'], Float[Tensor, 'batch internal_labels'], Int[Tensor, 'batch elements']]
|
Model output dataclass or tuple. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes are invalid. |
Examples:
>>> model = LayoutVAEModel(LayoutVAEConfig())
>>> label_set = torch.tensor([[0, 1, 0, 0, 0, 1]], dtype=torch.float32)
>>> out = model(label_set, class_counts=torch.tensor([[7, 1, 0, 0, 0, 1.]]))
>>> tuple(out.bbox.shape)
(1, 9, 4)
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
normalize_output_type ¶
normalize_output_type(
output_type: OutputType | str,
) -> OutputType
Normalize a public output type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_type
|
OutputType | str
|
Output type enum or string. |
required |
Returns:
| Type | Description |
|---|---|
OutputType
|
Normalized output type enum. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is unsupported. |
Examples:
>>> str(normalize_output_type("dict"))
'dict'
Source code in models/layoutvae/src/layoutvae/modeling_layoutvae.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
pipeline_layoutvae ¶
Pipeline interface for LayoutVAE layout generation.
GenerationOptions
dataclass
¶
Common generation options accepted by the pipeline.
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
GenerationOptionsKwargs ¶
Bases: TypedDict
Keyword dictionary used to avoid repeated call-site scaffolding.
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
LayoutVAEPipeline ¶
Bases: LayoutGenerationPipeline
Transformers pipeline for LayoutVAE label-conditioned generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
LayoutVAEModel
|
LayoutVAE model instance. |
required |
processor
|
LayoutVAEProcessor | None
|
Optional processor for label-set encoding. |
None
|
config
|
LayoutVAEConfig | None
|
Optional root pipeline config. Defaults to |
None
|
device
|
int | device | None
|
Optional torch device. |
None
|
binary_output
|
bool
|
Reserved compatibility flag. |
False
|
Examples:
>>> model = LayoutVAEModel(LayoutVAEConfig())
>>> pipe = LayoutVAEPipeline(model=model)
>>> pipe.config.model_type
'layoutvae'
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
__init__ ¶
__init__(
model: LayoutVAEModel,
processor: LayoutVAEProcessor | None = None,
config: LayoutVAEConfig | None = None,
device: int | device | None = None,
binary_output: bool = False,
) -> None
Initialize a LayoutVAE pipeline.
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
preprocess ¶
preprocess(
input_: list[list[str | int]]
| list[str | int]
| Int[Tensor, ...]
| None = None,
**preprocess_parameters: LayoutVAEParam,
) -> BatchEncoding
Encode labels into model inputs.
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
postprocess ¶
postprocess(
model_outputs: LayoutGenerationOutput
| LayoutVAEOutputDict,
**kwargs: str | int | float | bool | None,
) -> LayoutGenerationOutput | LayoutVAEOutputDict
Return generated layouts unchanged.
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
310 311 312 313 314 315 316 317 | |
__call__ ¶
__call__(
labels: list[list[str | int]]
| list[str | int]
| Int[Tensor, ...]
| None = None,
*,
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.label,
bbox: Float[Tensor, "batch elements 4"] | None = None,
mask: Bool[Tensor, "batch elements"] | None = None,
num_elements: int
| list[int]
| Int[Tensor, "batch"]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: OutputType | str = OutputType.dataclass,
return_intermediates: bool = False,
count_latents: Float[
Tensor, "batch internal_labels latent"
]
| None = None,
bbox_latents: Float[Tensor, "batch elements latent"]
| None = None,
bbox_noise: Float[Tensor, "batch elements 4"]
| None = None,
class_counts: Float[Tensor, "batch internal_labels"]
| None = None,
) -> LayoutGenerationOutput | LayoutVAEOutputDict
Generate PubLayNet layouts from label conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list[list[str | int]] | list[str | int] | Int[Tensor, ...] | None
|
Public PubLayNet label strings or IDs. |
None
|
batch_size
|
int
|
Used when |
1
|
seed
|
int | None
|
Optional random seed used when |
None
|
generator
|
Generator | None
|
Optional PyTorch random generator. Takes precedence. |
None
|
condition_type
|
ConditionType | str
|
Condition type or alias. Only |
label
|
bbox
|
Float[Tensor, 'batch elements 4'] | None
|
Reserved compatibility argument. |
None
|
mask
|
Bool[Tensor, 'batch elements'] | None
|
Reserved compatibility argument. |
None
|
num_elements
|
int | list[int] | Int[Tensor, 'batch'] | None
|
Reserved compatibility argument. |
None
|
box_format
|
BoxFormat | str
|
Reserved compatibility argument. |
xywh
|
normalized
|
bool
|
Reserved compatibility argument. |
True
|
canvas_size
|
tuple[int, int] | None
|
Reserved compatibility argument. |
None
|
num_inference_steps
|
int | None
|
Reserved compatibility argument. |
None
|
output_type
|
OutputType | str
|
Return format. |
dataclass
|
return_intermediates
|
bool
|
Whether to include raw generation tensors. |
False
|
count_latents
|
Float[Tensor, 'batch internal_labels latent'] | None
|
Optional fixed count latents. |
None
|
bbox_latents
|
Float[Tensor, 'batch elements latent'] | None
|
Optional fixed box latents. |
None
|
bbox_noise
|
Float[Tensor, 'batch elements 4'] | None
|
Optional fixed box output noise. |
None
|
class_counts
|
Float[Tensor, 'batch internal_labels'] | None
|
Optional fixed class counts. |
None
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | LayoutVAEOutputDict
|
Layout generation output. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If labels are missing or condition options are unsupported. |
Examples:
>>> pipe = LayoutVAEPipeline(LayoutVAEModel(LayoutVAEConfig()))
>>> out = pipe(labels=["text"], class_counts=torch.tensor([[8, 1, 0, 0, 0, 0.]]))
>>> tuple(out.bbox.shape)
(1, 9, 4)
Source code in models/layoutvae/src/layoutvae/pipeline_layoutvae.py
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 | |
processing_layoutvae ¶
Processor for LayoutVAE label-set encoding and layout decoding.
DecodedLayoutRecord ¶
Bases: TypedDict
One decoded LayoutVAE record.
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
20 21 22 23 24 25 | |
LayoutVAEProcessor ¶
Bases: ProcessorMixin
Encode PubLayNet labels into LayoutVAE label sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. The first release supports PubLayNet. |
publaynet
|
id2label
|
Id2LabelMapping | None
|
Optional public ID-to-label mapping. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset is unsupported. |
Examples:
>>> processor = LayoutVAEProcessor()
>>> processor.label2id["text"]
0
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
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 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 | |
__init__ ¶
__init__(
dataset_name: DatasetName | str = DatasetName.publaynet,
id2label: Id2LabelMapping | None = None,
) -> None
Initialize the processor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. The first release supports PubLayNet. |
publaynet
|
id2label
|
Id2LabelMapping | None
|
Optional public ID-to-label mapping. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset is unsupported. |
Examples:
>>> LayoutVAEProcessor("publaynet").id2label[4]
'figure'
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
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 | |
__call__ ¶
__call__(
labels: list[list[str | int]]
| list[str | int]
| Int[Tensor, "..."],
*,
return_tensors: Literal["pt"] = "pt",
) -> BatchEncoding
Encode public labels as a six-way label-set tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list[list[str | int]] | list[str | int] | Int[Tensor, '...']
|
Public label names or IDs. A flat list is treated as one row. |
required |
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
Returns:
| Type | Description |
|---|---|
BatchEncoding
|
Batch encoding with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If labels are empty, unknown, or tensors are unsupported. |
Examples:
>>> encoded = LayoutVAEProcessor()(["text", "figure"])
>>> encoded["label_set"].tolist()
[[0.0, 1.0, 0.0, 0.0, 0.0, 1.0]]
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
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 | |
public_from_internal ¶
public_from_internal(
internal_labels: Int[Tensor, "batch elements"],
) -> tuple[
Int[torch.Tensor, "batch elements"],
Bool[torch.Tensor, "batch elements"],
]
Map six-way labels to public labels and validity masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
internal_labels
|
Int[Tensor, 'batch elements']
|
Internal label IDs where zero marks empty slots. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Int[Tensor, 'batch elements'], Bool[Tensor, 'batch elements']]
|
Public label IDs and mask tensors. |
Examples:
>>> processor = LayoutVAEProcessor()
>>> labels, mask = processor.public_from_internal(torch.tensor([[0, 1, 5]]))
>>> labels.tolist(), mask.tolist()
([[0, 0, 4]], [[False, True, True]])
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
batch_decode ¶
batch_decode(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"] | None = None,
) -> list[list[DecodedLayoutRecord]]
Decode layout tensors into records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4']
|
Public normalized center |
required |
labels
|
Int[Tensor, 'batch elements']
|
Public label IDs. |
required |
mask
|
Bool[Tensor, 'batch elements'] | None
|
Optional valid-element mask. |
None
|
Returns:
| Type | Description |
|---|---|
list[list[DecodedLayoutRecord]]
|
Nested records with label text, label ID, and box coordinates. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a public label ID is unknown. |
Examples:
>>> records = LayoutVAEProcessor().batch_decode(
... torch.zeros(1, 1, 4), torch.tensor([[0]])
... )
>>> records[0][0]["label"]
'text'
Source code in models/layoutvae/src/layoutvae/processing_layoutvae.py
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 | |