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
- 引發:
FileNotFoundError – 如果
dir
沒有類別資料夾。- 返回:
所有類別的列表以及將每個類別映射到索引的字典。
- 返回類型:
- 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 文件而不是從磁碟讀取文件。
- 參數:
directory (str) – 根資料集目錄,對應於
self.root
。extensions (選填) – 允許的副檔名清單。extensions 或 is_valid_file 應該要傳入其中一個。預設為 None。
is_valid_file (選填) – 一個函數,接受檔案路徑作為輸入,並檢查該檔案是否為有效的檔案(用於檢查損壞的檔案)。extensions 和 is_valid_file 不應該同時傳入。預設為 None。
allow_empty (bool, 選填) – 如果為 True,則空的資料夾將被視為有效的類別。如果為 False(預設),則在空的資料夾上會引發錯誤。
- 引發:
ValueError – 如果
class_to_idx
是空的。ValueError – 如果
extensions
和is_valid_file
皆為 None,或兩者皆不為 None。FileNotFoundError – 如果任何類別都找不到有效的檔案。
- 返回:
樣本形式 (path_to_sample, class)
- 返回類型: