2

我有一种情况,我需要将 ImageFolder 与albumentations lib 一起使用以在 pytorch 中进行扩充 - 自定义数据加载器不是一个选项。

为此,我被难住了,我无法让 ImageFolder 与albumenations一起工作。我已经尝试过这些方面的东西:

class Transforms:
    def __init__(self, transforms: A.Compose):
        self.transforms = transforms

    def __call__(self, img, *args, **kwargs):
        return self.transforms(image=np.array(img))['image']

接着:

    trainset = datasets.ImageFolder(traindir,transform=Transforms(transforms=A.Resize(32 , 32)))

traindir一些带有图像的目录在哪里。然而,我得到了一个奇怪的错误:

RuntimeError: Given groups=1, weight of size [16, 3, 3, 3], expected input[1024, 32, 32, 3] to have 3 channels, but got 32 channels instead

而且我似乎无法找到一个可重现的示例来使简单的 aug pipleline 与 imagefolder 一起工作。

更新 根据@Shai 的建议,我现在已经这样做了:

class Transforms:
    def __init__(self):
        self.transforms = A.Compose([A.Resize(224,224),ToTensorV2()])

    def __call__(self, img, *args, **kwargs):
        return self.transforms(image=np.array(img))['image']
trainset = datasets.ImageFolder(traindir,transform=Transforms())

但我被抛出:

    self.padding, self.dilation, self.groups)
RuntimeError: Input type (torch.cuda.ByteTensor) and weight type (torch.cuda.FloatTensor) should be the same
4

1 回答 1

2

您需要使用ToTensorV2转换作为最后一个:

trainset = datasets.ImageFolder(traindir,transform=Transforms(transforms=A.Compose([A.Resize(32 , 32), ToTensorV2()]))
于 2021-09-12T12:08:43.747 回答