hmdb51.py 5.22 KB
Newer Older
1
2
3
4
import glob
import os

from .utils import list_dir
5
from .folder import find_classes, make_dataset
6
from .video_utils import VideoClips
7
8
9
10
from .vision import VisionDataset


class HMDB51(VisionDataset):
11
    """
12
    `HMDB51 <http://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database/>`_
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    dataset.

    HMDB51 is an action recognition video dataset.
    This dataset consider every video as a collection of video clips of fixed size, specified
    by ``frames_per_clip``, where the step in frames between each clip is given by
    ``step_between_clips``.

    To give an example, for 2 videos with 10 and 15 frames respectively, if ``frames_per_clip=5``
    and ``step_between_clips=5``, the dataset size will be (2 + 3) = 5, where the first two
    elements will come from video 1, and the next three elements from video 2.
    Note that we drop clips which do not have exactly ``frames_per_clip`` elements, so not all
    frames in a video might be present.

    Internally, it uses a VideoClips object to handle clip creation.

    Args:
        root (string): Root directory of the HMDB51 Dataset.
30
31
32
33
34
        annotation_path (str): Path to the folder containing the split files.
        frames_per_clip (int): Number of frames in a clip.
        step_between_clips (int): Number of frames between each clip.
        fold (int, optional): Which fold to use. Should be between 1 and 3.
        train (bool, optional): If ``True``, creates a dataset from the train split,
35
            otherwise from the ``test`` split.
36
        transform (callable, optional): A function/transform that takes in a TxHxWxC video
37
38
39
            and returns a transformed version.

    Returns:
40
41
42
43
44
45
        tuple: A 3-tuple with the following entries:

            - video (Tensor[T, H, W, C]): The `T` video frames
            - audio(Tensor[K, L]): the audio frames, where `K` is the number of channels
              and `L` is the number of points
            - label (int): class of the video clip
46
    """
47
48
49
50
51
52

    data_url = "http://serre-lab.clps.brown.edu/wp-content/uploads/2013/10/hmdb51_org.rar"
    splits = {
        "url": "http://serre-lab.clps.brown.edu/wp-content/uploads/2013/10/test_train_splits.rar",
        "md5": "15e67781e70dcfbdce2d7dbb9b3344b5"
    }
53
54
    TRAIN_TAG = 1
    TEST_TAG = 2
55
56

    def __init__(self, root, annotation_path, frames_per_clip, step_between_clips=1,
57
                 frame_rate=None, fold=1, train=True, transform=None,
58
59
                 _precomputed_metadata=None, num_workers=1, _video_width=0,
                 _video_height=0, _video_min_dimension=0, _audio_samples=0):
60
        super(HMDB51, self).__init__(root)
61
        if fold not in (1, 2, 3):
62
63
            raise ValueError("fold should be between 1 and 3, got {}".format(fold))

64
        extensions = ('avi',)
65
        self.classes, class_to_idx = find_classes(self.root)
66
67
68
69
70
        self.samples = make_dataset(
            self.root,
            class_to_idx,
            extensions,
        )
71

72
        video_paths = [path for (path, _) in self.samples]
73
        video_clips = VideoClips(
74
            video_paths,
75
76
77
78
            frames_per_clip,
            step_between_clips,
            frame_rate,
            _precomputed_metadata,
79
80
81
82
83
            num_workers=num_workers,
            _video_width=_video_width,
            _video_height=_video_height,
            _video_min_dimension=_video_min_dimension,
            _audio_samples=_audio_samples,
84
        )
85
86
87
88
        # we bookkeep the full version of video clips because we want to be able
        # to return the meta data of full version rather than the subset version of
        # video clips
        self.full_video_clips = video_clips
89
90
91
        self.fold = fold
        self.train = train
        self.indices = self._select_fold(video_paths, annotation_path, fold, train)
92
        self.video_clips = video_clips.subset(self.indices)
93
        self.transform = transform
94

95
96
    @property
    def metadata(self):
97
        return self.full_video_clips.metadata
98

99
100
101
102
103
    def _select_fold(self, video_list, annotations_dir, fold, train):
        target_tag = self.TRAIN_TAG if train else self.TEST_TAG
        split_pattern_name = "*test_split{}.txt".format(fold)
        split_pattern_path = os.path.join(annotations_dir, split_pattern_name)
        annotation_paths = glob.glob(split_pattern_path)
104
        selected_files = []
105
106
107
108
109
110
111
112
        for filepath in annotation_paths:
            with open(filepath) as fid:
                lines = fid.readlines()
            for line in lines:
                video_filename, tag_string = line.split()
                tag = int(tag_string)
                if tag == target_tag:
                    selected_files.append(video_filename)
113
        selected_files = set(selected_files)
114
115
116
117
118
119

        indices = []
        for video_index, video_path in enumerate(video_list):
            if os.path.basename(video_path) in selected_files:
                indices.append(video_index)

120
121
122
123
124
125
        return indices

    def __len__(self):
        return self.video_clips.num_clips()

    def __getitem__(self, idx):
126
127
128
        video, audio, _, video_idx = self.video_clips.get_clip(idx)
        sample_index = self.indices[video_idx]
        _, class_index = self.samples[sample_index]
129

130
131
132
        if self.transform is not None:
            video = self.transform(video)

133
        return video, audio, class_index