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的数据
举例:
上一篇: css实现不规则图片等宽高展示
下一篇: 说说Action和Jsp页面交互的那些事