Skip to content

Commit

Permalink
Rename Item to DatasetItem (openvinotoolkit#2289)
Browse files Browse the repository at this point in the history
  • Loading branch information
samet-akcay authored and djdameln committed Sep 12, 2024
1 parent 8490a01 commit 4021d13
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/anomalib/data/base/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from anomalib import TaskType
from anomalib.data.utils import LabelName, read_image, read_mask
from anomalib.dataclasses import ImageBatch, ImageItem, Item
from anomalib.dataclasses import DatasetItem, ImageBatch, ImageItem

_EXPECTED_COLUMNS_CLASSIFICATION = ["image_path", "split"]
_EXPECTED_COLUMNS_SEGMENTATION = [*_EXPECTED_COLUMNS_CLASSIFICATION, "mask_path"]
Expand Down Expand Up @@ -153,7 +153,7 @@ def has_anomalous(self) -> bool:
"""Check if the dataset contains any anomalous samples."""
return LabelName.ABNORMAL in list(self.samples.label_index)

def __getitem__(self, index: int) -> Item:
def __getitem__(self, index: int) -> DatasetItem:
"""Get dataset item for the index ``index``.
Args:
Expand Down
4 changes: 2 additions & 2 deletions src/anomalib/dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
)
from .torch import (
Batch,
DatasetItem,
DepthBatch,
DepthItem,
ImageBatch,
ImageItem,
InferenceBatch,
Item,
VideoBatch,
VideoItem,
)

__all__ = [
"Item",
"DatasetItem",
"Batch",
"InferenceBatch",
"ImageItem",
Expand Down
4 changes: 2 additions & 2 deletions src/anomalib/dataclasses/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def _validate_anomaly_map(self, anomaly_map: np.ndarray | None) -> np.ndarray |
if anomaly_map is None:
return None
assert isinstance(anomaly_map, np.ndarray), f"Anomaly map must be a numpy array, got {type(anomaly_map)}."
assert anomaly_map.ndim in [
assert anomaly_map.ndim in {
2,
3,
], f"Anomaly map must have shape [H, W] or [1, H, W], got shape {anomaly_map.shape}."
}, f"Anomaly map must have shape [H, W] or [1, H, W], got shape {anomaly_map.shape}."
if anomaly_map.ndim == 3:
assert (
anomaly_map.shape[0] == 1
Expand Down
30 changes: 15 additions & 15 deletions src/anomalib/dataclasses/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def to_numpy(self) -> NumpyT:


@dataclass
class Item(Generic[ImageT], _GenericItem[torch.Tensor, ImageT, Mask, str]):
class DatasetItem(Generic[ImageT], _GenericItem[torch.Tensor, ImageT, Mask, str]):
"""Dataclass for torch item."""


Expand All @@ -76,7 +76,7 @@ class Batch(Generic[ImageT], _GenericBatch[torch.Tensor, ImageT, Mask, list[str]
class ImageItem(
ToNumpyMixin[NumpyImageItem],
_ImageInputFields[str],
Item[Image],
DatasetItem[Image],
):
"""Dataclass for torch image output item."""

Expand Down Expand Up @@ -105,10 +105,10 @@ def _validate_gt_mask(self, gt_mask: torch.Tensor | None) -> Mask | None:
if gt_mask is None:
return None
assert isinstance(gt_mask, torch.Tensor), f"Ground truth mask must be a torch.Tensor, got {type(gt_mask)}."
assert gt_mask.ndim in [
assert gt_mask.ndim in {
2,
3,
], f"Ground truth mask must have shape [H, W] or [1, H, W] got shape {gt_mask.shape}."
}, f"Ground truth mask must have shape [H, W] or [1, H, W] got shape {gt_mask.shape}."
if gt_mask.ndim == 3:
assert gt_mask.shape[0] == 1, f"Ground truth mask must have 1 channel, got {gt_mask.shape[0]}."
gt_mask = gt_mask.squeeze(0)
Expand All @@ -123,10 +123,10 @@ def _validate_anomaly_map(self, anomaly_map: torch.Tensor | None) -> Mask | None
if anomaly_map is None:
return None
assert isinstance(anomaly_map, torch.Tensor), f"Anomaly map must be a torch.Tensor, got {type(anomaly_map)}."
assert anomaly_map.ndim in [
assert anomaly_map.ndim in {
2,
3,
], f"Anomaly map must have shape [H, W] or [1, H, W], got shape {anomaly_map.shape}."
}, f"Anomaly map must have shape [H, W] or [1, H, W], got shape {anomaly_map.shape}."
if anomaly_map.ndim == 3:
assert (
anomaly_map.shape[0] == 1
Expand All @@ -151,10 +151,10 @@ def _validate_pred_mask(self, pred_mask: torch.Tensor | None) -> Mask | None:
if pred_mask is None:
return None
assert isinstance(pred_mask, torch.Tensor), f"Predicted mask must be a torch.Tensor, got {type(pred_mask)}."
assert pred_mask.ndim in [
assert pred_mask.ndim in {
2,
3,
], f"Predicted mask must have shape [H, W] or [1, H, W] got shape {pred_mask.shape}."
}, f"Predicted mask must have shape [H, W] or [1, H, W] got shape {pred_mask.shape}."
if pred_mask.ndim == 3:
assert pred_mask.shape[0] == 1, f"Predicted mask must have 1 channel, got {pred_mask.shape[0]}."
pred_mask = pred_mask.squeeze(0)
Expand Down Expand Up @@ -193,7 +193,7 @@ class ImageBatch(

def _validate_image(self, image: Image) -> Image:
assert isinstance(image, torch.Tensor), f"Image must be a torch.Tensor, got {type(image)}."
assert image.ndim in [3, 4], f"Image must have shape [C, H, W] or [N, C, H, W], got shape {image.shape}."
assert image.ndim in {3, 4}, f"Image must have shape [C, H, W] or [N, C, H, W], got shape {image.shape}."
if image.ndim == 3:
image = image.unsqueeze(0) # add batch dimension
assert image.shape[1] == 3, f"Image must have 3 channels, got {image.shape[0]}."
Expand All @@ -219,11 +219,11 @@ def _validate_gt_mask(self, gt_mask: Mask | None) -> Mask | None:
if gt_mask is None:
return None
assert isinstance(gt_mask, torch.Tensor), f"Ground truth mask must be a torch.Tensor, got {type(gt_mask)}."
assert gt_mask.ndim in [
assert gt_mask.ndim in {
2,
3,
4,
], f"Ground truth mask must have shape [H, W] or [N, H, W] or [N, 1, H, W] got shape {gt_mask.shape}."
}, f"Ground truth mask must have shape [H, W] or [N, H, W] or [N, 1, H, W] got shape {gt_mask.shape}."
if gt_mask.ndim == 2:
assert (
self.batch_size == 1
Expand Down Expand Up @@ -259,11 +259,11 @@ def _validate_anomaly_map(self, anomaly_map: torch.Tensor | np.ndarray | None) -
except Exception as e:
msg = "Failed to convert anomaly_map to a torch.Tensor."
raise ValueError(msg) from e
assert anomaly_map.ndim in [
assert anomaly_map.ndim in {
2,
3,
4,
], f"Anomaly map must have shape [H, W] or [N, H, W] or [N, 1, H, W], got shape {anomaly_map.shape}."
}, f"Anomaly map must have shape [H, W] or [N, H, W] or [N, 1, H, W], got shape {anomaly_map.shape}."
if anomaly_map.ndim == 2:
assert (
self.batch_size == 1
Expand Down Expand Up @@ -294,7 +294,7 @@ def _validate_image_path(self, image_path: list[str]) -> list[str] | None:
class VideoItem(
ToNumpyMixin[NumpyVideoItem],
_VideoInputFields[torch.Tensor, Video, Mask, str],
Item[Video],
DatasetItem[Video],
):
"""Dataclass for torch video output item."""

Expand Down Expand Up @@ -402,7 +402,7 @@ def _validate_last_frame(self, last_frame: torch.Tensor) -> torch.Tensor:
class DepthItem(
ToNumpyMixin[NumpyImageItem],
_DepthInputFields[torch.Tensor, str],
Item[Image],
DatasetItem[Image],
):
"""Dataclass for torch depth output item."""

Expand Down

0 comments on commit 4021d13

Please sign in to comment.