[Error] The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

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

[Solution for busy people]

#Convert behind('RGB)And change the number of channels to 3
input_image = Image.open(filename).convert('RGB')

[Cause] Since the number of channels of the input image is 4, it is necessary to return it to 3.

from torchvision import transforms
    
input_image = Image.open('test.png')  #This is the problem
    
preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
input_tensor = preprocess(input_image)

from torchvision import transforms
    
input_image = Image.open('test.png')  #Corrected here
    
preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
input_tensor = preprocess(input_image)

reference

https://stackoverflow.com/questions/58496858/pytorch-runtimeerror-the-size-of-tensor-a-4-must-match-the-size-of-tensor-b