PyTorch 之 Datasets
实现一个定制的 Dataset 类 Datasets 的框架: class CustomDataset(data.Dataset): # 需要继承 data.Dataset def __init__(self): # TODO # Initialize file path or list of file names. pass def __getitem__(self, index): # TODO # 1. 从文件中读取指定 index 的数据(例:使用 numpy.fromfile, PIL.Image.open) # 2. 预处理读取的数据(例:torchvision.Transform) # 3. 返回数据对(例:图像和对应标签) pass def __len__(self): # TODO # You should change 0 to the total size of your dataset. return 0 下面是官方 MNIST 的例子: class MNIST(data.Dataset): """`MNIST <http://yann.lecun.com/exdb/mnist/>`_ Dataset. Args: root (string): Root directory of dataset where ``processed/training.pt``