出错的代码:
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
# img.shape: (112,112,1) #我的数据集是灰度图
报错:
KeyError: ((1, 1, 1), '|u1')
During handling of the above exception, another exception occurred:
TypeError: Cannot handle this data type
解决办法:问题是数据的形状。枕头的fromarray
功能只能是MxNx3阵列(RGB图像)或MxN阵列(灰度)。要使灰度图像工作,必须将MxNx1数组转换为MxN数组。使数据变平,然后将其放入不同的阵列形状。
在该代码修改为:
img = np.squeeze(img) # img.shape变为:(112,112)
img = Image.fromarray(img)
问题解决。