Display/Save 具有多个子图的多个图形(matplotlib / python)
Display/Save multiple figures with multiple subplots (matplotlib / python)
我正在预处理一些图像(裁剪后的图像,运行 通过网络等),然后将它们存储在单元中。然后我从这些单位绘制一个带有多个子图的图形,显示图像在特定卷积层的激活。
我正在努力实现的是,给定一组以上的图像,每幅图像的数字将显示或保存到目录中(如果这样更容易处理jupyter) 以及每个数字的子图。
单张图片的单位变成一个单位:
[[0.0000000e+00 0.0000000e+00 0.0000000e+00 ... 3.3075356e-01
0.0000000e+00 0.0000000e+00]
[0.0000000e+00 1.4396116e-01 0.0000000e+00 ... 0.0000000e+00
0.0000000e+00 0.0000000e+00]
[0.0000000e+00 5.4249477e-01 1.9857159e-01 ... 0.0000000e+00
1.5366032e+00 1.0890217e+00]
...
[7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
5.4128194e-01 4.9922270e-01]
[7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
5.4128194e-01 4.9922270e-01]
[7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
5.4128194e-01 4.9922270e-01]]
函数:
def getActivations(layer,stimuli):
with tf.Session(graph=graph) as sess:
#print (stimuli)
#im=stimuli
im=np.reshape(stimuli,[-1,224,224],order='F')#stimuli
im=np.expand_dims(im,axis=0)
#im=np.reshape(im,[-1,224,224],order='F')#stimuli
#plt.imshow(im,interpolation="nearest", cmap="gray")
#print (im)
#for im in stimuli:
#batch = np.array([im for i in range(1)])
x = graph.get_tensor_by_name('prefix/data:0')
#x2 = tf.reshape(x,[-1,224,224])
y=graph.get_tensor_by_name(layer)
units = sess.run(y,feed_dict={x: np.swapaxes(im,1,3)})#np.reshape(stimuli,[-1,224,224],order='F'),keep_prob:1.0})
#print (units)
plotNNFilter(units)
def plotNNFilter(units):
#for a in units:
#print ("###############################################################")
#print (units)
filters = units.shape[3]
#print ("###############################################################")
#print (filters)
plt.figure(1,figsize=(20,20))
n_columns = 6
n_rows = math.ceil(filters / n_columns) + 1
for i in range (filters):
#plt.subplot(n_rows,n_columns, i+1)
plt.subplot(n_rows,n_columns, i+1)
plt.title('Filter' + str(i))
plt.imshow(units[0,:,:,i],interpolation="nearest",cmap="gray")
我收到这个折旧错误:
MatplotlibDeprecationWarning: Adding an axes using the same arguments
as a previous axes currently reuses the earlier instance. In a future
version, a new instance will always be created and returned.
Meanwhile, this warning can be suppressed, and the future behavior
ensured, by passing a unique label to each axes instance.
warnings.warn(message, mplDeprecation, stacklevel=1)
我看到另一个问题有同样的错误警告:
但我认为答案不适用于我想要实现的目标?
Due to having less than 10 reputation i can not attach an image. The image has 64 subplots, as many as the filters
打印 len(units) 以便我可以使用该数字并将其用作迭代来执行 plt.figure(i,...) 为每个单独的单元打印 1。
您可以先使用 fig, axes = plt.subplots(numrows, numcols)
创建子图。 axes
将是一个子图数组,您可以对其进行迭代并绘制您喜欢的任何内容。
注意:行数和列数必须为整数
filters = units.shape[3]
n_columns = 6
n_rows = int(math.ceil(filters / n_columns) + 1)
fig, axes = plt.subplots(n_rows, n_columns, figsize=(20, 20))
for i, ax in enumerate(axes.flatten()):
if i>=filters:
ax.remove()
else:
ax.set_title('Filter' + str(i))
ax.imshow(units[0, :, :, i], interpolation="nearest", cmap="gray")
axes
是一个子图数组,因此要遍历它们,我们需要展平这个数组。然后我们遍历它们,将 ax
分配给每个子图,而 i
本质上是一个计数器。因为不是所有的子图都被使用(最后两个将是空的)我检查 i
是否大于或等于图像的数量 if i>=filters:
如果是的话我删除那些子图。如果不是真的,我们继续绘制图像。
我正在预处理一些图像(裁剪后的图像,运行 通过网络等),然后将它们存储在单元中。然后我从这些单位绘制一个带有多个子图的图形,显示图像在特定卷积层的激活。
我正在努力实现的是,给定一组以上的图像,每幅图像的数字将显示或保存到目录中(如果这样更容易处理jupyter) 以及每个数字的子图。
单张图片的单位变成一个单位:
[[0.0000000e+00 0.0000000e+00 0.0000000e+00 ... 3.3075356e-01
0.0000000e+00 0.0000000e+00]
[0.0000000e+00 1.4396116e-01 0.0000000e+00 ... 0.0000000e+00
0.0000000e+00 0.0000000e+00]
[0.0000000e+00 5.4249477e-01 1.9857159e-01 ... 0.0000000e+00
1.5366032e+00 1.0890217e+00]
...
[7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
5.4128194e-01 4.9922270e-01]
[7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
5.4128194e-01 4.9922270e-01]
[7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
5.4128194e-01 4.9922270e-01]]
函数:
def getActivations(layer,stimuli):
with tf.Session(graph=graph) as sess:
#print (stimuli)
#im=stimuli
im=np.reshape(stimuli,[-1,224,224],order='F')#stimuli
im=np.expand_dims(im,axis=0)
#im=np.reshape(im,[-1,224,224],order='F')#stimuli
#plt.imshow(im,interpolation="nearest", cmap="gray")
#print (im)
#for im in stimuli:
#batch = np.array([im for i in range(1)])
x = graph.get_tensor_by_name('prefix/data:0')
#x2 = tf.reshape(x,[-1,224,224])
y=graph.get_tensor_by_name(layer)
units = sess.run(y,feed_dict={x: np.swapaxes(im,1,3)})#np.reshape(stimuli,[-1,224,224],order='F'),keep_prob:1.0})
#print (units)
plotNNFilter(units)
def plotNNFilter(units):
#for a in units:
#print ("###############################################################")
#print (units)
filters = units.shape[3]
#print ("###############################################################")
#print (filters)
plt.figure(1,figsize=(20,20))
n_columns = 6
n_rows = math.ceil(filters / n_columns) + 1
for i in range (filters):
#plt.subplot(n_rows,n_columns, i+1)
plt.subplot(n_rows,n_columns, i+1)
plt.title('Filter' + str(i))
plt.imshow(units[0,:,:,i],interpolation="nearest",cmap="gray")
我收到这个折旧错误:
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. warnings.warn(message, mplDeprecation, stacklevel=1)
我看到另一个问题有同样的错误警告:
但我认为答案不适用于我想要实现的目标? Due to having less than 10 reputation i can not attach an image. The image has 64 subplots, as many as the filters
打印 len(units) 以便我可以使用该数字并将其用作迭代来执行 plt.figure(i,...) 为每个单独的单元打印 1。
您可以先使用 fig, axes = plt.subplots(numrows, numcols)
创建子图。 axes
将是一个子图数组,您可以对其进行迭代并绘制您喜欢的任何内容。
注意:行数和列数必须为整数
filters = units.shape[3]
n_columns = 6
n_rows = int(math.ceil(filters / n_columns) + 1)
fig, axes = plt.subplots(n_rows, n_columns, figsize=(20, 20))
for i, ax in enumerate(axes.flatten()):
if i>=filters:
ax.remove()
else:
ax.set_title('Filter' + str(i))
ax.imshow(units[0, :, :, i], interpolation="nearest", cmap="gray")
axes
是一个子图数组,因此要遍历它们,我们需要展平这个数组。然后我们遍历它们,将 ax
分配给每个子图,而 i
本质上是一个计数器。因为不是所有的子图都被使用(最后两个将是空的)我检查 i
是否大于或等于图像的数量 if i>=filters:
如果是的话我删除那些子图。如果不是真的,我们继续绘制图像。