YOLOSeg wrapper
YOLOSEGBackend(arch, pretrained, num_classes=None, hyp=None)
¶
Bases: torch.nn.Module
Ultralytics YOLO segmentation backend (v8/11-style SegmentationModel).
Builds the model from {arch}.yaml, optionally loads {arch}.pt weights,
and stores loss hyperparameters (YoloSegLossParams) in self.model.args.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arch
|
str
|
Ultralytics segmentation architecture, e.g. |
required |
pretrained
|
bool
|
If |
required |
num_classes
|
int | None
|
Number of classes; when |
None
|
hyp
|
deepvisiontools.models.yoloseg.yoloseg.YoloSegLossParams | None
|
Segmentation loss gains. If |
None
|
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
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 | |
forward(x, extra_args=None)
¶
Single forward for train/eval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
extra_args
|
dict | None
|
Reserved for backend-specific extensions. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Raw output structure consumed by the Ultralytics segmentation loss. |
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
369 370 371 372 373 374 375 376 377 378 379 380 | |
YOLOSEGLoss(criterion, *, loss_factor=1.0)
¶
Bases: deepvisiontools.models.base.basemodel.BaseLoss
Thin adapter for the Ultralytics YOLO-SEG loss.
Moves vendor criterion internals to the correct device.
Aligns nested targets to the same device as predictions.
Optionally scales reported losses by loss_factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
criterion
|
typing.Any
|
Ultralytics seg loss object (e.g., from |
required |
loss_factor
|
float
|
Overall divider to scale losses. Defaults to |
1.0
|
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
841 842 843 844 | |
to(*args, **kwargs)
¶
Mirror nn.Module.to and also move internal vendor tensors to the target device.
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
846 847 848 849 850 851 852 853 854 855 856 | |
forward(preds, targs)
¶
Compute the segmentation loss and return a standardized LossDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
typing.Any
|
Raw vendor predictions (tensor or nested structure). |
required |
targs
|
Dict[str, Tensor]
|
Ultralytics-style target dict. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, torch.Tensor]
|
Dict[str, Tensor]: Always contains |
dict[str, torch.Tensor]
|
|
dict[str, torch.Tensor]
|
and |
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 | |
YoloSegLossParams(box=7.5, cls=0.5, dfl=1.5, overlap_mask=False)
dataclass
¶
Loss gain multipliers for Ultralytics v8/11 segmentation criterion. Note that in ultralytics, the segmentation gain used is the box gain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
box
|
float
|
IoU/box regression loss gain. Defaults to 7.5. |
7.5
|
cls
|
float
|
Classification loss gain. Defaults to 0.5. |
0.5
|
dfl
|
float
|
Distribution Focal Loss gain. Defaults to 1.5. |
1.5
|
overlap_mask
|
bool
|
Whether to use overlapping mask loss (not supported by deepvisiontools yet). Defaults to False. |
False
|
YOLOSEGPost()
¶
Bases: torch.nn.Module
Decode YOLO-SEG raw outputs into BatchData[InstanceMaskData] (single-pass path).
Note
1) choose best class & confidence filter,
2) NMS in XYXY,
3) keep top-max_det,
4) project protos with coefficients to get mask logits; upsample to (H, W),
5) binarize at mask_logit_threshold,
6) pack into InstanceMaskData with labels/scores.
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
396 397 398 399 400 401 402 403 404 405 | |
preds_from_raw(raw, aux=None)
¶
Decode YOLO-seg outputs into BatchData[instance_mask].
Metric/eval path policy:
- use permissive pred params supplied through aux["pred"];
- keep boxes clipped to the padded model canvas;
- preserve separate instance masks in InstanceMaskData so metric
accumulation does not lose overlapping-mask information.
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 | |
undo_preproc(preds, *, original_hw=None)
¶
Undo input padding by center-cropping to the original size when provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
deepvisiontools.data.BatchData
|
Predictions after |
required |
original_hw
|
Tuple[int, int] | None
|
Original input |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
BatchData |
deepvisiontools.data.BatchData
|
Cropped predictions to |
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | |
YOLOSEGPre(pad_multiple)
¶
Bases: torch.nn.Module
Preprocessing for YOLO-seg: - Pads images to the model stride multiple (e.g., 32/64/16). - Pads targets consistently and converts them to Ultralytics seg-loss format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad_multiple
|
int
|
Target stride multiple for padding. |
required |
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
782 783 784 | |
images_only(images)
¶
Pad images so that height/width are divisible by pad_multiple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Tensor
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
torch.Tensor
|
Padded images tensor. |
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
786 787 788 789 790 791 792 793 794 795 796 797 | |
images_targets(images, targets)
¶
Pad images and targets, then produce Ultralytics-style segmentation targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Tensor
|
|
required |
targets
|
deepvisiontools.data.BatchData
|
Instance-mask targets aligned with |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Tuple[Tensor, Dict[str, Tensor]]: Padded images and the target dict |
dict[str, torch.Tensor]
|
|
tuple[torch.Tensor, dict[str, torch.Tensor]]
|
same device as |
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 | |
YoloSegPredParams(conf_threshold, nms_iou, max_det, mask_logit_threshold)
dataclass
¶
Bases: deepvisiontools.models.base.basemodel.BasePredParams
Inference-time parameters for YOLO instance segmentation decoding.
Attributes:
| Name | Type | Description |
|---|---|---|
conf_threshold |
float
|
Minimum class confidence required before NMS. |
nms_iou |
float
|
IoU threshold used by NMS (class-agnostic here). |
max_det |
int
|
Maximum number of instances to keep per image. |
mask_logit_threshold |
float
|
Threshold applied to mask logits to binarize instance masks (after projection from protos). |
Notes
These parameters are passed by DeepVisionModel to YOLOSEGPost.preds_from_raw
via aux={"pred": ...}, and are persisted automatically when saving the whole
model object.
build_yoloseg(*, arch='yolov8n-seg', pretrained=True, num_classes=None, loss=None, loss_factor=1.0, hyp=None, supports_amp=True, autocast_dtype=torch.float16, device=None, use_amp=None, **extras)
¶
Build a DeepVision YOLO instance segmentation wrapper, registered as "yoloseg".
This builder wraps an Ultralytics YOLO segmentation model inside the
DeepVisionTools DeepVisionModel API. It wires YOLO-seg preprocessing,
postprocessing, loss handling and prediction parameters so the model can be used
with train_step, eval_step, predict, checkpointing and export helpers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arch
|
Literal[...
|
YOLO segmentation architecture name. Supported
values include YOLOv8-seg and YOLO11-seg variants, with optional |
'yolov8n-seg'
|
pretrained
|
bool
|
If True, initialize the model from Ultralytics pretrained weights for the selected architecture. Defaults to True. |
True
|
num_classes
|
int | None
|
Number of instance segmentation classes.
When None, it is taken from |
None
|
loss
|
typing.Any | None
|
Custom Ultralytics-compatible segmentation criterion. If None, the default vendor criterion is created from the internal YOLO runner. Defaults to None. |
None
|
loss_factor
|
float
|
Divisor applied to the reported YOLO-seg losses. Useful to rescale vendor loss values. Defaults to 1.0. |
1.0
|
hyp
|
deepvisiontools.models.yoloseg.yoloseg.YoloSegLossParams | None
|
YOLO-seg loss gains for box, cls,
dfl and mask-related behavior. If None, default |
None
|
supports_amp
|
bool
|
Whether this backend supports autocast mixed precision. Defaults to True. |
True
|
autocast_dtype
|
torch.dtype
|
Autocast dtype used by
|
torch.float16
|
device
|
str | torch.device | None
|
Device used to place the model.
If None, device resolution is delegated to |
None
|
use_amp
|
bool | None
|
Force-enable or force-disable AMP. If None, the global/default DeepVisionTools AMP policy is used. Defaults to None. |
None
|
extras
|
typing.Any
|
Additional keyword arguments kept for registry/export compatibility. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
DeepVisionModel |
deepvisiontools.models.base.basemodel.DeepVisionModel
|
A fully wired instance segmentation model with |
deepvisiontools.models.base.basemodel.DeepVisionModel
|
|
|
deepvisiontools.models.base.basemodel.DeepVisionModel
|
current configuration. |
Export behavior
Saved artifacts are rebuilt with pretrained=False before loading the saved
state dict. This avoids downloading vendor weights again when restoring a
deepvisiontools checkpoint.
Usage
import torch
from deepvisiontools.models import ModelFactory
# Minimal YOLOv8 instance segmentation model
model = ModelFactory(
name="yoloseg",
arch="yolov8n-seg",
pretrained=True,
num_classes=3,
)
# YOLO11 instance segmentation model on CUDA
model = ModelFactory(
name="yoloseg",
arch="yolo11m-seg",
pretrained=True,
num_classes=3,
device="cuda",
use_amp=True,
)
# Larger stride variant
model = ModelFactory(
name="yoloseg",
arch="yolov8m-p6-seg",
pretrained=True,
num_classes=3,
)
# Custom loss gains
model = ModelFactory(
name="yoloseg",
arch="yolov8n-seg",
num_classes=3,
hyp=YoloSegLossParams(box=7.5, cls=0.5, dfl=1.5),
)
# Inference returns BatchData containing InstanceMaskData items.
images = torch.randn(2, 3, 512, 512)
preds = model.predict(images)
preds.data_type == "instance_mask"
>>> True
Source code in src/deepvisiontools/models/yoloseg/yoloseg.py
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 | |