Numpy Dataclasses#

The numpy dataclasses module provides numpy-based implementations of the generic dataclasses used in Anomalib. These classes are designed to work with numpy arrays for efficient data handling and processing in anomaly detection tasks.

Overview#

The module includes several categories of dataclasses:

  • Base Classes: Generic numpy-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#

NumpyItem#

class anomalib.data.dataclasses.numpy.NumpyItem(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: _GenericItem[ndarray, ndarray, ndarray, str]

Dataclass for a single item in Anomalib datasets using numpy arrays.

This class extends _GenericItem for numpy-based data representation. It includes both input data (e.g., images, labels) and output data (e.g., predictions, anomaly maps) as numpy arrays.

The class uses the following type parameters:
  • Image: numpy.ndarray

  • Label: numpy.ndarray

  • Mask: numpy.ndarray

  • Path: str

This implementation is suitable for numpy-based processing pipelines in Anomalib where GPU acceleration is not required.

NumpyBatch#

class anomalib.data.dataclasses.numpy.NumpyBatch(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: _GenericBatch[ndarray, ndarray, ndarray, list[str]]

Dataclass for a batch of items in Anomalib datasets using numpy arrays.

This class extends _GenericBatch for batches of numpy-based data. It represents multiple data points for batch processing in anomaly detection tasks.

The class uses the following type parameters:
  • Image: numpy.ndarray with shape (B, C, H, W)

  • Label: numpy.ndarray with shape (B,)

  • Mask: numpy.ndarray with shape (B, H, W)

  • Path: list of str

Where B represents the batch dimension that is prepended to all tensor-like fields.

Image Classes#

NumpyImageItem#

class anomalib.data.dataclasses.numpy.NumpyImageItem(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: NumpyImageValidator, _ImageInputFields[str], NumpyItem

Dataclass for a single image item in Anomalib datasets using numpy arrays.

This class combines _ImageInputFields and NumpyItem 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: numpy.ndarray with shape (H, W, C)

  • Label: numpy.ndarray

  • Mask: numpy.ndarray with shape (H, W)

  • Path: str

Example

>>> import numpy as np
>>> from anomalib.data.dataclasses.numpy import NumpyImageItem
>>> item = NumpyImageItem(
...     data=np.random.rand(224, 224, 3),
...     label=0,
...     image_path="path/to/image.jpg"
... )
>>> item.data.shape
(224, 224, 3)

NumpyImageBatch#

class anomalib.data.dataclasses.numpy.NumpyImageBatch(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: BatchIterateMixin[NumpyImageItem], NumpyImageBatchValidator, _ImageInputFields[list[str]], NumpyBatch

Dataclass for a batch of image items in Anomalib datasets using numpy arrays.

This class combines BatchIterateMixin, _ImageInputFields, and NumpyBatch for batches of image data. It supports batch operations and iteration over individual NumpyImageItem instances.

The class uses the following type parameters:
  • Image: numpy.ndarray with shape (B, H, W, C)

  • Label: numpy.ndarray with shape (B,)

  • Mask: numpy.ndarray with shape (B, H, W)

  • Path: list of str

Where B represents the batch dimension that is prepended to all tensor-like fields.

Example

>>> import numpy as np
>>> from anomalib.data.dataclasses.numpy import NumpyImageBatch
>>> batch = NumpyImageBatch(
...     data=np.random.rand(32, 224, 224, 3),
...     label=np.zeros(32),
...     image_path=[f"path/to/image_{i}.jpg" for i in range(32)]
... )
>>> batch.data.shape
(32, 224, 224, 3)
item_class#

alias of NumpyImageItem

Video Classes#

NumpyVideoItem#

class anomalib.data.dataclasses.numpy.NumpyVideoItem(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: NumpyVideoValidator, _VideoInputFields[ndarray, ndarray, ndarray, str], NumpyItem

Dataclass for a single video item in Anomalib datasets using numpy arrays.

This class combines _VideoInputFields and NumpyItem 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: numpy.ndarray with shape (T, H, W, C)

  • Label: numpy.ndarray

  • Mask: numpy.ndarray with shape (T, H, W)

  • Path: str

Where T represents the temporal dimension (number of frames).

NumpyVideoBatch#

class anomalib.data.dataclasses.numpy.NumpyVideoBatch(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: BatchIterateMixin[NumpyVideoItem], NumpyVideoBatchValidator, _VideoInputFields[ndarray, ndarray, ndarray, list[str]], NumpyBatch

Dataclass for a batch of video items in Anomalib datasets using numpy arrays.

This class combines BatchIterateMixin, _VideoInputFields, and NumpyBatch for batches of video data. It supports batch operations and iteration over individual NumpyVideoItem instances.

The class uses the following type parameters:
  • Video: numpy.ndarray with shape (B, T, H, W, C)

  • Label: numpy.ndarray with shape (B,)

  • Mask: numpy.ndarray with shape (B, T, H, W)

  • Path: list of str

Where B represents the batch dimension and T the temporal dimension.

item_class#

alias of NumpyVideoItem

Depth Classes#

NumpyDepthItem#

NumpyDepthBatch#

See Also#