|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +from copy import deepcopy |
| 3 | +from typing import Any, Callable, List, Optional, Sequence, Union |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from mmengine.dataset import COLLATE_FUNCTIONS, pseudo_collate |
| 7 | + |
| 8 | +from mmaction.registry import DATASETS |
| 9 | +from mmaction.utils import ConfigType |
| 10 | +from .video_dataset import VideoDataset |
| 11 | + |
| 12 | + |
| 13 | +def get_type(transform: Union[dict, Callable]) -> str: |
| 14 | + """get the type of the transform.""" |
| 15 | + if isinstance(transform, dict) and 'type' in transform: |
| 16 | + return transform['type'] |
| 17 | + elif callable(transform): |
| 18 | + return transform.__repr__().split('(')[0] |
| 19 | + else: |
| 20 | + raise TypeError |
| 21 | + |
| 22 | + |
| 23 | +@DATASETS.register_module() |
| 24 | +class RepeatAugDataset(VideoDataset): |
| 25 | + """Video dataset for action recognition. |
| 26 | +
|
| 27 | + The dataset loads raw videos and apply specified transforms to return a |
| 28 | + dict containing the frame tensors and other information. |
| 29 | +
|
| 30 | + The ann_file is a text file with multiple lines, and each line indicates |
| 31 | + a sample video with the filepath and label, which are split with a |
| 32 | + whitespace. Example of a annotation file: |
| 33 | +
|
| 34 | + .. code-block:: txt |
| 35 | +
|
| 36 | + some/path/000.mp4 1 |
| 37 | + some/path/001.mp4 1 |
| 38 | + some/path/002.mp4 2 |
| 39 | + some/path/003.mp4 2 |
| 40 | + some/path/004.mp4 3 |
| 41 | + some/path/005.mp4 3 |
| 42 | +
|
| 43 | +
|
| 44 | + Args: |
| 45 | + ann_file (str): Path to the annotation file. |
| 46 | + pipeline (List[Union[dict, ConfigDict, Callable]]): A sequence of |
| 47 | + data transforms. |
| 48 | + data_prefix (dict or ConfigDict): Path to a directory where videos |
| 49 | + are held. Defaults to ``dict(video='')``. |
| 50 | + multi_class (bool): Determines whether the dataset is a multi-class |
| 51 | + dataset. Defaults to False. |
| 52 | + num_classes (int, optional): Number of classes of the dataset, used in |
| 53 | + multi-class datasets. Defaults to None. |
| 54 | + start_index (int): Specify a start index for frames in consideration of |
| 55 | + different filename format. However, when taking videos as input, |
| 56 | + it should be set to 0, since frames loaded from videos count |
| 57 | + from 0. Defaults to 0. |
| 58 | + modality (str): Modality of data. Support ``RGB``, ``Flow``. |
| 59 | + Defaults to ``RGB``. |
| 60 | + test_mode (bool): Store True when building test or validation dataset. |
| 61 | + Defaults to False. |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__(self, |
| 65 | + ann_file: str, |
| 66 | + pipeline: List[Union[dict, Callable]], |
| 67 | + data_prefix: ConfigType = dict(video=''), |
| 68 | + num_repeats: int = 4, |
| 69 | + multi_class: bool = False, |
| 70 | + num_classes: Optional[int] = None, |
| 71 | + start_index: int = 0, |
| 72 | + modality: str = 'RGB', |
| 73 | + **kwargs) -> None: |
| 74 | + |
| 75 | + use_decord = get_type(pipeline[0]) == 'DecordInit' and \ |
| 76 | + get_type(pipeline[2]) == 'DecordDecode' |
| 77 | + |
| 78 | + assert use_decord, ( |
| 79 | + 'RepeatAugDataset requires decord as the video ' |
| 80 | + 'loading backend, will support more backends in the ' |
| 81 | + 'future') |
| 82 | + |
| 83 | + super().__init__( |
| 84 | + ann_file, |
| 85 | + pipeline=pipeline, |
| 86 | + data_prefix=data_prefix, |
| 87 | + multi_class=multi_class, |
| 88 | + num_classes=num_classes, |
| 89 | + start_index=start_index, |
| 90 | + modality=modality, |
| 91 | + test_mode=False, |
| 92 | + **kwargs) |
| 93 | + self.num_repeats = num_repeats |
| 94 | + |
| 95 | + def prepare_data(self, idx) -> List[dict]: |
| 96 | + """Get data processed by ``self.pipeline``. |
| 97 | +
|
| 98 | + Reduce the video loading and decompressing. |
| 99 | + Args: |
| 100 | + idx (int): The index of ``data_info``. |
| 101 | + Returns: |
| 102 | + List[dict]: A list of length num_repeats. |
| 103 | + """ |
| 104 | + transforms = self.pipeline.transforms |
| 105 | + |
| 106 | + data_info = self.get_data_info(idx) |
| 107 | + data_info = transforms[0](data_info) # DecordInit |
| 108 | + |
| 109 | + frame_inds_list, frame_inds_length = [], [0] |
| 110 | + |
| 111 | + fake_data_info = dict( |
| 112 | + total_frames=data_info['total_frames'], |
| 113 | + start_index=data_info['start_index']) |
| 114 | + |
| 115 | + for repeat in range(self.num_repeats): |
| 116 | + data_info_ = transforms[1](fake_data_info) # SampleFrames |
| 117 | + frame_inds = data_info_['frame_inds'] |
| 118 | + frame_inds_list.append(frame_inds.reshape(-1)) |
| 119 | + frame_inds_length.append(frame_inds.size + frame_inds_length[-1]) |
| 120 | + |
| 121 | + for key in data_info_: |
| 122 | + data_info[key] = data_info_[key] |
| 123 | + |
| 124 | + data_info['frame_inds'] = np.concatenate(frame_inds_list) |
| 125 | + |
| 126 | + data_info = transforms[2](data_info) # DecordDecode |
| 127 | + imgs = data_info.pop('imgs') |
| 128 | + |
| 129 | + data_info_list = [] |
| 130 | + for repeat in range(self.num_repeats): |
| 131 | + data_info_ = deepcopy(data_info) |
| 132 | + start = frame_inds_length[repeat] |
| 133 | + end = frame_inds_length[repeat + 1] |
| 134 | + data_info_['imgs'] = imgs[start:end] |
| 135 | + for transform in transforms[3:]: |
| 136 | + data_info_ = transform(data_info_) |
| 137 | + data_info_list.append(data_info_) |
| 138 | + del imgs |
| 139 | + return data_info_list |
| 140 | + |
| 141 | + |
| 142 | +@COLLATE_FUNCTIONS.register_module() |
| 143 | +def repeat_pseudo_collate(data_batch: Sequence) -> Any: |
| 144 | + data_batch = [i for j in data_batch for i in j] |
| 145 | + return pseudo_collate(data_batch) |
0 commit comments