np.stack文档解释:
Join a sequence of arrays along a new axis.
The
axisparameter specifies the index of the new axis in the dimensions of the result. For example, ifaxis=0it will be the first dimension and ifaxis=-1it will be the last dimension.
产生一个给定axis的新轴 ,沿着该轴堆砌数组
下面是代码测试:
a = np.arange(8).reshape((2,2,2))
array([[[0, 1],[2, 3]],[[4, 5],[6, 7]]])
b = np.arange(8).reshape((2,2,2))+1
array([[[1, 2],[3, 4]],[[5, 6],[7, 8]]])
第一个2是2个样本,后面2*2是像素矩阵,要将a,b变成2*2*2*2,第4个2表示颜色通道:
c = np.stack((a,b),axis=3)
array([[[[0, 1],[1, 2]],[[2, 3],[3, 4]]],[[[4, 5],[5, 6]],[[6, 7],[7, 8]]]])
验证,看c[:,:,:,0]:
c[:,:,:,0]
array([[[0, 1],[2, 3]],[[4, 5],[6, 7]]])
也可变成第2个2是颜色通道:
c = np.stack((a,b),axis=1)
array([[[[0, 1],[2, 3]],[[1, 2],[3, 4]]],[[[4, 5],[6, 7]],[[5, 6],[7, 8]]]])c[:,1,:,:]
array([[[1, 2],[3, 4]],[[5, 6],[7, 8]]])c[:,0,:,:]
array([[[0, 1],[2, 3]],[[4, 5],[6, 7]]])