python中卷积函数 python 卷积( 四 )


使用显著图的概念相当直接——计算输出类别相对于输入图像的梯度 。这应该告诉我们输出类别值对于输入图像像素中的微小变化是怎样变化的 。梯度中的所有正值告诉我们,像素的一个小变化会增加输出值 。因此,将这些梯度可视化可以提供一些直观的信息,这种方法突出了对输出贡献最大的显著图像区域 。
class_idx = 0indices = np.where(val_y[:, class_idx] == 1.)[0]
# pick some random input from here.idx = indices[0]
# Lets sanity check the picked image.from matplotlib import pyplot as plt%matplotlib inline
plt.rcParams['figure.figsize'] = (18, 6)plt.imshow(val_x[idx][..., 0])
from vis.visualization import visualize_saliency
from vis.utils import utilsfrom keras import activations# Utility to search for layer index by name.
# Alternatively we can specify this as -1 since it corresponds to the last layer.
layer_idx = utils.find_layer_idx(model, 'preds')
# Swap softmax with linearmodel.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)grads = visualize_saliency(model, layer_idx, filter_indices=class_idx, seed_input=val_x[idx])
# Plot with 'jet' colormap to visualize as a heatmap.plt.imshow(grads, cmap='jet')
# This corresponds to the Dense linear layer.for class_idx in np.arange(10):
indices = np.where(val_y[:, class_idx] == 1.)[0]
idx = indices[0]
f, ax = plt.subplots(1, 4)
ax[0].imshow(val_x[idx][..., 0])
for i, modifier in enumerate([None, 'guided', 'relu']):
grads = visualize_saliency(model, layer_idx, filter_indices=class_idx,
seed_input=val_x[idx], backprop_modifier=modifier)
if modifier is None:
modifier = 'vanilla'
ax[i+1].set_title(modifier)
ax[i+1].imshow(grads, cmap='jet')
类别激活映射(CAM)或grad-CAM是另外一种可视化模型的方法,这种方法使用的不是梯度的输出值 , 而是使用倒数第二个卷积层的输出,这样做是为了利用存储在倒数第二层的空间信息 。
from vis.visualization import visualize_cam
# This corresponds to the Dense linear layer.for class_idx in np.arange(10):
indices = np.where(val_y[:, class_idx] == 1.)[0]
idx = indices[0]f, ax = plt.subplots(1, 4)
ax[0].imshow(val_x[idx][..., 0])
for i, modifier in enumerate([None, 'guided', 'relu']):
grads = visualize_cam(model, layer_idx, filter_indices=class_idx,
seed_input=val_x[idx], backprop_modifier=modifier)
if modifier is None:
modifier = 'vanilla'
ax[i+1].set_title(modifier)
ax[i+1].imshow(grads, cmap='jet')
本文简单说明了CNN模型可视化的重要性,以及介绍了一些可视化CNN网络模型的方法,希望对读者有所帮助,使其能够在后续深度学习应用中构建更好的模型 。免费视频教程:
2021-02-08 Python OpenCV GaussianBlur()函数borderType= None)函数
此函数利用高斯滤波器平滑一张图像 。该函数将源图像与指定的高斯核进行卷积 。
src:输入图像
ksize:(核的宽度,核的高度),输入高斯核的尺寸,核的宽高都必须是正奇数 。否则,将会从参数sigma中计算得到 。
dst:输出图像,尺寸与输入图像一致 。
sigmaX:高斯核在X方向上的标准差 。
sigmaY:高斯核在Y方向上的标准差 。默认为None,如果sigmaY=0,则它将被设置为与sigmaX相等的值 。如果这两者都为0,则它们的值会从ksize中计算得到 。计算公式为:
borderType:像素外推法,默认为None(参考官方文档 BorderTypes
)
在图像处理中,高斯滤波主要有两种方式:
1.窗口滑动卷积
2.傅里叶变换
在此主要利用窗口滑动卷积 。其中二维高斯函数公式为:
根据上述公式 , 生成一个3x3的高斯核,其中最重要的参数就是标准差,标准差越大,核中心的值与周围的值差距越小 , 曲线越平滑 。标准差越?。酥行牡闹涤胫芪У闹挡罹嘣酱?,曲线越陡峭 。

推荐阅读