TensorStack#
- class TensorStack(tensor: torch.Tensor, colororder: str | None = None, logits: bool = False, dtype: DTypeLike | None = None, **kwargs: Any)[source][source]#
Lazy image source from a PyTorch batch tensor.
Each frame is a zero-copy view into the batch tensor, providing memory-efficient iteration over model outputs or other batch-processed tensors.
- Parameters:
tensor (torch.Tensor) – tensor of shape
(B, C, H, W)or(B, H, W)colororder (str, optional) – color plane order for multi-channel tensors, e.g.
"RGB"or"BGR", defaults to Nonelogits (bool, optional) – if True, take argmax over the channel dimension to convert per-class logits to a class label image, defaults to False
dtype (numpy dtype or None, optional) – data type for output image arrays, e.g.
np.uint8ornp.float32; if None, dtype is inferred from the tensor data, defaults to Nonekwargs – options applied to image frames, see
convert
Examples
>>> from machinevisiontoolbox import TensorStack >>> import torch >>> batch = torch.randn(10, 3, 64, 64) # 10 RGB images >>> source = TensorStack(batch, colororder="RGB") >>> print(source) TensorStack(10 frames, shape (3, 64, 64), dtype float32) >>> len(source) 10 >>> img = source[7] # Returns Image wrapping tensor[7] (zero-copy view) >>> print(img) Image: 64 x 64 (float32), R:G:B R: span=[-3.33973, 3.61569]; mean=-0.0275616, 𝜎=1.00145; median=-0.01917 G: span=[-3.5052, 4.08658]; mean=0.0193997, 𝜎=1.00146; median=0.0170656 B: span=[-3.73214, 3.53697]; mean=-0.0117258, 𝜎=1.00667; median=-0.0202868 >>> for img in source: ... features = mycode(img) # Your algorithm goes here, process each image lazily
See also
Initialize TensorStack from a batch tensor.
- Parameters:
tensor – batch tensor of shape
(B, C, H, W)or(B, H, W)colororder – color plane order for display/export
logits – if True, argmax the channel dimension for segmentation masks
dtype – output array dtype passed to Image constructor