如何将图像数据加载到pytorch dataLoader中?

时间:2019-02-12 20:18:11

标签: python deep-learning pytorch

我是深度学习的新手,我想使用pytorch编写的算法,pytorch教程中的示例非常具体。我的PC中有数据集,我想对其进行预处理。 谢谢

2 个答案:

答案 0 :(得分:0)

关注此-https://pytorch.org/tutorials/beginner/data_loading_tutorial.html 基本上,dataLoader旨在帮助进行过程学习,它消除了破坏,改组和加载数据的痛苦。

transformed_dataset = FaceLandmarksDataset(csv_file='data/faces/face_landmarks.csv',
                                           root_dir='data/faces/',
                                           transform=transforms.Compose([
                                               Rescale(256),
                                               RandomCrop(224),
                                               ToTensor()
                                           ]))
dataloader = DataLoader(transformed_dataset, batch_size=4,
                        shuffle=True, num_workers=4)

答案 1 :(得分:0)

class Get_Dataset( Dataset ):
    def __init__ (self) : #intital function define all member and  class variables 
        super(Get_Dataset , self).__init__()
        scale = 255
        path = '/home/singhv/data/train/'
        trainA = os.listdir(path + 'image')
        trainB = os.listdir(path + 'mask')
        self.len = min([len(image) , len(mask)])
        self.object = np.ones((self.len ,128,128 , 3 ))
        self.target = np.ones((self.len , 128 , 128 , 3))
        print("Loading Dataset...")
        for i in tqdm(range(self.len)) :
            self.object[i] = cv2.resize(cv2.imread(path + 'image/' + image[i]),  (128,128))
            self.target[i] = cv2.resize(cv2.imread(path + 'mask/' + mask[i]),  (128,128))
        self.object =  torch.from_numpy(((self.object/(scale/2))-1 ) ).transpose_(3 , 1).double()
        self.target = torch.from_numpy(((self.target/(scale / 2)) -1 )).transpose_(3 , 1).double()

    def __getitem__(self , index   ) : # function to return dataset size
        return self.object[index] ,  self.target[index]

    def __len__(self): #you must have this function
        return self.len
train_dataset = Get_Dataset()
train_loader = DataLoader(train_dataset , batch_size = 1 , shuffle = True )