目录

torchvision.datasets

所有数据集都是 torch.utils.data.Dataset 的子类, 即,它们实现了 __getitem____len__ 方法。 因此,它们都可以传递给一个 torch.utils.data.DataLoader, 该对象可以使用 torch.multiprocessing 个工作者并行加载多个样本。 例如:

imagenet_data = torchvision.datasets.ImageNet('path/to/imagenet_root/')
data_loader = torch.utils.data.DataLoader(imagenet_data,
                                          batch_size=4,
                                          shuffle=True,
                                          num_workers=args.nThreads)

所有数据集的API几乎相同。它们都有两个通用参数: transformtarget_transform 分别用于转换输入和目标。 您还可以使用提供的基类创建自己的数据集。

加州理工学院

class torchvision.datasets.Caltech101(root: str, target_type: Union[List[str], str] = 'category', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

Caltech 101 数据集。

警告

此类需要scipy来从.mat格式加载目标文件。

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中目录 caltech101 已存在或将被保存,如果设置了 download=True。

  • target_type (字符串列表, 可选) – 要使用的目标类型,categoryannotation。也可以是一个列表,以输出包含所有指定 目标类型的元组。 category 表示目标类别,而 annotation 是一个手动生成轮廓的点列表。 默认为 category

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(image, target) 其中 target 的类型由 target_type 指定。

Return type

元组

class torchvision.datasets.Caltech256(root: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

Caltech 256 数据集。

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中目录 caltech256 已存在或将被保存,如果设置了 download=True。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(图像,目标)其中目标是目标类别的索引。

Return type

元组

CelebA

class torchvision.datasets.CelebA(root: str, split: str = 'train', target_type: Union[List[str], str] = 'attr', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

大规模名人面部属性(CelebA)数据集 数据集。

Parameters
  • 根目录 (字符串) – 图像下载到的根目录。

  • 划分 (字符串) – 可以为 {‘训练’, ‘验证’, ‘测试’, ‘全部’}。 根据选择的数据集进行相应划分。

  • 目标类型 (字符串列表, 可选) –

    使用的目标类型,attridentitybbox, 或 landmarks。也可以是一个列表,以输出包含所有指定目标类型的元组。 这些目标代表:

    • attr (np.array shape=(40,) dtype=int): binary (0, 1) labels for attributes

    • identity (int): label for each person (data points with the same identity are the same person)

    • bbox (np.array shape=(4,) dtype=int): bounding box (x, y, width, height)

    • landmarks (np.array shape=(10,) dtype=int): landmark points (lefteye_x, lefteye_y, righteye_x, righteye_y, nose_x, nose_y, leftmouth_x, leftmouth_y, rightmouth_x, rightmouth_y)

    默认为 attr。如果为空,None 将作为目标返回。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.ToTensor

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

CIFAR

class torchvision.datasets.CIFAR10(root: str, train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

CIFAR10 Dataset.

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中目录 cifar-10-batches-py 已存在或将被保存,如果设置了 download=True。

  • 训练 (bool, 可选) – 如果为True,从训练集创建数据集,否则从测试集创建。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(图像,目标)其中目标是目标类别的索引。

Return type

元组

class torchvision.datasets.CIFAR100(root: str, train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

CIFAR100 Dataset.

这是CIFAR10数据集的一个子类。

城市景观

注意

需要下载 Cityscapes 数据集。

class torchvision.datasets.Cityscapes(root: str, split: str = 'train', mode: str = 'fine', target_type: Union[List[str], str] = 'instance', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, transforms: Optional[Callable] = None)[source]

城市景观 数据集。

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中包含目录 leftImg8bitgtFinegtCoarse

  • 分割 (字符串可选) – 要使用的图像分割,traintestval 如果模式为”fine”,否则 traintrain_extraval

  • 模式 (字符串可选) – 要使用的质量模式,finecoarse

  • 目标类型 (字符串列表, 可选) – 使用的目标类型,instance, semantic, polygoncolor. 也可以是一个列表以输出包含所有指定目标类型的元组。

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个PIL图像并返回变换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 变换 (可调用对象,可选) – 一个函数/变换,它以输入样本及其目标为输入,并返回变换后的版本。

示例

获取语义分割目标

dataset = Cityscapes('./data/cityscapes', split='train', mode='fine',
                     target_type='semantic')

img, smnt = dataset[0]

获取多个目标

dataset = Cityscapes('./data/cityscapes', split='train', mode='fine',
                     target_type=['instance', 'color', 'polygon'])

img, (inst, col, poly) = dataset[0]

在“粗略”数据集上验证

dataset = Cityscapes('./data/cityscapes', split='val', mode='coarse',
                     target_type='semantic')

img, smnt = dataset[0]
__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(image, target) 其中 target 是一个包含所有目标类型的元组,如果 target_type 是包含多个项目的列表。否则,如果 target_type 为 "polygon",target 是一个 json 对象,否则为图像分割。

Return type

元组

COCO

注意

这些需要安装COCO API

字幕

class torchvision.datasets.CocoCaptions(root: str, annFile: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, transforms: Optional[Callable] = None)[source]

MS Coco Captions 数据集。

Parameters
  • 根目录 (字符串) – 图像下载到的根目录。

  • annFile (字符串) – JSON注释文件的路径。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.ToTensor

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 变换 (可调用对象,可选) – 一个函数/变换,它以输入样本及其目标为输入,并返回变换后的版本。

示例

import torchvision.datasets as dset
import torchvision.transforms as transforms
cap = dset.CocoCaptions(root = 'dir where images are',
                        annFile = 'json annotation file',
                        transform=transforms.ToTensor())

print('Number of samples: ', len(cap))
img, target = cap[3] # load 4th sample

print("Image Size: ", img.size())
print(target)

Output:

Number of samples: 82783
Image Size: (3L, 427L, 640L)
[u'A plane emitting smoke stream flying over a mountain.',
u'A plane darts across a bright blue sky behind a mountain covered in snow',
u'A plane leaves a contrail above the snowy mountain top.',
u'A mountain that has a plane flying overheard in the distance.',
u'A mountain view with a plume of smoke in the background']
__getitem__(index: int)Tuple[Any, Any]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

检测

class torchvision.datasets.CocoDetection(root: str, annFile: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, transforms: Optional[Callable] = None)[source]

MS Coco Detection 数据集。

Parameters
  • 根目录 (字符串) – 图像下载到的根目录。

  • annFile (字符串) – JSON注释文件的路径。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.ToTensor

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 变换 (可调用对象,可选) – 一个函数/变换,它以输入样本及其目标为输入,并返回变换后的版本。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

EMNIST

class torchvision.datasets.EMNIST(root: str, split: str, **kwargs: Any)[source]

EMNIST Dataset.

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中存在 EMNIST/processed/training.ptEMNIST/processed/test.pt

  • 划分 (字符串) - 数据集有6种不同的划分:byclassbymergebalancedlettersdigitsmnist。此参数指定要使用哪一个。

  • 训练 (bool, 可选) – 如果为 True,从 training.pt 创建数据集, 否则从 test.pt

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

FakeData

class torchvision.datasets.FakeData(size: int = 1000, image_size: Tuple[int, int, int] = (3, 224, 224), num_classes: int = 10, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, random_offset: int = 0)[source]

一个返回随机生成图像的假数据集,并将其作为PIL图像返回。

Parameters
  • 大小 (int, 可选) – 数据集的大小。默认:1000张图像

  • image_size (元组, 可选) – 返回图像的大小。默认值:(3, 224, 224)

  • 类别数 (int, 可选) – 数据集中的类别数量。默认值:10

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • random_offset (int) – 偏移用于生成每个图像的基于索引的随机种子。默认值:0

Fashion-MNIST

class torchvision.datasets.FashionMNIST(root: str, train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

Fashion-MNIST Dataset.

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中存在 FashionMNIST/processed/training.ptFashionMNIST/processed/test.pt

  • 训练 (bool, 可选) – 如果为 True,从 training.pt 创建数据集, 否则从 test.pt

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

Flickr

class torchvision.datasets.Flickr8k(root: str, ann_file: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None)[source]

Flickr8k 实体 数据集。

Parameters
  • 根目录 (字符串) – 图像下载到的根目录。

  • ann_file (字符串) – 注释文件路径。

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个PIL图像并返回变换后的版本。例如,transforms.ToTensor

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

元组 (图像,目标)。目标是图像的一组字幕。

Return type

元组

class torchvision.datasets.Flickr30k(root: str, ann_file: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None)[source]

Flickr30k 实体 数据集。

Parameters
  • 根目录 (字符串) – 图像下载到的根目录。

  • ann_file (字符串) – 注释文件路径。

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个PIL图像并返回变换后的版本。例如,transforms.ToTensor

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

元组 (图像,目标)。目标是图像的一组字幕。

Return type

元组

HMDB51

class torchvision.datasets.HMDB51(root: str, annotation_path: str, frames_per_clip: int, step_between_clips: int = 1, frame_rate: Optional[int] = None, fold: int = 1, train: bool = True, transform: Optional[Callable] = None, _precomputed_metadata: Optional[Dict[str, Any]] = None, num_workers: int = 1, _video_width: int = 0, _video_height: int = 0, _video_min_dimension: int = 0, _audio_samples: int = 0)[source]

HMDB51 dataset.

HMDB51 是一个动作识别视频数据集。 该数据集将每个视频视为固定大小的视频片段集合,由 frames_per_clip 指定,其中每个片段之间的步长以帧为单位,由 step_between_clips 给出。

举个例子,对于分别有10帧和15帧的两段视频来说,如果frames_per_clip=5step_between_clips=5,数据集大小将是(2 + 3) = 5,其中前两个元素来自视频1,接下来的三个元素来自视频2。 请注意,我们会丢弃那些不正好包含frames_per_clip个元素的片段,因此视频中的某些帧可能不会出现。

内部使用 VideoClips 对象来处理剪辑创建。

Parameters
  • 根目录 (字符串) – HMDB51 数据集的根目录。

  • annotation_path (str) – 包含分割文件的文件夹路径。

  • frames_per_clip (int) – 视频片段中的帧数。

  • step_between_clips (int) – 每个片段之间的帧数。

  • 折迭 (int, 可选) – 使用哪个折迭。应在1和3之间。

  • train (bool, optional) – 如果 True,则从训练集创建数据集, 否则从 test 集创建。

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个TxHxWxC视频并返回变换后的版本。

Returns

一个包含以下条目的三元组:

  • video (Tensor[T, H, W, C]): The T video frames

  • audio(Tensor[K, L]): the audio frames, where K is the number of channels and L is the number of points

  • label (int): class of the video clip

Return type

元组

__getitem__(idx: int)Tuple[torch.Tensor, torch.Tensor, int][source]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

ImageNet

class torchvision.datasets.ImageNet(root: str, split: str = 'train', download: Optional[str] = None, **kwargs: Any)[source]

ImageNet 2012 分类数据集。

Parameters
  • 根目录 (字符串) – ImageNet 数据集的根目录。

  • 数据集划分 (字符串可选) – 数据集的划分,支持train,或val

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 加载器 – 给定其路径后加载图像的函数。

注意

这需要安装scipy

iNaturalist

class torchvision.datasets.INaturalist(root: str, version: str = '2021_train', target_type: Union[List[str], str] = 'full', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

iNaturalist Dataset.

Parameters
  • 根目录 (字符串) – 数据集所在根目录,图像文件存储于此。 此类不需要/不使用标注文件。

  • 版本 (字符串可选) – 要下载/使用的数据集版本。其中一个为 ‘2017’, ‘2018’, ‘2019’, ‘2021_train’, ‘2021_train_mini’, ‘2021_valid’。 默认值:2021_train

  • 目标类型 (字符串列表, 可选) –

    目标类型的选择,针对2021版本,可选之一:

    • full: 完整类别(物种)

    • kingdom: 例如 “Animalia”

    • phylum: 例如 “Arthropoda”

    • class: 例如 “昆虫纲”

    • order: 例如 “鞘翅目”

    • family: 例如 “Cleridae”

    • genus: 例如 “Trichodes”

    针对2017-2019版本,选择其中一个:

    • full: 完整(数字)类别

    • super: 超级类别,例如“两栖动物”

    也可以是一个列表,以输出包含所有指定目标类型的元组。 默认值为full

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(image, target) 其中 target 的类型由 target_type 指定。

Return type

元组

category_name(category_type: str, category_id: int)str[source]
Parameters
  • category_type (str) – 其中之一:“full”,“kingdom”,“phylum”,“class”,“order”,“family”,“genus”或“super”

  • category_id (int) – 该类别中的索引(类ID)

Returns

类别名称

Kinetics-400

class torchvision.datasets.Kinetics400(root: str, frames_per_clip: int, num_classes: Optional[Any] = None, split: Optional[Any] = None, download: Optional[Any] = None, num_download_workers: Optional[Any] = None, **kwargs: Any)[source]

Kinetics-400 dataset.

Kinetics-400 是一个动作识别视频数据集。 该数据集将每个视频视为固定大小的视频片段集合,指定为 frames_per_clip,其中每个片段之间的帧步长由 step_between_clips 给出。

举个例子,对于分别有10帧和15帧的两段视频来说,如果frames_per_clip=5step_between_clips=5,数据集大小将是(2 + 3) = 5,其中前两个元素来自视频1,接下来的三个元素来自视频2。 请注意,我们会丢弃那些不正好包含frames_per_clip个元素的片段,因此视频中的某些帧可能不会出现。

内部使用 VideoClips 对象来处理剪辑创建。

Parameters
  • (字符串) –

    Kinetics-400 数据集的根目录。应按照如下结构组织:

    root/
    ├── class1
    │   ├── clip1.avi
    │   ├── clip2.avi
    │   ├── clip3.mp4
    │   └── ...
    └── class2
        ├── clipx.avi
        └── ...
    

  • frames_per_clip (int) – 剪辑中的帧数

  • step_between_clips (int) – 每个片段之间的帧数

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个TxHxWxC视频并返回变换后的版本。

Returns

一个包含以下条目的三元组:

  • video (Tensor[T, H, W, C]): the T video frames

  • audio(Tensor[K, L]): the audio frames, where K is the number of channels and L is the number of points

  • label (int): class of the video clip

Return type

元组

__getitem__(idx: int)Tuple[torch.Tensor, torch.Tensor, int]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

KITTI

class torchvision.datasets.Kitti(root: str, train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, transforms: Optional[Callable] = None, download: bool = False)[source]

KITTI Dataset.

它对应于用于目标检测的“对象左侧彩色图像”数据集。

Parameters
  • (字符串) –

    图像下载到的根目录。 如果 download=False,期望以下文件夹结构:

    <root>
        └── Kitti
            └─ raw
                ├── training
                |   ├── image_2
                |   └── label_2
                └── testing
                    └── image_2
    

  • 训练 (布尔值, 可选) – 如果为真,则使用 train 分割,否则使用 test 分割。 默认为 train

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个PIL图像并返回变换后的版本。例如,transforms.ToTensor

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • transforms (可调用对象,可选) – 一个函数/变换,它以输入样本及其目标为输入,并返回一个变换后的版本。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]

获取指定索引处的项。

Parameters

索引 (整数) – 索引

Returns

(image, target),其中 target 是包含以下键的字典列表:

  • 类型:字符串

  • 截断:float

  • 遮挡:整数

  • alpha: 浮点数

  • 边界框:浮点数[4]

  • 维度:float[3]

  • 位置:float[3]

  • 绕y轴旋转:浮点数

Return type

元组

KMNIST

class torchvision.datasets.KMNIST(root: str, train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

Kuzushiji-MNIST Dataset.

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中存在 KMNIST/processed/training.ptKMNIST/processed/test.pt

  • 训练 (bool, 可选) – 如果为 True,从 training.pt 创建数据集, 否则从 test.pt

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

LFW

class torchvision.datasets.LFWPeople(root: str, split: str = '10fold', image_set: str = 'funneled', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

LFW Dataset.

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中目录 lfw-py 已存在或将被保存,如果设置了 download=True。

  • 分割 (字符串可选) – 要使用的图像分割。可以是traintest10fold(默认)。

  • 图像集 (str, 可选) – 使用的图像漏斗类型,originalfunneleddeepfunneled。默认为 funneled

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomRotation

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

元组 (图像,目标),其中目标是指人的身份。

Return type

元组

class torchvision.datasets.LFWPairs(root: str, split: str = '10fold', image_set: str = 'funneled', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

LFW Dataset.

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中目录 lfw-py 已存在或将被保存,如果设置了 download=True。

  • 分割 (字符串可选) – 要使用的图像分割。可以是traintest10fold。默认为10fold

  • 图像集 (str, 可选) – 使用的图像漏斗类型,originalfunneleddeepfunneled。默认为 funneled

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomRotation

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any, int][source]
Parameters

索引 (整数) – 索引

Returns

(image1, image2, target) 其中 target 为 0 表示不同身份,1 表示相同身份。

Return type

元组

LSUN

class torchvision.datasets.LSUN(root: str, classes: Union[str, List[str]] = 'train', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None)[source]

LSUN dataset.

您需要安装lmdb包才能使用此数据集:运行 pip install lmdb

Parameters
  • 根目录 (字符串) – 数据库文件的根目录。

  • 类别 (字符串列表) – 其中之一 {‘train’, ‘val’, ‘test’} 或一个要加载的类别列表。例如 [‘bedroom_train’, ‘church_outdoor_train’].

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

元组 (图像,目标),其中目标是目标类别的索引。

Return type

元组

MNIST

class torchvision.datasets.MNIST(root: str, train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

MNIST Dataset.

Parameters
  • 根目录 (字符串) – 数据集的根目录,其中存在 MNIST/processed/training.ptMNIST/processed/test.pt

  • 训练 (bool, 可选) – 如果为 True,从 training.pt 创建数据集, 否则从 test.pt

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

omniglot

class torchvision.datasets.Omniglot(root: str, background: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

Omniglot 数据集。

Parameters
  • 根目录 (字符串) – 数据集所在根目录,其中包含目录 omniglot-py

  • 背景 (bool, 可选) – 如果为 True,从“背景”数据集创建数据集,否则从“评估”数据集创建。此术语由作者定义。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集zip文件并将其放置在根目录中。如果zip文件已经下载,则不会再次下载。

PhotoTour

class torchvision.datasets.PhotoTour(root: str, name: str, train: bool = True, transform: Optional[Callable] = None, download: bool = False)[source]

多视图立体对应 数据集。

注意

我们只提供更新版本的数据集,因为作者说明了它

is more suitable for training descriptors based on difference of Gaussian, or Harris corners, as the patches are centred on real interest point detections, rather than being projections of 3D points as is the case in the old dataset.

原始数据集可在http://phototour.cs.washington.edu/patches/default.htm获取。

Parameters
  • 根目录 (字符串) – 图像所在的根目录。

  • 名称 (字符串) – 要加载的数据集名称。

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个PIL图像并返回变换后的版本。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Union[torch.Tensor, Tuple[Any, Any, torch.Tensor]][source]
Parameters

索引 (整数) – 索引

Returns

(data1、data2、matches)

Return type

元组

Places365

class torchvision.datasets.Places365(root: str, split: str = 'train-standard', small: bool = False, download: bool = False, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, loader: Callable[[str], Any] = <function default_loader>)[source]

Places365 分类数据集。

Parameters
  • 根目录 (字符串) – Places365 数据集的根目录。

  • 划分 (字符串可选) – 数据集的划分。可以是以下之一:train-standard(默认值),train-challengeval

  • small (bool, optional) – 如果为True,则使用小图像,即缩放至 256 x 256 像素的图像,而不是高分辨率图像。

  • 下载 (bool, 可选) – 如果 True,则下载数据集组件并将其放置在 root 中。已经下载的存档不会再次下载。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 加载器 – 给定其路径后加载图像的函数。

Raises
  • RuntimeError – 如果 download is False 和元文件(即开发工具包)不存在或已损坏。

  • RuntimeError – 如果 download is True 并且图像存档已经提取。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

QMNIST

class torchvision.datasets.QMNIST(root: str, what: Optional[str] = None, compat: bool = True, train: bool = True, **kwargs: Any)[source]

QMNIST Dataset.

Parameters
  • 根目录 (字符串) – 数据集的根目录,其子目录包含带有数据集的torch二进制文件。

  • 数据集类型 (字符串,可选) – 可以为 ‘train’,‘test’,‘test10k’, ‘test50k’ 或 ‘nist’,分别表示与 MNIST 兼容的训练集、60k qmnist 测试集、10k qmnist 示例(与 MNIST 测试集匹配)、剩余的 50k qmnist 测试示例,或所有 NIST 数字。默认情况下,根据兼容性参数 ‘train’ 选择 ‘train’ 或 ‘test’。

  • 兼容性 (bool,可选) – 一个布尔值,指示每个示例的目标是类别编号(与MNIST数据加载器兼容)还是包含完整qmnist信息的torch向量。默认为True。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,将不再重新下载。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回一个转换后的版本。例如,transforms.RandomCrop

  • target_transform (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 训练 (bool,可选,兼容性) – 当参数‘what’未指定时,此布尔值决定是否加载训练集或测试集。默认值:True。

SBD

class torchvision.datasets.SBDataset(root: str, image_set: str = 'train', mode: str = 'boundaries', download: bool = False, transforms: Optional[Callable] = None)[source]

语义边界数据集

当前的 SBD 包含了来自 PASCAL VOC 2011 数据集的 11355 张图像的标注。

注意

请注意,此数据集包含的训练集和验证集与PASCAL VOC数据集中的划分不同。特别是,一些“训练”图像可能属于VOC2012验证集。 如果您有兴趣在VOC 2012验证集上进行测试,请使用image_set=’train_noval’,这将排除所有的验证集图像。

警告

此类需要scipy来从.mat格式加载目标文件。

Parameters
  • 根目录 (字符串) – 半语义边界数据集的根目录

  • 图像集 (字符串可选) – 选择要使用的图像集,trainvaltrain_noval。 图像集 train_noval 排除了VOC 2012验证图像。

  • 模式 (字符串, 可选) – 选择目标类型。可能的值为‘边界’或‘分割’。 在‘边界’的情况下,目标是一个形状为[num_classes, H, W]的数组, 其中num_classes=20

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

  • 变换 (可调用对象,可选) – 一个函数/变换,它以输入样本及其目标为输入,并返回变换后的版本。输入样本是PIL图像,目标是numpy数组如果mode=’boundaries’,或者是PIL图像如果mode=’segmentation’

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

SBU

class torchvision.datasets.SBU(root: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = True)[source]

SBU 标注照片 数据集。

Parameters
  • 根目录 (字符串) – 数据集所在根目录,其中包含tarball SBUCaptionedPhotoDataset.tar.gz

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个PIL图像并返回变换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(图像,目标) 其中目标是该照片的字幕。

Return type

元组

SEMEION

class torchvision.datasets.SEMEION(root: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = True)[source]

SEMEION Dataset.

Parameters
  • 根目录 (字符串) – 数据集所在根目录,其中包含目录 semeion.py

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(图像,目标)其中目标是目标类别的索引。

Return type

元组

STL10

class torchvision.datasets.STL10(root: str, split: str = 'train', folds: Optional[int] = None, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

STL10 Dataset.

Parameters
  • 根目录 (字符串) – 数据集所在根目录,其中包含目录 stl10_binary

  • 划分 (字符串) – 可以为 {‘训练’, ‘测试’, ‘未标注’, ‘训练+未标注’}。 根据相应的数据集进行选择。

  • 折数 (int, 可选) – 取值为 {0-9} 或 None。 在训练时,加载10个预定义的1k样本折之一,用于标准评估过程。如果没有传递值,则加载5k样本。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(图像,目标)其中目标是目标类别的索引。

Return type

元组

SVHN

class torchvision.datasets.SVHN(root: str, split: str = 'train', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

SVHN 数据集。 注意:SVHN 数据集将数字 0 标注为 10。然而,在这个数据集中, 我们将数字 0 标注为 0,以与 PyTorch 损失函数兼容,这些函数期望类别标签在范围 [0, C-1] 内。

警告

此类需要scipy来加载.mat格式的数据。

Parameters
  • 根目录 (字符串) – 数据集所在根目录,其中包含目录 SVHN

  • 划分 (字符串) – 可以为 {‘训练’, ‘测试’, ‘额外’}。 根据选择的数据集进行相应划分。‘额外’为额外训练集。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(图像,目标)其中目标是目标类别的索引。

Return type

元组

UCF101

class torchvision.datasets.UCF101(root: str, annotation_path: str, frames_per_clip: int, step_between_clips: int = 1, frame_rate: Optional[int] = None, fold: int = 1, train: bool = True, transform: Optional[Callable] = None, _precomputed_metadata: Optional[Dict[str, Any]] = None, num_workers: int = 1, _video_width: int = 0, _video_height: int = 0, _video_min_dimension: int = 0, _audio_samples: int = 0)[source]

UCF101 dataset.

UCF101 是一个动作识别视频数据集。 该数据集将每个视频视为固定大小的视频片段集合,指定为 frames_per_clip,其中每个片段之间的步长以帧为单位,由 step_between_clips 给出。数据集本身可以从数据集网站下载;标注文件 annotation_path 应该指向的内容可以从 here <https://www.crcv.ucf.edu/data/UCF101/UCF101TrainTestSplits-RecognitionTask.zip> 下载。

举个例子,对于分别有10帧和15帧的两段视频来说,如果frames_per_clip=5step_between_clips=5,数据集大小将是(2 + 3) = 5,其中前两个元素来自视频1,接下来的三个元素来自视频2。 请注意,我们会丢弃那些不正好包含frames_per_clip个元素的片段,因此视频中的某些帧可能不会出现。

内部使用 VideoClips 对象来处理剪辑创建。

Parameters
  • 根目录 (字符串) – UCF101 数据集的根目录。

  • annotation_path (str) – 包含分割文件的文件夹路径; 有关这些文件下载说明,请参见上方文档字符串

  • frames_per_clip (int) – 视频片段中的帧数。

  • step_between_clips (int, 可选) – 每两段之间的帧数。

  • 折迭 (int, 可选) – 使用哪个折迭。应在1和3之间。

  • train (bool, optional) – 如果 True,则从训练集创建数据集, 否则从 test 集创建。

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个TxHxWxC视频并返回变换后的版本。

Returns

一个包含以下条目的三元组:

  • video (Tensor[T, H, W, C]): the T video frames

  • audio(Tensor[K, L]): the audio frames, where K is the number of channels and L is the number of points

  • label (int): class of the video clip

Return type

元组

__getitem__(idx: int)Tuple[torch.Tensor, torch.Tensor, int][source]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

USPS

class torchvision.datasets.USPS(root: str, train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

USPS 数据集。 数据格式为:[标签 [索引:值 ]*256 n] * 行数,其中 label 位于 [1, 10] 中。 每个像素的值位于 [-1, 1] 中。在这里我们将 label 转换为 [0, 9] 并将像素值设置为 [0, 255]

Parameters
  • 根目录 (字符串) – 存储``USPS``数据文件的数据集根目录。

  • 训练 (bool, 可选) – 如果为 True,从 usps.bz2 创建数据集, 否则从 usps.t.bz2

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(图像,目标)其中目标是目标类别的索引。

Return type

元组

VOC

class torchvision.datasets.VOCSegmentation(root: str, year: str = '2012', image_set: str = 'train', download: bool = False, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, transforms: Optional[Callable] = None)[source]

Pascal VOC 分割数据集。

Parameters
  • 根目录 (字符串) – VOC 数据集的根目录。

  • 年份 (字符串可选) – 数据集的年份,支持年份 "2007""2012"

  • 图像集 (字符串可选) – 选择要使用的图像集,"train""trainval""val"。如果是 year=="2007",也可以是 "test"

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 变换 (可调用对象,可选) – 一个函数/变换,它以输入样本及其目标为输入,并返回变换后的版本。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(图像,目标)其中目标是图像分割。

Return type

元组

class torchvision.datasets.VOCDetection(root: str, year: str = '2012', image_set: str = 'train', download: bool = False, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, transforms: Optional[Callable] = None)[source]

Pascal VOC 目标检测数据集。

Parameters
  • 根目录 (字符串) – VOC 数据集的根目录。

  • 年份 (字符串可选) – 数据集的年份,支持年份 "2007""2012"

  • 图像集 (字符串可选) – 选择要使用的图像集,"train""trainval""val"。如果是 year=="2007",也可以是 "test"

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。 (默认值:VOC的20个类别的字母顺序索引)。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象,必需) – 一个函数/转换,它接收目标并对其进行变换。

  • 变换 (可调用对象,可选) – 一个函数/变换,它以输入样本及其目标为输入,并返回变换后的版本。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(image, target) 其中 target 是 XML 树的字典。

Return type

元组

WIDERFace

class torchvision.datasets.WIDERFace(root: str, split: str = 'train', transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)[source]

WIDERFace Dataset.

Parameters
  • (字符串) –

    图像和注释下载到的根目录。 如果 download=False,期望以下文件夹结构:

    <root>
        └── widerface
            ├── wider_face_split ('wider_face_split.zip' if compressed)
            ├── WIDER_train ('WIDER_train.zip' if compressed)
            ├── WIDER_val ('WIDER_val.zip' if compressed)
            └── WIDER_test ('WIDER_test.zip' if compressed)
    

  • 划分 (字符串) – 要使用的数据集划分。可以是{train, val, test}之一。 默认为train

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一个PIL图像并返回变换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 下载 (bool, 可选) – 如果为真,从互联网下载数据集并将其放入根目录。如果数据集已经下载,则不会再次下载。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(image, target) 其中 target 是图像中所有人脸的标注信息组成的字典。 测试集的 target 为 None。

Return type

元组

自定义数据集的基础类

class torchvision.datasets.DatasetFolder(root: str, 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)[source]

一个通用的数据加载器。

此默认目录结构可以通过覆盖 find_classes() 方法来自定义。

Parameters
  • 根目录 (字符串) – 根目录路径。

  • 加载器 (可调用) – 给定其路径时用于加载样本的函数。

  • extensions (元组[字符串]) – 允许的扩展名列表。 不应同时传递 extensions 和 is_valid_file。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个样本并返回其变换版本。 例如,transforms.RandomCrop用于图像。

  • target_transform (可调用对象, 可选) – 一个函数/转换,它接收目标并对其进行变换。

  • is_valid_file – 一个接受文件路径的函数 并检查该文件是否为有效的文件(用于检查损坏的文件) extensions 和 is_valid_file 不能同时传递。

__getitem__(index: int)Tuple[Any, Any][source]
Parameters

索引 (整数) – 索引

Returns

(sample, target) 其中 target 是目标类别的 class_index。

Return type

元组

find_classes(directory: str)Tuple[List[str], Dict[str, int]][source]

查找按如下结构组织的数据集中的类文件夹:

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

此方法可以被重写,以仅考虑 一部分类别,或适应不同的数据集目录结构。

Parameters

目录 (str) – 根目录路径,对应于 self.root

Raises

FileNotFoundError – 如果dir没有类文件夹。

Returns

所有类别的列表以及将每个类别映射到索引的字典。

Return type

(元组[列表[字符串], 字典[字符串, 整数]])

static make_dataset(directory: str, class_to_idx: Dict[str, int], extensions: Optional[Tuple[str, ]] = None, is_valid_file: Optional[Callable[[str], bool]] = None)List[Tuple[str, int]][source]

生成一个样本列表,形式为 (样本路径, 类别)。

这可以被覆盖,例如,从压缩的zip文件中读取文件,而不是从磁盘中读取。

Parameters
  • 目录 (str) – 数据集根目录,对应于 self.root

  • class_to_idx (Dict[str, int]) – 字典,将类别名称映射到类别索引。

  • 扩展名 (可选) – 允许的扩展名列表。 extensions和is_valid_file应传入其中一个。默认为None。

  • is_valid_file (可选) – 一个接受文件路径并检查该文件是否为有效文件的函数(用于检查损坏的文件)。extensions和is_valid_file不应同时传递。默认为None。

Raises
  • ValueError – 在class_to_idx为空的情况下。

  • ValueError – 在extensionsis_valid_file为None或都不为None的情况下。

  • FileNotFoundError – 如果没有为任何类找到有效的文件。

Returns

样本的形式示例 (样本路径,类别)

Return type

列表[元组[字符串, 整数]]

class torchvision.datasets.ImageFolder(root: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, loader: Callable[[str], Any] = <function default_loader>, is_valid_file: Optional[Callable[[str], bool]] = None)[source]

一个通用的数据加载器,默认情况下以这种方式排列图像:

root/dog/xxx.png
root/dog/xxy.png
root/dog/[...]/xxz.png

root/cat/123.png
root/cat/nsdf3.png
root/cat/[...]/asd932_.png

此类继承自DatasetFolder,因此可以重写相同的方法来自定义数据集。

Parameters
  • 根目录 (字符串) – 根目录路径。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

  • 加载器 (可调用对象,可选) – 给定其路径后用于加载图像的函数。

  • is_valid_file – 一个接受图像文件路径的函数,并检查该文件是否为有效的文件(用于检查损坏的文件)

__getitem__(index: int)Tuple[Any, Any]
Parameters

索引 (整数) – 索引

Returns

(sample, target) 其中 target 是目标类别的 class_index。

Return type

元组

class torchvision.datasets.VisionDataset(root: str, transforms: Optional[Callable] = None, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None)[source]

用于制作与torchvision兼容的数据集的基础类。 必须重写__getitem____len__方法。

Parameters
  • 根目录 (字符串) – 数据集的根目录。

  • 变换 (可调用对象,可选) – 一个函数/变换,它接收一张图像和一个标签,并返回它们的变换版本。

  • 转换 (可调用对象,可选) – 一个函数/转换,它接收一个PIL图像并返回转换后的版本。例如,transforms.RandomCrop

  • 目标转换 (可调用对象, 可选) – 一个函数/变换,它接收目标并对其进行转换。

注意

transformstransform 以及 target_transform 是互斥的。

__getitem__(index: int)Any[source]
Parameters

索引 (整数) – 索引

Returns

示例数据和元数据,可选地通过相应的转换进行变换。

Return type

(Any)

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源