Data science/각종 에러

RuntimeError: stack expects each tensor to be equal size, but got [3, 224, 224] at entry 0

yunnaa 2023. 4. 8. 12:55

RuntimeError: stack expects each tensor to be equal size, but got [3, 224, 224] at entry 0 and [3, 225, 224] at entry 13

 

▶ 해결 방법 : 원래 코드 transforms.Resize(224) → transforms.Resize((224, 224))

train_dataset = datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.Resize(224),                  # 224 -> (224,224)
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]))

아래 링크를 보면 이미지의 height and width가 다르면 이미지 사이즈가 달라질 수 있으므로 tuple 형식으로 image size를 지정해주면 된다고 한다.

 

 

 

RuntimeError: stack expects each tensor to be equal size, but got [3, 224, 224] at entry 0 and [3, 224, 336] at entry 3

Im trying to implement pretrained resnet50 on a image classification task with 42 labels and received this error. I dont understand what caused the layer size to change. Below is my code, i stitched them up from different tutorials I can find. import torch

discuss.pytorch.org