pytorch random_split()无法在Google Colab上运行

62o28rlo  于 2022-11-09  发布在  Go
关注(0)|答案(1)|浏览(171)

我试图执行我的自定义数据加载器的random_split(),但它一直说我已经给了它一个 Torch 。_C.生成器,即使我没有传递任何生成器。
下面显示了我使用的类的代码:

class CustomDataset(Dataset):
    def __init__(self, filepath):
        self.imgs_path = filepath
        file_list = sorted(os.listdir(self.imgs_path))
        #print(file_list)
        self.data = []
        for group in file_list:
            number_group = group.split('/')[-1]
            for classes in sorted(os.listdir(self.imgs_path + number_group + '/')):
                class_name = classes.split('/')[-1]
                for img_name in sorted(os.listdir(self.imgs_path + number_group + '/' + class_name + '/')):
                    self.data.append([img_name, class_name, number_group])
        print(self.data[0])
        self.class_map = {file_list[0]: 0,
                          file_list[1]: 1,
                          file_list[2]: 2,
                          file_list[3]: 3,
                          file_list[4]: 4,
                          file_list[5]: 5,
                          file_list[6]: 6,
                          file_list[7]: 7,
                          file_list[8]: 8,
                          file_list[9]: 9}
        #print(self.class_map)
        self.img_dim = (227, 227)
    def __len__(self):
        return len(self.data)
    def __getitem__(self, idx):
        img_name, class_name, number_group = self.data[idx]
        input_image = Image.open(self.imgs_path + number_group + "/" + class_name + "/" + img_name)
        input_image = input_image.convert('RGB')
        class_id = self.class_map[class_name]
        class_id = np.asarray(class_id).squeeze()
        class_id = torch.from_numpy(class_id)

        # Preprocess Data
        preprocess = transforms.Compose([
            transforms.Resize(self.img_dim),
            transforms.ToTensor(),
            #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
            transforms.Normalize(mean=[0.4914, 0.4822, 0.4465],std=[0.2023, 0.1994, 0.2010]),
        ])
        input_tensor = preprocess(input_image)
        print(class_id)
        return input_tensor, class_id

下面是random_split()函数的代码:

dataset = CustomDataset('/content/drive/MyDrive/ColabNotebooks/leapGestRecog/')
train_size = int(0.8*len(dataset))
validation_test_size = len(dataset)-train_size
print("train_size: ", train_size)
print("validation_test_size: ", validation_test_size)
train_dataset, validation_dataset, test_dataset = random_split(dataset,[train_size,validation_test_size/2,validation_test_size/2])

最后,以下是包含错误消息的输出消息:

['frame_00_01_0001.png', '01_palm', '00']
train_size:  16000
validation_test_size:  4000
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-71dee9977e06> in <module>
      4 print("train_size: ", train_size)
      5 print("validation_test_size: ", validation_test_size)
----> 6 train_dataset, validation_dataset, test_dataset = random_split(dataset,[train_size,validation_test_size/2,validation_test_size/2])

/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataset.py in random_split(dataset, lengths, generator)
    311         raise ValueError("Sum of input lengths does not equal the length of the input dataset!")
    312 
--> 313     indices = randperm(sum(lengths)).tolist()
    314     return [Subset(dataset, indices[offset - length : offset]) for offset, length in zip(_accumulate(lengths), lengths)]

TypeError: randperm() received an invalid combination of arguments - got (float, generator=torch._C.Generator), but expected one of:
 * (int n, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 * (int n, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

torch._C.Generator到底是什么,我该如何摆脱它?

vc6uscn9

vc6uscn91#

请尝试更新Google Colab上的PyTorch版本,因为这可能是版本兼容性问题

相关问题