快捷方式

DatasetFolder

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)[原始碼]

一個通用的資料載入器。

可以透過重寫 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]][原始碼]

在如下結構的資料集中查詢類別資料夾

directory/
├── class_x
│   ├── xxx.ext
│   ├── xxy.ext
│   └── ...
│       └── xxz.ext
└── class_y
    ├── 123.ext
    ├── nsdf3.ext
    └── ...
    └── asd932_.ext

可以重寫此方法以僅考慮類別的子集,或適應不同的資料集目錄結構。

引數:

directory (str) – 根目錄路徑,對應於 self.root

引發:

FileNotFoundError – 如果 dir 沒有類別資料夾。

返回:

所有類別的列表以及將每個類別對映到索引的字典。

返回型別:

(Tuple[List[str], Dict[str, int]])

靜態 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]][原始碼]

生成形式為 (path_to_sample, class) 的樣本列表。

可以重寫此方法,例如,從壓縮的 zip 檔案而不是從磁碟讀取檔案。

引數:
  • directory (str) – 資料集根目錄,對應於 self.root

  • class_to_idx (Dict[str, int]) – 將類別名稱對映到類別索引的字典。

  • extensions (optional) – 允許的副檔名列表。應傳遞 extensions 或 is_valid_file 中的一個。預設為 None。

  • is_valid_file (optional) – 一個函式,它接收檔案路徑並檢查檔案是否有效(用於檢查損壞的檔案)。extensions 和 is_valid_file 不應同時傳遞。預設為 None。

  • allow_empty (bool, optional) – 如果為 True,空資料夾被視為有效類別。如果為 False(預設值),則在空資料夾上引發錯誤。

引發:
  • ValueError – 如果 class_to_idx 為空。

  • ValueError – 如果 extensionsis_valid_file 均為 None 或均不為 None。

  • FileNotFoundError – 如果任何類別都沒有找到有效檔案。

返回:

形式為 (path_to_sample, class) 的樣本

返回型別:

List[Tuple[str, int]]

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源