Reference:Pytorch官方文档
以上是Pytorch官方的文档,本文主要对其进行翻译整理,并加入一些自己的理解,仅作日后复习查阅所用。
Dataset存储samples和对应的label,Dataloaders为Dataset提供 easy access to the samples.
import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor()
)
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor()
)
root:数据路径。
train:表明是training data或是test data。
download:若root路径下没有data是否从网上下载。
transform\target_transform:指定data\label的转换。
labels_map = {
0: "T-Shirt",
1: "Trouser",
2: "Pullover",
3: "Dress",
4: "Coat",
5: "Sandal",
6: "Shirt",
7: "Sneaker",
8: "Bag",
9: "Ankle Boot",
}
figure = plt.figure(figsize=(8, 8))
cols, rows = 3, 3
for i in range(1, cols * rows + 1):
sample_idx = torch.randint(len(training_data), size=(1,)).item()
img, label = training_data[sample_idx]
figure.add_subplot(rows, cols, i)
plt.title(labels_map[label])
plt.axis("off")
plt.imshow(img.squeeze(), cmap="gray")
plt.show()
一个定制的Dataset需实现以下三个函数:__ init__ , __ len__ , and __ getitem __
import os
import pandas as pd
from torchvision.io import read_image
class CustomImageDataset(Dataset):
def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
self.img_labels = pd.read_csv(annotations_file)
self.img_dir = img_dir
self.transform = transform
self.target_transform = target_transform
def __len__(self):
return len(self.img_labels)
def __getitem__(self, idx):
img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
image = read_image(img_path)
label = self.img_labels.iloc[idx, 1]
if self.transform:
image = self.transform(image)
if self.target_transform:
label = self.target_transform(label)
return image, label
_init_:在实例化Dataset对象时运行一次,初始化图片存放的路径img_dir,和对应的label存放CSV文件annotations_file,和两种transform方式,其中label.csv文件格式如下:
tshirt1.jpg, 0
tshirt2.jpg, 0
…
ankleboot999.jpg, 9
_len_:返回dataset中的sample数量。
_getitem_:返回给定idx坐标下的的sample,并用read_image将图片转化为一个tensor。
通过dataset,我们已经实现了一次取一个sample和对应label这一功能,但在实际训练过程中,往往会一次训练一个minibatch的sample,并且会在每一个epoch重新排序数据从而减少模型的过拟合,并利用python的多线程加快数据检索。而DataLoader就是为了简化这些步骤。
将dataset Load入Dataloader中(此代码接的是 Fashion-MNIST dataset)
from torch.utils.data import DataLoader
train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True)
test_dataloader = DataLoader(test_data, batch_size=64, shuffle=True)
再根据需要对DataLoader中的数据进行遍历
# Display image and label.
train_features, train_labels = next(iter(train_dataloader))
print(f"Feature batch shape: {train_features.size()}")
print(f"Labels batch shape: {train_labels.size()}")
img = train_features[0].squeeze()
label = train_labels[0]
plt.imshow(img, cmap="gray")
plt.show()
print(f"Label: {label}")
Out:
Feature batch shape: torch.Size([64, 1, 28, 28])
Labels batch shape: torch.Size([64])
Label: 0
DataLoader(dataset, batch_size=1, shuffle=False, sampler=None,
batch_sampler=None, num_workers=0, collate_fn=None,
pin_memory=False, drop_last=False, timeout=0,
worker_init_fn=None, *, prefetch_factor=2,
persistent_workers=False)
TORCH.UTILS.DATA官方技术文档