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
_GenericItemfor 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.ndarrayLabel:
numpy.ndarrayMask:
numpy.ndarrayPath:
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
_GenericBatchfor batches of numpy-based data. It represents multiple data points for batch processing in anomaly detection tasks.- The class uses the following type parameters:
Where
Brepresents the batch dimension that is prepended to all tensor-like fields.- keys(include_none=True)#
Return a list of field names in the NumpyBatch.
- Parameters:
include_none (
bool) – If True, returns all possible field names including those that are None. If False, returns only field names that have non-None values. Defaults to True for backward compatibility.- Return type:
- Returns:
List of field names that can be accessed on this NumpyBatch instance. When include_none=True, includes all fields from the input, output, and any additional field classes that the specific batch type inherits from. When include_none=False, includes only fields with actual data.
Example
>>> batch = NumpyBatch(image=np.random.rand(2, 224, 224, 3)) >>> all_keys = batch.keys() # Default: include_none=True >>> 'pred_score' in all_keys # True (even though it's None) True >>> set_keys = batch.keys(include_none=False) >>> 'pred_score' in set_keys # False (because it's None) False
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],NumpyItemDataclass for a single image item in Anomalib datasets using numpy arrays.
This class combines
_ImageInputFieldsandNumpyItemfor 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.ndarraywith shape(H, W, C)Label:
numpy.ndarrayMask:
numpy.ndarraywith 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]],NumpyBatchDataclass for a batch of image items in Anomalib datasets using numpy arrays.
This class combines
BatchIterateMixin,_ImageInputFields, andNumpyBatchfor batches of image data. It supports batch operations and iteration over individualNumpyImageIteminstances.- The class uses the following type parameters:
Where
Brepresents 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],NumpyItemDataclass for a single video item in Anomalib datasets using numpy arrays.
This class combines
_VideoInputFieldsandNumpyItemfor 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.ndarraywith shape(T, H, W, C)Label:
numpy.ndarrayMask:
numpy.ndarraywith shape(T, H, W)Path:
str
Where
Trepresents 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]],NumpyBatchDataclass for a batch of video items in Anomalib datasets using numpy arrays.
This class combines
BatchIterateMixin,_VideoInputFields, andNumpyBatchfor batches of video data. It supports batch operations and iteration over individualNumpyVideoIteminstances.- The class uses the following type parameters:
Where
Brepresents the batch dimension andTthe temporal dimension.- item_class#
alias of
NumpyVideoItem
Depth Classes#
NumpyDepthItem#
NumpyDepthBatch#
See Also#
../torch