Smarttext
SmartText Transformers-style text placement package.
SmartTextBackbone ¶
Bases: StrEnum
Backbone names supported by the original SmartText scorer.
Source code in models/smarttext/src/smarttext/configuration_smarttext.py
26 27 28 29 30 31 32 | |
SmartTextConfig ¶
Bases: PretrainedConfig
Configuration for SmartText scorer, saliency model, and pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id2label
|
Mapping[int | str, str] | None
|
Public label mapping. Defaults to one |
None
|
scorer_scale
|
str
|
Original scorer scale mode. |
'multi'
|
scorer_backbone
|
SmartTextBackbone | str
|
Original scorer backbone. |
shufflenetv2
|
align_size
|
int
|
RoI/RoD pooled spatial size. |
9
|
reduction_dim
|
int
|
Reduced feature channels before scoring. |
8
|
downsample
|
int
|
Scorer feature-map downsampling factor. |
4
|
model_type_name
|
SmartTextRegionMode | str
|
Original |
RoE
|
image_size
|
int
|
Short-side normalization target for scorer preprocessing. |
256
|
ratio_list
|
Sequence[float]
|
Per-line font-size ratios. |
(1.0, 0.8)
|
text_spacing
|
int
|
Pixel spacing between prompt lines. |
20
|
exp_prop
|
int
|
Original expanded-region coefficient. |
6
|
grid_num
|
int
|
Candidate search grid count. |
120
|
saliency_coef
|
float
|
Saliency suppression coefficient. |
2.6
|
max_text_area_coef
|
float
|
Maximum candidate area divisor. |
17.0
|
min_text_area_coef
|
float
|
Minimum candidate area divisor. |
7.0
|
min_font_size
|
int
|
Minimum candidate font size. |
10
|
max_font_size
|
int
|
Maximum candidate font size. |
500
|
font_inc_unit
|
int
|
Candidate font-size step. |
5
|
candi_res
|
int
|
Number of selected candidates. |
3
|
contrast_threshold
|
float
|
Foreground/background contrast threshold. |
5.0
|
mos_mean
|
float
|
MOS score mean used by the original demo. |
2.95
|
mos_std
|
float
|
MOS score standard deviation. |
0.8
|
rgb_mean
|
Sequence[float]
|
RGB normalization mean for scorer inputs. |
(0.485, 0.456, 0.406)
|
rgb_std
|
Sequence[float]
|
RGB normalization std for scorer inputs. |
(0.229, 0.224, 0.225)
|
scorer_subfolder
|
str
|
Pipeline scorer subfolder. |
'scorer'
|
saliency_subfolder
|
str
|
Pipeline saliency-model subfolder. |
'saliency_model'
|
processor_subfolder
|
str
|
Pipeline processor subfolder. |
'processor'
|
original_options
|
Mapping[str, SmartTextMetadataValue] | None
|
Raw reference option values preserved for audit. |
None
|
conversion_report
|
Mapping[str, SmartTextMetadataValue | list[str]] | None
|
Conversion metadata persisted in configs. |
None
|
kwargs
|
str | int | float | bool | None
|
Extra |
{}
|
Examples:
>>> config = SmartTextConfig()
>>> config.id2label
{0: 'text'}
Source code in models/smarttext/src/smarttext/configuration_smarttext.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
uses_expanded_region
property
¶
uses_expanded_region: bool
Return whether the original RoE expanded-region mode is active.
scorer_input_channels
property
¶
scorer_input_channels: int
Return the RGB scorer input channel count.
__init__ ¶
__init__(
*,
id2label: Mapping[int | str, str] | None = None,
scorer_scale: str = "multi",
scorer_backbone: SmartTextBackbone
| str = SmartTextBackbone.shufflenetv2,
align_size: int = 9,
reduction_dim: int = 8,
downsample: int = 4,
model_type_name: SmartTextRegionMode
| str = SmartTextRegionMode.RoE,
image_size: int = 256,
ratio_list: Sequence[float] = (1.0, 0.8),
text_spacing: int = 20,
exp_prop: int = 6,
grid_num: int = 120,
saliency_coef: float = 2.6,
max_text_area_coef: float = 17.0,
min_text_area_coef: float = 7.0,
min_font_size: int = 10,
max_font_size: int = 500,
font_inc_unit: int = 5,
candi_res: int = 3,
contrast_threshold: float = 5.0,
mos_mean: float = 2.95,
mos_std: float = 0.8,
rgb_mean: Sequence[float] = (0.485, 0.456, 0.406),
rgb_std: Sequence[float] = (0.229, 0.224, 0.225),
scorer_subfolder: str = "scorer",
saliency_subfolder: str = "saliency_model",
processor_subfolder: str = "processor",
original_options: Mapping[str, SmartTextMetadataValue]
| None = None,
conversion_report: Mapping[
str, SmartTextMetadataValue | list[str]
]
| None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize SmartText configuration.
Source code in models/smarttext/src/smarttext/configuration_smarttext.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 | |
SmartTextRegionMode ¶
Bases: StrEnum
Supported SmartText region scoring modes.
Source code in models/smarttext/src/smarttext/configuration_smarttext.py
19 20 21 22 23 | |
SmartTextImageProcessor ¶
Bases: BaseImageProcessor
Prepare SmartText scorer and BASNet image tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_size
|
int
|
Scorer short-side target. |
256
|
rgb_mean
|
Sequence[float]
|
Scorer RGB normalization mean. |
(0.485, 0.456, 0.406)
|
rgb_std
|
Sequence[float]
|
Scorer RGB normalization standard deviation. |
(0.229, 0.224, 0.225)
|
Examples:
>>> processor = SmartTextImageProcessor()
>>> batch = processor.preprocess(Image.new("RGB", (32, 32)))
>>> tuple(batch["pixel_values"].shape[:2])
(1, 3)
Source code in models/smarttext/src/smarttext/image_processing_smarttext.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 | |
__init__ ¶
__init__(
image_size: int = 256,
rgb_mean: Sequence[float] = (0.485, 0.456, 0.406),
rgb_std: Sequence[float] = (0.229, 0.224, 0.225),
**kwargs: str | int | float | bool | None,
) -> None
Initialize image processor settings.
Source code in models/smarttext/src/smarttext/image_processing_smarttext.py
37 38 39 40 41 42 43 44 45 46 47 48 | |
from_config
classmethod
¶
from_config(
config: SmartTextConfig,
) -> "SmartTextImageProcessor"
Build an image processor from SmartText configuration.
Source code in models/smarttext/src/smarttext/image_processing_smarttext.py
50 51 52 53 54 55 56 57 | |
preprocess ¶
preprocess(
images: ImageInput | Sequence[ImageInput],
*,
return_tensors: Literal["pt"] = "pt",
target_min_side: int | None = None,
rgb_mean: Sequence[float] | None = None,
rgb_std: Sequence[float] | None = None,
**kwargs: str | int | float | bool | None,
) -> BatchFeature
Preprocess images for the SmartText scorer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput]
|
RGB image or image batch. |
required |
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
target_min_side
|
int | None
|
Optional short-side target override. |
None
|
rgb_mean
|
Sequence[float] | None
|
Optional RGB mean override. |
None
|
rgb_std
|
Sequence[float] | None
|
Optional RGB std override. |
None
|
kwargs
|
str | int | float | bool | None
|
Ignored compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
BatchFeature
|
Batch feature with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in models/smarttext/src/smarttext/image_processing_smarttext.py
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 | |
preprocess_basnet ¶
preprocess_basnet(
images: ImageInput | Sequence[ImageInput],
*,
return_tensors: Literal["pt"] = "pt",
) -> BatchFeature
Preprocess images for BASNet saliency prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput]
|
RGB image or image batch. |
required |
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
Returns:
| Type | Description |
|---|---|
BatchFeature
|
Batch feature with |
Source code in models/smarttext/src/smarttext/image_processing_smarttext.py
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 | |
SmartTextBASNet ¶
Bases: BASNetModel
SmartText saliency component backed by the shared BASNet model.
Source code in models/smarttext/src/smarttext/modeling_basnet.py
17 18 19 20 21 22 23 24 | |
__init__ ¶
__init__(config: SmartTextConfig) -> None
Initialize the SmartText saliency component.
Source code in models/smarttext/src/smarttext/modeling_basnet.py
22 23 24 | |
SmartTextScorer ¶
Bases: PreTrainedModel
Reference-compatible SMT candidate scorer.
The module names match the original smtModel.py for
build_smt_model(scale="multi", alignsize=9, reddim=8,
model="shufflenetv2", downsample=4) so SMT.pth loads directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SmartTextConfig
|
SmartText configuration. |
required |
Examples:
>>> config = SmartTextConfig()
>>> model = SmartTextScorer(config)
>>> "Feat_ext.feature3.0.0.weight" in model.state_dict()
True
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
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 | |
__init__ ¶
__init__(config: SmartTextConfig) -> None
Initialize scorer scorer modules.
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
forward ¶
forward(
pixel_values: Float[
Tensor, "batch channels height width"
],
boxes: Float[Tensor, "candidates 5"],
return_dict: bool | None = None,
) -> (
SmartTextScorerOutput
| tuple[Float[torch.Tensor, "candidates"]]
)
Score candidate text regions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values
|
Float[Tensor, 'batch channels height width']
|
RGB scorer tensor shaped |
required |
boxes
|
Float[Tensor, 'candidates 5']
|
RoI rows shaped |
required |
return_dict
|
bool | None
|
Whether to return a |
None
|
Returns:
| Type | Description |
|---|---|
SmartTextScorerOutput | tuple[Float[Tensor, 'candidates']]
|
Candidate scores as a |
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
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 | |
SmartTextScorerOutput
dataclass
¶
Bases: ModelOutput
Output of SmartTextScorer.forward.
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
18 19 20 21 22 | |
SmartTextPipeline ¶
Bases: LayoutGenerationPipeline
Transformers-side SmartText pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scorer
|
SmartTextScorer
|
Candidate scoring model. |
required |
saliency_model
|
SmartTextBASNet
|
BASNet saliency model. |
required |
processor
|
SmartTextProcessor | None
|
Input/output processor. |
None
|
config
|
SmartTextConfig | None
|
Optional root pipeline config. |
None
|
device
|
str | device | None
|
Optional runtime device. |
None
|
Examples:
>>> config = SmartTextConfig(align_size=3, reduction_dim=4, grid_num=16, max_font_size=20)
>>> pipe = SmartTextPipeline(SmartTextScorer(config), SmartTextBASNet(config), config=config)
>>> pipe.config.model_type
'smarttext'
Source code in models/smarttext/src/smarttext/pipeline_smarttext.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
__init__ ¶
__init__(
scorer: SmartTextScorer,
saliency_model: SmartTextBASNet,
processor: SmartTextProcessor | None = None,
config: SmartTextConfig | None = None,
device: str | device | None = None,
) -> None
Initialize SmartText pipeline.
Source code in models/smarttext/src/smarttext/pipeline_smarttext.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
__call__ ¶
__call__(
images: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch channels height width"]
| None = None,
*,
content: Mapping[
str,
ImageInput
| Sequence[ImageInput]
| str
| Sequence[str]
| Float[Tensor, "batch height width"]
| Sequence[CandidateBoxRow]
| Sequence[Sequence[CandidateBoxRow]],
]
| None = None,
prompt: str | Sequence[str] | None = None,
text: str | Sequence[str] | None = None,
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.content_image,
labels: Int[Tensor, "batch elements"]
| Sequence[Sequence[int]]
| Sequence[int]
| None = None,
bbox: Float[Tensor, "batch elements 4"]
| Sequence[Sequence[Sequence[float]]]
| Sequence[Sequence[float]]
| None = None,
mask: Bool[Tensor, "batch elements"]
| Sequence[Sequence[bool]]
| Sequence[bool]
| None = None,
num_elements: int
| Sequence[int]
| Int[Tensor, "batch"]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: OutputType
| Literal["dataclass", "dict"] = OutputType.dataclass,
return_intermediates: bool = False,
font: str
| Path
| FreeTypeFont
| ImageFont
| None = None,
ratio_list: Sequence[float] | None = None,
text_spacing: int | None = None,
candi_res: int | None = None,
saliency: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch height width"]
| None = None,
candidate_boxes: Sequence[CandidateBoxRow]
| Sequence[Sequence[CandidateBoxRow]]
| None = None,
return_text_lines: bool = False,
score_normalization: Literal["mos", "raw"] = "mos",
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| dict[
str,
Shaped[torch.Tensor, "..."]
| str
| list[int]
| list[SmartTextCandidate]
| None,
]
| None,
]
)
Generate text placement boxes for content images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput] | Float[Tensor, 'batch channels height width'] | None
|
RGB image or image batch. |
None
|
content
|
Mapping[str, ImageInput | Sequence[ImageInput] | str | Sequence[str] | Float[Tensor, 'batch height width'] | Sequence[CandidateBoxRow] | Sequence[Sequence[CandidateBoxRow]]] | None
|
Optional content carrier. |
None
|
prompt
|
str | Sequence[str] | None
|
Prompt text payload. |
None
|
text
|
str | Sequence[str] | None
|
Alias for prompt. |
None
|
batch_size
|
int
|
Expected batch size for validation. |
1
|
seed
|
int | None
|
Optional seed used only when |
None
|
generator
|
Generator | None
|
Explicit torch generator; wins over |
None
|
condition_type
|
ConditionType | str
|
Must normalize to |
content_image
|
labels
|
Int[Tensor, 'batch elements'] | Sequence[Sequence[int]] | Sequence[int] | None
|
Unsupported v1 compatibility argument. |
None
|
bbox
|
Float[Tensor, 'batch elements 4'] | Sequence[Sequence[Sequence[float]]] | Sequence[Sequence[float]] | None
|
Unsupported v1 compatibility argument. |
None
|
mask
|
Bool[Tensor, 'batch elements'] | Sequence[Sequence[bool]] | Sequence[bool] | None
|
Unsupported v1 compatibility argument. |
None
|
num_elements
|
int | Sequence[int] | Int[Tensor, 'batch'] | None
|
Unsupported v1 compatibility argument. |
None
|
box_format
|
BoxFormat | str
|
Public v1 compatibility argument. |
xywh
|
normalized
|
bool
|
Public v1 compatibility argument. |
True
|
canvas_size
|
tuple[int, int] | None
|
Optional source canvas size override. |
None
|
num_inference_steps
|
int | None
|
Unused v1 compatibility argument. |
None
|
output_type
|
OutputType | Literal['dataclass', 'dict']
|
|
dataclass
|
return_intermediates
|
bool
|
Whether to include intermediate payloads. |
False
|
font
|
str | Path | FreeTypeFont | ImageFont | None
|
TrueType font path or PIL font object. |
None
|
ratio_list
|
Sequence[float] | None
|
Optional per-line font ratios. |
None
|
text_spacing
|
int | None
|
Optional text-spacing override. |
None
|
candi_res
|
int | None
|
Optional top-k override. |
None
|
saliency
|
ImageInput | Sequence[ImageInput] | Float[Tensor, 'batch height width'] | None
|
Optional saliency map bypassing BASNet. |
None
|
candidate_boxes
|
Sequence[CandidateBoxRow] | Sequence[Sequence[CandidateBoxRow]] | None
|
Optional reference-style candidates. |
None
|
return_text_lines
|
bool
|
Return per-line boxes for top candidate. |
False
|
score_normalization
|
Literal['mos', 'raw']
|
|
'mos'
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Shaped[Tensor, '...'] | dict[int, str] | dict[str, Shaped[Tensor, '...'] | str | list[int] | list[SmartTextCandidate] | None] | None]
|
Shared layout output. |
Source code in models/smarttext/src/smarttext/pipeline_smarttext.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
SmartTextProcessor ¶
Bases: ProcessorMixin
Normalize SmartText content payloads and decode candidate scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_processor
|
SmartTextImageProcessor | None
|
Image processor for RGB and BASNet tensors. |
None
|
config
|
SmartTextConfig
|
SmartText configuration. |
required |
Examples:
>>> processor = SmartTextProcessor(config=SmartTextConfig())
>>> processor.id2label
{0: 'text'}
Source code in models/smarttext/src/smarttext/processing_smarttext.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 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 | |
__init__ ¶
__init__(
*,
image_processor: SmartTextImageProcessor | None = None,
config: SmartTextConfig,
id2label: Mapping[int | str, str] | None = None,
) -> None
Initialize processor.
Source code in models/smarttext/src/smarttext/processing_smarttext.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
save_pretrained ¶
save_pretrained(
save_directory: str | Path,
push_to_hub: bool = False,
**kwargs: str | int | float | bool | None,
) -> None
Save processor metadata and image-processor config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_directory
|
str | Path
|
Directory receiving |
required |
push_to_hub
|
bool
|
Accepted for |
False
|
kwargs
|
str | int | float | bool | None
|
Accepted for |
{}
|
Source code in models/smarttext/src/smarttext/processing_smarttext.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
cache_dir: str | PathLike[str] | None = None,
force_download: bool = False,
local_files_only: bool = False,
token: str | bool | None = None,
revision: str = "main",
subfolder: str | None = None,
**kwargs: str | int | float | bool | None,
) -> Self
Load processor metadata from a local checkpoint directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pretrained_model_name_or_path
|
str | PathLike[str]
|
Root path or processor subfolder. |
required |
cache_dir
|
str | PathLike[str] | None
|
Accepted for |
None
|
force_download
|
bool
|
Accepted for |
False
|
local_files_only
|
bool
|
Accepted for API compatibility. |
False
|
token
|
str | bool | None
|
Accepted for |
None
|
revision
|
str
|
Accepted for |
'main'
|
subfolder
|
str | None
|
Optional processor subfolder. |
None
|
kwargs
|
str | int | float | bool | None
|
Ignored compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
Loaded SmartText processor. |
Source code in models/smarttext/src/smarttext/processing_smarttext.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 131 132 133 134 | |
__call__ ¶
__call__(
images: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch channels height width"]
| None = None,
*,
content: Mapping[
str,
ImageInput
| Sequence[ImageInput]
| str
| Sequence[str]
| Float[Tensor, "batch height width"]
| Sequence[CandidateBoxRow]
| Sequence[Sequence[CandidateBoxRow]],
]
| None = None,
prompt: str | Sequence[str] | None = None,
text: str | Sequence[str] | None = None,
saliency: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch height width"]
| None = None,
candidate_boxes: Sequence[CandidateBoxRow]
| Sequence[Sequence[CandidateBoxRow]]
| None = None,
font: str
| Path
| FreeTypeFont
| ImageFont
| None = None,
return_tensors: Literal["pt"] = "pt",
**kwargs: str | int | float | bool | None,
) -> BatchEncoding
Encode SmartText public inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput] | Float[Tensor, 'batch channels height width'] | None
|
Image or image batch. |
None
|
content
|
Mapping[str, ImageInput | Sequence[ImageInput] | str | Sequence[str] | Float[Tensor, 'batch height width'] | Sequence[CandidateBoxRow] | Sequence[Sequence[CandidateBoxRow]]] | None
|
Optional content carrier with |
None
|
prompt
|
str | Sequence[str] | None
|
Prompt text payload. |
None
|
text
|
str | Sequence[str] | None
|
Alias for prompt text. |
None
|
saliency
|
ImageInput | Sequence[ImageInput] | Float[Tensor, 'batch height width'] | None
|
Optional saliency map. |
None
|
candidate_boxes
|
Sequence[CandidateBoxRow] | Sequence[Sequence[CandidateBoxRow]] | None
|
Optional reference-style candidate rows. |
None
|
font
|
str | Path | FreeTypeFont | ImageFont | None
|
Font path or PIL font object. |
None
|
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
kwargs
|
str | int | float | bool | None
|
Ignored forward-compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
BatchEncoding
|
Batch encoding containing normalized payloads. |
Source code in models/smarttext/src/smarttext/processing_smarttext.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 | |
decode ¶
decode(
*,
candidates: Sequence[SmartTextCandidate],
scores: Float[Tensor, "candidates"],
image_size: tuple[int, int],
output_type: Literal["dataclass", "dict"] = "dataclass",
return_text_lines: bool = False,
top_k: int = 3,
score_normalization: Literal["mos", "raw"] = "mos",
text_color: str | None = None,
intermediates: dict[
str,
Shaped[Tensor, "..."]
| str
| list[SmartTextCandidate]
| list[int]
| None,
]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| dict[
str,
Shaped[torch.Tensor, "..."]
| str
| list[SmartTextCandidate]
| list[int]
| None,
]
| None,
]
)
Decode sorted candidates into the shared layout schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
Sequence[SmartTextCandidate]
|
Candidate metadata. |
required |
scores
|
Float[Tensor, 'candidates']
|
Raw scorer outputs. |
required |
image_size
|
tuple[int, int]
|
Source image size as |
required |
output_type
|
Literal['dataclass', 'dict']
|
Return dataclass or dict. |
'dataclass'
|
return_text_lines
|
bool
|
Return per-line boxes instead of top-level boxes. |
False
|
top_k
|
int
|
Number of top candidates to return. |
3
|
score_normalization
|
Literal['mos', 'raw']
|
|
'mos'
|
text_color
|
str | None
|
Optional selected text color. |
None
|
intermediates
|
dict[str, Shaped[Tensor, '...'] | str | list[SmartTextCandidate] | list[int] | None] | None
|
Optional extra intermediate payload. |
None
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Shaped[Tensor, '...'] | dict[int, str] | dict[str, Shaped[Tensor, '...'] | str | list[SmartTextCandidate] | list[int] | None] | None]
|
Shared |
Source code in models/smarttext/src/smarttext/processing_smarttext.py
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 | |
candidate_generation ¶
Candidate generation helpers for SmartText text placement.
CandidateBoxRow ¶
Bases: TypedDict
JSON-compatible candidate row.
Source code in models/smarttext/src/smarttext/candidate_generation.py
21 22 23 24 25 26 27 28 29 30 31 | |
SmartTextLine
dataclass
¶
One rendered prompt line within a candidate region.
Source code in models/smarttext/src/smarttext/candidate_generation.py
34 35 36 37 38 39 40 | |
SmartTextCandidate
dataclass
¶
A candidate text block and its line-level boxes.
Source code in models/smarttext/src/smarttext/candidate_generation.py
43 44 45 46 47 48 49 | |
split_prompt_lines ¶
split_prompt_lines(
prompt: str, ratio_list: Sequence[float]
) -> tuple[str, ...]
Split prompt text into non-empty lines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
User text payload. |
required |
ratio_list
|
Sequence[float]
|
Font-size ratio list. The argument is accepted here so tests can verify prompt/ratio length behavior at one public boundary. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
Non-empty prompt lines, preserving internal spaces. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no non-empty line remains. |
Examples:
>>> split_prompt_lines("A\\nB", (1.0, 0.8))
('A', 'B')
Source code in models/smarttext/src/smarttext/candidate_generation.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 | |
generate_candidates ¶
generate_candidates(
image: Image,
saliency: Shaped[ndarray, "..."]
| Shaped[Tensor, "..."],
*,
prompt: str,
font: str | Path | FreeTypeFont | ImageFont,
config: SmartTextConfig,
ratio_list: Sequence[float] | None = None,
) -> list[SmartTextCandidate]
Generate deterministic candidate text boxes from image, saliency, and text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Source RGB image. |
required |
saliency
|
Shaped[ndarray, '...'] | Shaped[Tensor, '...']
|
Saliency map in image space. |
required |
prompt
|
str
|
Text payload split on newlines. |
required |
font
|
str | Path | FreeTypeFont | ImageFont
|
TrueType font path or loaded PIL font object. |
required |
config
|
SmartTextConfig
|
SmartText configuration. |
required |
ratio_list
|
Sequence[float] | None
|
Optional per-line font-size ratios. |
None
|
Returns:
| Type | Description |
|---|---|
list[SmartTextCandidate]
|
Candidate text regions sorted in deterministic search order. |
Examples:
>>> img = Image.new("RGB", (64, 64), "white")
>>> candidates = generate_candidates(
... img,
... np.zeros((64, 64), dtype=np.float32),
... prompt="Hi",
... font=ImageFont.load_default(),
... config=SmartTextConfig(grid_num=16, max_font_size=20),
... )
>>> bool(candidates)
True
Source code in models/smarttext/src/smarttext/candidate_generation.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 | |
prepare_scorer_batch ¶
prepare_scorer_batch(
image: Image,
candidates: Sequence[SmartTextCandidate],
*,
config: SmartTextConfig,
) -> tuple[
Float[torch.Tensor, "batch channels height width"],
Float[torch.Tensor, "candidates 5"],
list[SmartTextCandidate],
]
Prepare scorer image tensor and RoI/RoD boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Source image. |
required |
candidates
|
Sequence[SmartTextCandidate]
|
Candidate boxes. |
required |
config
|
SmartTextConfig
|
SmartText configuration. |
required |
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'batch channels height width']
|
|
Float[Tensor, 'candidates 5']
|
batch index in column zero, and the candidate list. |
Source code in models/smarttext/src/smarttext/candidate_generation.py
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 | |
candidate_to_reference_json ¶
candidate_to_reference_json(
candidate: SmartTextCandidate,
) -> list[CandidateBoxRow]
Convert a candidate to the reference JSON row format.
Source code in models/smarttext/src/smarttext/candidate_generation.py
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 | |
candidate_from_reference_json ¶
candidate_from_reference_json(
row: Sequence[CandidateBoxRow],
) -> SmartTextCandidate
Convert one reference JSON candidate row to typed metadata.
Source code in models/smarttext/src/smarttext/candidate_generation.py
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 | |
color ¶
Text color helpers ported from the SmartText demo path.
SmartTextColorCandidate ¶
Bases: TypedDict
Foreground color candidate and contrast score.
Source code in models/smarttext/src/smarttext/color.py
20 21 22 23 24 | |
dominant_colors ¶
dominant_colors(
image: Shaped[ndarray, "height width channels"],
clusters: int,
) -> list[Shaped[np.ndarray, "channels"]]
Return reference-sorted KMeans dominant colors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Shaped[ndarray, 'height width channels']
|
RGB image array. |
required |
clusters
|
int
|
Number of KMeans clusters. |
required |
Returns:
| Type | Description |
|---|---|
list[Shaped[ndarray, 'channels']]
|
Cluster centers sorted by RGB tuple, matching |
list[Shaped[ndarray, 'channels']]
|
the original |
Source code in models/smarttext/src/smarttext/color.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
rgb_distance ¶
rgb_distance(
rgb: Shaped[ndarray, "channels"]
| list[float]
| list[int],
) -> float
Return the channel-spread distance.
Source code in models/smarttext/src/smarttext/color.py
47 48 49 50 51 | |
rgb_to_hex ¶
rgb_to_hex(
rgb: Shaped[ndarray, "channels"]
| list[float]
| list[int],
) -> str
Convert an RGB row to the uppercase hex form.
Source code in models/smarttext/src/smarttext/color.py
54 55 56 57 58 59 | |
luminance ¶
luminance(rgb: list[float]) -> float
Return WCAG relative luminance using the reference formula.
Source code in models/smarttext/src/smarttext/color.py
62 63 64 65 66 67 68 69 70 | |
contrast_rate ¶
contrast_rate(
rgb_a: Shaped[ndarray, "channels"]
| list[float]
| list[int],
rgb_b: Shaped[ndarray, "channels"]
| list[float]
| list[int],
) -> float
Return reference-rounded contrast ratio between two RGB colors.
Source code in models/smarttext/src/smarttext/color.py
73 74 75 76 77 78 79 80 81 82 83 84 | |
best_color_candidates ¶
best_color_candidates(
image: Image | Shaped[ndarray, "height width channels"],
crop: Shaped[
ndarray, "crop_height crop_width channels"
],
*,
contrast_threshold: float,
random_seed: int | None = 0,
) -> list[SmartTextColorCandidate]
Return reference-ordered color candidates for a text region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image | Shaped[ndarray, 'height width channels']
|
Source RGB image. |
required |
crop
|
Shaped[ndarray, 'crop_height crop_width channels']
|
Candidate crop from |
required |
contrast_threshold
|
float
|
Minimum contrast ratio accepted before fallback. |
required |
random_seed
|
int | None
|
Seed used to make the reference KMeans path deterministic. |
0
|
Returns:
| Type | Description |
|---|---|
list[SmartTextColorCandidate]
|
Candidate dictionaries with |
Source code in models/smarttext/src/smarttext/color.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
choose_text_color ¶
choose_text_color(
image: Image | Shaped[ndarray, "height width channels"],
crop_bbox_ltrb_px: tuple[int, int, int, int],
*,
contrast_threshold: float,
) -> str
Choose the SmartText foreground for a candidate region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image | Shaped[ndarray, 'height width channels']
|
Source RGB image. |
required |
crop_bbox_ltrb_px
|
tuple[int, int, int, int]
|
Candidate box as |
required |
contrast_threshold
|
float
|
Threshold above which white text is preferred. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Hex foreground color in the uppercase form. |
Examples:
>>> choose_text_color(Image.new("RGB", (8, 8), "black"), (0, 0, 8, 8), contrast_threshold=5)
'#FFFFFF'
Source code in models/smarttext/src/smarttext/color.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
configuration_smarttext ¶
Configuration objects for SmartText text placement.
The defaults mirror the original test_opt.yml and the scorer settings
used by the original smtModel.py::build_smt_model.
SmartTextRegionMode ¶
Bases: StrEnum
Supported SmartText region scoring modes.
Source code in models/smarttext/src/smarttext/configuration_smarttext.py
19 20 21 22 23 | |
SmartTextBackbone ¶
Bases: StrEnum
Backbone names supported by the original SmartText scorer.
Source code in models/smarttext/src/smarttext/configuration_smarttext.py
26 27 28 29 30 31 32 | |
SmartTextConfig ¶
Bases: PretrainedConfig
Configuration for SmartText scorer, saliency model, and pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id2label
|
Mapping[int | str, str] | None
|
Public label mapping. Defaults to one |
None
|
scorer_scale
|
str
|
Original scorer scale mode. |
'multi'
|
scorer_backbone
|
SmartTextBackbone | str
|
Original scorer backbone. |
shufflenetv2
|
align_size
|
int
|
RoI/RoD pooled spatial size. |
9
|
reduction_dim
|
int
|
Reduced feature channels before scoring. |
8
|
downsample
|
int
|
Scorer feature-map downsampling factor. |
4
|
model_type_name
|
SmartTextRegionMode | str
|
Original |
RoE
|
image_size
|
int
|
Short-side normalization target for scorer preprocessing. |
256
|
ratio_list
|
Sequence[float]
|
Per-line font-size ratios. |
(1.0, 0.8)
|
text_spacing
|
int
|
Pixel spacing between prompt lines. |
20
|
exp_prop
|
int
|
Original expanded-region coefficient. |
6
|
grid_num
|
int
|
Candidate search grid count. |
120
|
saliency_coef
|
float
|
Saliency suppression coefficient. |
2.6
|
max_text_area_coef
|
float
|
Maximum candidate area divisor. |
17.0
|
min_text_area_coef
|
float
|
Minimum candidate area divisor. |
7.0
|
min_font_size
|
int
|
Minimum candidate font size. |
10
|
max_font_size
|
int
|
Maximum candidate font size. |
500
|
font_inc_unit
|
int
|
Candidate font-size step. |
5
|
candi_res
|
int
|
Number of selected candidates. |
3
|
contrast_threshold
|
float
|
Foreground/background contrast threshold. |
5.0
|
mos_mean
|
float
|
MOS score mean used by the original demo. |
2.95
|
mos_std
|
float
|
MOS score standard deviation. |
0.8
|
rgb_mean
|
Sequence[float]
|
RGB normalization mean for scorer inputs. |
(0.485, 0.456, 0.406)
|
rgb_std
|
Sequence[float]
|
RGB normalization std for scorer inputs. |
(0.229, 0.224, 0.225)
|
scorer_subfolder
|
str
|
Pipeline scorer subfolder. |
'scorer'
|
saliency_subfolder
|
str
|
Pipeline saliency-model subfolder. |
'saliency_model'
|
processor_subfolder
|
str
|
Pipeline processor subfolder. |
'processor'
|
original_options
|
Mapping[str, SmartTextMetadataValue] | None
|
Raw reference option values preserved for audit. |
None
|
conversion_report
|
Mapping[str, SmartTextMetadataValue | list[str]] | None
|
Conversion metadata persisted in configs. |
None
|
kwargs
|
str | int | float | bool | None
|
Extra |
{}
|
Examples:
>>> config = SmartTextConfig()
>>> config.id2label
{0: 'text'}
Source code in models/smarttext/src/smarttext/configuration_smarttext.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
uses_expanded_region
property
¶
uses_expanded_region: bool
Return whether the original RoE expanded-region mode is active.
scorer_input_channels
property
¶
scorer_input_channels: int
Return the RGB scorer input channel count.
__init__ ¶
__init__(
*,
id2label: Mapping[int | str, str] | None = None,
scorer_scale: str = "multi",
scorer_backbone: SmartTextBackbone
| str = SmartTextBackbone.shufflenetv2,
align_size: int = 9,
reduction_dim: int = 8,
downsample: int = 4,
model_type_name: SmartTextRegionMode
| str = SmartTextRegionMode.RoE,
image_size: int = 256,
ratio_list: Sequence[float] = (1.0, 0.8),
text_spacing: int = 20,
exp_prop: int = 6,
grid_num: int = 120,
saliency_coef: float = 2.6,
max_text_area_coef: float = 17.0,
min_text_area_coef: float = 7.0,
min_font_size: int = 10,
max_font_size: int = 500,
font_inc_unit: int = 5,
candi_res: int = 3,
contrast_threshold: float = 5.0,
mos_mean: float = 2.95,
mos_std: float = 0.8,
rgb_mean: Sequence[float] = (0.485, 0.456, 0.406),
rgb_std: Sequence[float] = (0.229, 0.224, 0.225),
scorer_subfolder: str = "scorer",
saliency_subfolder: str = "saliency_model",
processor_subfolder: str = "processor",
original_options: Mapping[str, SmartTextMetadataValue]
| None = None,
conversion_report: Mapping[
str, SmartTextMetadataValue | list[str]
]
| None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize SmartText configuration.
Source code in models/smarttext/src/smarttext/configuration_smarttext.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 | |
conversion ¶
Checkpoint conversion helpers for SmartText.
strip_module_prefix ¶
strip_module_prefix(
state_dict: Mapping[str, Shaped[Tensor, "..."]],
) -> dict[str, Shaped[torch.Tensor, "..."]]
Remove DataParallel module. prefixes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
Mapping[str, Shaped[Tensor, '...']]
|
Raw PyTorch state dict. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Shaped[Tensor, '...']]
|
State dict with prefixes removed. |
Examples:
>>> strip_module_prefix({"module.a": torch.tensor(1)})["a"].item()
1
Source code in models/smarttext/src/smarttext/conversion.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
file_sha256 ¶
file_sha256(path: Path) -> str
Compute SHA256 for a local file.
Source code in models/smarttext/src/smarttext/conversion.py
42 43 44 45 46 47 48 | |
convert_original_checkpoints ¶
convert_original_checkpoints(
*,
smt_checkpoint: Path,
basnet_checkpoint: Path,
output_dir: Path,
config: SmartTextConfig,
) -> dict[str, str | int | list[str]]
Convert raw SmartText checkpoints into a pipeline directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smt_checkpoint
|
Path
|
Raw SMT scorer checkpoint. |
required |
basnet_checkpoint
|
Path
|
Raw BASNet checkpoint. |
required |
output_dir
|
Path
|
Output pipeline directory. |
required |
config
|
SmartTextConfig
|
SmartText config. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str | int | list[str]]
|
Conversion report dictionary. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If converted keys do not strictly match the target models. |
Source code in models/smarttext/src/smarttext/conversion.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
image_processing_smarttext ¶
Image processor for SmartText RGB and BASNet inputs.
SmartTextImageProcessor ¶
Bases: BaseImageProcessor
Prepare SmartText scorer and BASNet image tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_size
|
int
|
Scorer short-side target. |
256
|
rgb_mean
|
Sequence[float]
|
Scorer RGB normalization mean. |
(0.485, 0.456, 0.406)
|
rgb_std
|
Sequence[float]
|
Scorer RGB normalization standard deviation. |
(0.229, 0.224, 0.225)
|
Examples:
>>> processor = SmartTextImageProcessor()
>>> batch = processor.preprocess(Image.new("RGB", (32, 32)))
>>> tuple(batch["pixel_values"].shape[:2])
(1, 3)
Source code in models/smarttext/src/smarttext/image_processing_smarttext.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 | |
__init__ ¶
__init__(
image_size: int = 256,
rgb_mean: Sequence[float] = (0.485, 0.456, 0.406),
rgb_std: Sequence[float] = (0.229, 0.224, 0.225),
**kwargs: str | int | float | bool | None,
) -> None
Initialize image processor settings.
Source code in models/smarttext/src/smarttext/image_processing_smarttext.py
37 38 39 40 41 42 43 44 45 46 47 48 | |
from_config
classmethod
¶
from_config(
config: SmartTextConfig,
) -> "SmartTextImageProcessor"
Build an image processor from SmartText configuration.
Source code in models/smarttext/src/smarttext/image_processing_smarttext.py
50 51 52 53 54 55 56 57 | |
preprocess ¶
preprocess(
images: ImageInput | Sequence[ImageInput],
*,
return_tensors: Literal["pt"] = "pt",
target_min_side: int | None = None,
rgb_mean: Sequence[float] | None = None,
rgb_std: Sequence[float] | None = None,
**kwargs: str | int | float | bool | None,
) -> BatchFeature
Preprocess images for the SmartText scorer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput]
|
RGB image or image batch. |
required |
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
target_min_side
|
int | None
|
Optional short-side target override. |
None
|
rgb_mean
|
Sequence[float] | None
|
Optional RGB mean override. |
None
|
rgb_std
|
Sequence[float] | None
|
Optional RGB std override. |
None
|
kwargs
|
str | int | float | bool | None
|
Ignored compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
BatchFeature
|
Batch feature with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in models/smarttext/src/smarttext/image_processing_smarttext.py
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 | |
preprocess_basnet ¶
preprocess_basnet(
images: ImageInput | Sequence[ImageInput],
*,
return_tensors: Literal["pt"] = "pt",
) -> BatchFeature
Preprocess images for BASNet saliency prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput]
|
RGB image or image batch. |
required |
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
Returns:
| Type | Description |
|---|---|
BatchFeature
|
Batch feature with |
Source code in models/smarttext/src/smarttext/image_processing_smarttext.py
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 | |
model_card ¶
Model-card helpers for SmartText.
build_smarttext_model_card ¶
build_smarttext_model_card(
*,
hub_id: str = "creative-graphic-design/smarttext-smt",
parity_results: dict[str, str | int | float | bool]
| None = None,
) -> str
Render a SmartText Hub model card.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hub_id
|
str
|
Target Hub repository id. |
'creative-graphic-design/smarttext-smt'
|
parity_results
|
dict[str, str | int | float | bool] | None
|
Optional parity result payload. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Markdown model-card text. |
Source code in models/smarttext/src/smarttext/model_card.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
modeling_basnet ¶
SmartText BASNet compatibility exports.
SmartTextBASNet ¶
Bases: BASNetModel
SmartText saliency component backed by the shared BASNet model.
Source code in models/smarttext/src/smarttext/modeling_basnet.py
17 18 19 20 21 22 23 24 | |
__init__ ¶
__init__(config: SmartTextConfig) -> None
Initialize the SmartText saliency component.
Source code in models/smarttext/src/smarttext/modeling_basnet.py
22 23 24 | |
normalize_saliency ¶
normalize_saliency(
pred: Float[Tensor, "... height width"],
) -> Float[torch.Tensor, "... height width"]
Normalize saliency maps independently over each spatial map.
Source code in models/basnet/src/basnet/modeling_basnet.py
149 150 151 152 153 154 155 | |
modeling_smarttext ¶
SmartText scorer model ported from the original SMT architecture.
SmartTextScorerOutput
dataclass
¶
Bases: ModelOutput
Output of SmartTextScorer.forward.
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
18 19 20 21 22 | |
SmartTextRoIAlignAvg ¶
Bases: _AlignBase
PyTorch port of the reference RoIAlignAvg forward kernel.
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
241 242 243 244 245 246 247 248 249 250 251 | |
forward ¶
forward(
features: Float[Tensor, "batch channels height width"],
rois: Float[Tensor, "candidates 5"],
) -> Float[
torch.Tensor,
"candidates channels aligned_height aligned_width",
]
Align RoI features and average adjacent samples.
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
244 245 246 247 248 249 250 251 | |
SmartTextRoDAlignAvg ¶
Bases: _AlignBase
PyTorch port of the reference RoDAlignAvg forward kernel.
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
254 255 256 257 258 259 260 261 262 263 264 | |
forward ¶
forward(
features: Float[Tensor, "batch channels height width"],
rois: Float[Tensor, "candidates 5"],
) -> Float[
torch.Tensor,
"candidates channels aligned_height aligned_width",
]
Align outside-region features and average adjacent samples.
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
257 258 259 260 261 262 263 264 | |
SmartTextScorer ¶
Bases: PreTrainedModel
Reference-compatible SMT candidate scorer.
The module names match the original smtModel.py for
build_smt_model(scale="multi", alignsize=9, reddim=8,
model="shufflenetv2", downsample=4) so SMT.pth loads directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SmartTextConfig
|
SmartText configuration. |
required |
Examples:
>>> config = SmartTextConfig()
>>> model = SmartTextScorer(config)
>>> "Feat_ext.feature3.0.0.weight" in model.state_dict()
True
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
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 | |
__init__ ¶
__init__(config: SmartTextConfig) -> None
Initialize scorer scorer modules.
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
forward ¶
forward(
pixel_values: Float[
Tensor, "batch channels height width"
],
boxes: Float[Tensor, "candidates 5"],
return_dict: bool | None = None,
) -> (
SmartTextScorerOutput
| tuple[Float[torch.Tensor, "candidates"]]
)
Score candidate text regions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values
|
Float[Tensor, 'batch channels height width']
|
RGB scorer tensor shaped |
required |
boxes
|
Float[Tensor, 'candidates 5']
|
RoI rows shaped |
required |
return_dict
|
bool | None
|
Whether to return a |
None
|
Returns:
| Type | Description |
|---|---|
SmartTextScorerOutput | tuple[Float[Tensor, 'candidates']]
|
Candidate scores as a |
Source code in models/smarttext/src/smarttext/modeling_smarttext.py
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 | |
pipeline_smarttext ¶
Pipeline interface for SmartText content-image text placement.
OutputType ¶
Bases: StrEnum
Supported SmartText pipeline output containers.
Source code in models/smarttext/src/smarttext/pipeline_smarttext.py
44 45 46 47 48 | |
SmartTextPipeline ¶
Bases: LayoutGenerationPipeline
Transformers-side SmartText pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scorer
|
SmartTextScorer
|
Candidate scoring model. |
required |
saliency_model
|
SmartTextBASNet
|
BASNet saliency model. |
required |
processor
|
SmartTextProcessor | None
|
Input/output processor. |
None
|
config
|
SmartTextConfig | None
|
Optional root pipeline config. |
None
|
device
|
str | device | None
|
Optional runtime device. |
None
|
Examples:
>>> config = SmartTextConfig(align_size=3, reduction_dim=4, grid_num=16, max_font_size=20)
>>> pipe = SmartTextPipeline(SmartTextScorer(config), SmartTextBASNet(config), config=config)
>>> pipe.config.model_type
'smarttext'
Source code in models/smarttext/src/smarttext/pipeline_smarttext.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
__init__ ¶
__init__(
scorer: SmartTextScorer,
saliency_model: SmartTextBASNet,
processor: SmartTextProcessor | None = None,
config: SmartTextConfig | None = None,
device: str | device | None = None,
) -> None
Initialize SmartText pipeline.
Source code in models/smarttext/src/smarttext/pipeline_smarttext.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
__call__ ¶
__call__(
images: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch channels height width"]
| None = None,
*,
content: Mapping[
str,
ImageInput
| Sequence[ImageInput]
| str
| Sequence[str]
| Float[Tensor, "batch height width"]
| Sequence[CandidateBoxRow]
| Sequence[Sequence[CandidateBoxRow]],
]
| None = None,
prompt: str | Sequence[str] | None = None,
text: str | Sequence[str] | None = None,
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.content_image,
labels: Int[Tensor, "batch elements"]
| Sequence[Sequence[int]]
| Sequence[int]
| None = None,
bbox: Float[Tensor, "batch elements 4"]
| Sequence[Sequence[Sequence[float]]]
| Sequence[Sequence[float]]
| None = None,
mask: Bool[Tensor, "batch elements"]
| Sequence[Sequence[bool]]
| Sequence[bool]
| None = None,
num_elements: int
| Sequence[int]
| Int[Tensor, "batch"]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: OutputType
| Literal["dataclass", "dict"] = OutputType.dataclass,
return_intermediates: bool = False,
font: str
| Path
| FreeTypeFont
| ImageFont
| None = None,
ratio_list: Sequence[float] | None = None,
text_spacing: int | None = None,
candi_res: int | None = None,
saliency: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch height width"]
| None = None,
candidate_boxes: Sequence[CandidateBoxRow]
| Sequence[Sequence[CandidateBoxRow]]
| None = None,
return_text_lines: bool = False,
score_normalization: Literal["mos", "raw"] = "mos",
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| dict[
str,
Shaped[torch.Tensor, "..."]
| str
| list[int]
| list[SmartTextCandidate]
| None,
]
| None,
]
)
Generate text placement boxes for content images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput] | Float[Tensor, 'batch channels height width'] | None
|
RGB image or image batch. |
None
|
content
|
Mapping[str, ImageInput | Sequence[ImageInput] | str | Sequence[str] | Float[Tensor, 'batch height width'] | Sequence[CandidateBoxRow] | Sequence[Sequence[CandidateBoxRow]]] | None
|
Optional content carrier. |
None
|
prompt
|
str | Sequence[str] | None
|
Prompt text payload. |
None
|
text
|
str | Sequence[str] | None
|
Alias for prompt. |
None
|
batch_size
|
int
|
Expected batch size for validation. |
1
|
seed
|
int | None
|
Optional seed used only when |
None
|
generator
|
Generator | None
|
Explicit torch generator; wins over |
None
|
condition_type
|
ConditionType | str
|
Must normalize to |
content_image
|
labels
|
Int[Tensor, 'batch elements'] | Sequence[Sequence[int]] | Sequence[int] | None
|
Unsupported v1 compatibility argument. |
None
|
bbox
|
Float[Tensor, 'batch elements 4'] | Sequence[Sequence[Sequence[float]]] | Sequence[Sequence[float]] | None
|
Unsupported v1 compatibility argument. |
None
|
mask
|
Bool[Tensor, 'batch elements'] | Sequence[Sequence[bool]] | Sequence[bool] | None
|
Unsupported v1 compatibility argument. |
None
|
num_elements
|
int | Sequence[int] | Int[Tensor, 'batch'] | None
|
Unsupported v1 compatibility argument. |
None
|
box_format
|
BoxFormat | str
|
Public v1 compatibility argument. |
xywh
|
normalized
|
bool
|
Public v1 compatibility argument. |
True
|
canvas_size
|
tuple[int, int] | None
|
Optional source canvas size override. |
None
|
num_inference_steps
|
int | None
|
Unused v1 compatibility argument. |
None
|
output_type
|
OutputType | Literal['dataclass', 'dict']
|
|
dataclass
|
return_intermediates
|
bool
|
Whether to include intermediate payloads. |
False
|
font
|
str | Path | FreeTypeFont | ImageFont | None
|
TrueType font path or PIL font object. |
None
|
ratio_list
|
Sequence[float] | None
|
Optional per-line font ratios. |
None
|
text_spacing
|
int | None
|
Optional text-spacing override. |
None
|
candi_res
|
int | None
|
Optional top-k override. |
None
|
saliency
|
ImageInput | Sequence[ImageInput] | Float[Tensor, 'batch height width'] | None
|
Optional saliency map bypassing BASNet. |
None
|
candidate_boxes
|
Sequence[CandidateBoxRow] | Sequence[Sequence[CandidateBoxRow]] | None
|
Optional reference-style candidates. |
None
|
return_text_lines
|
bool
|
Return per-line boxes for top candidate. |
False
|
score_normalization
|
Literal['mos', 'raw']
|
|
'mos'
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Shaped[Tensor, '...'] | dict[int, str] | dict[str, Shaped[Tensor, '...'] | str | list[int] | list[SmartTextCandidate] | None] | None]
|
Shared layout output. |
Source code in models/smarttext/src/smarttext/pipeline_smarttext.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
normalize_condition_type ¶
normalize_condition_type(
condition_type: ConditionType | str | None,
) -> ConditionType
Normalize SmartText condition aliases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition_type
|
ConditionType | str | None
|
Canonical condition, alias, or |
required |
Returns:
| Type | Description |
|---|---|
ConditionType
|
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the requested mode is unsupported by SmartText. |
Source code in models/smarttext/src/smarttext/pipeline_smarttext.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
processing_smarttext ¶
Processor for SmartText content-image inputs and layout decoding.
SmartTextProcessor ¶
Bases: ProcessorMixin
Normalize SmartText content payloads and decode candidate scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_processor
|
SmartTextImageProcessor | None
|
Image processor for RGB and BASNet tensors. |
None
|
config
|
SmartTextConfig
|
SmartText configuration. |
required |
Examples:
>>> processor = SmartTextProcessor(config=SmartTextConfig())
>>> processor.id2label
{0: 'text'}
Source code in models/smarttext/src/smarttext/processing_smarttext.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 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 | |
__init__ ¶
__init__(
*,
image_processor: SmartTextImageProcessor | None = None,
config: SmartTextConfig,
id2label: Mapping[int | str, str] | None = None,
) -> None
Initialize processor.
Source code in models/smarttext/src/smarttext/processing_smarttext.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
save_pretrained ¶
save_pretrained(
save_directory: str | Path,
push_to_hub: bool = False,
**kwargs: str | int | float | bool | None,
) -> None
Save processor metadata and image-processor config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_directory
|
str | Path
|
Directory receiving |
required |
push_to_hub
|
bool
|
Accepted for |
False
|
kwargs
|
str | int | float | bool | None
|
Accepted for |
{}
|
Source code in models/smarttext/src/smarttext/processing_smarttext.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
cache_dir: str | PathLike[str] | None = None,
force_download: bool = False,
local_files_only: bool = False,
token: str | bool | None = None,
revision: str = "main",
subfolder: str | None = None,
**kwargs: str | int | float | bool | None,
) -> Self
Load processor metadata from a local checkpoint directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pretrained_model_name_or_path
|
str | PathLike[str]
|
Root path or processor subfolder. |
required |
cache_dir
|
str | PathLike[str] | None
|
Accepted for |
None
|
force_download
|
bool
|
Accepted for |
False
|
local_files_only
|
bool
|
Accepted for API compatibility. |
False
|
token
|
str | bool | None
|
Accepted for |
None
|
revision
|
str
|
Accepted for |
'main'
|
subfolder
|
str | None
|
Optional processor subfolder. |
None
|
kwargs
|
str | int | float | bool | None
|
Ignored compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
Loaded SmartText processor. |
Source code in models/smarttext/src/smarttext/processing_smarttext.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 131 132 133 134 | |
__call__ ¶
__call__(
images: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch channels height width"]
| None = None,
*,
content: Mapping[
str,
ImageInput
| Sequence[ImageInput]
| str
| Sequence[str]
| Float[Tensor, "batch height width"]
| Sequence[CandidateBoxRow]
| Sequence[Sequence[CandidateBoxRow]],
]
| None = None,
prompt: str | Sequence[str] | None = None,
text: str | Sequence[str] | None = None,
saliency: ImageInput
| Sequence[ImageInput]
| Float[Tensor, "batch height width"]
| None = None,
candidate_boxes: Sequence[CandidateBoxRow]
| Sequence[Sequence[CandidateBoxRow]]
| None = None,
font: str
| Path
| FreeTypeFont
| ImageFont
| None = None,
return_tensors: Literal["pt"] = "pt",
**kwargs: str | int | float | bool | None,
) -> BatchEncoding
Encode SmartText public inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput] | Float[Tensor, 'batch channels height width'] | None
|
Image or image batch. |
None
|
content
|
Mapping[str, ImageInput | Sequence[ImageInput] | str | Sequence[str] | Float[Tensor, 'batch height width'] | Sequence[CandidateBoxRow] | Sequence[Sequence[CandidateBoxRow]]] | None
|
Optional content carrier with |
None
|
prompt
|
str | Sequence[str] | None
|
Prompt text payload. |
None
|
text
|
str | Sequence[str] | None
|
Alias for prompt text. |
None
|
saliency
|
ImageInput | Sequence[ImageInput] | Float[Tensor, 'batch height width'] | None
|
Optional saliency map. |
None
|
candidate_boxes
|
Sequence[CandidateBoxRow] | Sequence[Sequence[CandidateBoxRow]] | None
|
Optional reference-style candidate rows. |
None
|
font
|
str | Path | FreeTypeFont | ImageFont | None
|
Font path or PIL font object. |
None
|
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
kwargs
|
str | int | float | bool | None
|
Ignored forward-compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
BatchEncoding
|
Batch encoding containing normalized payloads. |
Source code in models/smarttext/src/smarttext/processing_smarttext.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 | |
decode ¶
decode(
*,
candidates: Sequence[SmartTextCandidate],
scores: Float[Tensor, "candidates"],
image_size: tuple[int, int],
output_type: Literal["dataclass", "dict"] = "dataclass",
return_text_lines: bool = False,
top_k: int = 3,
score_normalization: Literal["mos", "raw"] = "mos",
text_color: str | None = None,
intermediates: dict[
str,
Shaped[Tensor, "..."]
| str
| list[SmartTextCandidate]
| list[int]
| None,
]
| None = None,
) -> (
LayoutGenerationOutput
| dict[
str,
Shaped[torch.Tensor, "..."]
| dict[int, str]
| dict[
str,
Shaped[torch.Tensor, "..."]
| str
| list[SmartTextCandidate]
| list[int]
| None,
]
| None,
]
)
Decode sorted candidates into the shared layout schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
Sequence[SmartTextCandidate]
|
Candidate metadata. |
required |
scores
|
Float[Tensor, 'candidates']
|
Raw scorer outputs. |
required |
image_size
|
tuple[int, int]
|
Source image size as |
required |
output_type
|
Literal['dataclass', 'dict']
|
Return dataclass or dict. |
'dataclass'
|
return_text_lines
|
bool
|
Return per-line boxes instead of top-level boxes. |
False
|
top_k
|
int
|
Number of top candidates to return. |
3
|
score_normalization
|
Literal['mos', 'raw']
|
|
'mos'
|
text_color
|
str | None
|
Optional selected text color. |
None
|
intermediates
|
dict[str, Shaped[Tensor, '...'] | str | list[SmartTextCandidate] | list[int] | None] | None
|
Optional extra intermediate payload. |
None
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | dict[str, Shaped[Tensor, '...'] | dict[int, str] | dict[str, Shaped[Tensor, '...'] | str | list[SmartTextCandidate] | list[int] | None] | None]
|
Shared |
Source code in models/smarttext/src/smarttext/processing_smarttext.py
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 | |