ShanghaiTech Datamodule#
ShanghaiTech Campus Data Module.
This module provides a PyTorch Lightning DataModule for the ShanghaiTech Campus dataset. The dataset must be downloaded manually from the official project page (the original automated mirror is no longer available). The video files are converted to a format readable by pyav during preparation.
Example
Create a ShanghaiTech datamodule:
>>> from anomalib.data import ShanghaiTech
>>> datamodule = ShanghaiTech(
... root="./datasets/shanghaitech",
... scene=1,
... clip_length_in_frames=2,
... frames_between_clips=1,
... )
>>> datamodule.setup()
>>> i, data = next(enumerate(datamodule.train_dataloader()))
>>> data.keys()
dict_keys(['image', 'video_path', 'frames', 'label'])
Notes
The directory structure after preparation will be:
root/
├── testing/
│ ├── frames/
│ ├── test_frame_mask/
│ └── test_pixel_mask/
└── training/
├── frames/
├── converted_videos/
└── videos/
- License:
ShanghaiTech Campus Dataset is released under the BSD 2-Clause License.
- Reference:
Liu, W., Luo, W., Lian, D., & Gao, S. (2018). Future frame prediction for anomaly detection–a new baseline. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 6536-6545).
- class anomalib.data.datamodules.video.shanghaitech.ShanghaiTech(root='./datasets/shanghaitech', scene=1, clip_length_in_frames=2, frames_between_clips=1, target_frame=VideoTargetFrame.LAST, train_batch_size=32, eval_batch_size=32, num_workers=8, train_augmentations=None, val_augmentations=None, test_augmentations=None, augmentations=None, val_split_mode=ValSplitMode.SAME_AS_TEST, val_split_ratio=0.5, seed=None)#
Bases:
AnomalibVideoDataModuleShanghaiTech DataModule class.
- Parameters:
root (
Path|str|None) – Path to the root directory of the dataset. Defaults to"./datasets/shanghaitech".scene (
int) – Scene index in range [1, 13]. Defaults to1.clip_length_in_frames (
int) – Number of frames in each video clip. Defaults to2.frames_between_clips (
int) – Number of frames between consecutive clips. Defaults to1.target_frame (
VideoTargetFrame) – Specifies which frame in the clip should be used for ground truth. Defaults toVideoTargetFrame.LAST.train_batch_size (
int) – Training batch size. Defaults to32.eval_batch_size (
int) – Test batch size. Defaults to32.num_workers (
int) – Number of workers for data loading. Defaults to8.train_augmentations (
Transform|None) – Augmentations to apply to the training images Defaults toNone.val_augmentations (
Transform|None) – Augmentations to apply to the validation images. Defaults toNone.test_augmentations (
Transform|None) – Augmentations to apply to the test images. Defaults toNone.augmentations (
Transform|None) – General augmentations to apply if stage-specific augmentations are not provided.val_split_mode (
ValSplitMode) – Setting that determines how validation subset is obtained. Defaults toValSplitMode.SAME_AS_TEST.val_split_ratio (
float) – Fraction of train or test images that will be reserved for validation. Defaults to0.5.seed (
int|None) – Random seed for reproducibility. Defaults toNone.
- prepare_data()#
Verify that the dataset is available and convert video files.
The dataset must be downloaded manually. If it is not found, a
RuntimeErroris raised with instructions pointing to the official project page.- Raises:
RuntimeError – If the dataset is missing, with instructions for downloading it manually.
- Return type:
- anomalib.data.datamodules.video.shanghaitech.get_download_instructions(root_path)#
Get manual download instructions for the ShanghaiTech Campus dataset.
The automated mirror originally hosted by the dataset authors is no longer reachable, so the dataset must be downloaded manually from the official project page.