[エラー] 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
[忙しい人のための解決策]
#後ろにconvert('RGB)をつけてchannels数を3に変更する
input_image = Image.open(filename).convert('RGB')
[原因] 入力された画像のchannel数が4であるために3に戻してあげる必要がある。
#後ろにconvert('RGB)をつけてchannels数を3に変更する
input_image = Image.open(filename).convert('RGB')
<該当コード>
from torchvision import transforms
input_image = Image.open('test.png') #ここが問題
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') #ここを訂正した
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)
参考
Author And Source
この問題について([エラー] The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0), 我々は、より多くの情報をここで見つけました https://qiita.com/white1107/items/548cbfc0e0981b9e51c9著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .