Layout detr
Transformers-style LayoutDETR package.
BackgroundPreprocessing ¶
Bases: StrEnum
Supported public background preprocessing modes.
Source code in models/layout-detr/src/layout_detr/configuration_layout_detr.py
14 15 16 17 18 19 20 21 | |
LayoutDetrConfig ¶
Bases: PretrainedConfig
Configuration for LayoutDETR model, processor, and pipeline.
Source code in models/layout-detr/src/layout_detr/configuration_layout_detr.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 | |
__init__ ¶
__init__(
*,
dataset_name: str = "ad_banner",
id2label: Mapping[int | str, str] | None = None,
max_seq_length: int = 9,
z_dim: int = 4,
img_channels: int = 3,
img_height: int = 256,
img_width: int = 256,
background_size: int = 256,
hidden_dim: int = 256,
bert_f_dim: int = 768,
bert_num_encoder_layers: int = 12,
bert_num_decoder_layers: int = 2,
bert_num_heads: int = 4,
max_text_length: int = 256,
text_vocab_size: int = 30522,
med_config: Mapping[str, LayoutDetrMetadataValue]
| None = None,
backbone_name: str = "resnet50",
image_mean: Sequence[float] = (0.485, 0.456, 0.406),
image_std: Sequence[float] = (0.229, 0.224, 0.225),
architecture: Literal[
"lightweight", "reference"
] = "lightweight",
model_subfolder: str = "model",
processor_subfolder: str = "processor",
original_training_options: Mapping[
str, LayoutDetrMetadataValue
]
| None = None,
conversion_report: Mapping[str, LayoutDetrMetadataValue]
| None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize LayoutDETR configuration.
Source code in models/layout-detr/src/layout_detr/configuration_layout_detr.py
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 | |
LayoutDetrImageProcessor ¶
Bases: BaseImageProcessor
Prepare ImageNet-normalized background tensors for LayoutDETR.
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.py
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 | |
__init__ ¶
__init__(
background_size: int = 256,
image_mean: Sequence[float] = (0.485, 0.456, 0.406),
image_std: Sequence[float] = (0.229, 0.224, 0.225),
**kwargs: str | int | float | bool | None,
) -> None
Initialize image normalization settings.
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.py
24 25 26 27 28 29 30 31 32 33 34 35 | |
from_config
classmethod
¶
from_config(
config: LayoutDetrConfig,
) -> "LayoutDetrImageProcessor"
Build an image processor from a LayoutDETR config.
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.py
37 38 39 40 41 42 43 44 | |
preprocess ¶
preprocess(
images: ImageInput | Sequence[ImageInput],
*,
background_preprocessing: BackgroundPreprocessing
| str = BackgroundPreprocessing.none,
canvas_size: tuple[int, int] | None = None,
return_tensors: Literal["pt"] = "pt",
**kwargs: str | int | float | bool | None,
) -> BatchFeature
Preprocess a background image or batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput]
|
PIL, NumPy, or torch image input. |
required |
background_preprocessing
|
BackgroundPreprocessing | str
|
Released-checkpoint-compatible mode. |
none
|
canvas_size
|
tuple[int, int] | None
|
Optional canvas metadata override. |
None
|
return_tensors
|
Literal['pt']
|
Only |
'pt'
|
kwargs
|
str | int | float | bool | None
|
Ignored compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
BatchFeature
|
|
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
LayoutDetrForConditionalGeneration ¶
Bases: PreTrainedModel
A standard PreTrainedModel wrapper for LayoutDETR forward inference.
Source code in models/layout-detr/src/layout_detr/modeling_layout_detr.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 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 | |
__init__ ¶
__init__(config: LayoutDetrConfig) -> None
Initialize LayoutDETR layers.
Source code in models/layout-detr/src/layout_detr/modeling_layout_detr.py
59 60 61 62 63 64 65 66 67 | |
forward ¶
forward(
*,
pixel_values: Float[
Tensor, "batch channels height width"
],
input_ids: Int[Tensor, "batch elements tokens"],
text_attention_mask: Bool[
Tensor, "batch elements tokens"
],
bbox_labels: Int[Tensor, "batch elements"],
layout_mask: Bool[Tensor, "batch elements"],
latents: Float[Tensor, "batch elements latent"],
text_lengths: Int[Tensor, "batch elements"]
| None = None,
return_dict: bool | None = None,
) -> (
LayoutDetrModelOutput
| tuple[
Float[torch.Tensor, "batch elements 4"],
Int[torch.Tensor, "batch elements"],
Bool[torch.Tensor, "batch elements"],
]
)
Run the LayoutDETR conditional forward pass.
Source code in models/layout-detr/src/layout_detr/modeling_layout_detr.py
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 | |
LayoutDetrModelOutput
dataclass
¶
Bases: ModelOutput
Raw LayoutDETR model output.
Source code in models/layout-detr/src/layout_detr/modeling_layout_detr.py
36 37 38 39 40 41 42 43 44 45 46 47 48 | |
LayoutDetrPipeline ¶
Bases: LayoutGenerationPipeline
Transformers-side LayoutDETR pipeline.
Source code in models/layout-detr/src/layout_detr/pipeline_layout_detr.py
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 | |
__init__ ¶
__init__(
model: LayoutDetrForConditionalGeneration,
processor: LayoutDetrProcessor | None = None,
config: LayoutDetrConfig | None = None,
device: str | device | None = None,
) -> None
Initialize a LayoutDETR pipeline.
Source code in models/layout-detr/src/layout_detr/pipeline_layout_detr.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
__call__ ¶
__call__(
images: ImageInput
| Sequence[ImageInput]
| Shaped[Tensor, "..."]
| None = None,
*,
content: Mapping[
str,
ImageInput
| Sequence[ImageInput]
| Sequence[Sequence[str]]
| Sequence[str]
| Sequence[Sequence[int | str]]
| Sequence[int | str],
]
| None = None,
prompt: str | Sequence[str] | None = None,
texts: Sequence[Sequence[str]]
| Sequence[str]
| None = None,
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.content_image,
labels: Int[Tensor, "batch elements"]
| Sequence[Sequence[int | str]]
| Sequence[int | str]
| None = None,
bbox: Float[Tensor, "batch elements 4"] | None = None,
mask: Bool[Tensor, "batch elements"]
| Sequence[Sequence[bool]]
| Sequence[bool]
| None = None,
num_elements: int
| Sequence[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: Literal["dataclass", "dict"] = "dataclass",
return_intermediates: bool = False,
background_preprocessing: BackgroundPreprocessing
| str = BackgroundPreprocessing.none,
out_jittering_strength: float = 0.0,
out_postprocessing: PostprocessingMode
| str = PostprocessingMode.none,
latents: Float[Tensor, "batch elements latent"]
| None = None,
) -> LayoutGenerationOutput
Generate layouts for a background image and per-element text labels.
Source code in models/layout-detr/src/layout_detr/pipeline_layout_detr.py
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 | |
PostprocessingMode ¶
Bases: StrEnum
Supported LayoutDETR postprocessing modes.
Source code in models/layout-detr/src/layout_detr/postprocessing.py
13 14 15 16 17 18 | |
LayoutDetrProcessor ¶
Bases: ProcessorMixin
Normalize LayoutDETR image, text, label, and mask payloads.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
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 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 | |
__init__ ¶
__init__(
*,
image_processor: LayoutDetrImageProcessor | None = None,
config: LayoutDetrConfig,
id2label: Mapping[int | str, str] | None = None,
) -> None
Initialize the processor.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
save_pretrained ¶
save_pretrained(
save_directory: str | Path,
push_to_hub: bool = False,
**kwargs: str | int | float | bool | None,
) -> None
Save processor metadata and image-processor config.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
58 59 60 61 62 63 64 65 66 67 68 69 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
cache_dir: str | PathLike[str] | None = None,
force_download: bool = False,
local_files_only: bool = False,
token: str | bool | None = None,
revision: str = "main",
subfolder: str | None = None,
**kwargs: str | int | float | bool | None,
) -> "LayoutDetrProcessor"
Load processor metadata from a checkpoint directory.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
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 | |
__call__ ¶
__call__(
*,
images: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch channels height width"]
| None = None,
content: Mapping[
str,
ImageInput
| Sequence[ImageInput]
| Sequence[Sequence[str]]
| Sequence[str]
| Sequence[Sequence[int | str]]
| Sequence[int | str],
]
| None = None,
prompt: str | Sequence[str] | None = None,
texts: Sequence[Sequence[str]]
| Sequence[str]
| None = None,
labels: Int[Tensor, "batch elements"]
| Sequence[Sequence[int | str]]
| Sequence[int | str]
| None = None,
mask: Bool[Tensor, "batch elements"]
| Sequence[Sequence[bool]]
| Sequence[bool]
| None = None,
condition_type: str = "content_image",
background_preprocessing: BackgroundPreprocessing
| str = BackgroundPreprocessing.none,
batch_size: int = 1,
return_tensors: Literal["pt"] = "pt",
canvas_size: tuple[int, int] | None = None,
) -> BatchEncoding
Encode public inputs for the LayoutDETR model.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
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 | |
post_process_layouts ¶
post_process_layouts(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"],
*,
output_type: Literal["dataclass", "dict"] = "dataclass",
return_intermediates: bool = False,
intermediates: dict[
str, Shaped[Tensor, "..."] | list[list[str]] | str
]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| dict[
str,
Shaped[torch.Tensor, "..."]
| list[list[str]]
| str,
]
| None,
]
)
Return generated boxes in the shared layout output schema.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
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 | |
id2label_for_ad_banner ¶
id2label_for_ad_banner() -> dict[int, str]
Return the LayoutDETR Ad Banner label vocabulary.
Source code in models/layout-detr/src/layout_detr/datasets.py
43 44 45 | |
configuration_layout_detr ¶
Configuration for the Transformers-style LayoutDETR generator.
BackgroundPreprocessing ¶
Bases: StrEnum
Supported public background preprocessing modes.
Source code in models/layout-detr/src/layout_detr/configuration_layout_detr.py
14 15 16 17 18 19 20 21 | |
LayoutDetrConfig ¶
Bases: PretrainedConfig
Configuration for LayoutDETR model, processor, and pipeline.
Source code in models/layout-detr/src/layout_detr/configuration_layout_detr.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 | |
__init__ ¶
__init__(
*,
dataset_name: str = "ad_banner",
id2label: Mapping[int | str, str] | None = None,
max_seq_length: int = 9,
z_dim: int = 4,
img_channels: int = 3,
img_height: int = 256,
img_width: int = 256,
background_size: int = 256,
hidden_dim: int = 256,
bert_f_dim: int = 768,
bert_num_encoder_layers: int = 12,
bert_num_decoder_layers: int = 2,
bert_num_heads: int = 4,
max_text_length: int = 256,
text_vocab_size: int = 30522,
med_config: Mapping[str, LayoutDetrMetadataValue]
| None = None,
backbone_name: str = "resnet50",
image_mean: Sequence[float] = (0.485, 0.456, 0.406),
image_std: Sequence[float] = (0.229, 0.224, 0.225),
architecture: Literal[
"lightweight", "reference"
] = "lightweight",
model_subfolder: str = "model",
processor_subfolder: str = "processor",
original_training_options: Mapping[
str, LayoutDetrMetadataValue
]
| None = None,
conversion_report: Mapping[str, LayoutDetrMetadataValue]
| None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize LayoutDETR configuration.
Source code in models/layout-detr/src/layout_detr/configuration_layout_detr.py
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 | |
datasets ¶
Dataset helpers for the LayoutDETR Ad Banner checkpoint.
LayoutDetrDatasetRow ¶
Bases: TypedDict
One local Ad Banner JSON file row.
Source code in models/layout-detr/src/layout_detr/datasets.py
28 29 30 31 32 | |
NormalizedAdBannerAnnotation ¶
Bases: TypedDict
Normalized Ad Banner annotation row.
Source code in models/layout-detr/src/layout_detr/datasets.py
35 36 37 38 39 40 | |
id2label_for_ad_banner ¶
id2label_for_ad_banner() -> dict[int, str]
Return the LayoutDETR Ad Banner label vocabulary.
Source code in models/layout-detr/src/layout_detr/datasets.py
43 44 45 | |
label2id_for_ad_banner ¶
label2id_for_ad_banner() -> dict[str, int]
Return the inverse Ad Banner label mapping.
Source code in models/layout-detr/src/layout_detr/datasets.py
48 49 50 | |
normalize_ad_banner_annotation ¶
normalize_ad_banner_annotation(
sample: Mapping[str, AdBannerAnnotationValue],
) -> NormalizedAdBannerAnnotation
Normalize one Ad Banner annotation row to public center xywh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Mapping[str, AdBannerAnnotationValue]
|
Ad Banner element with |
required |
Returns:
| Type | Description |
|---|---|
NormalizedAdBannerAnnotation
|
A normalized row with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the label or canvas metadata is invalid. |
Source code in models/layout-detr/src/layout_detr/datasets.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 | |
load_ad_banner_dataset ¶
load_ad_banner_dataset(
root: str | Path,
*,
split: Literal["train", "validation"],
source: Literal["ad_banner"] = "ad_banner",
) -> Iterable[LayoutDetrDatasetRow]
Iterate a local Ad Banner directory without downloading assets.
TODO: switch this adapter to a creative-graphic-design Hugging Face
dataset once Ad Banner is imported into the org.
Source code in models/layout-detr/src/layout_detr/datasets.py
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 | |
image_processing_layout_detr ¶
Image processor for LayoutDETR background images.
LayoutDetrImageProcessor ¶
Bases: BaseImageProcessor
Prepare ImageNet-normalized background tensors for LayoutDETR.
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.py
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 | |
__init__ ¶
__init__(
background_size: int = 256,
image_mean: Sequence[float] = (0.485, 0.456, 0.406),
image_std: Sequence[float] = (0.229, 0.224, 0.225),
**kwargs: str | int | float | bool | None,
) -> None
Initialize image normalization settings.
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.py
24 25 26 27 28 29 30 31 32 33 34 35 | |
from_config
classmethod
¶
from_config(
config: LayoutDetrConfig,
) -> "LayoutDetrImageProcessor"
Build an image processor from a LayoutDETR config.
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.py
37 38 39 40 41 42 43 44 | |
preprocess ¶
preprocess(
images: ImageInput | Sequence[ImageInput],
*,
background_preprocessing: BackgroundPreprocessing
| str = BackgroundPreprocessing.none,
canvas_size: tuple[int, int] | None = None,
return_tensors: Literal["pt"] = "pt",
**kwargs: str | int | float | bool | None,
) -> BatchFeature
Preprocess a background image or batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput]
|
PIL, NumPy, or torch image input. |
required |
background_preprocessing
|
BackgroundPreprocessing | str
|
Released-checkpoint-compatible mode. |
none
|
canvas_size
|
tuple[int, int] | None
|
Optional canvas metadata override. |
None
|
return_tensors
|
Literal['pt']
|
Only |
'pt'
|
kwargs
|
str | int | float | bool | None
|
Ignored compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
BatchFeature
|
|
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
normalize_background_preprocessing ¶
normalize_background_preprocessing(
mode: BackgroundPreprocessing | str,
) -> BackgroundPreprocessing
Normalize a public background preprocessing mode.
Source code in models/layout-detr/src/layout_detr/image_processing_layout_detr.py
96 97 98 99 100 101 102 103 104 105 | |
modeling_layout_detr ¶
Transformers-compatible LayoutDETR model.
LayoutDetrModelOutput
dataclass
¶
Bases: ModelOutput
Raw LayoutDETR model output.
Source code in models/layout-detr/src/layout_detr/modeling_layout_detr.py
36 37 38 39 40 41 42 43 44 45 46 47 48 | |
LayoutDetrForConditionalGeneration ¶
Bases: PreTrainedModel
A standard PreTrainedModel wrapper for LayoutDETR forward inference.
Source code in models/layout-detr/src/layout_detr/modeling_layout_detr.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 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 | |
__init__ ¶
__init__(config: LayoutDetrConfig) -> None
Initialize LayoutDETR layers.
Source code in models/layout-detr/src/layout_detr/modeling_layout_detr.py
59 60 61 62 63 64 65 66 67 | |
forward ¶
forward(
*,
pixel_values: Float[
Tensor, "batch channels height width"
],
input_ids: Int[Tensor, "batch elements tokens"],
text_attention_mask: Bool[
Tensor, "batch elements tokens"
],
bbox_labels: Int[Tensor, "batch elements"],
layout_mask: Bool[Tensor, "batch elements"],
latents: Float[Tensor, "batch elements latent"],
text_lengths: Int[Tensor, "batch elements"]
| None = None,
return_dict: bool | None = None,
) -> (
LayoutDetrModelOutput
| tuple[
Float[torch.Tensor, "batch elements 4"],
Int[torch.Tensor, "batch elements"],
Bool[torch.Tensor, "batch elements"],
]
)
Run the LayoutDETR conditional forward pass.
Source code in models/layout-detr/src/layout_detr/modeling_layout_detr.py
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 | |
pipeline_layout_detr ¶
Pipeline interface for LayoutDETR content-image layout generation.
LayoutDetrPipeline ¶
Bases: LayoutGenerationPipeline
Transformers-side LayoutDETR pipeline.
Source code in models/layout-detr/src/layout_detr/pipeline_layout_detr.py
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 | |
__init__ ¶
__init__(
model: LayoutDetrForConditionalGeneration,
processor: LayoutDetrProcessor | None = None,
config: LayoutDetrConfig | None = None,
device: str | device | None = None,
) -> None
Initialize a LayoutDETR pipeline.
Source code in models/layout-detr/src/layout_detr/pipeline_layout_detr.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
__call__ ¶
__call__(
images: ImageInput
| Sequence[ImageInput]
| Shaped[Tensor, "..."]
| None = None,
*,
content: Mapping[
str,
ImageInput
| Sequence[ImageInput]
| Sequence[Sequence[str]]
| Sequence[str]
| Sequence[Sequence[int | str]]
| Sequence[int | str],
]
| None = None,
prompt: str | Sequence[str] | None = None,
texts: Sequence[Sequence[str]]
| Sequence[str]
| None = None,
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.content_image,
labels: Int[Tensor, "batch elements"]
| Sequence[Sequence[int | str]]
| Sequence[int | str]
| None = None,
bbox: Float[Tensor, "batch elements 4"] | None = None,
mask: Bool[Tensor, "batch elements"]
| Sequence[Sequence[bool]]
| Sequence[bool]
| None = None,
num_elements: int
| Sequence[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: Literal["dataclass", "dict"] = "dataclass",
return_intermediates: bool = False,
background_preprocessing: BackgroundPreprocessing
| str = BackgroundPreprocessing.none,
out_jittering_strength: float = 0.0,
out_postprocessing: PostprocessingMode
| str = PostprocessingMode.none,
latents: Float[Tensor, "batch elements latent"]
| None = None,
) -> LayoutGenerationOutput
Generate layouts for a background image and per-element text labels.
Source code in models/layout-detr/src/layout_detr/pipeline_layout_detr.py
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 | |
normalize_condition_type ¶
normalize_condition_type(
condition_type: ConditionType | str,
) -> ConditionType
Normalize LayoutDETR condition modes.
Source code in models/layout-detr/src/layout_detr/pipeline_layout_detr.py
74 75 76 77 78 79 80 81 82 | |
postprocessing ¶
Pure tensor LayoutDETR postprocessing helpers.
PostprocessingMode ¶
Bases: StrEnum
Supported LayoutDETR postprocessing modes.
Source code in models/layout-detr/src/layout_detr/postprocessing.py
13 14 15 16 17 18 | |
normalize_postprocessing_mode ¶
normalize_postprocessing_mode(
mode: PostprocessingMode | str,
) -> PostprocessingMode
Normalize a public postprocessing mode value.
Source code in models/layout-detr/src/layout_detr/postprocessing.py
21 22 23 24 25 26 27 28 29 30 | |
jitter_boxes ¶
jitter_boxes(
bbox: Float[Tensor, "batch elements 4"],
*,
strength: float,
generator: Generator | None,
) -> Float[torch.Tensor, "batch elements 4"]
Apply multiplicative jitter to generated boxes.
Source code in models/layout-detr/src/layout_detr/postprocessing.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
horizontal_center_aligned ¶
horizontal_center_aligned(
bbox: Float[Tensor, "batch elements 4"],
mask: Bool[Tensor, "batch elements"],
) -> Float[torch.Tensor, "batch elements 4"]
Align valid boxes to the mean center-x coordinate.
Source code in models/layout-detr/src/layout_detr/postprocessing.py
56 57 58 59 60 61 62 63 64 65 66 | |
horizontal_left_aligned ¶
horizontal_left_aligned(
bbox: Float[Tensor, "batch elements 4"],
mask: Bool[Tensor, "batch elements"],
) -> Float[torch.Tensor, "batch elements 4"]
Align valid boxes to the mean left edge.
Source code in models/layout-detr/src/layout_detr/postprocessing.py
69 70 71 72 73 74 75 76 77 78 79 80 81 | |
de_overlap ¶
de_overlap(
bbox: Float[Tensor, "batch elements 4"],
mask: Bool[Tensor, "batch elements"],
) -> Float[torch.Tensor, "batch elements 4"]
Reduce vertical overlaps with deterministic LayoutDETR arithmetic.
Source code in models/layout-detr/src/layout_detr/postprocessing.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
apply_postprocessing ¶
apply_postprocessing(
bbox: Float[Tensor, "batch elements 4"],
mask: Bool[Tensor, "batch elements"],
*,
mode: PostprocessingMode
| str = PostprocessingMode.none,
jitter_strength: float = 0.0,
generator: Generator | None = None,
) -> Float[torch.Tensor, "batch elements 4"]
Apply LayoutDETR jitter/alignment/de-overlap without rendering.
Source code in models/layout-detr/src/layout_detr/postprocessing.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
processing_layout_detr ¶
Processor for LayoutDETR content-image conditions.
LayoutDetrProcessor ¶
Bases: ProcessorMixin
Normalize LayoutDETR image, text, label, and mask payloads.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
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 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 | |
__init__ ¶
__init__(
*,
image_processor: LayoutDetrImageProcessor | None = None,
config: LayoutDetrConfig,
id2label: Mapping[int | str, str] | None = None,
) -> None
Initialize the processor.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
save_pretrained ¶
save_pretrained(
save_directory: str | Path,
push_to_hub: bool = False,
**kwargs: str | int | float | bool | None,
) -> None
Save processor metadata and image-processor config.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
58 59 60 61 62 63 64 65 66 67 68 69 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
cache_dir: str | PathLike[str] | None = None,
force_download: bool = False,
local_files_only: bool = False,
token: str | bool | None = None,
revision: str = "main",
subfolder: str | None = None,
**kwargs: str | int | float | bool | None,
) -> "LayoutDetrProcessor"
Load processor metadata from a checkpoint directory.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
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 | |
__call__ ¶
__call__(
*,
images: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch channels height width"]
| None = None,
content: Mapping[
str,
ImageInput
| Sequence[ImageInput]
| Sequence[Sequence[str]]
| Sequence[str]
| Sequence[Sequence[int | str]]
| Sequence[int | str],
]
| None = None,
prompt: str | Sequence[str] | None = None,
texts: Sequence[Sequence[str]]
| Sequence[str]
| None = None,
labels: Int[Tensor, "batch elements"]
| Sequence[Sequence[int | str]]
| Sequence[int | str]
| None = None,
mask: Bool[Tensor, "batch elements"]
| Sequence[Sequence[bool]]
| Sequence[bool]
| None = None,
condition_type: str = "content_image",
background_preprocessing: BackgroundPreprocessing
| str = BackgroundPreprocessing.none,
batch_size: int = 1,
return_tensors: Literal["pt"] = "pt",
canvas_size: tuple[int, int] | None = None,
) -> BatchEncoding
Encode public inputs for the LayoutDETR model.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
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 | |
post_process_layouts ¶
post_process_layouts(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"],
*,
output_type: Literal["dataclass", "dict"] = "dataclass",
return_intermediates: bool = False,
intermediates: dict[
str, Shaped[Tensor, "..."] | list[list[str]] | str
]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| dict[
str,
Shaped[torch.Tensor, "..."]
| list[list[str]]
| str,
]
| None,
]
)
Return generated boxes in the shared layout output schema.
Source code in models/layout-detr/src/layout_detr/processing_layout_detr.py
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 | |
vendor_state ¶
Vendor checkpoint extraction and state-dict conversion helpers.
LayoutDetrConversionReport ¶
Bases: TypedDict
Structured conversion metadata persisted with converted checkpoints.
Source code in models/layout-detr/src/layout_detr/vendor_state.py
19 20 21 22 23 24 25 26 27 28 | |
remap_generator_key ¶
remap_generator_key(source_key: str) -> str
Map a vendor G_ema key to the local model key when possible.
Source code in models/layout-detr/src/layout_detr/vendor_state.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
build_conversion_report ¶
build_conversion_report(
source_state: Mapping[str, Shaped[Tensor, "..."]],
target_state: Mapping[str, Shaped[Tensor, "..."]],
remapped_state: Mapping[str, Shaped[Tensor, "..."]],
*,
custom_op_import_required: bool,
) -> LayoutDetrConversionReport
Build strict-load diagnostics for a remapped state dict.
Source code in models/layout-detr/src/layout_detr/vendor_state.py
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 | |
extract_generator_state ¶
extract_generator_state(
pickle_path: str | Path,
*,
vendor_root: str | Path,
device: str = "cpu",
) -> tuple[
dict[str, Shaped[torch.Tensor, "..."]],
LayoutDetrConfig,
LayoutDetrConversionReport,
]
Extract G_ema from the original LayoutDETR pickle.
The import is isolated to the conversion path. Normal converted
from_pretrained inference never imports vendor/layout-detr or
torch_utils.ops.
Source code in models/layout-detr/src/layout_detr/vendor_state.py
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 | |
load_vendor_generator ¶
load_vendor_generator(
pickle_path: str | Path,
*,
vendor_root: str | Path,
device: str | device = "cpu",
) -> tuple[_VendorGeneratorStateProtocol, bool]
Load the original G_ema generator with conversion-only shims.
Source code in models/layout-detr/src/layout_detr/vendor_state.py
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 | |
strict_load_converted_state ¶
strict_load_converted_state(
model: LayoutDetrForConditionalGeneration,
state: Mapping[str, Shaped[Tensor, "..."]],
) -> LayoutDetrConversionReport
Strict-load a remapped state dict and return diagnostics.
Source code in models/layout-detr/src/layout_detr/vendor_state.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
temporary_sys_path ¶
temporary_sys_path(path: Path) -> Iterator[None]
Temporarily prepend a vendor path during conversion-only imports.
Source code in models/layout-detr/src/layout_detr/vendor_state.py
287 288 289 290 291 292 293 294 295 296 297 298 | |