|
11 | 11 | from torchvision.datasets import utils
|
12 | 12 | from common_utils import get_tmp_dir
|
13 | 13 | from fakedata_generation import mnist_root, cifar_root, imagenet_root, \
|
14 |
| - cityscapes_root, svhn_root, ucf101_root, places365_root, widerface_root, stl10_root |
| 14 | + cityscapes_root, svhn_root, places365_root, widerface_root, stl10_root |
15 | 15 | import xml.etree.ElementTree as ET
|
16 | 16 | from urllib.request import Request, urlopen
|
17 | 17 | import itertools
|
|
22 | 22 | import torch
|
23 | 23 | import shutil
|
24 | 24 | import json
|
| 25 | +import random |
25 | 26 |
|
26 | 27 |
|
27 | 28 | try:
|
@@ -261,29 +262,6 @@ def test_svhn(self, mock_check):
|
261 | 262 | dataset = torchvision.datasets.SVHN(root, split="extra")
|
262 | 263 | self.generic_classification_dataset_test(dataset, num_images=2)
|
263 | 264 |
|
264 |
| - @unittest.skipIf(not HAS_PYAV, "PyAV unavailable") |
265 |
| - def test_ucf101(self): |
266 |
| - cached_meta_data = None |
267 |
| - with ucf101_root() as (root, ann_root): |
268 |
| - for split in {True, False}: |
269 |
| - for fold in range(1, 4): |
270 |
| - for length in {10, 15, 20}: |
271 |
| - dataset = torchvision.datasets.UCF101(root, ann_root, length, fold=fold, train=split, |
272 |
| - num_workers=2, _precomputed_metadata=cached_meta_data) |
273 |
| - if cached_meta_data is None: |
274 |
| - cached_meta_data = dataset.metadata |
275 |
| - self.assertGreater(len(dataset), 0) |
276 |
| - |
277 |
| - video, audio, label = dataset[0] |
278 |
| - self.assertEqual(video.size(), (length, 320, 240, 3)) |
279 |
| - self.assertEqual(audio.numel(), 0) |
280 |
| - self.assertEqual(label, 0) |
281 |
| - |
282 |
| - video, audio, label = dataset[len(dataset) - 1] |
283 |
| - self.assertEqual(video.size(), (length, 320, 240, 3)) |
284 |
| - self.assertEqual(audio.numel(), 0) |
285 |
| - self.assertEqual(label, 1) |
286 |
| - |
287 | 265 | def test_places365(self):
|
288 | 266 | for split, small in itertools.product(("train-standard", "train-challenge", "val"), (False, True)):
|
289 | 267 | with places365_root(split=split, small=small) as places365:
|
@@ -905,5 +883,56 @@ def test_captions(self):
|
905 | 883 | self.assertEqual(tuple(captions), tuple(info["captions"]))
|
906 | 884 |
|
907 | 885 |
|
| 886 | +class UCF101TestCase(datasets_utils.VideoDatasetTestCase): |
| 887 | + DATASET_CLASS = datasets.UCF101 |
| 888 | + |
| 889 | + CONFIGS = datasets_utils.combinations_grid(fold=(1, 2, 3), train=(True, False)) |
| 890 | + |
| 891 | + def inject_fake_data(self, tmpdir, config): |
| 892 | + tmpdir = pathlib.Path(tmpdir) |
| 893 | + |
| 894 | + video_folder = tmpdir / "videos" |
| 895 | + os.makedirs(video_folder) |
| 896 | + video_files = self._create_videos(video_folder) |
| 897 | + |
| 898 | + annotations_folder = annotations_folder = tmpdir / "annotations" |
| 899 | + os.makedirs(annotations_folder) |
| 900 | + num_examples = self._create_annotation_files(annotations_folder, video_files, config["fold"], config["train"]) |
| 901 | + |
| 902 | + return (str(video_folder), str(annotations_folder)), num_examples |
| 903 | + |
| 904 | + def _create_videos(self, root, num_examples_per_class=3): |
| 905 | + def file_name_fn(cls, idx, clips_per_group=2): |
| 906 | + return f"v_{cls}_g{(idx // clips_per_group) + 1:02d}_c{(idx % clips_per_group) + 1:02d}.avi" |
| 907 | + |
| 908 | + video_files = [ |
| 909 | + datasets_utils.create_video_folder(root, cls, lambda idx: file_name_fn(cls, idx), num_examples_per_class) |
| 910 | + for cls in ("ApplyEyeMakeup", "YoYo") |
| 911 | + ] |
| 912 | + return [path.relative_to(root) for path in itertools.chain(*video_files)] |
| 913 | + |
| 914 | + def _create_annotation_files(self, root, video_files, fold, train): |
| 915 | + current_videos = random.sample(video_files, random.randrange(1, len(video_files) - 1)) |
| 916 | + current_annotation = self._annotation_file_name(fold, train) |
| 917 | + self._create_annotation_file(root, current_annotation, current_videos) |
| 918 | + |
| 919 | + other_videos = set(video_files) - set(current_videos) |
| 920 | + other_annotations = [ |
| 921 | + self._annotation_file_name(fold, train) for fold, train in itertools.product((1, 2, 3), (True, False)) |
| 922 | + ] |
| 923 | + other_annotations.remove(current_annotation) |
| 924 | + for name in other_annotations: |
| 925 | + self._create_annotation_file(root, name, other_videos) |
| 926 | + |
| 927 | + return len(current_videos) |
| 928 | + |
| 929 | + def _annotation_file_name(self, fold, train): |
| 930 | + return f"{'train' if train else 'test'}list{fold:02d}.txt" |
| 931 | + |
| 932 | + def _create_annotation_file(self, root, name, video_files): |
| 933 | + with open(pathlib.Path(root) / name, "w") as fh: |
| 934 | + fh.writelines(f"{file}\n" for file in sorted(video_files)) |
| 935 | + |
| 936 | + |
908 | 937 | if __name__ == "__main__":
|
909 | 938 | unittest.main()
|
0 commit comments