|
| 1 | +import io |
| 2 | +import pathlib |
| 3 | +from typing import Any, Callable, Dict, List, Optional, Tuple |
| 4 | + |
| 5 | +import torch |
| 6 | +from torchdata.datapipes.iter import ( |
| 7 | + IterDataPipe, |
| 8 | + Mapper, |
| 9 | + Shuffler, |
| 10 | + Filter, |
| 11 | + IterKeyZipper, |
| 12 | + Demultiplexer, |
| 13 | + LineReader, |
| 14 | + CSVParser, |
| 15 | +) |
| 16 | +from torchvision.prototype.datasets.utils import ( |
| 17 | + Dataset, |
| 18 | + DatasetConfig, |
| 19 | + DatasetInfo, |
| 20 | + HttpResource, |
| 21 | + OnlineResource, |
| 22 | + DatasetType, |
| 23 | +) |
| 24 | +from torchvision.prototype.datasets.utils._internal import ( |
| 25 | + INFINITE_BUFFER_SIZE, |
| 26 | + hint_sharding, |
| 27 | + path_comparator, |
| 28 | + getitem, |
| 29 | +) |
| 30 | +from torchvision.prototype.features import Label |
| 31 | + |
| 32 | + |
| 33 | +class DTD(Dataset): |
| 34 | + def _make_info(self) -> DatasetInfo: |
| 35 | + return DatasetInfo( |
| 36 | + "dtd", |
| 37 | + type=DatasetType.IMAGE, |
| 38 | + homepage="https://www.robots.ox.ac.uk/~vgg/data/dtd/", |
| 39 | + valid_options=dict( |
| 40 | + split=("train", "test", "val"), |
| 41 | + fold=tuple(str(fold) for fold in range(1, 11)), |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + def resources(self, config: DatasetConfig) -> List[OnlineResource]: |
| 46 | + archive = HttpResource( |
| 47 | + "https://www.robots.ox.ac.uk/~vgg/data/dtd/download/dtd-r1.0.1.tar.gz", |
| 48 | + sha256="e42855a52a4950a3b59612834602aa253914755c95b0cff9ead6d07395f8e205", |
| 49 | + decompress=True, |
| 50 | + ) |
| 51 | + return [archive] |
| 52 | + |
| 53 | + def _classify_archive(self, data: Tuple[str, Any]) -> Optional[int]: |
| 54 | + path = pathlib.Path(data[0]) |
| 55 | + if path.parent.name == "labels": |
| 56 | + if path.name == "labels_joint_anno.txt": |
| 57 | + return 1 |
| 58 | + |
| 59 | + return 0 |
| 60 | + elif path.parents[1].name == "images": |
| 61 | + return 2 |
| 62 | + else: |
| 63 | + return None |
| 64 | + |
| 65 | + def _image_key_fn(self, data: Tuple[str, Any]) -> str: |
| 66 | + path = pathlib.Path(data[0]) |
| 67 | + return str(path.relative_to(path.parents[1])) |
| 68 | + |
| 69 | + def _collate_and_decode_sample( |
| 70 | + self, |
| 71 | + data: Tuple[Tuple[str, List[str]], Tuple[str, io.IOBase]], |
| 72 | + *, |
| 73 | + decoder: Optional[Callable[[io.IOBase], torch.Tensor]], |
| 74 | + ) -> Dict[str, Any]: |
| 75 | + (_, joint_categories_data), image_data = data |
| 76 | + _, *joint_categories = joint_categories_data |
| 77 | + path, buffer = image_data |
| 78 | + |
| 79 | + category = pathlib.Path(path).parent.name |
| 80 | + |
| 81 | + return dict( |
| 82 | + joint_categories={category for category in joint_categories if category}, |
| 83 | + label=Label(self.info.categories.index(category), category=category), |
| 84 | + path=path, |
| 85 | + image=decoder(buffer) if decoder else buffer, |
| 86 | + ) |
| 87 | + |
| 88 | + def _make_datapipe( |
| 89 | + self, |
| 90 | + resource_dps: List[IterDataPipe], |
| 91 | + *, |
| 92 | + config: DatasetConfig, |
| 93 | + decoder: Optional[Callable[[io.IOBase], torch.Tensor]], |
| 94 | + ) -> IterDataPipe[Dict[str, Any]]: |
| 95 | + archive_dp = resource_dps[0] |
| 96 | + |
| 97 | + splits_dp, joint_categories_dp, images_dp = Demultiplexer( |
| 98 | + archive_dp, 3, self._classify_archive, drop_none=True, buffer_size=INFINITE_BUFFER_SIZE |
| 99 | + ) |
| 100 | + |
| 101 | + splits_dp = Filter(splits_dp, path_comparator("name", f"{config.split}{config.fold}.txt")) |
| 102 | + splits_dp = LineReader(splits_dp, decode=True, return_path=False) |
| 103 | + splits_dp = Shuffler(splits_dp, buffer_size=INFINITE_BUFFER_SIZE) |
| 104 | + splits_dp = hint_sharding(splits_dp) |
| 105 | + |
| 106 | + joint_categories_dp = CSVParser(joint_categories_dp, delimiter=" ") |
| 107 | + |
| 108 | + dp = IterKeyZipper( |
| 109 | + splits_dp, |
| 110 | + joint_categories_dp, |
| 111 | + key_fn=getitem(), |
| 112 | + ref_key_fn=getitem(0), |
| 113 | + buffer_size=INFINITE_BUFFER_SIZE, |
| 114 | + ) |
| 115 | + dp = IterKeyZipper( |
| 116 | + dp, |
| 117 | + images_dp, |
| 118 | + key_fn=getitem(0), |
| 119 | + ref_key_fn=self._image_key_fn, |
| 120 | + buffer_size=INFINITE_BUFFER_SIZE, |
| 121 | + ) |
| 122 | + return Mapper(dp, self._collate_and_decode_sample, fn_kwargs=dict(decoder=decoder)) |
| 123 | + |
| 124 | + def _filter_images(self, data: Tuple[str, Any]) -> bool: |
| 125 | + return self._classify_archive(data) == 2 |
| 126 | + |
| 127 | + def _generate_categories(self, root: pathlib.Path) -> List[str]: |
| 128 | + dp = self.resources(self.default_config)[0].load(pathlib.Path(root) / self.name) |
| 129 | + dp = Filter(dp, self._filter_images) |
| 130 | + return sorted({pathlib.Path(path).parent.name for path, _ in dp}) |
0 commit comments