索引1超出了轴3的大小为1的范围

时间:2018-01-21 15:56:50

标签: scikit-learn deep-learning keras theano spyder

output_layer = model.layers[4].output 
output_fn = K.function([model.layers[0].input], [output_layer])
#_____________________________________________________________________________________________________________________

input_image= X_train[0:1,:,:,:]
print(input_image.shape)

plt.imshow(input_image[0,0,:,:], cmap='gray')
plt.imshow(input_image[0,0,:,:])

output_image = output_fn([input_image])
output_image = np.array([output_image])
print([output_image.shape])

# Rearrnge dimension so we can plot the result as RGB images
output_image = np.rollaxis(np.rollaxis(output_image , 3 , 1) , 3 , 1)
print(output_image.shape)

fig = plt.figure(figsize=(8,8))
for i in range(32):
    ax = fig.add_subplot(6, 6, i+1)
    ax.imshow(output_image[0,:,:,i],interpolation='nearest') 
    ax.imshow(output_image[0,:,:,i],cmap=matplotlib.cm.gray)
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))
    plt.tight_layout()

嗨,自己的卷积网络没有问题。但For循环收到此错误:

IndexError: index 1 is out of bounds for axis 3 with size 1

有人帮助我吗?所以thnx。

1 个答案:

答案 0 :(得分:0)

从错误来看,它似乎来自ax.imshow()。检查图像的尺寸排序,您可能试图在尺寸为1的图像的channels尺寸上循环。然后正确的线将是

ax.imshow(output_image[i,:,:,0],interpolation='nearest') 
ax.imshow(output_image[i,:,:,0],cmap=matplotlib.cm.gray)