DatasetFolder¶
- class torchvision.datasets.DatasetFolder(root: Union[str, Path], loader: Callable[[str], Any], extensions: Optional[Tuple[str, ...]] = None, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, is_valid_file: Optional[Callable[[str], bool]] = None, allow_empty: bool = False)[source]¶
通用数据加载器。
可以通过重写
find_classes()
方法来自定义此默认目录结构。- 参数:
root (str 或
pathlib.Path
) – 根目录路径。loader (callable) – 给定路径时加载样本的函数。
extensions (tuple[string]) – 允许的扩展名列表。extensions 和 is_valid_file 不应同时传递。
transform (callable, optional) – 接收样本并返回转换版本的函数/变换。例如,图像的
transforms.RandomCrop
。target_transform (callable, optional) – 接收目标并对其进行变换的函数/变换。
is_valid_file (callable, optional) – 接收文件路径并检查文件是否有效的函数(用于检查损坏的文件)。extensions 和 is_valid_file 不应同时传递。
allow_empty – 如果为 True,则空文件夹被视为有效的类。如果为 False(默认),则在空文件夹上引发错误。
- find_classes(directory: Union[str, Path]) Tuple[List[str], Dict[str, int]] [source]¶
在结构如下的数据集中查找类文件夹
directory/ ├── class_x │ ├── xxx.ext │ ├── xxy.ext │ └── ... │ └── xxz.ext └── class_y ├── 123.ext ├── nsdf3.ext └── ... └── asd932_.ext
可以重写此方法以仅考虑类的子集,或适应不同的数据集目录结构。
- 参数:
directory (str) – 根目录路径,对应于
self.root
- Raises:
FileNotFoundError – 如果
dir
没有类文件夹。- Returns:
所有类的列表以及将每个类映射到索引的字典。
- Return type:
- static make_dataset(directory: Union[str, Path], class_to_idx: Dict[str, int], extensions: Optional[Tuple[str, ...]] = None, is_valid_file: Optional[Callable[[str], bool]] = None, allow_empty: bool = False) List[Tuple[str, int]] [source]¶
生成 (path_to_sample, class) 形式的样本列表。
可以重写此方法,例如从压缩的 zip 文件而不是从磁盘读取文件。
- 参数:
- Raises:
ValueError – 如果
class_to_idx
为空。ValueError – 如果
extensions
和is_valid_file
均为 None 或均不为 None。FileNotFoundError – 如果任何类都找不到有效文件。
- Returns:
(path_to_sample, class) 形式的样本
- Return type: