Skip to content

Commit 0eaa49c

Browse files
zhangguanheng66datumbox
authored andcommitted
add tests for HMDB51 dataset (#3458)
Summary: * add tests for HMDB51 dataset * lint Reviewed By: fmassa Differential Revision: D26756280 fbshipit-source-id: 9a13c7fd3f115da6b8051b7deeea81faccd36f0b Co-authored-by: Vasilis Vryniotis <[email protected]>
1 parent 1e39ab4 commit 0eaa49c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test/test_datasets.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,5 +1032,65 @@ def test_not_found_or_corrupted(self):
10321032
self.skipTest("Dataset currently does not handle the case of no found videos.")
10331033

10341034

1035+
class HMDB51TestCase(datasets_utils.VideoDatasetTestCase):
1036+
DATASET_CLASS = datasets.HMDB51
1037+
1038+
CONFIGS = datasets_utils.combinations_grid(fold=(1, 2, 3), train=(True, False))
1039+
1040+
_VIDEO_FOLDER = "videos"
1041+
_SPLITS_FOLDER = "splits"
1042+
_CLASSES = ("brush_hair", "wave")
1043+
1044+
def dataset_args(self, tmpdir, config):
1045+
tmpdir = pathlib.Path(tmpdir)
1046+
root = tmpdir / self._VIDEO_FOLDER
1047+
annotation_path = tmpdir / self._SPLITS_FOLDER
1048+
return root, annotation_path
1049+
1050+
def inject_fake_data(self, tmpdir, config):
1051+
tmpdir = pathlib.Path(tmpdir)
1052+
1053+
video_folder = tmpdir / self._VIDEO_FOLDER
1054+
os.makedirs(video_folder)
1055+
video_files = self._create_videos(video_folder)
1056+
1057+
splits_folder = tmpdir / self._SPLITS_FOLDER
1058+
os.makedirs(splits_folder)
1059+
num_examples = self._create_split_files(splits_folder, video_files, config["fold"], config["train"])
1060+
1061+
return num_examples
1062+
1063+
def _create_videos(self, root, num_examples_per_class=3):
1064+
def file_name_fn(cls, idx, clips_per_group=2):
1065+
return f"{cls}_{(idx // clips_per_group) + 1:d}_{(idx % clips_per_group) + 1:d}.avi"
1066+
1067+
return [
1068+
(
1069+
cls,
1070+
datasets_utils.create_video_folder(
1071+
root,
1072+
cls,
1073+
lambda idx: file_name_fn(cls, idx),
1074+
num_examples_per_class,
1075+
),
1076+
)
1077+
for cls in self._CLASSES
1078+
]
1079+
1080+
def _create_split_files(self, root, video_files, fold, train):
1081+
num_videos = num_train_videos = 0
1082+
1083+
for cls, videos in video_files:
1084+
num_videos += len(videos)
1085+
1086+
train_videos = set(random.sample(videos, random.randrange(1, len(videos) - 1)))
1087+
num_train_videos += len(train_videos)
1088+
1089+
with open(pathlib.Path(root) / f"{cls}_test_split{fold}.txt", "w") as fh:
1090+
fh.writelines(f"{file.name} {1 if file in train_videos else 2}\n" for file in videos)
1091+
1092+
return num_train_videos if train else (num_videos - num_train_videos)
1093+
1094+
10351095
if __name__ == "__main__":
10361096
unittest.main()

0 commit comments

Comments
 (0)