惠勒提问法
问题1:如何在自己定义的类WineDateset(继承自Dateset)实现transform?
问题2:多个transform如何作用在dataset上?
代码
'''
Transforms can be applied to PIL images, tensors, ndarrays, or custom data
during creation of the DataSet
complete list of built-in transforms:
https://pytorch.org/docs/stable/torchvision/transforms.html
'''
import torch
import torchvision
from torch.utils.data import Dataset
import numpy as np
class WineDataset(Dataset):
def __init__(self, transform=None):
xy = np.loadtxt('./data/wine/wine.csv', delimiter=',', dtype=np.float32, skiprows=1)
self.n_samples = xy.shape[0]
# note that we do not convert to tensor here
self.x_data = xy[:, 1:]
self.y_data = xy[:, [0]]
self.transform = transform
def __getitem__(self, index):
sample = self.x_data[index], self.y_data[index]
if self.transform:
sample = self.transform(sample)
return sample
def __len__(self):
return self.n_samples
# 答案1 在自定义的数据集类中加上transform参数,然后自定义类,实现对数据的变换,
# 利用__call__类函数实现调用。
# Custom Transforms
# implement __call__(self, sample)
class ToTensor:
# Convert ndarrays to Tensors
def __call__(self, sample):
inputs, targets = sample
return torch.from_numpy(inputs), torch.from_numpy(targets)
class MulTransform:
# multiply inputs with a given factor
def __init__(self, factor):
self.factor = factor
def __call__(self, sample):
inputs, targets = sample
inputs *= self.factor
return inputs, targets
print('Without Transform')
dataset = WineDataset()
first_data = dataset[0]
features, labels = first_data
print(type(features), type(labels))
print(features, labels)
print('\nWith Tensor Transform')
dataset = WineDataset(transform=ToTensor())
first_data = dataset[0]
features, labels = first_data
print(type(features), type(labels))
print(features, labels)
# 答案2:借助torchvision.transforms.Compose实现。
print('\nWith Tensor and Multiplication Transform')
composed = torchvision.transforms.Compose([ToTensor(), MulTransform(4)])
dataset = WineDataset(transform=composed)
first_data = dataset[0]
features, labels = first_data
print(type(features), type(labels))
print(features, labels)
输出结果