SMP wrapper
SemanticPredParams(binary_logits_threshold)
dataclass
¶
Bases: deepvisiontools.models.base.basemodel.BasePredParams
Prediction-time knobs for semantic segmentation decoding.
Attributes:
| Name | Type | Description |
|---|---|---|
binary_logits_threshold |
float
|
Threshold applied to logits for binary segmentation (num_classes == 1). Not used for multiclass. |
SMPBackend(arch, **kwargs)
¶
Bases: torch.nn.Module
Thin wrapper around an SMP architecture. Accepts either an SMP class name (str)
or an already-constructed torch.nn.Module. Exposes a single forward(x) that
returns raw logits [B, C, H, W].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arch
|
str | torch.nn.Module
|
Either the SMP architecture name (e.g., |
required |
**kwargs
|
typing.Any
|
Keyword args forwarded to the SMP constructor when |
{}
|
Source code in src/deepvisiontools/models/smp/smp.py
51 52 53 54 55 56 57 | |
forward(x, extra_args=None)
¶
Forward once through the SMP model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
torch.Tensor
|
Input image batch of shape |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
torch.Tensor: Raw logits of shape |
Source code in src/deepvisiontools/models/smp/smp.py
59 60 61 62 63 64 65 66 67 68 69 | |
SMPLoss(smp_loss)
¶
Bases: deepvisiontools.models.base.basemodel.BaseLoss
Adapter for losses from segmentation_models_pytorch.losses.
It accepts an already-constructed SMP loss object (e.g., FocalLoss, DiceLoss)
and validates that it operates on logits (from_logits=True) since the backend
returns raw scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smp_loss
|
torch.nn.Module
|
Instantiated SMP loss module. Must be compatible
with the target tensor shape/dtype produced by |
required |
Usage
import segmentation_models_pytorch as smp
loss = SMPLoss(smp.losses.FocalLoss(mode="binary")) # logits-compatible
logits = torch.randn(2, 1, 64, 64)
target = torch.randint(0, 2, (2, 1, 64, 64)).float()
out = loss(logits, target)
isinstance(out, dict) and "loss" in out
>>> True
Source code in src/deepvisiontools/models/smp/smp.py
230 231 232 233 234 235 236 237 238 239 240 | |
forward(raw_logits, targets)
¶
Compute the wrapped SMP loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_logits
|
torch.Tensor
|
Logits |
required |
targets
|
Union[torch.Tensor, deepvisiontools.data.BatchData]
|
Semantic masks as tensors or
|
required |
Returns:
| Type | Description |
|---|---|
|
Dict[str, torch.Tensor]: |
Source code in src/deepvisiontools/models/smp/smp.py
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 | |
SMPPost(num_classes=None)
¶
Bases: torch.nn.Module
SMP postprocessor that converts raw logits [B, C, H, W] into BatchData by
wrapping each item in SemanticLogits, which can further materialize
SemanticMaskData as needed.
Note
- Uses
current().data_semantic_mask.num_classesto parameterize decoding. undo_preprocis a no-op here (SMP path assumes spatial parity), but kept for API consistency with other wrappers.
Source code in src/deepvisiontools/models/smp/smp.py
84 85 86 87 88 89 90 91 92 | |
preds_from_raw(raw_outputs, aux=None)
¶
Decode SMP raw logits to BatchData (single-pass path).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_outputs
|
torch.Tensor
|
Logits |
required |
aux
|
typing.Any
|
Additional context (unused here; accepted for API compatibility). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
BatchData |
deepvisiontools.data.BatchData
|
Collection of per-image semantic predictions. |
Source code in src/deepvisiontools/models/smp/smp.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
undo_preproc(preds, *, original_hw=None)
¶
Reverse preprocessing effects (no-op for SMP baseline).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
deepvisiontools.data.BatchData
|
Predictions produced by |
required |
original_hw
|
Tuple[int, int]
|
Original |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
BatchData |
deepvisiontools.data.BatchData
|
Unmodified |
Source code in src/deepvisiontools/models/smp/smp.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
SMPPre(num_classes=None)
¶
Bases: torch.nn.Module
Prepares images and targets for SMP workflows.
images_onlyis identity (expects[B, C, H, W]).images_targetsadaptsBatchDataof semantic masks into tensors expected by SMP:- binary (num_classes == 1): targets → float tensor
[B, 1, H, W]; - multiclass: targets → long tensor
[B, H, W].
- binary (num_classes == 1): targets → float tensor
The number of classes is read from current().data_semantic_mask.num_classes.
Source code in src/deepvisiontools/models/smp/smp.py
148 149 150 151 152 153 154 155 156 | |
images_only(images)
¶
Identity preprocessing for images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
torch.Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
torch.Tensor: Same tensor passed through unchanged. |
Source code in src/deepvisiontools/models/smp/smp.py
158 159 160 161 162 163 164 165 166 167 168 | |
images_targets(images, targets)
¶
Prepare (images, targets) for training/eval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
torch.Tensor
|
|
required |
targets
|
deepvisiontools.data.BatchData
|
Semantic mask batch data. |
required |
Returns:
| Type | Description |
|---|---|
tuple[torch.Tensor, torch.Tensor]
|
Tuple[torch.Tensor, torch.Tensor]: |
Source code in src/deepvisiontools/models/smp/smp.py
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 | |
build_smp(*, arch='Unet', encoder_name=None, encoder_weights=None, in_channels=3, num_classes=None, aux_params=None, loss=None, supports_amp=True, autocast_dtype=torch.float16, device=None, use_amp=None, **extra_arch_kwargs)
¶
Build a DeepVision SMP wrapper (registered as "smp" in MODEL_REGISTRY).
This constructs an SMP model (e.g., Unet, DeepLabV3, FPN, PSPNet, …),
wires the standard DeepVision pre/post/loss pipeline, and returns a DeepVisionModel
ready for train_step, eval_step, and predict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arch
|
str | torch.nn.Module
|
SMP architecture to wrap. Provide either
the class name (e.g., |
'Unet'
|
encoder_name
|
Optional[str]
|
Encoder backbone name (e.g., |
None
|
encoder_weights
|
Optional[str]
|
Pretrained weights name (e.g., |
None
|
in_channels
|
int
|
Number of image channels. Defaults to |
3
|
num_classes
|
int | None
|
Number of output classes. When |
None
|
aux_params
|
Optional[Dict[str, typing.Any]]
|
Additional SMP classifier head params
(e.g., for auxiliary classification). Passed through to the SMP constructor.
Defaults to |
None
|
loss
|
Optional[torch.nn.Module]
|
SMP-compatible loss instance. If |
None
|
supports_amp
|
bool
|
Whether the backend supports autocast mixed precision.
Defaults to |
True
|
autocast_dtype
|
torch.dtype
|
Autocast dtype used by |
torch.float16
|
**extra_arch_kwargs
|
typing.Any
|
Any additional keyword arguments forwarded to the SMP constructor
(only when |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
DeepVisionModel |
deepvisiontools.models.base.basemodel.DeepVisionModel
|
Fully wired model (backend/pre/post/loss) with |
deepvisiontools.models.base.basemodel.DeepVisionModel
|
populated and |
Example
# Minimal binary Unet
from deepvisiontools.models import ModelFactory
m = ModelFactory(
name="smp",
arch="Unet",
encoder_name="resnet18",
in_channels=3,
num_classes=1, # binary
)
# Multiclass with a custom loss
dice = smp.losses.DiceLoss(mode="multiclass", from_logits=True)
m2 = ModelFactory(
name="smp",
arch="FPN",
encoder_name="resnet34",
in_channels=3,
num_classes=4,
loss=dice,
)
# Already constructed SMP module
net = smp.Unet(encoder_name="resnet18", in_channels=1, classes=1)
m3 = ModelFactory(name="smp", arch=net)
# Training/eval/predict
x = torch.randn(2, 3, 128, 128)
y = torch.randint(0, 2, (2, 1, 128, 128)).float()
_ = m.train_step(x, y) # doctest: +ELLIPSIS
_, preds = m.eval_step(x, y) # doctest: +ELLIPSIS
preds.nb_object == x.shape[0]
>>> True
Source code in src/deepvisiontools/models/smp/smp.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |