wondrive / errors

solutions for errors i've met
0 stars 0 forks source link

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

Open wondrive opened 5 days ago

wondrive commented 5 days ago

Error

preprocess = transforms.Compose([
    transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )])

img = Image.open("bobby.jpg")
img_t = preprocess(img)

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

Solution

사용한 이미지가 png여서 발생한 에러. (png 이미지의 확장자를 jpg로 수정했는데 여전히 채널이 4개로 남아있는 상태였음.)
따라서 4개의 채널(R, G, B, transparent) 중 마지막 채널을 삭제하여 사용.

# example
img = np.array(img)[..., :3]       # to array
img = Image.fromarray(img)   # to PIL.Image
wondrive commented 5 days ago

Appendix

참고로 채널별로 시각화 해보면 마지막 채널은 아무것도 없다

red_channel = np.array(img)[..., 0]
green_channel = np.array(img)[..., 1]
blue_channel = np.array(img)[..., 2]
last_channel = np.array(img)[..., 3]

red_img   = Image.fromarray(red_channel)
green_img = Image.fromarray(green_channel)
blue_img  = Image.fromarray(blue_channel)
last_img  = Image.fromarray(last_channel)

import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 4, figsize=(12, 4))
axes[0].imshow(red_img)
axes[1].imshow(green_img)
axes[2].imshow(blue_img)
axes[3].imshow(last_img)

image