pytorch 数据集预处理
pytorch 数据集预处理
1、定义transforms
train_tfm = transforms.Compose([ # transforms.ToPILImage(), # Resize the image into a fixed shape (height = width = 128) # transforms.Resize((128, 128)), # You may add some transforms here. # ToTensor() should be the last one of the transforms. transforms.ToTensor(), #具有维度转化功能 transforms.Normalize(mean=[57.53], std=[35.825]) ])
2、定义train_loader、test_loader
totall_set = DatasetFolder("D:/PythonProject/QTNLS/resources/TN-Face/labeled", loader=tifffile.imread, extensions="tif", transform=train_tfm) # totall_set = DatasetFolder("D:/PythonProject/QTNLS/resources/TN-Face/labeled", loader=tifffile.imread, extensions="tif", transform=None) size_train = int(0.6 * len(totall_set)) size_test = len(totall_set) - size_train train_set, test_set = torch.utils.data.random_split(totall_set, [size_train, size_test]) train_loader = DataLoader(train_set, batch_size=batch_size_train, shuffle=True) test_loader = DataLoader(test_set, batch_size=batch_size_test, shuffle=True)
3、在训练的时候,还需要进行数据维度操纵
data = data.permute(0,2,1,3) #对维度重新进行调整 data = data.unsqueeze(1)
4、同理,测试的test的时候,也需要。