Ds gan
Transformers-style DS-GAN components for PosterLayout generation.
ConditionType ¶
Bases: StrEnum
Canonical condition names used by layout generation interfaces.
Source code in lib/laygen/src/laygen/common/conditions.py
9 10 11 12 13 14 15 16 17 18 19 20 21 | |
LayoutGenerationOutput
dataclass
¶
Bases: ModelOutput
Canonical layout-generation output for Transformers-style APIs.
Attributes:
| Name | Type | Description |
|---|---|---|
bbox |
Float[ndarray, 'batch elements 4'] | Float[Tensor, 'batch elements 4']
|
Normalized center |
labels |
Int[ndarray, 'batch elements'] | Int[Tensor, 'batch elements']
|
Dataset-local integer labels with shape |
mask |
Bool[ndarray, 'batch elements'] | Bool[Tensor, 'batch elements']
|
Boolean valid-element mask with shape |
id2label |
dict[int, str]
|
Mapping from integer label ids to display names. |
sequences |
object | None
|
Optional raw token sequences. |
scores |
object | None
|
Optional per-token or per-element scores. |
trajectory |
object | None
|
Optional sampling trajectory. |
intermediates |
object | None
|
Optional model-specific debug or auxiliary data. |
Examples:
>>> import numpy as np
>>> output = LayoutGenerationOutput(
... bbox=np.zeros((1, 1, 4), dtype=np.float32),
... labels=np.zeros((1, 1), dtype=np.int64),
... mask=np.ones((1, 1), dtype=bool),
... id2label={0: "text"},
... )
>>> output["bbox"].shape
(1, 1, 4)
Source code in lib/laygen/src/laygen/modeling_outputs.py
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 | |
DSGANConfig ¶
Bases: PretrainedConfig
Store DS-GAN architecture and dataset metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. DS-GAN currently supports PKU PosterLayout. |
pku_posterlayout
|
backbone
|
str
|
Timm ResNet backbone name used by the reference checkpoint. |
'resnet50'
|
max_elem
|
int
|
Maximum number of generated elements. |
32
|
in_channels
|
int
|
CNN-LSTM input channels after flattening class and box planes. |
8
|
out_channels
|
int
|
CNN-LSTM convolution output channels. |
32
|
hidden_size
|
int | None
|
Bidirectional LSTM hidden size. |
None
|
num_layers
|
int
|
Number of LSTM layers. |
4
|
output_size
|
int
|
Combined class and box output width in the internal model. |
8
|
image_size
|
tuple[int, int] | list[int]
|
Processor/model input size as |
(350, 240)
|
reference_canvas_size
|
tuple[int, int] | list[int]
|
Reference normalization canvas as |
(513, 750)
|
backbone_feature_size
|
int
|
Flattened ResNet-FPN spatial size. The reference
default is |
330
|
model_num_classes
|
int
|
Internal class channels including |
4
|
id2label
|
Id2LabelMapping | None
|
Public zero-based semantic label mapping. |
None
|
model_subfolder
|
str
|
Pipeline subfolder for the model component. |
'model'
|
processor_subfolder
|
str
|
Pipeline subfolder for the processor component. |
'processor'
|
Examples:
>>> DSGANConfig().max_elem
32
Source code in models/ds-gan/src/ds_gan/configuration_ds_gan.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 | |
__init__ ¶
__init__(
dataset_name: DatasetName
| str = DatasetName.pku_posterlayout,
backbone: str = "resnet50",
max_elem: int = 32,
in_channels: int = 8,
out_channels: int = 32,
hidden_size: int | None = None,
num_layers: int = 4,
output_size: int = 8,
image_size: tuple[int, int] | list[int] = (350, 240),
reference_canvas_size: tuple[int, int] | list[int] = (
513,
750,
),
backbone_feature_size: int = 330,
model_num_classes: int = 4,
id2label: Id2LabelMapping | None = None,
label2id: dict[str, int] | None = None,
model_subfolder: str = "model",
processor_subfolder: str = "processor",
condition_types: list[str]
| tuple[str, ...]
| None = None,
architectures: list[str] | None = None,
model_type: str | None = None,
transformers_version: str | None = None,
torch_dtype: str | None = None,
dtype: str | None = None,
name_or_path: str = "",
_commit_hash: str | None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize DS-GAN configuration.
Source code in models/ds-gan/src/ds_gan/configuration_ds_gan.py
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 | |
DSGANModel ¶
Bases: PreTrainedModel
Transformers-compatible DS-GAN generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
DSGANConfig
|
DS-GAN model configuration. |
required |
Examples:
>>> config = DSGANConfig(backbone="resnet18", max_elem=4, hidden_size=32, num_layers=2, image_size=(64, 64), backbone_feature_size=16)
>>> model = DSGANModel(config)
>>> model.config.model_type
'ds_gan'
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
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 | |
__init__ ¶
__init__(config: DSGANConfig) -> None
Initialize DS-GAN generator layers.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
150 151 152 153 154 155 156 157 | |
forward ¶
forward(
pixel_values: Float[Tensor, "batch 4 height width"],
layout: Float[Tensor, "batch elements 2 4"],
return_dict: bool = True,
) -> (
DSGANModelOutput
| tuple[
Float[torch.Tensor, "batch elements 4"],
Float[torch.Tensor, "batch elements 4"],
]
)
Run a DS-GAN generator forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values
|
Float[Tensor, 'batch 4 height width']
|
RGB plus saliency tensor shaped |
required |
layout
|
Float[Tensor, 'batch elements 2 4']
|
Initial internal layout shaped |
required |
return_dict
|
bool
|
Whether to return a dataclass output. |
True
|
Returns:
| Type | Description |
|---|---|
DSGANModelOutput | tuple[Float[Tensor, 'batch elements 4'], Float[Tensor, 'batch elements 4']]
|
Raw class probabilities and normalized center |
Raises:
| Type | Description |
|---|---|
ValueError
|
If tensor shapes do not match the config. |
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
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 | |
DSGANModelOutput
dataclass
¶
Bases: ModelOutput
Raw DS-GAN generator output.
Attributes:
| Name | Type | Description |
|---|---|---|
class_probs |
Float[Tensor, 'batch elements 4']
|
Internal class probabilities with shape
|
bbox |
Float[Tensor, 'batch elements 4'] | None
|
Normalized center |
initial_layout |
Float[Tensor, 'batch elements 2 4'] | None
|
Initial class/box layout passed to the generator. |
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
DSGANPipeline ¶
Bases: LayoutGenerationPipeline
Transformers-side pipeline for content-aware PosterLayout generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
DSGANModel
|
DS-GAN generator. |
required |
processor
|
DSGANProcessor | None
|
Optional processor for images and output decoding. |
None
|
config
|
DSGANConfig | None
|
Optional root pipeline config. |
None
|
device
|
str | device | None
|
Optional runtime device. |
None
|
Examples:
>>> config = DSGANConfig(backbone="resnet18", max_elem=4, hidden_size=32, num_layers=2, image_size=(64, 64), backbone_feature_size=16)
>>> pipe = DSGANPipeline(DSGANModel(config))
>>> pipe.config.model_type
'ds_gan'
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
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 | |
__init__ ¶
__init__(
model: DSGANModel,
processor: DSGANProcessor | None = None,
config: DSGANConfig | None = None,
device: str | device | None = None,
) -> None
Initialize DS-GAN pipeline.
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
__call__ ¶
__call__(
images: ImageInput
| list[ImageInput]
| Float[Tensor, "..."]
| 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[ArrayLikeInput]
| None = None,
bbox: Float[Tensor, "batch elements 4"]
| Sequence[ArrayLikeInput]
| None = None,
mask: Bool[Tensor, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
num_elements: int
| list[int]
| Int[Tensor, "batch"]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: OutputType | str = OutputType.dataclass,
return_intermediates: bool = False,
saliency: ImageInput
| list[ImageInput]
| Float[Tensor, "..."]
| None = None,
saliency_pfpnet: ImageInput
| list[ImageInput]
| Float[Tensor, "..."]
| None = None,
saliency_basnet: ImageInput
| list[ImageInput]
| Float[Tensor, "..."]
| None = None,
pixel_values: Float[Tensor, "batch 4 height width"]
| None = None,
initial_layout: Float[Tensor, "batch elements 2 4"]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| Mapping[
str,
Shaped[torch.Tensor, "..."]
| ConditionType
| str
| bool,
]
| None,
]
)
Generate layouts from content images and saliency maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | list[ImageInput] | Float[Tensor, '...'] | None
|
RGB image or batch. Required unless |
None
|
batch_size
|
int
|
Batch size used when |
1
|
seed
|
int | None
|
Convenience seed. Ignored when |
None
|
generator
|
Generator | None
|
Explicit torch generator. |
None
|
condition_type
|
ConditionType | str
|
Must normalize to |
content_image
|
labels
|
Int[Tensor, 'batch elements'] | Sequence[ArrayLikeInput] | None
|
Optional public labels used only with |
None
|
bbox
|
Float[Tensor, 'batch elements 4'] | Sequence[ArrayLikeInput] | None
|
Optional public boxes used with |
None
|
mask
|
Bool[Tensor, 'batch elements'] | Sequence[ArrayLikeInput] | None
|
Optional valid-element mask for fixed initial layouts. |
None
|
num_elements
|
int | list[int] | Int[Tensor, 'batch'] | None
|
Reserved compatibility argument. |
None
|
box_format
|
BoxFormat | str
|
Format of optional |
xywh
|
normalized
|
bool
|
Whether optional |
True
|
canvas_size
|
tuple[int, int] | None
|
Pixel canvas size for optional unnormalized |
None
|
num_inference_steps
|
int | None
|
Reserved compatibility argument. |
None
|
output_type
|
OutputType | str
|
|
dataclass
|
return_intermediates
|
bool
|
Whether to include raw model tensors. |
False
|
saliency
|
ImageInput | list[ImageInput] | Float[Tensor, '...'] | None
|
Optional single merged saliency map. |
None
|
saliency_pfpnet
|
ImageInput | list[ImageInput] | Float[Tensor, '...'] | None
|
Optional PFPNet saliency map. |
None
|
saliency_basnet
|
ImageInput | list[ImageInput] | Float[Tensor, '...'] | None
|
Optional BASNet saliency map. |
None
|
pixel_values
|
Float[Tensor, 'batch 4 height width'] | None
|
Preprocessed |
None
|
initial_layout
|
Float[Tensor, 'batch elements 2 4'] | None
|
Optional internal layout |
None
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Shaped[Tensor, '...'] | dict[int, str] | Mapping[str, Shaped[Tensor, '...'] | ConditionType | str | bool] | None]
|
Shared layout-generation output. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the condition, image inputs, or fixed layout are invalid. |
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
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 | |
OutputType ¶
Bases: StrEnum
Supported DS-GAN pipeline output containers.
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
36 37 38 39 40 | |
DSGANProcessor ¶
Bases: ProcessorMixin
Prepare PosterLayout RGB/saliency inputs and decode DS-GAN outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. Only PKU PosterLayout is supported. |
pku_posterlayout
|
id2label
|
dict[int | str, str] | None
|
Public semantic labels excluding model |
None
|
image_size
|
tuple[int, int] | list[int]
|
Resize target as |
(350, 240)
|
Examples:
>>> processor = DSGANProcessor()
>>> processor.id2label[0]
'text'
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
__init__ ¶
__init__(
dataset_name: DatasetName
| str = DatasetName.pku_posterlayout,
id2label: dict[int | str, str] | None = None,
image_size: tuple[int, int] | list[int] = (350, 240),
) -> None
Initialize processor metadata.
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
__call__ ¶
__call__(
images: DSGANImageInput | Sequence[DSGANImageInput],
*,
saliency: DSGANImageInput
| Sequence[DSGANImageInput]
| None = None,
saliency_pfpnet: DSGANImageInput
| Sequence[DSGANImageInput]
| None = None,
saliency_basnet: DSGANImageInput
| Sequence[DSGANImageInput]
| None = None,
return_tensors: Literal["pt"] = "pt",
) -> BatchEncoding
Encode content images into pixel_values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
DSGANImageInput | Sequence[DSGANImageInput]
|
RGB image or batch of RGB images. |
required |
saliency
|
DSGANImageInput | Sequence[DSGANImageInput] | None
|
Optional saliency image or batch. If omitted and both saliency maps are given, the maps are merged by pixelwise max. |
None
|
saliency_pfpnet
|
DSGANImageInput | Sequence[DSGANImageInput] | None
|
Optional PFPNet saliency map. |
None
|
saliency_basnet
|
DSGANImageInput | Sequence[DSGANImageInput] | None
|
Optional BASNet saliency map. |
None
|
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
Returns:
| Type | Description |
|---|---|
BatchEncoding
|
Batch encoding containing |
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
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 | |
decode ¶
decode(
*,
class_probs: Float[Tensor, "batch elements 4"],
bbox: Float[Tensor, "batch elements 4"],
output_type: Literal["dataclass", "dict"] = "dataclass",
scores: Float[Tensor, "batch elements"] | None = None,
intermediates: Mapping[
str, Shaped[Tensor, "..."] | str | bool
]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| Mapping[
str, Shaped[torch.Tensor, "..."] | str | bool
]
| None,
]
)
Decode raw DS-GAN class probabilities and boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_probs
|
Float[Tensor, 'batch elements 4']
|
Internal class probabilities shaped |
required |
bbox
|
Float[Tensor, 'batch elements 4']
|
Normalized center |
required |
output_type
|
Literal['dataclass', 'dict']
|
Return format. |
'dataclass'
|
scores
|
Float[Tensor, 'batch elements'] | None
|
Optional per-element class scores. |
None
|
intermediates
|
Mapping[str, Shaped[Tensor, '...'] | str | bool] | None
|
Optional model-specific intermediate tensors. |
None
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Shaped[Tensor, '...'] | dict[int, str] | Mapping[str, Shaped[Tensor, '...'] | str | bool] | None]
|
Shared layout output with public labels and mask semantics. |
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
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 | |
encode_layout ¶
encode_layout(
*,
bbox: Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Sequence[ArrayLikeInput],
labels: Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Sequence[ArrayLikeInput],
mask: Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
max_elem: int = 32,
) -> dict[str, Shaped[torch.Tensor, "..."]]
Encode public boxes/labels into the internal layout tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4'] | Float[ndarray, 'batch elements 4'] | Sequence[ArrayLikeInput]
|
Public boxes. |
required |
labels
|
Int[Tensor, 'batch elements'] | Int[ndarray, 'batch elements'] | Sequence[ArrayLikeInput]
|
Public zero-based semantic labels. |
required |
mask
|
Bool[Tensor, 'batch elements'] | Bool[ndarray, 'batch elements'] | Sequence[ArrayLikeInput] | None
|
Optional valid-element mask. |
None
|
box_format
|
BoxFormat | str
|
Input box format. |
xywh
|
normalized
|
bool
|
Whether the boxes are normalized. |
True
|
canvas_size
|
tuple[int, int] | None
|
Pixel canvas size used when |
None
|
max_elem
|
int
|
Output slot count. |
32
|
Returns:
| Type | Description |
|---|---|
dict[str, Shaped[Tensor, '...']]
|
Dictionary with internal |
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
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 | |
pad ¶
pad(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"],
*,
max_elem: int,
) -> tuple[
Float[torch.Tensor, "batch padded_elements 4"],
Int[torch.Tensor, "batch padded_elements"],
Bool[torch.Tensor, "batch padded_elements"],
]
Pad layout tensors to DS-GAN max_elem slots.
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
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 | |
default_ds_gan_config ¶
default_ds_gan_config() -> DSGANConfig
Return the reference-compatible DS-GAN default configuration.
Examples:
>>> default_ds_gan_config().backbone
'resnet50'
Source code in models/ds-gan/src/ds_gan/configuration_ds_gan.py
125 126 127 128 129 130 131 132 | |
convert_vendor_state_dict ¶
convert_vendor_state_dict(
state_dict: Mapping[str, Shaped[Tensor, "..."]],
) -> dict[str, Shaped[torch.Tensor, "..."]]
Convert vendor DS-GAN generator keys to DSGANModel keys.
The released checkpoint was commonly saved from torch.nn.DataParallel;
this helper strips the leading module. prefix and keeps all generator
module names otherwise unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
Mapping[str, Shaped[Tensor, '...']]
|
Original checkpoint mapping. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Shaped[Tensor, '...']]
|
Converted state dictionary. |
Examples:
>>> convert_vendor_state_dict({"module.fc1.weight": torch.zeros(1)})["fc1.weight"].shape
torch.Size([1])
Source code in models/ds-gan/src/ds_gan/conversion.py
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 | |
random_initial_layout ¶
random_initial_layout(
batch_size: int,
max_elem: int,
*,
generator: Generator | None = None,
seed: int | None = None,
device: device | str | None = None,
dtype: dtype = torch.float32,
weighted_classes: bool = True,
use_numpy_classes: bool = False,
) -> Float[torch.Tensor, "batch elements 2 4"]
Sample the DS-GAN initial layout tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Batch size. |
required |
max_elem
|
int
|
Number of layout slots. |
required |
generator
|
Generator | None
|
Optional torch generator. Takes precedence over |
None
|
seed
|
int | None
|
Convenience seed used only when |
None
|
device
|
device | str | None
|
Target torch device. |
None
|
dtype
|
dtype
|
Target floating dtype. |
float32
|
weighted_classes
|
bool
|
Whether to use the released inference class prior. |
True
|
use_numpy_classes
|
bool
|
Use NumPy's legacy |
False
|
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'batch elements 2 4']
|
Tensor shaped |
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
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 | |
annotations_from_pku_example ¶
annotations_from_pku_example(
example: Mapping[str, DSGANExampleValue],
*,
max_elem: int = 32,
) -> dict[
str, Shaped[torch.Tensor, "..."] | tuple[int, int]
]
Convert a PKU PosterLayout dataset row into public layout tensors.
The adapter filters INVALID annotations, converts pixel ltrb boxes
to normalized center xywh, derives canvas size from the image columns,
and applies the reference designSeq.reorder ordering policy.
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
processor_for_dataset ¶
processor_for_dataset(
dataset_name: DatasetName | str,
) -> DSGANProcessor
Create a DS-GAN processor for a supported dataset.
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
303 304 305 | |
configuration_ds_gan ¶
Configuration for converted DS-GAN checkpoints.
DSGANConfig ¶
Bases: PretrainedConfig
Store DS-GAN architecture and dataset metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. DS-GAN currently supports PKU PosterLayout. |
pku_posterlayout
|
backbone
|
str
|
Timm ResNet backbone name used by the reference checkpoint. |
'resnet50'
|
max_elem
|
int
|
Maximum number of generated elements. |
32
|
in_channels
|
int
|
CNN-LSTM input channels after flattening class and box planes. |
8
|
out_channels
|
int
|
CNN-LSTM convolution output channels. |
32
|
hidden_size
|
int | None
|
Bidirectional LSTM hidden size. |
None
|
num_layers
|
int
|
Number of LSTM layers. |
4
|
output_size
|
int
|
Combined class and box output width in the internal model. |
8
|
image_size
|
tuple[int, int] | list[int]
|
Processor/model input size as |
(350, 240)
|
reference_canvas_size
|
tuple[int, int] | list[int]
|
Reference normalization canvas as |
(513, 750)
|
backbone_feature_size
|
int
|
Flattened ResNet-FPN spatial size. The reference
default is |
330
|
model_num_classes
|
int
|
Internal class channels including |
4
|
id2label
|
Id2LabelMapping | None
|
Public zero-based semantic label mapping. |
None
|
model_subfolder
|
str
|
Pipeline subfolder for the model component. |
'model'
|
processor_subfolder
|
str
|
Pipeline subfolder for the processor component. |
'processor'
|
Examples:
>>> DSGANConfig().max_elem
32
Source code in models/ds-gan/src/ds_gan/configuration_ds_gan.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 | |
__init__ ¶
__init__(
dataset_name: DatasetName
| str = DatasetName.pku_posterlayout,
backbone: str = "resnet50",
max_elem: int = 32,
in_channels: int = 8,
out_channels: int = 32,
hidden_size: int | None = None,
num_layers: int = 4,
output_size: int = 8,
image_size: tuple[int, int] | list[int] = (350, 240),
reference_canvas_size: tuple[int, int] | list[int] = (
513,
750,
),
backbone_feature_size: int = 330,
model_num_classes: int = 4,
id2label: Id2LabelMapping | None = None,
label2id: dict[str, int] | None = None,
model_subfolder: str = "model",
processor_subfolder: str = "processor",
condition_types: list[str]
| tuple[str, ...]
| None = None,
architectures: list[str] | None = None,
model_type: str | None = None,
transformers_version: str | None = None,
torch_dtype: str | None = None,
dtype: str | None = None,
name_or_path: str = "",
_commit_hash: str | None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize DS-GAN configuration.
Source code in models/ds-gan/src/ds_gan/configuration_ds_gan.py
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 | |
default_ds_gan_config ¶
default_ds_gan_config() -> DSGANConfig
Return the reference-compatible DS-GAN default configuration.
Examples:
>>> default_ds_gan_config().backbone
'resnet50'
Source code in models/ds-gan/src/ds_gan/configuration_ds_gan.py
125 126 127 128 129 130 131 132 | |
pku_model_label2id ¶
pku_model_label2id() -> dict[str, int]
Return DS-GAN model labels including the no-object class.
Examples:
>>> pku_model_label2id()["no_object"]
0
Source code in models/ds-gan/src/ds_gan/configuration_ds_gan.py
135 136 137 138 139 140 141 142 | |
pku_dataset_label2id ¶
pku_dataset_label2id() -> dict[str, int]
Return PKU dataset annotation labels including INVALID.
Source code in models/ds-gan/src/ds_gan/configuration_ds_gan.py
145 146 147 | |
conversion ¶
Conversion helpers for original PosterLayout DS-GAN checkpoints.
convert_vendor_state_dict ¶
convert_vendor_state_dict(
state_dict: Mapping[str, Shaped[Tensor, "..."]],
) -> dict[str, Shaped[torch.Tensor, "..."]]
Convert vendor DS-GAN generator keys to DSGANModel keys.
The released checkpoint was commonly saved from torch.nn.DataParallel;
this helper strips the leading module. prefix and keeps all generator
module names otherwise unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
Mapping[str, Shaped[Tensor, '...']]
|
Original checkpoint mapping. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Shaped[Tensor, '...']]
|
Converted state dictionary. |
Examples:
>>> convert_vendor_state_dict({"module.fc1.weight": torch.zeros(1)})["fc1.weight"].shape
torch.Size([1])
Source code in models/ds-gan/src/ds_gan/conversion.py
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 | |
config_from_vendor_args ¶
config_from_vendor_args(
args: Namespace
| SimpleNamespace
| Mapping[str, DSGANArgValue]
| None = None,
) -> DSGANConfig
Build a DS-GAN config from vendor args or defaults.
Source code in models/ds-gan/src/ds_gan/conversion.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
model_card ¶
Hub model-card helper for converted DS-GAN checkpoints.
dsgan_model_card ¶
dsgan_model_card() -> ModelCard
Build the DS-GAN PKU PosterLayout Hub model card.
Source code in models/ds-gan/src/ds_gan/model_card.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
write_dsgan_model_card ¶
write_dsgan_model_card(output_dir: str | Path) -> Path
Write README.md for a converted DS-GAN checkpoint directory.
Source code in models/ds-gan/src/ds_gan/model_card.py
73 74 75 76 77 | |
modeling_ds_gan ¶
PyTorch/Transformers implementation of the PosterLayout DS-GAN generator.
DSGANModelOutput
dataclass
¶
Bases: ModelOutput
Raw DS-GAN generator output.
Attributes:
| Name | Type | Description |
|---|---|---|
class_probs |
Float[Tensor, 'batch elements 4']
|
Internal class probabilities with shape
|
bbox |
Float[Tensor, 'batch elements 4'] | None
|
Normalized center |
initial_layout |
Float[Tensor, 'batch elements 2 4'] | None
|
Initial class/box layout passed to the generator. |
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
ResnetBackbone ¶
Bases: Module
DS-GAN ResNet-FPN encoder used to initialize the DS-GAN LSTM state.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.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 | |
__init__ ¶
__init__(config: DSGANConfig) -> None
Initialize the ResNet-FPN encoder.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.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 | |
forward ¶
forward(
pixel_values: Float[Tensor, "batch 4 height width"],
) -> Float[torch.Tensor, "layers2 batch hidden"]
Encode image/saliency tensors into an LSTM initial hidden state.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
CNNLSTM ¶
Bases: Module
DS-GAN CNN-LSTM sequence model.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
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 | |
__init__ ¶
__init__(config: DSGANConfig) -> None
Initialize the CNN-LSTM block.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
forward ¶
forward(
layout: Float[Tensor, "batch elements 2 4"],
h0: Float[Tensor, "layers2 batch hidden"],
) -> Float[torch.Tensor, "batch elements hidden2"]
Run the DS-GAN CNN-LSTM over initial layout tensors.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
120 121 122 123 124 125 126 127 128 129 130 | |
DSGANModel ¶
Bases: PreTrainedModel
Transformers-compatible DS-GAN generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
DSGANConfig
|
DS-GAN model configuration. |
required |
Examples:
>>> config = DSGANConfig(backbone="resnet18", max_elem=4, hidden_size=32, num_layers=2, image_size=(64, 64), backbone_feature_size=16)
>>> model = DSGANModel(config)
>>> model.config.model_type
'ds_gan'
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
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 | |
__init__ ¶
__init__(config: DSGANConfig) -> None
Initialize DS-GAN generator layers.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
150 151 152 153 154 155 156 157 | |
forward ¶
forward(
pixel_values: Float[Tensor, "batch 4 height width"],
layout: Float[Tensor, "batch elements 2 4"],
return_dict: bool = True,
) -> (
DSGANModelOutput
| tuple[
Float[torch.Tensor, "batch elements 4"],
Float[torch.Tensor, "batch elements 4"],
]
)
Run a DS-GAN generator forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values
|
Float[Tensor, 'batch 4 height width']
|
RGB plus saliency tensor shaped |
required |
layout
|
Float[Tensor, 'batch elements 2 4']
|
Initial internal layout shaped |
required |
return_dict
|
bool
|
Whether to return a dataclass output. |
True
|
Returns:
| Type | Description |
|---|---|
DSGANModelOutput | tuple[Float[Tensor, 'batch elements 4'], Float[Tensor, 'batch elements 4']]
|
Raw class probabilities and normalized center |
Raises:
| Type | Description |
|---|---|
ValueError
|
If tensor shapes do not match the config. |
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
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 | |
xyxy_to_xywh ¶
xyxy_to_xywh(
bbox: Float[Tensor, "... 4"],
) -> Float[torch.Tensor, "... 4"]
Convert left/top/right/bottom boxes to center xywh boxes.
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
206 207 208 209 210 211 212 213 214 | |
random_initial_layout ¶
random_initial_layout(
batch_size: int,
max_elem: int,
*,
generator: Generator | None = None,
seed: int | None = None,
device: device | str | None = None,
dtype: dtype = torch.float32,
weighted_classes: bool = True,
use_numpy_classes: bool = False,
) -> Float[torch.Tensor, "batch elements 2 4"]
Sample the DS-GAN initial layout tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Batch size. |
required |
max_elem
|
int
|
Number of layout slots. |
required |
generator
|
Generator | None
|
Optional torch generator. Takes precedence over |
None
|
seed
|
int | None
|
Convenience seed used only when |
None
|
device
|
device | str | None
|
Target torch device. |
None
|
dtype
|
dtype
|
Target floating dtype. |
float32
|
weighted_classes
|
bool
|
Whether to use the released inference class prior. |
True
|
use_numpy_classes
|
bool
|
Use NumPy's legacy |
False
|
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'batch elements 2 4']
|
Tensor shaped |
Source code in models/ds-gan/src/ds_gan/modeling_ds_gan.py
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 | |
pipeline_ds_gan ¶
Pipeline interface for PosterLayout DS-GAN generation.
DSGANPipelineComponent ¶
Bases: Protocol
Runtime-checkable loaded pipeline component marker.
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
31 32 33 | |
OutputType ¶
Bases: StrEnum
Supported DS-GAN pipeline output containers.
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
36 37 38 39 40 | |
DSGANPipeline ¶
Bases: LayoutGenerationPipeline
Transformers-side pipeline for content-aware PosterLayout generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
DSGANModel
|
DS-GAN generator. |
required |
processor
|
DSGANProcessor | None
|
Optional processor for images and output decoding. |
None
|
config
|
DSGANConfig | None
|
Optional root pipeline config. |
None
|
device
|
str | device | None
|
Optional runtime device. |
None
|
Examples:
>>> config = DSGANConfig(backbone="resnet18", max_elem=4, hidden_size=32, num_layers=2, image_size=(64, 64), backbone_feature_size=16)
>>> pipe = DSGANPipeline(DSGANModel(config))
>>> pipe.config.model_type
'ds_gan'
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
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 | |
__init__ ¶
__init__(
model: DSGANModel,
processor: DSGANProcessor | None = None,
config: DSGANConfig | None = None,
device: str | device | None = None,
) -> None
Initialize DS-GAN pipeline.
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
__call__ ¶
__call__(
images: ImageInput
| list[ImageInput]
| Float[Tensor, "..."]
| 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[ArrayLikeInput]
| None = None,
bbox: Float[Tensor, "batch elements 4"]
| Sequence[ArrayLikeInput]
| None = None,
mask: Bool[Tensor, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
num_elements: int
| list[int]
| Int[Tensor, "batch"]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: OutputType | str = OutputType.dataclass,
return_intermediates: bool = False,
saliency: ImageInput
| list[ImageInput]
| Float[Tensor, "..."]
| None = None,
saliency_pfpnet: ImageInput
| list[ImageInput]
| Float[Tensor, "..."]
| None = None,
saliency_basnet: ImageInput
| list[ImageInput]
| Float[Tensor, "..."]
| None = None,
pixel_values: Float[Tensor, "batch 4 height width"]
| None = None,
initial_layout: Float[Tensor, "batch elements 2 4"]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| Mapping[
str,
Shaped[torch.Tensor, "..."]
| ConditionType
| str
| bool,
]
| None,
]
)
Generate layouts from content images and saliency maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | list[ImageInput] | Float[Tensor, '...'] | None
|
RGB image or batch. Required unless |
None
|
batch_size
|
int
|
Batch size used when |
1
|
seed
|
int | None
|
Convenience seed. Ignored when |
None
|
generator
|
Generator | None
|
Explicit torch generator. |
None
|
condition_type
|
ConditionType | str
|
Must normalize to |
content_image
|
labels
|
Int[Tensor, 'batch elements'] | Sequence[ArrayLikeInput] | None
|
Optional public labels used only with |
None
|
bbox
|
Float[Tensor, 'batch elements 4'] | Sequence[ArrayLikeInput] | None
|
Optional public boxes used with |
None
|
mask
|
Bool[Tensor, 'batch elements'] | Sequence[ArrayLikeInput] | None
|
Optional valid-element mask for fixed initial layouts. |
None
|
num_elements
|
int | list[int] | Int[Tensor, 'batch'] | None
|
Reserved compatibility argument. |
None
|
box_format
|
BoxFormat | str
|
Format of optional |
xywh
|
normalized
|
bool
|
Whether optional |
True
|
canvas_size
|
tuple[int, int] | None
|
Pixel canvas size for optional unnormalized |
None
|
num_inference_steps
|
int | None
|
Reserved compatibility argument. |
None
|
output_type
|
OutputType | str
|
|
dataclass
|
return_intermediates
|
bool
|
Whether to include raw model tensors. |
False
|
saliency
|
ImageInput | list[ImageInput] | Float[Tensor, '...'] | None
|
Optional single merged saliency map. |
None
|
saliency_pfpnet
|
ImageInput | list[ImageInput] | Float[Tensor, '...'] | None
|
Optional PFPNet saliency map. |
None
|
saliency_basnet
|
ImageInput | list[ImageInput] | Float[Tensor, '...'] | None
|
Optional BASNet saliency map. |
None
|
pixel_values
|
Float[Tensor, 'batch 4 height width'] | None
|
Preprocessed |
None
|
initial_layout
|
Float[Tensor, 'batch elements 2 4'] | None
|
Optional internal layout |
None
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Shaped[Tensor, '...'] | dict[int, str] | Mapping[str, Shaped[Tensor, '...'] | ConditionType | str | bool] | None]
|
Shared layout-generation output. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the condition, image inputs, or fixed layout are invalid. |
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
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 | |
normalize_condition_type ¶
normalize_condition_type(
condition_type: ConditionType | str | None,
) -> ConditionType
Normalize DS-GAN condition aliases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition_type
|
ConditionType | str | None
|
Canonical condition enum, alias, or |
required |
Returns:
| Type | Description |
|---|---|
ConditionType
|
Canonical |
Raises:
| Type | Description |
|---|---|
ValueError
|
If DS-GAN does not support the requested mode. |
Examples:
>>> str(normalize_condition_type("content"))
'content_image'
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
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 | |
normalize_output_type ¶
normalize_output_type(
output_type: OutputType | str,
) -> OutputType
Normalize public output type aliases.
Source code in models/ds-gan/src/ds_gan/pipeline_ds_gan.py
85 86 87 88 89 90 91 92 | |
processing_ds_gan ¶
Processor for DS-GAN content-image inputs and layout decoding.
DSGANProcessor ¶
Bases: ProcessorMixin
Prepare PosterLayout RGB/saliency inputs and decode DS-GAN outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
DatasetName | str
|
Dataset key. Only PKU PosterLayout is supported. |
pku_posterlayout
|
id2label
|
dict[int | str, str] | None
|
Public semantic labels excluding model |
None
|
image_size
|
tuple[int, int] | list[int]
|
Resize target as |
(350, 240)
|
Examples:
>>> processor = DSGANProcessor()
>>> processor.id2label[0]
'text'
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
__init__ ¶
__init__(
dataset_name: DatasetName
| str = DatasetName.pku_posterlayout,
id2label: dict[int | str, str] | None = None,
image_size: tuple[int, int] | list[int] = (350, 240),
) -> None
Initialize processor metadata.
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
__call__ ¶
__call__(
images: DSGANImageInput | Sequence[DSGANImageInput],
*,
saliency: DSGANImageInput
| Sequence[DSGANImageInput]
| None = None,
saliency_pfpnet: DSGANImageInput
| Sequence[DSGANImageInput]
| None = None,
saliency_basnet: DSGANImageInput
| Sequence[DSGANImageInput]
| None = None,
return_tensors: Literal["pt"] = "pt",
) -> BatchEncoding
Encode content images into pixel_values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
DSGANImageInput | Sequence[DSGANImageInput]
|
RGB image or batch of RGB images. |
required |
saliency
|
DSGANImageInput | Sequence[DSGANImageInput] | None
|
Optional saliency image or batch. If omitted and both saliency maps are given, the maps are merged by pixelwise max. |
None
|
saliency_pfpnet
|
DSGANImageInput | Sequence[DSGANImageInput] | None
|
Optional PFPNet saliency map. |
None
|
saliency_basnet
|
DSGANImageInput | Sequence[DSGANImageInput] | None
|
Optional BASNet saliency map. |
None
|
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
Returns:
| Type | Description |
|---|---|
BatchEncoding
|
Batch encoding containing |
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
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 | |
decode ¶
decode(
*,
class_probs: Float[Tensor, "batch elements 4"],
bbox: Float[Tensor, "batch elements 4"],
output_type: Literal["dataclass", "dict"] = "dataclass",
scores: Float[Tensor, "batch elements"] | None = None,
intermediates: Mapping[
str, Shaped[Tensor, "..."] | str | bool
]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| Mapping[
str, Shaped[torch.Tensor, "..."] | str | bool
]
| None,
]
)
Decode raw DS-GAN class probabilities and boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_probs
|
Float[Tensor, 'batch elements 4']
|
Internal class probabilities shaped |
required |
bbox
|
Float[Tensor, 'batch elements 4']
|
Normalized center |
required |
output_type
|
Literal['dataclass', 'dict']
|
Return format. |
'dataclass'
|
scores
|
Float[Tensor, 'batch elements'] | None
|
Optional per-element class scores. |
None
|
intermediates
|
Mapping[str, Shaped[Tensor, '...'] | str | bool] | None
|
Optional model-specific intermediate tensors. |
None
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Shaped[Tensor, '...'] | dict[int, str] | Mapping[str, Shaped[Tensor, '...'] | str | bool] | None]
|
Shared layout output with public labels and mask semantics. |
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
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 | |
encode_layout ¶
encode_layout(
*,
bbox: Float[Tensor, "batch elements 4"]
| Float[ndarray, "batch elements 4"]
| Sequence[ArrayLikeInput],
labels: Int[Tensor, "batch elements"]
| Int[ndarray, "batch elements"]
| Sequence[ArrayLikeInput],
mask: Bool[Tensor, "batch elements"]
| Bool[ndarray, "batch elements"]
| Sequence[ArrayLikeInput]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
max_elem: int = 32,
) -> dict[str, Shaped[torch.Tensor, "..."]]
Encode public boxes/labels into the internal layout tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Float[Tensor, 'batch elements 4'] | Float[ndarray, 'batch elements 4'] | Sequence[ArrayLikeInput]
|
Public boxes. |
required |
labels
|
Int[Tensor, 'batch elements'] | Int[ndarray, 'batch elements'] | Sequence[ArrayLikeInput]
|
Public zero-based semantic labels. |
required |
mask
|
Bool[Tensor, 'batch elements'] | Bool[ndarray, 'batch elements'] | Sequence[ArrayLikeInput] | None
|
Optional valid-element mask. |
None
|
box_format
|
BoxFormat | str
|
Input box format. |
xywh
|
normalized
|
bool
|
Whether the boxes are normalized. |
True
|
canvas_size
|
tuple[int, int] | None
|
Pixel canvas size used when |
None
|
max_elem
|
int
|
Output slot count. |
32
|
Returns:
| Type | Description |
|---|---|
dict[str, Shaped[Tensor, '...']]
|
Dictionary with internal |
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
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 | |
pad ¶
pad(
bbox: Float[Tensor, "batch elements 4"],
labels: Int[Tensor, "batch elements"],
mask: Bool[Tensor, "batch elements"],
*,
max_elem: int,
) -> tuple[
Float[torch.Tensor, "batch padded_elements 4"],
Int[torch.Tensor, "batch padded_elements"],
Bool[torch.Tensor, "batch padded_elements"],
]
Pad layout tensors to DS-GAN max_elem slots.
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
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 | |
processor_for_dataset ¶
processor_for_dataset(
dataset_name: DatasetName | str,
) -> DSGANProcessor
Create a DS-GAN processor for a supported dataset.
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
303 304 305 | |
annotations_from_pku_example ¶
annotations_from_pku_example(
example: Mapping[str, DSGANExampleValue],
*,
max_elem: int = 32,
) -> dict[
str, Shaped[torch.Tensor, "..."] | tuple[int, int]
]
Convert a PKU PosterLayout dataset row into public layout tensors.
The adapter filters INVALID annotations, converts pixel ltrb boxes
to normalized center xywh, derives canvas size from the image columns,
and applies the reference designSeq.reorder ordering policy.
Source code in models/ds-gan/src/ds_gan/processing_ds_gan.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |