Data
BatchData(datas, device='cpu')
¶
Source code in src/deepvisiontools/data/data.py
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 | |
device = device
instance-attribute
¶
Batch a list of BaseData of the same concrete type and normalize their shapes through padding.
Note
- Ensures all elements are of the same
BaseDatasubclass (e.g., allBboxData). - Moves items to the requested
device. - Pads each item to a common
canvas_size = (max(H_i), max(W_i)). - Exposes
.datas,.data_type,.nb_object,.canvas_size,.device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datas
|
List[deepvisiontools.data.data.BaseData]
|
Items of the same type (may be empty). |
required |
device
|
typing.Literal['cpu', 'cuda']
|
Target device. Defaults to |
required |
Usage
b1 = BboxData(torch.tensor([[1,1,3,3]]), torch.tensor([0]), (8,8), "XYXY")
b2 = BboxData(torch.tensor([[2,2,4,4]]), torch.tensor([1]), (6,6), "XYXY")
batch = BatchData([b1, b2])
len(batch), batch.canvas_size
>>> (2, (8, 8))
sub = batch[1:]
len(sub), sub.canvas_size
>>> (1, (8, 8))
#Concatenation:
merged = sub + sub
len(merged)
>>> 2
empty(device='cpu')
classmethod
¶
Construct an empty batch on the given device.
Source code in src/deepvisiontools/data/data.py
1735 1736 1737 1738 | |
to(device)
¶
Move all elements to device (in-place) and return self.
Source code in src/deepvisiontools/data/data.py
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 | |
padto(new_size)
¶
Pad all elements to new_size=(H, W) and return a new BatchData.
Returns:
| Name | Type | Description |
|---|---|---|
BatchData |
deepvisiontools.data.data.BatchData
|
with all items padded to the same canvas. |
Source code in src/deepvisiontools/data/data.py
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 | |
labels()
¶
Return labels tensor for each item as list.
Source code in src/deepvisiontools/data/data.py
1878 1879 1880 1881 1882 | |
values()
¶
Return geometry tensors for each item (boxes/masks etc.) as list.
Source code in src/deepvisiontools/data/data.py
1884 1885 1886 1887 1888 | |
scores()
¶
Return scores tensors for each item as a list.
Source code in src/deepvisiontools/data/data.py
1890 1891 1892 1893 1894 | |
BaseData
¶
Bases: abc.ABC
Minimal, uniform abstract interface for model-ready annotations (boxes, instance masks,
semantic masks). Concrete implementations must subclass BaseData.
Key ideas
- A
BaseDatawraps the geometry/labels/scores for a single image. - It remembers its
canvas_size=(H, W)so it can be padded/cropped safely. - It exposes common utilities (
sanitize,pad,crop,augment,visualize) implemented via operator visitors. Concrete implementation of these utilities is implemented for concrete daughter classes. - BaseData must be implemented through concrete classes. See BboxData for examples.
Basic Usage
# BaseData should not be used directly, use concretes implementations like BboxData.
from deepvisiontools.data import BboxData
b = BboxData(
value=torch.tensor([[10, 10, 20, 20]]), # XYXY
labels=torch.tensor([1]),
canvas_size=(64, 64),
boxformat="XYXY",
)
ib = BatchData([b]) # batch of 1 box annotation
ib.data_type
>>> 'bbox'
ib.canvas_size
>>> (64, 64)
empty(canvas_size, device='cpu')
abstractmethod
classmethod
¶
Create an empty annotation object for a given canvas size (no objects).
Source code in src/deepvisiontools/data/data.py
59 60 61 62 63 64 65 | |
accept(op, **context)
¶
Visitor pattern entry-point. Operators are registered per-type and dispatched here.
Source code in src/deepvisiontools/data/data.py
105 106 107 108 109 110 111 112 113 | |
sanitize()
¶
Clean the object (remove tiny boxes/instances, fix labels/scores sizes, etc.).
Source code in src/deepvisiontools/data/data.py
116 117 118 119 120 121 122 | |
crop(t, l, h, w)
¶
Crop the annotation to a (h, w) window with top/left offsets (t, l).
This mirrors the exact crop applied to the image.
Source code in src/deepvisiontools/data/data.py
124 125 126 127 128 129 130 131 132 | |
pad(l, t, r, b)
¶
Pad annotation with (left, top, right, bottom) pixels.
Source code in src/deepvisiontools/data/data.py
134 135 136 137 138 139 140 | |
padto(new_size)
¶
Pad annotation to exactly new_size=(H, W) (no crop).
Source code in src/deepvisiontools/data/data.py
142 143 144 145 146 147 148 | |
augment(image, transform)
¶
Apply a torchvision-compatible transform jointly to (annotation, image).
Source code in src/deepvisiontools/data/data.py
150 151 152 153 154 155 156 157 158 | |
visualize(image=None)
¶
Render a preview of the annotation over an optional image.
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
torch.Tensor: Visualized RGB image tensor. |
Source code in src/deepvisiontools/data/data.py
160 161 162 163 164 165 166 167 168 169 | |
to(device)
¶
Move all internal tensors to device (in-place) and return self.
Source code in src/deepvisiontools/data/data.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
BboxData(value, labels, canvas_size, boxformat='XYXY', scores=None, device='cpu')
¶
Bases: deepvisiontools.data.data.BaseData
Source code in src/deepvisiontools/data/data.py
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 | |
device = device
instance-attribute
¶
Concrete BaseData for bounding boxes annotations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
torch.Tensor | torchvision.tv_tensors.BoundingBoxes
|
|
required |
labels
|
torch.Tensor
|
|
required |
canvas_size
|
Tuple[int, int]
|
Image size |
required |
boxformat
|
Optional[typing.Literal['XYXY', 'XYWH', 'CXCYWH'] | torchvision.tv_tensors.BoundingBoxFormat]
|
Format for input boxes; the |
required |
scores
|
torch.Tensor | None
|
|
required |
device
|
typing.Literal['cpu', 'cuda']
|
Device for tensors. Defaults to |
required |
Tips
- You can construct from
InstanceMaskDatawithBboxData.from_instance_mask(...). - You can convert formats later by modifying attribute (property)
.boxformat = "CXCYWH"etc.
Basic Usage
boxes = torch.tensor([[10, 10, 20, 20], [5, 5, 7, 9]])
labels = torch.tensor([1, 2])
b = BboxData(boxes, labels, canvas_size=(64, 64), boxformat="XYXY")
b.nb_object
>>> 2
b.boxformat
>>> BoundingBoxFormat.XYXY
b.boxformat = "CXCYWH" # modify box format
b.value.shape
>>> torch.Size([2, 4])
sample = boxes[0]
sample.nb_object
>>> 1
sample.labels
>>> torch.Tensor([1])
boxformat
property
writable
¶
Convert boxes in-place to the requested format.
Usage
b = BboxData(
value=torch.tensor([[10,10,20,20]]), labels=torch.tensor([1]),
canvas_size=(32,32), boxformat="XYXY"
)
b.boxformat = "CXCYWH" # internally change box coordinates
b.boxformat
>>> BoundingBoxFormat.CXCYWH
from_instance_mask(instance_mask, boxformat=None)
classmethod
¶
Build boxes by boxing each instance in an InstanceMaskData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance_mask
|
deepvisiontools.data.data.InstanceMaskData
|
Source instances. |
required |
boxformat
|
typing.Literal['XYXY', 'XYWH', 'CXCYWH'] | None
|
Optional output format. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
BboxData |
deepvisiontools.data.data.BboxData
|
Boxes + labels (+ scores if present on the instance mask). |
Source code in src/deepvisiontools/data/data.py
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 | |
empty(canvas_size, boxformat='XYXY', device='cpu')
classmethod
¶
Create an empty BboxData for the given canvas.
Source code in src/deepvisiontools/data/data.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
InstanceMaskData(value=None, labels=None, canvas_size=None, scores=None, separate_masks=None, separate_mask_scores=None, device=None)
¶
Bases: deepvisiontools.data.data.BaseData
Source code in src/deepvisiontools/data/data.py
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 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 | |
separate_masks
property
writable
¶
Returns (N,H,W) bool on self.device.
- If packed separate masks exist: decode from CPU packbits.
- Else: compute from stacked id-map on demand (fast enough for metrics).
empty(canvas_size, device='cpu')
classmethod
¶
Return empty InstanceMask data object (0 objects)
Source code in src/deepvisiontools/data/data.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
SemanticLogits(logits, num_classes=_default_num_classes(), binary_threshold=_default_binary_threshold(), mode=None)
dataclass
¶
Container for semantic logits with a stable API and shape checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
torchvision.tv_tensors.Mask | torch.Tensor
|
Raw (pre-softmax) logits of shape |
required |
num_classes
|
int
|
Number of classes |
deepvisiontools.data.data._default_num_classes()
|
Note
- Validates the
(C, H, W)shape and rejects already-softmaxed probabilities. - Keeps logits as a
Mask(Tensor subclass) for dtype/device convenience. - Provides
to(...),cpu(),cuda(),clone(),detach()that preservenum_classes.
Tips
.softmax()→(C,H,W)probabilities (asMask)..toSemanticMaskData()→SemanticMaskDatabyargmax.
Basic Usage
C, H, W = 3, 8, 8
logits = torch.randn(C, H, W)
sl = SemanticLogits(logits, num_classes=C) # if C=None value infered from current() config.
sm = sl.toSemanticMaskData()
sm.value.shape
>>> torch.Size([8, 8])
toSemanticMaskData()
¶
Convert logits to SemanticMaskData by:
1) softmax over classes,
2) argmax over channels.
Returns:
| Name | Type | Description |
|---|---|---|
SemanticMaskData |
deepvisiontools.data.data.SemanticMaskData
|
Semantic map with |
Source code in src/deepvisiontools/data/data.py
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 | |
to(*args, **kwargs)
¶
Return a copy with tensors moved/cast like Tensor.to.
Source code in src/deepvisiontools/data/data.py
1656 1657 1658 | |
cpu()
¶
Return a CPU copy.
Source code in src/deepvisiontools/data/data.py
1660 1661 1662 | |
cuda(device=None, non_blocking=False)
¶
Return a CUDA copy.
Source code in src/deepvisiontools/data/data.py
1664 1665 1666 1667 1668 | |
clone()
¶
Return a clone that preserves num_classes.
Source code in src/deepvisiontools/data/data.py
1670 1671 1672 | |
detach()
¶
Return a detached copy that preserves num_classes.
Source code in src/deepvisiontools/data/data.py
1674 1675 1676 | |
probas()
¶
Convert logits to normalized probabilities across classes.
Returns:
| Name | Type | Description |
|---|---|---|
Mask |
torchvision.tv_tensors.Mask
|
|
Source code in src/deepvisiontools/data/data.py
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 | |
SemanticMaskData(value, labels=None, canvas_size=None, scores=None, device='cpu')
¶
Bases: deepvisiontools.data.data.BaseData
Source code in src/deepvisiontools/data/data.py
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 | |
device = device
instance-attribute
¶
Concrete BaseData for semantic segmentation (single class per pixel).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
torchvision.tv_tensors.Mask | torch.Tensor
|
Semantic class map |
required |
labels
|
torch.Tensor | None
|
Classes present; if |
required |
canvas_size
|
Tuple[int, int]
|
size of mask/corresponding image. If |
required |
scores
|
deepvisiontools.data.data.SemanticLogits | torch.Tensor | None
|
Semantic segmentation scores as logits. Can be tensor |
required |
device
|
typing.Literal['cpu', 'cuda']
|
Defaults to |
required |
Tips
- Can be built from
IntanceMaskData:SemanticMaskData.from_instance_mask(instance_mask). - Can be built from
SemanticLogits:SemanticLogits.toSemanticMaskData()
Usage
sem = SemanticMaskData(torch.zeros(4,4, dtype=torch.long), device="cpu")
sem.nb_object
>>> 0
# build from instances
ids = torch.tensor([[0,1,1],[0,0,2]])
lbl = torch.tensor([4, 7]) # two instances with semantic labels 4 and 7
im = InstanceMaskData(ids, lbl, canvas_size=(2,3))
sm = SemanticMaskData.from_instance_mask(im)
set(torch.unique(sm.value).tolist()) == {0, 4, 7}
>>> True
from_instance_mask(instance_mask)
classmethod
¶
Convert an instance mask to semantic by assigning each instance its class label and keeping 0 as background.
Source code in src/deepvisiontools/data/data.py
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 | |
empty(canvas_size, device='cpu')
classmethod
¶
Create an empty semantic mask (all background).
Source code in src/deepvisiontools/data/data.py
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 | |
confidence()
¶
Convert logits to per-class probabilities (C, H, W).
Returns:
| Type | Description |
|---|---|
torchvision.tv_tensors.Mask | None
|
Mask | None: Probabilities as a tensor-like |
Source code in src/deepvisiontools/data/data.py
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 | |
Augment(augment)
¶
Bases: deepvisiontools.data.operators.BaseOperator
Apply a torchvision v2 transform to a (annotation, image) pair.
The paired image tensor must be supplied via the context dict:
Augment(transform)(data, image=image_tensor)
Note
- The concrete logic to align boxes/masks is registered per data type in
ops_registry. - If you only need to modify the annotation (no image), use
Crop/Pad/Padto.
Source code in src/deepvisiontools/data/operators.py
160 161 162 | |
Crop(top, left, height, width)
¶
Bases: deepvisiontools.data.operators.BaseOperator
Crop annotations to a fixed image window.
Source code in src/deepvisiontools/data/operators.py
97 98 99 | |
Pad(left, top, right, bottom)
¶
Bases: deepvisiontools.data.operators.BaseOperator
Spatial crop by (top, left, height, width).
Pad value tensor and adapt canvas_size, num object (via sanitize) etc.
Source code in src/deepvisiontools/data/operators.py
116 117 118 | |
Padto(new_size)
¶
Bases: deepvisiontools.data.operators.BaseOperator
Pad annotations to exactly a target image size without cropping.
Parameters
new_size: Target size as(height, width).
Source code in src/deepvisiontools/data/operators.py
136 137 138 | |
Sanitize(*args, **kwargs)
¶
Bases: deepvisiontools.data.operators.BaseOperator
Clean an annotation: enforce minimal sizes, fix ids, align labels & scores. Congigs (min_size etc.) are accessed through current() global variable
Note
- Remove degenerate boxes or tiny instances per the current config
(e.g.,
current().data_box.min_size, etc.). - Enforce contiguity of instance ids for
InstanceMaskData(1..K). - Align lengths of
labels/scoreswith the number of objects. - etc.
Returns:
| Type | Description |
|---|---|
|
Cleaned |
Source code in src/deepvisiontools/data/operators.py
48 49 | |
Visualize(*args, **kwargs)
¶
Bases: deepvisiontools.data.operators.BaseOperator
Render a visualization tensor (uint8 CHW) of an annotation on an image. If Image is None use a zeroed tensor.
Note
- Draws boxes, masks, class labels and (optionally) scores onto a blank
canvas or over a provided
image(if supplied viactx['image']). - Uses styling options from
current().viz(colors, thickness, alpha…). - Concrete implementation logic is concrete BaseData type dependant
Source code in src/deepvisiontools/data/operators.py
48 49 | |
current()
¶
Get the current global DVConfig (context-local) object. You can configure a lot of options from the usage of current together with set_current
Returns:
| Name | Type | Description |
|---|---|---|
DVConfig |
deepvisiontools.config.config.DVConfig
|
The active configuration object. |
Tips
- Using
ContextVarmakes the config safe for async/task contexts - override it in a context manager
with use_config(...)block without affecting other concurrent tasks.
Usage
from deepvisiontools.config import current, set_current, use_config
# Access global configs values with current()
cfg = current()
isinstance(cfg, DVConfig)
>>> True
cfg.log.level
>>> 30 # warning level from logging library
cfg.device
>>> "cpu"
cfg.data_box.max_det = 500
# Set a config as a global config
cfg1 = DVConfig(device="cuda")
cfg2 = DVConfig(device="cpu")
set_current(cfg1)
current().device
>>> "cuda"
set_current(cfg2)
>>> "cpu"
# Use a context manager for temporary config
set_current(cfg1)
with cfg2:
print(current().device)
>>> "cpu"
Source code in src/deepvisiontools/config/config.py
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 | |