【Clipping input data to the .... for integers).】的解決辦法:
參考資料:https://www.freesion.com/article/7534157435/
在用matplotlib.pyplot顯示輸入圖像時,一直報警告錯誤“Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).”,但圖像顯示結果是正常的。
檢索了一些資料,有博主報錯的原因是用float32的圖像佔位符直接顯示了uint8格式的圖像,添加了“image = image/255”後停止報錯,但我添加後沒有效果。
出現這個錯誤的根本原因是要顯示的圖像像素值超出了inshow()函數的有效範圍【integers:[0,255];floats:[0,1]】,可以先print出現問題的圖像找出問題的根本原因,我的圖像打印爲:
tensor([[[-0.7804, -0.7804, -0.7882, ..., 0.2706, 0.2549, 0.3020],
[-0.7725, -0.7725, -0.7725, ..., 0.2863, 0.2941, 0.2941],
[-0.7725, -0.7725, -0.7647, ..., 0.2235, 0.2706, 0.3098],
...,
[ 0.2627, 0.2000, 0.1922, ..., 0.2784, 0.2157, 0.2392],
[ 0.3569, 0.3176, 0.3098, ..., 0.2706, 0.1922, 0.2706],
[ 0.3490, 0.3020, 0.3725, ..., 0.2941, 0.2157, 0.2627]],
[[-0.7804, -0.7804, -0.7882, ..., 0.2706, 0.2549, 0.3020],
[-0.7725, -0.7725, -0.7725, ..., 0.2863, 0.2941, 0.2941],
[-0.7725, -0.7725, -0.7647, ..., 0.2235, 0.2706, 0.3098],
...,
[ 0.2627, 0.2000, 0.1922, ..., 0.2784, 0.2157, 0.2392],
[ 0.3569, 0.3176, 0.3098, ..., 0.2706, 0.1922, 0.2706],
[ 0.3490, 0.3020, 0.3725, ..., 0.2941, 0.2157, 0.2627]],
[[-0.7804, -0.7804, -0.7882, ..., 0.2706, 0.2549, 0.3020],
[-0.7725, -0.7725, -0.7725, ..., 0.2863, 0.2941, 0.2941],
[-0.7725, -0.7725, -0.7647, ..., 0.2235, 0.2706, 0.3098],
...,
[ 0.2627, 0.2000, 0.1922, ..., 0.2784, 0.2157, 0.2392],
[ 0.3569, 0.3176, 0.3098, ..., 0.2706, 0.1922, 0.2706],
[ 0.3490, 0.3020, 0.3725, ..., 0.2941, 0.2157, 0.2627]]])
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
tensor([[[ 0.3647, 0.3647, 0.3882, ..., 0.3255, 0.3176, 0.2941],
[ 0.3647, 0.3804, 0.4039, ..., 0.3176, 0.3569, 0.3647],
[ 0.3490, 0.3412, 0.3725, ..., 0.2784, 0.3255, 0.3333],
...,
[ 0.2941, 0.3255, 0.2863, ..., -0.1137, -0.5843, -0.6784],
[ 0.2784, 0.2784, 0.2941, ..., -0.1922, -0.6235, -0.6706],
[ 0.2706, 0.2784, 0.2314, ..., -0.3412, -0.6392, -0.6784]],
[[ 0.3647, 0.3647, 0.3882, ..., 0.3255, 0.3176, 0.2941],
[ 0.3647, 0.3804, 0.4039, ..., 0.3176, 0.3569, 0.3647],
[ 0.3490, 0.3412, 0.3725, ..., 0.2784, 0.3255, 0.3333],
...,
[ 0.2941, 0.3255, 0.2863, ..., -0.1137, -0.5843, -0.6784],
[ 0.2784, 0.2784, 0.2941, ..., -0.1922, -0.6235, -0.6706],
[ 0.2706, 0.2784, 0.2314, ..., -0.3412, -0.6392, -0.6784]],
[[ 0.3647, 0.3647, 0.3882, ..., 0.3255, 0.3176, 0.2941],
[ 0.3647, 0.3804, 0.4039, ..., 0.3176, 0.3569, 0.3647],
[ 0.3490, 0.3412, 0.3725, ..., 0.2784, 0.3255, 0.3333],
...,
[ 0.2941, 0.3255, 0.2863, ..., -0.1137, -0.5843, -0.6784],
[ 0.2784, 0.2784, 0.2941, ..., -0.1922, -0.6235, -0.6706],
[ 0.2706, 0.2784, 0.2314, ..., -0.3412, -0.6392, -0.6784]]])
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
可見我的程序報出警告的原因是待顯示圖像的像素值中出現了負值,不符合imshow()函數的要求,添加了“img[img < 0] = 0”一句將小於0的部分置0後停止報錯。如果造成警告的是其他問題可以根據輸出的圖像情況修改,進行歸一化/剪裁等對應操作。
img = x[0]
img[img < 0] = 0
img = img.numpy() # FloatTensor轉爲ndarray
img = np.transpose(img, (1, 2, 0)) # 把channel那一維放到最後
plt.imshow(img)