Raises: ValueError: When ``section`` is not one of ["training", "validation", "test"].
(self, dataset_dir: PathLike)
| 153 | return self.num_class |
| 154 | |
| 155 | def _generate_data_list(self, dataset_dir: PathLike) -> list[dict]: |
| 156 | """ |
| 157 | Raises: |
| 158 | ValueError: When ``section`` is not one of ["training", "validation", "test"]. |
| 159 | |
| 160 | """ |
| 161 | dataset_dir = Path(dataset_dir) |
| 162 | class_names = sorted(f"{x.name}" for x in dataset_dir.iterdir() if x.is_dir()) # folder name as the class name |
| 163 | self.num_class = len(class_names) |
| 164 | image_files = [[f"{x}" for x in (dataset_dir / class_names[i]).iterdir()] for i in range(self.num_class)] |
| 165 | num_each = [len(image_files[i]) for i in range(self.num_class)] |
| 166 | image_files_list = [] |
| 167 | image_class = [] |
| 168 | class_name = [] |
| 169 | for i in range(self.num_class): |
| 170 | image_files_list.extend(image_files[i]) |
| 171 | image_class.extend([i] * num_each[i]) |
| 172 | class_name.extend([class_names[i]] * num_each[i]) |
| 173 | |
| 174 | length = len(image_files_list) |
| 175 | indices = np.arange(length) |
| 176 | self.randomize(indices) |
| 177 | |
| 178 | test_length = int(length * self.test_frac) |
| 179 | val_length = int(length * self.val_frac) |
| 180 | if self.section == "test": |
| 181 | section_indices = indices[:test_length] |
| 182 | elif self.section == "validation": |
| 183 | section_indices = indices[test_length : test_length + val_length] |
| 184 | elif self.section == "training": |
| 185 | section_indices = indices[test_length + val_length :] |
| 186 | else: |
| 187 | raise ValueError( |
| 188 | f'Unsupported section: {self.section}, available options are ["training", "validation", "test"].' |
| 189 | ) |
| 190 | # the types of label and class name should be compatible with the pytorch dataloader |
| 191 | return [ |
| 192 | {"image": image_files_list[i], "label": image_class[i], "class_name": class_name[i]} |
| 193 | for i in section_indices |
| 194 | ] |
| 195 | |
| 196 | |
| 197 | class DecathlonDataset(Randomizable, CacheDataset): |