會產生這一個錯誤,原因是圖片的格式造成,是彩色還是灰階(黑白)圖片。
Let me clarify, if the img has three channels, you should have three number for mean, for example, img is RGB, mean is [0.5, 0.5, 0.5], the normalize result is R * 0.5, G * 0.5, B * 0.5. If img is grey type that only one channel, so mean should be [0.5], the normalize result is R * 0.5
資料來源:
https://github.com/yunjey/pytorch-tutorial/issues/161
解法很多,需要配合程式變成彩色,或修改程式的流程。如果是在python 裡使用 PIL 讀取圖片,轉換方式如下:
img = Image.open(GIF_FILENAME)
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)
rgbimg.save('foo.jpg')