欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pytorch常用的数据预处理

程序员文章站 2022-05-05 08:47:03
...

DataLoader

  • 使用dataloader方便数据取出。定义CustomDataset类方便对接dataloader类型
class CustomDataset(torch.utils.data.Dataset):
    def __init__(self):
        # TODO
        # 1. Initialize file paths or a list of file names. 
        pass
    def __getitem__(self, index):
        # TODO
        # 1. Read one data from file (e.g. using numpy.fromfile, PIL.Image.open).
        # 2. Preprocess the data (e.g. torchvision.Transform).
        # 3. Return a data pair (e.g. image and label).
        pass
    def __len__(self):
        # You should change 0 to the total size of your dataset.
        return 0 
custom_dataset = CustomDataset()
train_loader=torch.utils.data.DataLoader(dataset=custom_dataset,batch_size=64,shuffle=True)
data_iter=iter(train_loader)#迭代器方便取minibatch
images,labels=data_iter.next()#取出一个mini-batch的数据

举例:


相关标签: 预处理