Cgb dm
CGB-DM content-aware poster layout generation package.
CGBDMConfig ¶
Bases: ConfigMixin
Store CGB-DM architecture, schedule, and dataset metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Poster/content dataset key. |
pku_posterlayout
|
num_labels
|
int | None
|
Internal class-channel count, including invalid/pad. |
None
|
max_seq_length
|
int
|
Maximum number of layout elements. |
16
|
image_size
|
tuple[int, int] | list[int]
|
Model image size as |
(384, 256)
|
canvas_size
|
tuple[int, int] | list[int]
|
Dataset canvas size as |
(513, 750)
|
num_train_timesteps
|
int
|
DDPM training timesteps. |
1000
|
ddim_num_steps
|
int
|
Default DDIM inference steps. |
100
|
dim_model
|
int
|
Transformer hidden dimension. |
512
|
n_head
|
int
|
Attention head count. |
8
|
num_layers
|
int
|
Number of layout decoder layers. |
4
|
feature_dim
|
int
|
Feed-forward hidden dimension. |
1024
|
id2label
|
Id2LabelMapping | None
|
Public id-to-label mapping, excluding invalid/pad. |
None
|
Examples:
>>> CGBDMConfig().dataset_name
'pku_posterlayout'
Source code in models/cgb-dm/src/cgb_dm/configuration_cgb_dm.py
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 | |
__init__ ¶
__init__(
*,
dataset_name: DatasetName
| str = DatasetName.pku_posterlayout,
num_labels: int | None = None,
max_seq_length: int = 16,
image_size: tuple[int, int] | list[int] = (384, 256),
canvas_size: tuple[int, int] | list[int] = (513, 750),
num_train_timesteps: int = 1000,
ddim_num_steps: int = 100,
dim_model: int = 512,
n_head: int = 8,
num_layers: int = 4,
feature_dim: int = 1024,
id2label: Id2LabelMapping | None = None,
condition_types: list[str]
| tuple[str, ...]
| None = None,
train_beta_schedule: str = "cosine",
sampling_beta_schedule: str = "linear",
model_subfolder: str = "model",
scheduler_subfolder: str = "scheduler",
processor_subfolder: str = "processor",
) -> None
Initialize CGB-DM configuration.
Source code in models/cgb-dm/src/cgb_dm/configuration_cgb_dm.py
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 | |
CGBDMModelOutput
dataclass
¶
Bases: BaseOutput
Output returned by the CGB-DM denoiser.
Attributes:
| Name | Type | Description |
|---|---|---|
sample |
Float[Tensor, 'batch elements channels']
|
Predicted epsilon tensor with the same shape as the input layout. |
cgb_weight |
Float[Tensor, 'batch 1 1'] | None
|
Content-graphic balance weight estimated from image tokens. |
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
22 23 24 25 26 27 28 29 30 31 32 | |
CGBDMTransformerModel ¶
Bases: ModelMixin, ConfigMixin
CGB-DM transformer denoiser with image and saliency conditioning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_labels
|
int
|
Internal class-channel count including invalid/pad. |
4
|
max_seq_length
|
int
|
Maximum number of layout elements. |
16
|
image_size
|
tuple[int, int] | list[int]
|
Image tensor size as |
(384, 256)
|
patch_size
|
int
|
Image patch size. |
32
|
dim_model
|
int
|
Hidden dimension. |
512
|
n_head
|
int
|
Attention head count. |
8
|
feature_dim
|
int
|
Feed-forward hidden dimension. |
1024
|
num_layers
|
int
|
Number of decoder layers. |
4
|
num_train_timesteps
|
int
|
Number of training diffusion steps. |
1000
|
Examples:
>>> model = CGBDMTransformerModel(num_labels=4, max_seq_length=2, image_size=(32, 32), dim_model=16, n_head=2, feature_dim=32, num_layers=1)
>>> model.seq_dim
8
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 558 559 560 561 562 563 564 565 566 567 568 569 | |
__init__ ¶
__init__(
*,
num_labels: int = 4,
max_seq_length: int = 16,
image_size: tuple[int, int] | list[int] = (384, 256),
patch_size: int = 32,
dim_model: int = 512,
n_head: int = 8,
feature_dim: int = 1024,
num_layers: int = 4,
num_train_timesteps: int = 1000,
) -> None
Initialize the CGB-DM denoising network.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 | |
forward ¶
forward(
sample: Float[Tensor, "batch elements channels"],
image: Float[Tensor, "batch 4 height width"],
saliency_box: Float[Tensor, "batch 1 4"],
timestep: Int[Tensor, "batch"],
return_dict: bool = True,
) -> (
CGBDMModelOutput
| tuple[Float[torch.Tensor, "batch elements channels"]]
)
Predict epsilon for a noisy layout tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Float[Tensor, 'batch elements channels']
|
Noisy class-plus-box layout tensor. |
required |
image
|
Float[Tensor, 'batch 4 height width']
|
Four-channel RGB/saliency tensor in |
required |
saliency_box
|
Float[Tensor, 'batch 1 4']
|
Saliency box tensor in internal |
required |
timestep
|
Int[Tensor, 'batch']
|
Per-example diffusion timestep ids. |
required |
return_dict
|
bool
|
Whether to return |
True
|
Returns:
| Type | Description |
|---|---|
CGBDMModelOutput | tuple[Float[Tensor, 'batch elements channels']]
|
Output dataclass or one-item tuple containing predicted epsilon. |
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
CGBDMPipeline ¶
Bases: DiffusionPipeline
Generate content-aware poster layouts with CGB-DM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
CGBDMTransformerModel
|
CGB-DM denoiser. |
required |
scheduler
|
CGBDMScheduler
|
CGB-DM scheduler. |
required |
processor
|
CGBDMProcessor
|
Processor for images and layouts. |
required |
Examples:
>>> model = CGBDMTransformerModel(num_labels=4, max_seq_length=2, image_size=(32, 32), dim_model=16, n_head=2, feature_dim=32, num_layers=1)
>>> pipe = CGBDMPipeline(model=model, scheduler=CGBDMScheduler(num_train_timesteps=10, ddim_num_steps=1), processor=CGBDMProcessor(max_seq_length=2, image_size=(32, 32)))
>>> pipe.processor.seq_dim
8
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
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 | |
components
property
¶
components: dict[
str,
CGBDMTransformerModel | CGBDMScheduler | CGBDMProcessor,
]
Return serializable pipeline components.
__init__ ¶
__init__(
model: CGBDMTransformerModel,
scheduler: CGBDMScheduler,
processor: CGBDMProcessor,
) -> None
Initialize pipeline components.
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
110 111 112 113 114 115 116 117 118 119 120 121 122 | |
__call__ ¶
__call__(
*,
image: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| None = None,
content: dict[
str,
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes],
]
| None = None,
saliency: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| None = None,
saliency_isnet: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| None = None,
saliency_basnet: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| None = None,
saliency_box: Float[Tensor, "..."] | None = None,
pixel_values: Float[
Tensor, "batch channels height width"
]
| None = None,
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.content_image,
labels: Int[Tensor, "..."]
| Int[ndarray, "..."]
| list[list[int]]
| list[int]
| list[ArrayLikeInput]
| None = None,
bbox: Float[Tensor, "..."]
| Float[ndarray, "..."]
| list[list[list[float]]]
| list[list[float]]
| list[ArrayLikeInput]
| None = None,
mask: Bool[Tensor, "..."]
| Bool[ndarray, "..."]
| list[list[bool]]
| list[bool]
| list[ArrayLikeInput]
| None = None,
num_elements: int
| list[int]
| Int[Tensor, "..."]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
completion_ratio: float = 0.2,
output_type: OutputType | str = OutputType.dataclass,
return_intermediates: bool = False,
) -> (
LayoutGenerationOutput
| dict[
str,
Float[torch.Tensor, "..."]
| Int[torch.Tensor, "..."]
| Bool[torch.Tensor, "..."]
| dict[int, str]
| list[Float[torch.Tensor, "..."]]
| dict[str, str | Float[torch.Tensor, "..."] | None]
| None,
]
)
Run DDIM sampling and return generated layouts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes] | None
|
RGB image or batch of images. |
None
|
content
|
dict[str, Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes]] | None
|
Optional content container with |
None
|
saliency
|
Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes] | None
|
Optional merged saliency map. |
None
|
saliency_isnet
|
Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes] | None
|
Optional first saliency map. |
None
|
saliency_basnet
|
Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes] | None
|
Optional second saliency map. |
None
|
saliency_box
|
Float[Tensor, '...'] | None
|
Optional normalized center |
None
|
pixel_values
|
Float[Tensor, 'batch channels height width'] | None
|
Preprocessed four-channel image tensor. |
None
|
batch_size
|
int
|
Number of layouts when |
1
|
seed
|
int | None
|
Convenience seed used when |
None
|
generator
|
Generator | None
|
Optional torch generator. Takes precedence over |
None
|
condition_type
|
ConditionType | str
|
Canonical condition mode or alias. |
content_image
|
labels
|
Int[Tensor, '...'] | Int[ndarray, '...'] | list[list[int]] | list[int] | list[ArrayLikeInput] | None
|
Conditioning labels for constrained modes. |
None
|
bbox
|
Float[Tensor, '...'] | Float[ndarray, '...'] | list[list[list[float]]] | list[list[float]] | list[ArrayLikeInput] | None
|
Conditioning boxes for constrained modes. |
None
|
mask
|
Bool[Tensor, '...'] | Bool[ndarray, '...'] | list[list[bool]] | list[bool] | list[ArrayLikeInput] | None
|
Optional valid-element mask. |
None
|
num_elements
|
int | list[int] | Int[Tensor, '...'] | None
|
Accepted for interface compatibility. |
None
|
box_format
|
BoxFormat | str
|
Input box format. |
xywh
|
normalized
|
bool
|
Whether input boxes are normalized. |
True
|
canvas_size
|
tuple[int, int] | None
|
Canvas size required for pixel boxes. |
None
|
num_inference_steps
|
int | None
|
DDIM step count. |
None
|
completion_ratio
|
float
|
Completion conditioning keep ratio. |
0.2
|
output_type
|
OutputType | str
|
|
dataclass
|
return_intermediates
|
bool
|
Whether to include trajectory/debug tensors. |
False
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Float[Tensor, '...'] | Int[Tensor, '...'] | Bool[Tensor, '...'] | dict[int, str] | list[Float[Tensor, '...']] | dict[str, str | Float[Tensor, '...'] | None] | None]
|
Layout output dataclass or dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required content or conditioning inputs are absent. |
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.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 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 | |
OutputType ¶
Bases: StrEnum
Supported CGB-DM pipeline output containers.
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
25 26 27 28 29 | |
CGBDMProcessor ¶
Bases: ProcessorMixin
Prepare RGB/saliency inputs and decode CGB-DM layout tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Poster/content dataset key. |
pku_posterlayout
|
id2label
|
Mapping[int, str] | Mapping[str, str] | None
|
Public id-to-label mapping excluding invalid/pad. |
None
|
num_labels
|
int
|
Internal class-channel count. |
4
|
max_seq_length
|
int
|
Maximum number of elements. |
16
|
image_size
|
tuple[int, int] | list[int]
|
Resize target as |
(384, 256)
|
Examples:
>>> CGBDMProcessor().seq_dim
8
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
__init__ ¶
__init__(
dataset_name: DatasetName
| str = DatasetName.pku_posterlayout,
id2label: Mapping[int, str]
| Mapping[str, str]
| None = None,
num_labels: int = 4,
max_seq_length: int = 16,
image_size: tuple[int, int] | list[int] = (384, 256),
) -> None
Initialize processor metadata.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
__call__ ¶
__call__(
images: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| Sequence[
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
]
| None = None,
*,
saliency: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| Sequence[
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
]
| None = None,
saliency_isnet: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| Sequence[
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
]
| None = None,
saliency_basnet: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| Sequence[
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
]
| None = None,
saliency_box: Float[Tensor, "..."] | None = None,
return_tensors: Literal["pt"] = "pt",
) -> BatchEncoding
Encode image and saliency inputs into model tensors.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
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 | |
encode_layout ¶
encode_layout(
*,
bbox: Float[Tensor, "..."]
| Float[ndarray, "..."]
| Sequence[Sequence[Sequence[float]]]
| Sequence[Sequence[float]]
| Sequence[ArrayLikeInput],
labels: Int[Tensor, "..."]
| Int[ndarray, "..."]
| Sequence[Sequence[int]]
| Sequence[int]
| Sequence[ArrayLikeInput],
mask: Bool[Tensor, "..."]
| Bool[ndarray, "..."]
| Sequence[Sequence[bool]]
| Sequence[bool]
| Sequence[ArrayLikeInput]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
) -> CGBDMEncodedLayout
Encode public layout tensors into CGB-DM latent layout format.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
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 | |
pad ¶
pad(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"] | None = None,
) -> tuple[
Float[torch.Tensor, "batch elements 4"],
Int[torch.Tensor, "batch elements"],
Bool[torch.Tensor, "batch elements"],
]
Pad public layout tensors to max_seq_length.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
encode ¶
encode(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"] | None = None,
) -> Float[torch.Tensor, "batch elements channels"]
Encode normalized boxes and public labels into internal tensors.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
decode ¶
decode(
layout: Float[Tensor, "batch elements channels"],
*,
output_type: Literal["dataclass", "dict"] = "dataclass",
scores: Float[Tensor, "..."] | None = None,
intermediates: dict[
str, str | Float[Tensor, "..."] | None
]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Float[torch.Tensor, "..."]
| Int[torch.Tensor, "..."]
| Bool[torch.Tensor, "..."]
| dict[int, str]
| dict[str, str | Float[torch.Tensor, "..."] | None]
| None,
]
)
Decode internal layout tensors into the public schema.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
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 | |
CGBDMScheduler ¶
Bases: SchedulerMixin, ConfigMixin
CGB-DM scheduler preserving separate training and sampling schedules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_train_timesteps
|
int
|
Number of DDPM training steps. |
1000
|
ddim_num_steps
|
int
|
Default DDIM inference step count. |
100
|
train_beta_schedule
|
CGBDMBetaSchedule | str
|
Schedule for training noising buffers. |
cosine
|
sampling_beta_schedule
|
CGBDMBetaSchedule | str
|
Schedule for DDIM sampling buffers. |
linear
|
eta
|
float
|
DDIM stochasticity. |
0.0
|
Examples:
>>> scheduler = CGBDMScheduler(num_train_timesteps=10, ddim_num_steps=2)
>>> scheduler.ddim_timesteps.tolist()
[0, 5]
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
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 | |
__init__ ¶
__init__(
*,
num_train_timesteps: int = 1000,
ddim_num_steps: int = 100,
train_beta_schedule: CGBDMBetaSchedule
| str = CGBDMBetaSchedule.cosine,
sampling_beta_schedule: CGBDMBetaSchedule
| str = CGBDMBetaSchedule.linear,
eta: float = 0.0,
) -> None
Initialize noising and sampling buffers.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
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 | |
set_timesteps ¶
set_timesteps(
num_inference_steps: int | None = None,
device: device | None = None,
) -> None
Set DDIM timesteps and derived sampling parameters.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.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 | |
sample_timesteps ¶
sample_timesteps(
batch_size: int,
*,
device: device,
generator: Generator | None = None,
t_max: int | None = None,
) -> Int[torch.Tensor, "batch"]
Sample training timesteps.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
168 169 170 171 172 173 174 175 176 177 178 | |
add_noise ¶
add_noise(
original_samples: Float[
Tensor, "batch elements channels"
],
noise: Float[Tensor, "batch elements channels"],
timesteps: Int[Tensor, "batch"],
*,
fix_mask: Bool[Tensor, "batch elements channels"]
| None = None,
) -> Float[torch.Tensor, "batch elements channels"]
Add training noise, preserving fixed channels when requested.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
initial_sample ¶
initial_sample(
batch_size: int,
seq_len: int,
seq_dim: int,
*,
device: device,
generator: Generator | None = None,
) -> Float[torch.Tensor, "batch elements channels"]
Create the initial DDIM sample.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
198 199 200 201 202 203 204 205 206 207 208 209 210 | |
condition_mask ¶
condition_mask(
layout: Float[Tensor, "batch elements channels"],
condition_type: ConditionType,
*,
completion_ratio: float = 0.2,
generator: Generator | None = None,
) -> Bool[torch.Tensor, "batch elements channels"]
Build a channel-level mask for fixed conditioning values.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
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 | |
step ¶
step(
model_output: Float[Tensor, "batch elements channels"],
timestep: Int[Tensor, "batch"],
sample: Float[Tensor, "batch elements channels"],
index: int,
generator: Generator | None = None,
) -> CGBDMSchedulerOutput
Take one DDIM reverse step.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
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 | |
CGBDMSchedulerOutput
dataclass
¶
Bases: BaseOutput
Output returned by one CGB-DM DDIM step.
Attributes:
| Name | Type | Description |
|---|---|---|
prev_sample |
Float[Tensor, 'batch elements channels']
|
Layout sample for the next denoising step. |
pred_original_sample |
Float[Tensor, 'batch elements channels']
|
Estimated clean layout sample. |
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
29 30 31 32 33 34 35 36 37 38 39 | |
cgb_dm_config_for_dataset ¶
cgb_dm_config_for_dataset(
dataset_name: DatasetName | str,
) -> CGBDMConfig
Build a CGB-DM config for a supported dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key or enum. |
required |
Returns:
| Type | Description |
|---|---|
CGBDMConfig
|
Dataset-specific CGB-DM config. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset is unsupported. |
Examples:
>>> cgb_dm_config_for_dataset("cgl").num_labels
5
Source code in models/cgb-dm/src/cgb_dm/configuration_cgb_dm.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
convert_state_dict ¶
convert_state_dict(
state_dict: Mapping[str, Float[Tensor, "..."]],
) -> dict[str, Float[torch.Tensor, "..."]]
Normalize CGB-DM checkpoint keys for CGBDMTransformerModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
Mapping[str, Float[Tensor, '...']]
|
Original or Lightning checkpoint state dictionary. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Float[Tensor, '...']]
|
Converted state dictionary with common wrapper prefixes stripped. |
Examples:
>>> convert_state_dict({"model.module.img_encoder.patch.weight": torch.zeros(1)})["img_encoder.patch.weight"].shape
torch.Size([1])
Source code in models/cgb-dm/src/cgb_dm/conversion.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
normalize_condition_type ¶
normalize_condition_type(
condition_type: ConditionType | str | None,
) -> ConditionType
Normalize CGB-DM condition aliases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition_type
|
ConditionType | str | None
|
Canonical condition enum, alias, or |
required |
Returns:
| Type | Description |
|---|---|
ConditionType
|
Canonical condition enum. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the condition is unsupported. |
Examples:
>>> str(normalize_condition_type("uncond"))
'content_image'
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
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 | |
configuration_cgb_dm ¶
Configuration metadata for CGB-DM checkpoints.
CGBDMDatasetSpec
dataclass
¶
Dataset defaults used by CGB-DM training and inference configs.
Attributes:
| Name | Type | Description |
|---|---|---|
dataset_name |
DatasetName
|
Canonical poster/content dataset enum. |
num_labels |
int
|
Number of internal class channels, including invalid/pad. |
train_batch_size |
int
|
Reference training batch size. |
id2label |
dict[int, str]
|
Public label map persisted in checkpoints. |
Source code in models/cgb-dm/src/cgb_dm/configuration_cgb_dm.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
CGBDMConfig ¶
Bases: ConfigMixin
Store CGB-DM architecture, schedule, and dataset metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Poster/content dataset key. |
pku_posterlayout
|
num_labels
|
int | None
|
Internal class-channel count, including invalid/pad. |
None
|
max_seq_length
|
int
|
Maximum number of layout elements. |
16
|
image_size
|
tuple[int, int] | list[int]
|
Model image size as |
(384, 256)
|
canvas_size
|
tuple[int, int] | list[int]
|
Dataset canvas size as |
(513, 750)
|
num_train_timesteps
|
int
|
DDPM training timesteps. |
1000
|
ddim_num_steps
|
int
|
Default DDIM inference steps. |
100
|
dim_model
|
int
|
Transformer hidden dimension. |
512
|
n_head
|
int
|
Attention head count. |
8
|
num_layers
|
int
|
Number of layout decoder layers. |
4
|
feature_dim
|
int
|
Feed-forward hidden dimension. |
1024
|
id2label
|
Id2LabelMapping | None
|
Public id-to-label mapping, excluding invalid/pad. |
None
|
Examples:
>>> CGBDMConfig().dataset_name
'pku_posterlayout'
Source code in models/cgb-dm/src/cgb_dm/configuration_cgb_dm.py
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 | |
__init__ ¶
__init__(
*,
dataset_name: DatasetName
| str = DatasetName.pku_posterlayout,
num_labels: int | None = None,
max_seq_length: int = 16,
image_size: tuple[int, int] | list[int] = (384, 256),
canvas_size: tuple[int, int] | list[int] = (513, 750),
num_train_timesteps: int = 1000,
ddim_num_steps: int = 100,
dim_model: int = 512,
n_head: int = 8,
num_layers: int = 4,
feature_dim: int = 1024,
id2label: Id2LabelMapping | None = None,
condition_types: list[str]
| tuple[str, ...]
| None = None,
train_beta_schedule: str = "cosine",
sampling_beta_schedule: str = "linear",
model_subfolder: str = "model",
scheduler_subfolder: str = "scheduler",
processor_subfolder: str = "processor",
) -> None
Initialize CGB-DM configuration.
Source code in models/cgb-dm/src/cgb_dm/configuration_cgb_dm.py
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 | |
cgb_dm_config_for_dataset ¶
cgb_dm_config_for_dataset(
dataset_name: DatasetName | str,
) -> CGBDMConfig
Build a CGB-DM config for a supported dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key or enum. |
required |
Returns:
| Type | Description |
|---|---|
CGBDMConfig
|
Dataset-specific CGB-DM config. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset is unsupported. |
Examples:
>>> cgb_dm_config_for_dataset("cgl").num_labels
5
Source code in models/cgb-dm/src/cgb_dm/configuration_cgb_dm.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
conversion ¶
Conversion helpers for CGB-DM checkpoints.
convert_state_dict ¶
convert_state_dict(
state_dict: Mapping[str, Float[Tensor, "..."]],
) -> dict[str, Float[torch.Tensor, "..."]]
Normalize CGB-DM checkpoint keys for CGBDMTransformerModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
Mapping[str, Float[Tensor, '...']]
|
Original or Lightning checkpoint state dictionary. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Float[Tensor, '...']]
|
Converted state dictionary with common wrapper prefixes stripped. |
Examples:
>>> convert_state_dict({"model.module.img_encoder.patch.weight": torch.zeros(1)})["img_encoder.patch.weight"].shape
torch.Size([1])
Source code in models/cgb-dm/src/cgb_dm/conversion.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
load_state_dict ¶
load_state_dict(
path: str | Path,
) -> dict[str, Float[torch.Tensor, "..."]]
Load a state-dict-like checkpoint from disk.
Source code in models/cgb-dm/src/cgb_dm/conversion.py
44 45 46 47 48 49 50 51 | |
build_model_from_config ¶
build_model_from_config(
config: CGBDMConfig,
) -> CGBDMTransformerModel
Build the CGB-DM denoiser shape described by config.
Source code in models/cgb-dm/src/cgb_dm/conversion.py
54 55 56 57 58 59 60 61 62 63 64 65 | |
build_pipeline_from_checkpoint ¶
build_pipeline_from_checkpoint(
checkpoint_path: str | Path, *, config: CGBDMConfig
) -> CGBDMPipeline
Build a CGB-DM pipeline from a package-local training checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint_path
|
str | Path
|
Path to a PyTorch checkpoint. |
required |
config
|
CGBDMConfig
|
CGB-DM config that matches the training checkpoint. |
required |
Returns:
| Type | Description |
|---|---|
CGBDMPipeline
|
Pipeline with converted model weights loaded. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the checkpoint keys do not match the model. |
Source code in models/cgb-dm/src/cgb_dm/conversion.py
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 | |
data ¶
Dataset utilities for CGB-DM original zip extracts.
CGBDMDataPaths
dataclass
¶
Paths for one CGB-DM split in an extracted dataset tree.
Source code in models/cgb-dm/src/cgb_dm/data.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 | |
CGBDMOriginalDataset ¶
Bases: Dataset[dict[str, Float[Tensor, '...']]]
Read an extracted CGB-DM dataset split without downloading assets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str | Path
|
Extracted dataset root. |
required |
split
|
Literal['train', 'val', 'test']
|
Dataset split name. |
'train'
|
processor
|
CGBDMProcessor | None
|
Processor used for image/layout normalization. |
None
|
Examples:
>>> CGBDMDataPaths(Path("/tmp/data")).annotation_csv.name
'train.csv'
Source code in models/cgb-dm/src/cgb_dm/data.py
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 | |
__init__ ¶
__init__(
root: str | Path,
*,
split: Literal["train", "val", "test"] = "train",
processor: CGBDMProcessor | None = None,
name_manifest: str
| Path
| list[str]
| tuple[str, ...]
| None = None,
encoding: Literal["public", "reference"] = "public",
) -> None
Initialize file lists and CSV indexes.
Source code in models/cgb-dm/src/cgb_dm/data.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
__len__ ¶
__len__() -> int
Return number of image rows.
Source code in models/cgb-dm/src/cgb_dm/data.py
86 87 88 | |
__getitem__ ¶
__getitem__(
index: int,
) -> dict[str, Float[torch.Tensor, "..."]]
Return one normalized CGB-DM training row.
Source code in models/cgb-dm/src/cgb_dm/data.py
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 | |
modeling_cgb_dm ¶
Transformer denoiser used by CGB-DM checkpoints.
CGBDMModelOutput
dataclass
¶
Bases: BaseOutput
Output returned by the CGB-DM denoiser.
Attributes:
| Name | Type | Description |
|---|---|---|
sample |
Float[Tensor, 'batch elements channels']
|
Predicted epsilon tensor with the same shape as the input layout. |
cgb_weight |
Float[Tensor, 'batch 1 1'] | None
|
Content-graphic balance weight estimated from image tokens. |
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
22 23 24 25 26 27 28 29 30 31 32 | |
CGBDMImageEncoder ¶
Bases: Module
Patch image encoder used for content-aware conditioning.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 | |
__init__ ¶
__init__(
*,
image_size: tuple[int, int],
patch_size: int,
in_channels: int,
dim_model: int,
depth: int = 6,
heads: int = 8,
mlp_dim: int = 2048,
dim_head: int = 64,
dropout: float = 0.1,
emb_dropout: float = 0.1,
) -> None
Initialize patch embedding and image transformer blocks.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 | |
forward ¶
forward(
image: Float[Tensor, "batch channels height width"],
) -> Float[torch.Tensor, "batch tokens channels"]
Encode image tensors into patch tokens.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
164 165 166 167 168 169 170 171 172 173 | |
CGBDMLayoutModule ¶
Bases: Module
Timestep-conditioned layout encoder or decoder block stack.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 | |
__init__ ¶
__init__(
*,
seq_dim: int,
dim_model: int,
n_head: int,
feature_dim: int,
num_layers: int,
num_train_timesteps: int,
max_seq_length: int,
if_encoder: bool,
) -> None
Initialize layout projections and timestep-aware blocks.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 | |
forward ¶
forward(
src: Float[Tensor, "batch elements channels"],
img_encode: Float[Tensor, "..."] | None,
cgb_w: Float[Tensor, "..."] | None,
salbox_encode: Float[Tensor, "..."] | None,
timestep: Int[Tensor, "batch"],
) -> Float[Tensor, "batch elements channels"]
Run the layout encoder or decoder path.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.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 | |
CGBDMQFormer ¶
Bases: Module
Estimate a scalar content-graphic balance weight from image tokens.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 | |
__init__ ¶
__init__(
in_dim: int = 512,
out_dim: int = 1,
num_heads: int = 8,
num_tokens: int = 1,
n_layers: int = 2,
) -> None
Initialize query-token transformer and scalar projection.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
forward ¶
forward(
image_tokens: Float[Tensor, "batch tokens channels"],
) -> Float[torch.Tensor, "batch tokens 1"]
Pool image tokens into a content-graphic balance weight.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
440 441 442 443 444 445 446 447 448 449 | |
CGBDMMLP ¶
Bases: _LayoutMLP
Softplus MLP used for saliency-box embeddings.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
452 453 454 455 456 457 | |
__init__ ¶
__init__(
input_dim: int, hidden_dim: int, output_dim: int
) -> None
Initialize saliency-box embedding layers.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
455 456 457 | |
CGBDMTransformerModel ¶
Bases: ModelMixin, ConfigMixin
CGB-DM transformer denoiser with image and saliency conditioning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_labels
|
int
|
Internal class-channel count including invalid/pad. |
4
|
max_seq_length
|
int
|
Maximum number of layout elements. |
16
|
image_size
|
tuple[int, int] | list[int]
|
Image tensor size as |
(384, 256)
|
patch_size
|
int
|
Image patch size. |
32
|
dim_model
|
int
|
Hidden dimension. |
512
|
n_head
|
int
|
Attention head count. |
8
|
feature_dim
|
int
|
Feed-forward hidden dimension. |
1024
|
num_layers
|
int
|
Number of decoder layers. |
4
|
num_train_timesteps
|
int
|
Number of training diffusion steps. |
1000
|
Examples:
>>> model = CGBDMTransformerModel(num_labels=4, max_seq_length=2, image_size=(32, 32), dim_model=16, n_head=2, feature_dim=32, num_layers=1)
>>> model.seq_dim
8
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 558 559 560 561 562 563 564 565 566 567 568 569 | |
__init__ ¶
__init__(
*,
num_labels: int = 4,
max_seq_length: int = 16,
image_size: tuple[int, int] | list[int] = (384, 256),
patch_size: int = 32,
dim_model: int = 512,
n_head: int = 8,
feature_dim: int = 1024,
num_layers: int = 4,
num_train_timesteps: int = 1000,
) -> None
Initialize the CGB-DM denoising network.
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
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 | |
forward ¶
forward(
sample: Float[Tensor, "batch elements channels"],
image: Float[Tensor, "batch 4 height width"],
saliency_box: Float[Tensor, "batch 1 4"],
timestep: Int[Tensor, "batch"],
return_dict: bool = True,
) -> (
CGBDMModelOutput
| tuple[Float[torch.Tensor, "batch elements channels"]]
)
Predict epsilon for a noisy layout tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Float[Tensor, 'batch elements channels']
|
Noisy class-plus-box layout tensor. |
required |
image
|
Float[Tensor, 'batch 4 height width']
|
Four-channel RGB/saliency tensor in |
required |
saliency_box
|
Float[Tensor, 'batch 1 4']
|
Saliency box tensor in internal |
required |
timestep
|
Int[Tensor, 'batch']
|
Per-example diffusion timestep ids. |
required |
return_dict
|
bool
|
Whether to return |
True
|
Returns:
| Type | Description |
|---|---|
CGBDMModelOutput | tuple[Float[Tensor, 'batch elements channels']]
|
Output dataclass or one-item tuple containing predicted epsilon. |
Source code in models/cgb-dm/src/cgb_dm/modeling_cgb_dm.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
pipeline_cgb_dm ¶
Diffusers pipeline for CGB-DM content-aware layout generation.
OutputType ¶
Bases: StrEnum
Supported CGB-DM pipeline output containers.
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
25 26 27 28 29 | |
CGBDMPipeline ¶
Bases: DiffusionPipeline
Generate content-aware poster layouts with CGB-DM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
CGBDMTransformerModel
|
CGB-DM denoiser. |
required |
scheduler
|
CGBDMScheduler
|
CGB-DM scheduler. |
required |
processor
|
CGBDMProcessor
|
Processor for images and layouts. |
required |
Examples:
>>> model = CGBDMTransformerModel(num_labels=4, max_seq_length=2, image_size=(32, 32), dim_model=16, n_head=2, feature_dim=32, num_layers=1)
>>> pipe = CGBDMPipeline(model=model, scheduler=CGBDMScheduler(num_train_timesteps=10, ddim_num_steps=1), processor=CGBDMProcessor(max_seq_length=2, image_size=(32, 32)))
>>> pipe.processor.seq_dim
8
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
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 | |
components
property
¶
components: dict[
str,
CGBDMTransformerModel | CGBDMScheduler | CGBDMProcessor,
]
Return serializable pipeline components.
__init__ ¶
__init__(
model: CGBDMTransformerModel,
scheduler: CGBDMScheduler,
processor: CGBDMProcessor,
) -> None
Initialize pipeline components.
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
110 111 112 113 114 115 116 117 118 119 120 121 122 | |
__call__ ¶
__call__(
*,
image: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| None = None,
content: dict[
str,
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes],
]
| None = None,
saliency: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| None = None,
saliency_isnet: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| None = None,
saliency_basnet: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| None = None,
saliency_box: Float[Tensor, "..."] | None = None,
pixel_values: Float[
Tensor, "batch channels height width"
]
| None = None,
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.content_image,
labels: Int[Tensor, "..."]
| Int[ndarray, "..."]
| list[list[int]]
| list[int]
| list[ArrayLikeInput]
| None = None,
bbox: Float[Tensor, "..."]
| Float[ndarray, "..."]
| list[list[list[float]]]
| list[list[float]]
| list[ArrayLikeInput]
| None = None,
mask: Bool[Tensor, "..."]
| Bool[ndarray, "..."]
| list[list[bool]]
| list[bool]
| list[ArrayLikeInput]
| None = None,
num_elements: int
| list[int]
| Int[Tensor, "..."]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
completion_ratio: float = 0.2,
output_type: OutputType | str = OutputType.dataclass,
return_intermediates: bool = False,
) -> (
LayoutGenerationOutput
| dict[
str,
Float[torch.Tensor, "..."]
| Int[torch.Tensor, "..."]
| Bool[torch.Tensor, "..."]
| dict[int, str]
| list[Float[torch.Tensor, "..."]]
| dict[str, str | Float[torch.Tensor, "..."] | None]
| None,
]
)
Run DDIM sampling and return generated layouts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes] | None
|
RGB image or batch of images. |
None
|
content
|
dict[str, Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes]] | None
|
Optional content container with |
None
|
saliency
|
Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes] | None
|
Optional merged saliency map. |
None
|
saliency_isnet
|
Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes] | None
|
Optional first saliency map. |
None
|
saliency_basnet
|
Float[Tensor, '...'] | Image | str | bytes | Path | IO[bytes] | None
|
Optional second saliency map. |
None
|
saliency_box
|
Float[Tensor, '...'] | None
|
Optional normalized center |
None
|
pixel_values
|
Float[Tensor, 'batch channels height width'] | None
|
Preprocessed four-channel image tensor. |
None
|
batch_size
|
int
|
Number of layouts when |
1
|
seed
|
int | None
|
Convenience seed used when |
None
|
generator
|
Generator | None
|
Optional torch generator. Takes precedence over |
None
|
condition_type
|
ConditionType | str
|
Canonical condition mode or alias. |
content_image
|
labels
|
Int[Tensor, '...'] | Int[ndarray, '...'] | list[list[int]] | list[int] | list[ArrayLikeInput] | None
|
Conditioning labels for constrained modes. |
None
|
bbox
|
Float[Tensor, '...'] | Float[ndarray, '...'] | list[list[list[float]]] | list[list[float]] | list[ArrayLikeInput] | None
|
Conditioning boxes for constrained modes. |
None
|
mask
|
Bool[Tensor, '...'] | Bool[ndarray, '...'] | list[list[bool]] | list[bool] | list[ArrayLikeInput] | None
|
Optional valid-element mask. |
None
|
num_elements
|
int | list[int] | Int[Tensor, '...'] | None
|
Accepted for interface compatibility. |
None
|
box_format
|
BoxFormat | str
|
Input box format. |
xywh
|
normalized
|
bool
|
Whether input boxes are normalized. |
True
|
canvas_size
|
tuple[int, int] | None
|
Canvas size required for pixel boxes. |
None
|
num_inference_steps
|
int | None
|
DDIM step count. |
None
|
completion_ratio
|
float
|
Completion conditioning keep ratio. |
0.2
|
output_type
|
OutputType | str
|
|
dataclass
|
return_intermediates
|
bool
|
Whether to include trajectory/debug tensors. |
False
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Float[Tensor, '...'] | Int[Tensor, '...'] | Bool[Tensor, '...'] | dict[int, str] | list[Float[Tensor, '...']] | dict[str, str | Float[Tensor, '...'] | None] | None]
|
Layout output dataclass or dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required content or conditioning inputs are absent. |
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.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 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 | |
normalize_condition_type ¶
normalize_condition_type(
condition_type: ConditionType | str | None,
) -> ConditionType
Normalize CGB-DM condition aliases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition_type
|
ConditionType | str | None
|
Canonical condition enum, alias, or |
required |
Returns:
| Type | Description |
|---|---|
ConditionType
|
Canonical condition enum. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the condition is unsupported. |
Examples:
>>> str(normalize_condition_type("uncond"))
'content_image'
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
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 | |
normalize_output_type ¶
normalize_output_type(
output_type: OutputType | str,
) -> OutputType
Normalize output container aliases.
Source code in models/cgb-dm/src/cgb_dm/pipeline_cgb_dm.py
83 84 85 86 87 88 89 90 | |
processing_cgb_dm ¶
Processor for CGB-DM content images and layout tensors.
CGBDMEncodedLayout ¶
Bases: TypedDict
Encoded CGB-DM layout tensors.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
30 31 32 33 34 35 36 | |
CGBDMProcessor ¶
Bases: ProcessorMixin
Prepare RGB/saliency inputs and decode CGB-DM layout tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Poster/content dataset key. |
pku_posterlayout
|
id2label
|
Mapping[int, str] | Mapping[str, str] | None
|
Public id-to-label mapping excluding invalid/pad. |
None
|
num_labels
|
int
|
Internal class-channel count. |
4
|
max_seq_length
|
int
|
Maximum number of elements. |
16
|
image_size
|
tuple[int, int] | list[int]
|
Resize target as |
(384, 256)
|
Examples:
>>> CGBDMProcessor().seq_dim
8
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
__init__ ¶
__init__(
dataset_name: DatasetName
| str = DatasetName.pku_posterlayout,
id2label: Mapping[int, str]
| Mapping[str, str]
| None = None,
num_labels: int = 4,
max_seq_length: int = 16,
image_size: tuple[int, int] | list[int] = (384, 256),
) -> None
Initialize processor metadata.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
__call__ ¶
__call__(
images: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| Sequence[
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
]
| None = None,
*,
saliency: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| Sequence[
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
]
| None = None,
saliency_isnet: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| Sequence[
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
]
| None = None,
saliency_basnet: Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
| Sequence[
Float[Tensor, "..."]
| Image
| str
| bytes
| Path
| IO[bytes]
]
| None = None,
saliency_box: Float[Tensor, "..."] | None = None,
return_tensors: Literal["pt"] = "pt",
) -> BatchEncoding
Encode image and saliency inputs into model tensors.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
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 | |
encode_layout ¶
encode_layout(
*,
bbox: Float[Tensor, "..."]
| Float[ndarray, "..."]
| Sequence[Sequence[Sequence[float]]]
| Sequence[Sequence[float]]
| Sequence[ArrayLikeInput],
labels: Int[Tensor, "..."]
| Int[ndarray, "..."]
| Sequence[Sequence[int]]
| Sequence[int]
| Sequence[ArrayLikeInput],
mask: Bool[Tensor, "..."]
| Bool[ndarray, "..."]
| Sequence[Sequence[bool]]
| Sequence[bool]
| Sequence[ArrayLikeInput]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
) -> CGBDMEncodedLayout
Encode public layout tensors into CGB-DM latent layout format.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
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 | |
pad ¶
pad(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"] | None = None,
) -> tuple[
Float[torch.Tensor, "batch elements 4"],
Int[torch.Tensor, "batch elements"],
Bool[torch.Tensor, "batch elements"],
]
Pad public layout tensors to max_seq_length.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
encode ¶
encode(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"] | None = None,
) -> Float[torch.Tensor, "batch elements channels"]
Encode normalized boxes and public labels into internal tensors.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
decode ¶
decode(
layout: Float[Tensor, "batch elements channels"],
*,
output_type: Literal["dataclass", "dict"] = "dataclass",
scores: Float[Tensor, "..."] | None = None,
intermediates: dict[
str, str | Float[Tensor, "..."] | None
]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Float[torch.Tensor, "..."]
| Int[torch.Tensor, "..."]
| Bool[torch.Tensor, "..."]
| dict[int, str]
| dict[str, str | Float[torch.Tensor, "..."] | None]
| None,
]
)
Decode internal layout tensors into the public schema.
Source code in models/cgb-dm/src/cgb_dm/processing_cgb_dm.py
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 | |
scheduling_cgb_dm ¶
Schedulers for CGB-DM training noising and DDIM sampling.
CGBDMBetaSchedule ¶
Bases: StrEnum
Supported CGB-DM beta schedules.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
22 23 24 25 26 | |
CGBDMSchedulerOutput
dataclass
¶
Bases: BaseOutput
Output returned by one CGB-DM DDIM step.
Attributes:
| Name | Type | Description |
|---|---|---|
prev_sample |
Float[Tensor, 'batch elements channels']
|
Layout sample for the next denoising step. |
pred_original_sample |
Float[Tensor, 'batch elements channels']
|
Estimated clean layout sample. |
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
29 30 31 32 33 34 35 36 37 38 39 | |
CGBDMScheduler ¶
Bases: SchedulerMixin, ConfigMixin
CGB-DM scheduler preserving separate training and sampling schedules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_train_timesteps
|
int
|
Number of DDPM training steps. |
1000
|
ddim_num_steps
|
int
|
Default DDIM inference step count. |
100
|
train_beta_schedule
|
CGBDMBetaSchedule | str
|
Schedule for training noising buffers. |
cosine
|
sampling_beta_schedule
|
CGBDMBetaSchedule | str
|
Schedule for DDIM sampling buffers. |
linear
|
eta
|
float
|
DDIM stochasticity. |
0.0
|
Examples:
>>> scheduler = CGBDMScheduler(num_train_timesteps=10, ddim_num_steps=2)
>>> scheduler.ddim_timesteps.tolist()
[0, 5]
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
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 | |
__init__ ¶
__init__(
*,
num_train_timesteps: int = 1000,
ddim_num_steps: int = 100,
train_beta_schedule: CGBDMBetaSchedule
| str = CGBDMBetaSchedule.cosine,
sampling_beta_schedule: CGBDMBetaSchedule
| str = CGBDMBetaSchedule.linear,
eta: float = 0.0,
) -> None
Initialize noising and sampling buffers.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
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 | |
set_timesteps ¶
set_timesteps(
num_inference_steps: int | None = None,
device: device | None = None,
) -> None
Set DDIM timesteps and derived sampling parameters.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.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 | |
sample_timesteps ¶
sample_timesteps(
batch_size: int,
*,
device: device,
generator: Generator | None = None,
t_max: int | None = None,
) -> Int[torch.Tensor, "batch"]
Sample training timesteps.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
168 169 170 171 172 173 174 175 176 177 178 | |
add_noise ¶
add_noise(
original_samples: Float[
Tensor, "batch elements channels"
],
noise: Float[Tensor, "batch elements channels"],
timesteps: Int[Tensor, "batch"],
*,
fix_mask: Bool[Tensor, "batch elements channels"]
| None = None,
) -> Float[torch.Tensor, "batch elements channels"]
Add training noise, preserving fixed channels when requested.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
initial_sample ¶
initial_sample(
batch_size: int,
seq_len: int,
seq_dim: int,
*,
device: device,
generator: Generator | None = None,
) -> Float[torch.Tensor, "batch elements channels"]
Create the initial DDIM sample.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
198 199 200 201 202 203 204 205 206 207 208 209 210 | |
condition_mask ¶
condition_mask(
layout: Float[Tensor, "batch elements channels"],
condition_type: ConditionType,
*,
completion_ratio: float = 0.2,
generator: Generator | None = None,
) -> Bool[torch.Tensor, "batch elements channels"]
Build a channel-level mask for fixed conditioning values.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
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 | |
step ¶
step(
model_output: Float[Tensor, "batch elements channels"],
timestep: Int[Tensor, "batch"],
sample: Float[Tensor, "batch elements channels"],
index: int,
generator: Generator | None = None,
) -> CGBDMSchedulerOutput
Take one DDIM reverse step.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
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 | |
make_beta_schedule ¶
make_beta_schedule(
schedule: CGBDMBetaSchedule | str,
num_timesteps: int,
*,
start: float = 0.0002,
end: float = 0.04,
) -> Float[torch.Tensor, "timesteps"]
Create a CGB-DM beta schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedule
|
CGBDMBetaSchedule | str
|
Schedule name. |
required |
num_timesteps
|
int
|
Number of diffusion timesteps. |
required |
start
|
float
|
Linear schedule start. |
0.0002
|
end
|
float
|
Linear schedule end. |
0.04
|
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'timesteps']
|
Beta tensor. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the schedule is unsupported. |
Examples:
>>> make_beta_schedule("linear", 4).shape
torch.Size([4])
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.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 | |
make_ddim_timesteps ¶
make_ddim_timesteps(
*,
num_ddim_timesteps: int,
num_ddpm_timesteps: int,
mode: Literal["uniform", "refine"] = "uniform",
) -> Int[np.ndarray, "timesteps"]
Create DDIM timestep ids using CGB-DM discretization rules.
Source code in models/cgb-dm/src/cgb_dm/scheduling_cgb_dm.py
78 79 80 81 82 83 84 85 86 87 88 89 90 | |
training ¶
Training utilities for CGB-DM.
config ¶
Training configuration literals for CGB-DM.
datamodule ¶
PyTorch Lightning data module for CGB-DM training.
CGBDMDataModule ¶
Bases: LightningDataModule
Data module for original-zip or synthetic CGB-DM rows.
Source code in models/cgb-dm/src/cgb_dm/training/datamodule.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 | |
__init__ ¶
__init__(
*,
config: CGBDMConfig
| dict[
str,
str
| int
| tuple[int, int]
| list[int]
| list[str]
| dict[int | str, str]
| None,
],
source: CGBDMDataSource = "synthetic",
data_root: str | None = None,
batch_size: int = 2,
num_workers: int = 0,
source_order_manifest: str | None = None,
original_encoding: Literal[
"public", "reference"
] = "reference",
) -> None
Initialize data module options.
Source code in models/cgb-dm/src/cgb_dm/training/datamodule.py
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 | |
setup ¶
setup(stage: str | None = None) -> None
Create train and validation datasets.
Source code in models/cgb-dm/src/cgb_dm/training/datamodule.py
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 | |
train_dataloader ¶
train_dataloader() -> DataLoader[
dict[str, Float[torch.Tensor, "..."]]
]
Return the training dataloader.
Source code in models/cgb-dm/src/cgb_dm/training/datamodule.py
102 103 104 105 106 107 108 109 | |
val_dataloader ¶
val_dataloader() -> DataLoader[
dict[str, Float[torch.Tensor, "..."]]
]
Return the validation dataloader.
Source code in models/cgb-dm/src/cgb_dm/training/datamodule.py
111 112 113 114 115 116 117 | |
dataset ¶
Training datasets for CGB-DM.
CGBDMOriginalDataset ¶
Bases: Dataset[dict[str, Float[Tensor, '...']]]
Read an extracted CGB-DM dataset split without downloading assets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str | Path
|
Extracted dataset root. |
required |
split
|
Literal['train', 'val', 'test']
|
Dataset split name. |
'train'
|
processor
|
CGBDMProcessor | None
|
Processor used for image/layout normalization. |
None
|
Examples:
>>> CGBDMDataPaths(Path("/tmp/data")).annotation_csv.name
'train.csv'
Source code in models/cgb-dm/src/cgb_dm/data.py
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 | |
__init__ ¶
__init__(
root: str | Path,
*,
split: Literal["train", "val", "test"] = "train",
processor: CGBDMProcessor | None = None,
name_manifest: str
| Path
| list[str]
| tuple[str, ...]
| None = None,
encoding: Literal["public", "reference"] = "public",
) -> None
Initialize file lists and CSV indexes.
Source code in models/cgb-dm/src/cgb_dm/data.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
__len__ ¶
__len__() -> int
Return number of image rows.
Source code in models/cgb-dm/src/cgb_dm/data.py
86 87 88 | |
__getitem__ ¶
__getitem__(
index: int,
) -> dict[str, Float[torch.Tensor, "..."]]
Return one normalized CGB-DM training row.
Source code in models/cgb-dm/src/cgb_dm/data.py
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 | |
CGBDMSyntheticDataset ¶
Bases: Dataset[dict[str, Float[Tensor, '...']]]
Tiny deterministic dataset used by tests and smoke configs.
Source code in models/cgb-dm/src/cgb_dm/training/dataset.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 | |
__init__ ¶
__init__(
*,
length: int = 4,
max_seq_length: int = 4,
seq_dim: int = 8,
image_size: tuple[int, int] = (32, 32),
) -> None
Initialize synthetic tensor shapes.
Source code in models/cgb-dm/src/cgb_dm/training/dataset.py
15 16 17 18 19 20 21 22 23 24 25 26 27 | |
__len__ ¶
__len__() -> int
Return dataset length.
Source code in models/cgb-dm/src/cgb_dm/training/dataset.py
29 30 31 | |
__getitem__ ¶
__getitem__(
index: int,
) -> dict[str, Float[torch.Tensor, "..."]]
Return one deterministic row.
Source code in models/cgb-dm/src/cgb_dm/training/dataset.py
33 34 35 36 37 38 39 40 41 42 43 44 | |
lightning_module ¶
PyTorch Lightning module for CGB-DM training.
CGBDMTrainingModule ¶
Bases: LightningModule
Training wrapper that mirrors CGB-DM denoising-step order.
Source code in models/cgb-dm/src/cgb_dm/training/lightning_module.py
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 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 | |
__init__ ¶
__init__(
*,
config: CGBDMConfig
| dict[
str,
str
| int
| tuple[int, int]
| list[int]
| list[str]
| dict[int | str, str]
| None,
],
optimizer: OptimizerCallable | None = None,
lr_scheduler: LRSchedulerCallable | None = None,
model: CGBDMTransformerModel | None = None,
condition_type: CGBDMCondition = "content_image",
seed_mode: CGBDMSeedMode = "default",
) -> None
Initialize model, scheduler, and optimizer settings.
Source code in models/cgb-dm/src/cgb_dm/training/lightning_module.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 64 65 66 67 68 | |
forward ¶
forward(
sample: Float[Tensor, "batch elements channels"],
image: Float[Tensor, "batch channels height width"],
saliency_box: Float[Tensor, "batch 1 4"],
timestep: Int[Tensor, "batch"],
) -> Float[torch.Tensor, "batch elements channels"]
Predict epsilon for a training sample.
Source code in models/cgb-dm/src/cgb_dm/training/lightning_module.py
70 71 72 73 74 75 76 77 78 | |
training_step ¶
training_step(
batch: dict[str, Float[Tensor, "..."]], batch_idx: int
) -> Float[torch.Tensor, ""]
Run one CGB-DM denoising training step.
Source code in models/cgb-dm/src/cgb_dm/training/lightning_module.py
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 | |
configure_optimizers ¶
configure_optimizers() -> OptimizerLRScheduler
Build optimizers injected by LightningCLI.
Source code in models/cgb-dm/src/cgb_dm/training/lightning_module.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
losses ¶
Loss functions for CGB-DM training.
denoising_mse ¶
denoising_mse(
predicted: Float[Tensor, "..."],
target: Float[Tensor, "..."],
) -> Float[torch.Tensor, ""]
Return the CGB-DM epsilon prediction MSE.
Source code in models/cgb-dm/src/cgb_dm/training/losses.py
9 10 11 12 13 | |
parity ¶
S0-S2 parity adapters for CGB-DM.
CGBDMStepTraceAdapter ¶
Adapter exposing comparable CGB-DM training-step trace tensors.
Source code in models/cgb-dm/src/cgb_dm/training/parity.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 | |
comparable_batch ¶
comparable_batch(
batch: Mapping[str, Float[Tensor, "..."]]
| tuple[
Float[Tensor, "..."],
Float[Tensor, "..."],
Float[Tensor, "..."],
],
) -> Mapping[str, Float[torch.Tensor, "..."]]
Normalize dict or tuple batches to comparable tensor mappings.
Source code in models/cgb-dm/src/cgb_dm/training/parity.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
capture_source_order ¶
capture_source_order(
data_root: str | Path, *, split: str = "train"
) -> list[str]
Capture the filename order used by the original CGB-DM training loader.
Source code in models/cgb-dm/src/cgb_dm/training/parity.py
52 53 54 | |
write_source_order_manifest ¶
write_source_order_manifest(
*,
data_root: str | Path,
output: str | Path,
dataset: str,
split: str = "train",
seed: int = 1,
) -> Path
Write a regenerated source-order manifest outside the repository.
Source code in models/cgb-dm/src/cgb_dm/training/parity.py
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_source_order_manifest ¶
load_source_order_manifest(path: str | Path) -> list[str]
Load names from a regenerated source-order manifest.
Source code in models/cgb-dm/src/cgb_dm/training/parity.py
86 87 88 89 | |
build_reference_dataset ¶
build_reference_dataset(
data_root: str | Path,
*,
manifest: str | Path,
split: Literal["train", "val", "test"] = "train",
) -> CGBDMOriginalDataset
Build a CGB-DM dataset that replays captured source order and encoding.
Source code in models/cgb-dm/src/cgb_dm/training/parity.py
92 93 94 95 96 97 98 99 100 101 102 103 104 | |
seed ¶
Seed helpers for CGB-DM training.
apply_seed_mode ¶
apply_seed_mode(
mode: CGBDMSeedMode, seed: int = 1
) -> dict[str, str | int | bool]
Apply CGB-DM seed behavior and return metadata.
Source code in models/cgb-dm/src/cgb_dm/training/seed.py
13 14 15 16 17 18 19 20 21 22 | |