你知道这个问题,有些图片是一个痛苦,你必须调试我猜。每次都试着执行np.stack(),并找出出错的地方。当堆栈不再工作时,将打印所有图像的形状,这可能会让您了解出了什么问题。因为我不能复制它,所以没法再继续了。堆栈将所有图像连接到每个图像的顶部,但每个图像的形状必须相同。
for image_name in train.Id:
img_path = os.path.join(data_dir, 'train', img_name)
img = imageio.imread(img_path)
img = skimage.transform.resize(img, (32, 32), mode='constant')
img = img.astype('float32') # this will help us in later stage
temp.append(img)
try:
train_x = np.stack(temp)
except ValueError:
[print(im.shape) for im in temp]
break
为了让您更好地使用np.stack,请考虑以下代码:
tmp = [np.zeros((400,400,3)), np.zeros((400,400,3))]
print(np.stack(tmp).shape)
>>> (2, 400, 400, 3)
tmp = [np.zeros((400,400,3)), np.zeros((400,400))]
print(np.stack(tmp).shape)
>>> ValueError: all input arrays must have the same shape
在第一个示例中,我有一个列表,其中包含两个具有完全相同形状的三维数组。numpy将它们堆叠在一起并创建一个新维度,通常称为批处理大小。在第二个例子中,我在列表中有一个3d和一个2d数组,当你尝试用不同的形状(400,400,3)和(400,400)堆叠两个数组时,你会得到你提到的valueerror。
我还冒昧地看了一眼
here
在报税表上,您可以看到以下信息:
img_u数组:ndarray
不同的色带/通道存储在三维中,使得灰度图像是mxn、rgb图像mxnx3和rgb a图像mxnx4。
我感觉你的一些图像被读取为mxnx4数组而不是mxnx3
希望我帮了点忙,可以多问我一些。
编辑
for image_name in train.Id:
img_path = os.path.join(data_dir, 'train', img_name)
img = imageio.imread(img_path)
img = skimage.transform.resize(img, (32, 32), mode='constant')
img = img.astype('float32') # this will help us in later stage
if len(img.shape) == 3 and img.shape[-1] == 3 # only append if 3D and last dimension is a 3, standing for RGB
temp.append(img)
train_x = np.stack(temp)