【发布时间】:2018-11-13 16:30:24
【问题描述】:
我正在使用来自here 的代码可视化我的 cnn 模型过滤器(内核),如下所示:
from mpl_toolkits.axes_grid1 import make_axes_locatable
def nice_imshow(ax, data, vmin=None, vmax=None, cmap=None):
"""Wrapper around pl.imshow"""
if cmap is None:
cmap = cm.jet
if vmin is None:
vmin = data.min()
if vmax is None:
vmax = data.max()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
im = ax.imshow(data, vmin=vmin, vmax=vmax, interpolation='nearest', cmap=cmap)
pl.colorbar(im, cax=cax)
# pl.savefig("featuremaps--{}".format(layer_num) + '.jpg')
import numpy.ma as ma
def make_mosaic(imgs, nrows, ncols, border=1):
"""
Given a set of images with all the same shape, makes a
mosaic with nrows and ncols
"""
nimgs = imgs.shape[0]
imshape = imgs.shape[1:]
mosaic = ma.masked_all((nrows * imshape[0] + (nrows - 1) * border,
ncols * imshape[1] + (ncols - 1) * border),
dtype=np.float32)
paddedh = imshape[0] + border
paddedw = imshape[1] + border
for i in range(nimgs):
row = int(np.floor(i / ncols))
col = i % ncols
mosaic[row * paddedh:row * paddedh + imshape[0],
col * paddedw:col * paddedw + imshape[1]] = imgs[i]
return mosaic
# Visualize weights
W=model.layers[8].get_weights()[0][:,:,0,:]
W=np.swapaxes(W,0,2)
W = np.squeeze(W)
print("W shape : ", W.shape)
pl.figure(figsize=(15, 15))
pl.title('conv1 weights')
nice_imshow(pl.gca(), make_mosaic(W, 8, 8), cmap=cm.binary)
我想保存过滤器图像。通常我们使用fig.savefig("featuremaps-kernel-{}".format(layer_num) + '.jpg') 来保存数字。但在这种情况下它不起作用,可能是因为 nice_ 函数。请帮助我使用命令而不是手动编写什么命令来保存图形。因为如果有大型网络,就会有很多人工工作。
【问题讨论】:
-
savefig怎么不起作用?你得到空图像吗?错误?尝试使用像mpl.use('Agg')这样的无头后端和/或使用plt.ioff()禁用交互模式 -
我尝试了你提到的所有选项,仍然得到空图像。
-
W的形状是什么? -
有 32 个大小为 (2x2) 的过滤器
-
mpl.use('Agg')为我工作
标签: python numpy matplotlib keras