AI

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

전자둥이 2021. 11. 9. 11:05
반응형

안녕하세요

 

Pytorch를 사용하여 학습을 진행하는 과정에서 DataLoader를 진행하는 과정에서 다음과 같은 에러가 발생하는걸 종종 확인 할 수 있습니다.

저의 케이스에서는 학습 시킬 이미지 중에 채널이 3개만 있는 이미지 뿐만 아니라 채널이 4개가 있는 이미지가 포함되어 있을 때 발생합니다.

왜냐하면 저의 경우 normalize를 ((0.5,0.5,0.5),(0.5,0.5,0.5)) 3개의 채널에 대해서만 진행을 하기 때문입니다.

따라서 이 에러를 해결하기 위해서는 Dataset 클래스에서 이미지를 불러올 다음과 같이 수정했습니다.

image = Image.open(self.img_list[idx]) 
-> image = Image.open(self.img_list[idx]).convert('RGB')

 

    def __getitem__(self, idx):
        """
        to get 1 specific sample from a dataset
        """
        image = Image.open(self.img_list[idx]).convert('RGB')
        if self.transforms is not None:
            image = self.transforms(image)
        
        return image, self.label_list[idx]

긴글 읽어주셔서 감사합니다~

 

반응형