Torch Dataclasses#
The torch dataclasses module provides PyTorch-based implementations of the generic dataclasses used in Anomalib. These classes are designed to work with PyTorch tensors for efficient data handling and processing in anomaly detection tasks.
Overview#
The module includes several categories of dataclasses:
Base Classes: Generic PyTorch-based data structures
Image Classes: Specialized for image data processing
Video Classes: Designed for video data handling
Depth Classes: Specific to depth-based anomaly detection
Base Classes#
DatasetItem#
- class anomalib.data.dataclasses.torch.DatasetItem(image, gt_label=None, gt_mask=None, mask_path=None, anomaly_map=None, pred_score=None, pred_mask=None, pred_label=None, explanation=None)#
Bases:
Generic
[ImageT
],_GenericItem
[Tensor
,ImageT
,Mask
,str
]Base dataclass for individual items in Anomalib datasets using PyTorch.
This class extends the generic
_GenericItem
class to provide a PyTorch-specific implementation for single data items in Anomalib datasets. It handles various types of data (e.g., images, labels, masks) represented as PyTorch tensors.The class uses generic types to allow flexibility in the image representation, which can vary depending on the specific use case (e.g., standard images, video clips).
Note
This class is typically subclassed to create more specific item types (e.g.,
ImageItem
,VideoItem
) with additional fields and methods.
Batch#
- class anomalib.data.dataclasses.torch.Batch(image, gt_label=None, gt_mask=None, mask_path=None, anomaly_map=None, pred_score=None, pred_mask=None, pred_label=None, explanation=None)#
Bases:
Generic
[ImageT
],_GenericBatch
[Tensor
,ImageT
,Mask
,list
[str
]]Base dataclass for batches of items in Anomalib datasets using PyTorch.
This class extends the generic
_GenericBatch
class to provide a PyTorch-specific implementation for batches of data in Anomalib datasets. It handles collections of data items (e.g., multiple images, labels, masks) represented as PyTorch tensors.The class uses generic types to allow flexibility in the image representation, which can vary depending on the specific use case (e.g., standard images, video clips).
Note
This class is typically subclassed to create more specific batch types (e.g.,
ImageBatch
,VideoBatch
) with additional fields and methods.
InferenceBatch#
- class anomalib.data.dataclasses.torch.InferenceBatch(pred_score: Tensor | None = None, pred_label: Tensor | None = None, anomaly_map: Tensor | None = None, pred_mask: Tensor | None = None)#
Bases:
NamedTuple
Batch for use in torch and inference models.
- Parameters:
pred_score (torch.Tensor | None) – Predicted anomaly scores. Defaults to
None
.pred_label (torch.Tensor | None) – Predicted anomaly labels. Defaults to
None
.anomaly_map (torch.Tensor | None) – Generated anomaly maps. Defaults to
None
.pred_mask (torch.Tensor | None) – Predicted anomaly masks. Defaults to
None
.
ToNumpyMixin#
- class anomalib.data.dataclasses.torch.ToNumpyMixin#
Bases:
Generic
[NumpyT
]Mixin for converting torch-based dataclasses to numpy.
This mixin provides functionality to convert PyTorch tensor data to numpy arrays. It requires the subclass to define a
numpy_class
attribute specifying the corresponding numpy-based class.Examples
>>> from anomalib.dataclasses.numpy import NumpyImageItem >>> @dataclass ... class TorchImageItem(ToNumpyMixin[NumpyImageItem]): ... numpy_class = NumpyImageItem ... image: torch.Tensor ... gt_label: torch.Tensor ... >>> torch_item = TorchImageItem( ... image=torch.rand(3, 224, 224), ... gt_label=torch.tensor(1) ... ) >>> numpy_item = torch_item.to_numpy() >>> isinstance(numpy_item, NumpyImageItem) True
- to_numpy()#
Convert the batch to a NumpyBatch object.
- Returns:
The converted numpy batch object.
- Return type:
NumpyT
Image Classes#
ImageItem#
- class anomalib.data.dataclasses.torch.ImageItem(image, gt_label=None, gt_mask=None, mask_path=None, anomaly_map=None, pred_score=None, pred_mask=None, pred_label=None, explanation=None, image_path=None)#
Bases:
ToNumpyMixin
[NumpyImageItem
],ImageValidator
,_ImageInputFields
[str
],DatasetItem
[Image
]Dataclass for individual image items in Anomalib datasets using PyTorch.
This class combines
_ImageInputFields
andDatasetItem
for image-based anomaly detection. It includes image-specific fields and validation methods to ensure proper formatting for Anomalib’s image-based models.- The class uses the following type parameters:
Image:
torch.Tensor
with shape(C, H, W)
Label:
torch.Tensor
Mask:
torch.Tensor
with shape(H, W)
Path:
str
Example
>>> import torch >>> from anomalib.data.dataclasses.torch import ImageItem >>> item = ImageItem( ... image=torch.rand(3, 224, 224), ... gt_label=torch.tensor(0), ... image_path="path/to/image.jpg" ... ) >>> item.image.shape torch.Size([3, 224, 224])
Convert to numpy format: >>> numpy_item = item.to_numpy() >>> type(numpy_item).__name__ ‘NumpyImageItem’
- numpy_class#
alias of
NumpyImageItem
ImageBatch#
- class anomalib.data.dataclasses.torch.ImageBatch(image, gt_label=None, gt_mask=None, mask_path=None, anomaly_map=None, pred_score=None, pred_mask=None, pred_label=None, explanation=None, image_path=None)#
Bases:
ToNumpyMixin
[NumpyImageBatch
],BatchIterateMixin
[ImageItem
],ImageBatchValidator
,_ImageInputFields
[list
[str
]],Batch
[Image
]Dataclass for batches of image items in Anomalib datasets using PyTorch.
This class combines
_ImageInputFields
andBatch
for batches of image data. It includes image-specific fields and methods for batch operations and iteration.- The class uses the following type parameters:
Image:
torch.Tensor
with shape(B, C, H, W)
Label:
torch.Tensor
with shape(B,)
Mask:
torch.Tensor
with shape(B, H, W)
Where
B
represents the batch dimension.Example
>>> import torch >>> from anomalib.data.dataclasses.torch import ImageBatch >>> batch = ImageBatch( ... image=torch.rand(32, 3, 224, 224), ... gt_label=torch.randint(0, 2, (32,)), ... image_path=[f"path/to/image_{i}.jpg" for i in range(32)] ... ) >>> batch.image.shape torch.Size([32, 3, 224, 224])
Iterate over batch: >>> for item in batch: … assert item.image.shape == torch.Size([3, 224, 224])
Convert to numpy format: >>> numpy_batch = batch.to_numpy() >>> type(numpy_batch).__name__ ‘NumpyImageBatch’
- numpy_class#
alias of
NumpyImageBatch
Video Classes#
VideoItem#
- class anomalib.data.dataclasses.torch.VideoItem(image, gt_label=None, gt_mask=None, mask_path=None, anomaly_map=None, pred_score=None, pred_mask=None, pred_label=None, explanation=None, original_image=None, video_path=None, target_frame=None, frames=None, last_frame=None)#
Bases:
ToNumpyMixin
[NumpyVideoItem
],VideoValidator
,_VideoInputFields
[Tensor
,Video
,Mask
,str
],DatasetItem
[Video
]Dataclass for individual video items in Anomalib datasets using PyTorch.
This class combines
_VideoInputFields
andDatasetItem
for video-based anomaly detection. It includes video-specific fields and validation methods to ensure proper formatting for Anomalib’s video-based models.- The class uses the following type parameters:
Video:
torch.Tensor
with shape(T, C, H, W)
Label:
torch.Tensor
Mask:
torch.Tensor
with shape(T, H, W)
Path:
str
Where
T
represents the temporal dimension (number of frames).Example
>>> import torch >>> from anomalib.data.dataclasses.torch import VideoItem >>> item = VideoItem( ... image=torch.rand(10, 3, 224, 224), # 10 frames ... gt_label=torch.tensor(0), ... video_path="path/to/video.mp4" ... ) >>> item.image.shape torch.Size([10, 3, 224, 224])
Convert to numpy format: >>> numpy_item = item.to_numpy() >>> type(numpy_item).__name__ ‘NumpyVideoItem’
- numpy_class#
alias of
NumpyVideoItem
VideoBatch#
- class anomalib.data.dataclasses.torch.VideoBatch(image, gt_label=None, gt_mask=None, mask_path=None, anomaly_map=None, pred_score=None, pred_mask=None, pred_label=None, explanation=None, original_image=None, video_path=None, target_frame=None, frames=None, last_frame=None)#
Bases:
ToNumpyMixin
[NumpyVideoBatch
],BatchIterateMixin
[VideoItem
],VideoBatchValidator
,_VideoInputFields
[Tensor
,Video
,Mask
,list
[str
]],Batch
[Video
]Dataclass for batches of video items in Anomalib datasets using PyTorch.
This class represents batches of video data for batch processing in anomaly detection tasks. It combines functionality from multiple mixins to handle batched video data efficiently.
- The class uses the following type parameters:
Video:
torch.Tensor
with shape(B, T, C, H, W)
Label:
torch.Tensor
with shape(B,)
Mask:
torch.Tensor
with shape(B, T, H, W)
Where
B
represents the batch dimension andT
the temporal dimension.Example
>>> import torch >>> from anomalib.data.dataclasses.torch import VideoBatch >>> batch = VideoBatch( ... image=torch.rand(32, 10, 3, 224, 224), # 32 videos, 10 frames ... gt_label=torch.randint(0, 2, (32,)), ... video_path=["video_{}.mp4".format(i) for i in range(32)] ... ) >>> batch.image.shape torch.Size([32, 10, 3, 224, 224])
Iterate over items in batch: >>> next(iter(batch)).image.shape torch.Size([10, 3, 224, 224])
Convert to numpy format: >>> numpy_batch = batch.to_numpy() >>> type(numpy_batch).__name__ ‘NumpyVideoBatch’
- numpy_class#
alias of
NumpyVideoBatch
Depth Classes#
DepthItem#
- class anomalib.data.dataclasses.torch.DepthItem(image, gt_label=None, gt_mask=None, mask_path=None, anomaly_map=None, pred_score=None, pred_mask=None, pred_label=None, explanation=None, image_path=None, depth_map=None, depth_path=None)#
Bases:
ToNumpyMixin
[NumpyImageItem
],DepthValidator
,_DepthInputFields
[Tensor
,str
],DatasetItem
[Image
]Dataclass for individual depth items in Anomalib datasets using PyTorch.
This class represents a single depth item in Anomalib datasets using PyTorch tensors. It combines the functionality of
ToNumpyMixin
,_DepthInputFields
, andDatasetItem
to handle depth data, including depth maps, labels, and metadata.- Parameters:
image (torch.Tensor) – Image tensor of shape
(C, H, W)
.gt_label (torch.Tensor) – Ground truth label tensor.
depth_map (torch.Tensor) – Depth map tensor of shape
(H, W)
.image_path (str) – Path to the source image file.
depth_path (str) – Path to the depth map file.
Examples
>>> import torch >>> item = DepthItem( ... image=torch.rand(3, 224, 224), ... gt_label=torch.tensor(1), ... depth_map=torch.rand(224, 224), ... image_path="path/to/image.jpg", ... depth_path="path/to/depth.png" ... ) >>> print(item.image.shape, item.depth_map.shape) torch.Size([3, 224, 224]) torch.Size([224, 224])
- numpy_class#
alias of
NumpyImageItem
DepthBatch#
- class anomalib.data.dataclasses.torch.DepthBatch(image, gt_label=None, gt_mask=None, mask_path=None, anomaly_map=None, pred_score=None, pred_mask=None, pred_label=None, explanation=None, image_path=None, depth_map=None, depth_path=None)#
Bases:
BatchIterateMixin
[DepthItem
],DepthBatchValidator
,_DepthInputFields
[Tensor
,list
[str
]],Batch
[Image
]Dataclass for batches of depth items in Anomalib datasets using PyTorch.
This class represents a batch of depth items in Anomalib datasets using PyTorch tensors. It combines the functionality of
BatchIterateMixin
,_DepthInputFields
, andBatch
to handle batches of depth data, including depth maps, labels, and metadata.- Parameters:
image (torch.Tensor) – Batch of images of shape
(B, C, H, W)
.gt_label (torch.Tensor) – Batch of ground truth labels of shape
(B,)
.depth_map (torch.Tensor) – Batch of depth maps of shape
(B, H, W)
.image_path (list[str]) – List of paths to the source image files.
depth_path (list[str]) – List of paths to the depth map files.
Examples
>>> import torch >>> batch = DepthBatch( ... image=torch.rand(32, 3, 224, 224), ... gt_label=torch.randint(0, 2, (32,)), ... depth_map=torch.rand(32, 224, 224), ... image_path=["path/to/image_{}.jpg".format(i) for i in range(32)], ... depth_path=["path/to/depth_{}.png".format(i) for i in range(32)] ... ) >>> print(batch.image.shape, batch.depth_map.shape) torch.Size([32, 3, 224, 224]) torch.Size([32, 224, 224]) >>> for item in batch: ... print(item.image.shape, item.depth_map.shape) torch.Size([3, 224, 224]) torch.Size([224, 224])
See Also#
../numpy