我正在尝试提出一种编程方式来为卷积神经网络(CNN)生成视觉效果。与此类似:
这涉及到很多Axes3D和SubPlots的玩法。
假设我有一些随机生成的尺寸为128x128的输入图像。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
import matplotlib.pyplot as plt
data = np.random.randint(255,size=(128,128))
然后,我试图将正在“喂食”的数据绘制到CNN中。我创建了一个图形并使用GridSpec设置了子图布置。
fig = plt.figure(figsize=(6,3))
gs = gridspec.GridSpec(nrows=1, ncols=2) # no spacing between subplots
gs.update(wspace=0, hspace=0)
这是第一个子图:
## INPUT IMAGE
ax = plt.subplot(gs[0], projection='3d')
xx, yy = np.meshgrid(np.linspace(-1,1,data.shape[0]), np.linspace(1,-1,data.shape[1]))
X = 0 * xx
Y = xx
Z = yy
ax.set_facecolor('#AAAAFF')
# ax.set_axis_off()
ax.plot_surface(X, Y, Z, rstride=10, cstride=10, facecolors=plt.cm.Greys_r(data), shade=False)
ax.view_init(20, -60) # adjust rotation
ax.set_xlim(np.array([-0.8,0.8]))
ax.set_ylim(np.array([-0.8,0.8]))
ax.set_zlim(np.array([-0.8,0.8]))
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
然后是第二个子图:
## CONV LAYER 1
ax = plt.subplot(gs[1], projection='3d')
ax.set_facecolor('#AAFFAA')
# ax.set_axis_off()
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
ax.view_init(20, -60) # adjust rotation
ax.set_ylim(np.array([-0.8,0.8]))
ax.set_ylim(np.array([-0.8,0.8]))
ax.set_zlim(np.array([-0.8,0.8]))
num_channels = 64; channels = np.linspace(-1, 1, int(np.sqrt(num_channels)))
layer_size = 1
# vertices for each "channel" of the CNN layer
v = np.array([
[a,b,c] for a in channels for b in [-layer_size/2, layer_size/2] for c in [-layer_size/2, layer_size/2]
])
for k in range(int(np.sqrt(num_channels))):
verts = [[v[x] for x in [4*k+0,4*k+1,4*k+3,4*k+2]] ]
face = Poly3DCollection(verts, linewidths=1, edgecolors='w')
#face.set_alpha(1/(k+1))
face.set_facecolor('#ff99cc')
ax.add_collection3d(face)
最后,我绘制结果:
plt.show()
然后我得到这个:
我通过使用偏蓝色和偏绿色的背景并显示了轴说明了我的问题。我真的很想减少这些3D图所占用的空间,以便看起来像这样:
有人知道这是否可能吗?我如何植入子图以获取尽可能少的填充?在Matplotlib中甚至可以有宽度不相等的子图吗?
首先感谢尝试此操作的任何人。