关于人工智能:可视化CNN和特征图

11次阅读

共计 2965 个字符,预计需要花费 8 分钟才能阅读完成。

卷积神经网络 (cnn) 是一种神经网络,通常用于图像分类、指标检测和其余计算机视觉工作。CNN 的要害组件之一是特色图,它是通过对图像利用卷积滤波器生成的输出图像的示意。

了解卷积层

1、卷积操作

卷积的概念是 CNN 操作的外围。卷积是一种数学运算,它把两个函数联合起来产生第三个函数。在 cnn 的上下文中,这两个函数是输出图像和滤波器,而失去的后果就是特色图。

2、卷积的层

卷积层包含在输出图像上滑动滤波器,并计算滤波器与输出图像的相应补丁之间的点积。而后将后果输入值存储在特色映射中的相应地位。通过利用多个过滤器,每个过滤器检测一个不同的特色,咱们能够生成多个特色映射。

3、重要参数

Stride: Stride 是指卷积滤波器在卷积运算过程中在输出数据上挪动的步长。

Padding:Padding 是指在利用卷积操作之前在输出图像或特色映射的边界四周增加额定像素。

Padding 的目标是管制输入特色图的大小,保障滤波窗口可能笼罩输出图像或特色图的边缘。如果没有填充,过滤器窗口将无奈笼罩输出数据的边缘,导致输入特色映射的大小减小和信息失落。有两种类型的填充“valid”和“same”。

kernel/filter :kernel(也称为 filter 或 weight)是一个可学习参数的小矩阵,用于从输出数据中提取特色。

在下图中,输出图像的大小为(5,5),过滤器 filter 的大小为(3,3),绿色为输出图像,黄色区域为该图像的过滤器。在输出图像上滑动滤波器,计算滤波器与输出图像的相应像素之间的点积。Padding 是 valid (也就是没有填充)。stride 值为 1。

4、特色图:

特色图是卷积神经网络 (CNN) 中卷积层的输入。它们是二维数组,蕴含卷积滤波器从输出图像或信号中提取的特色。

卷积层中特色图的数量对应于该层中应用的过滤器的数量。每个过滤器通过对输出数据利用卷积操作来生成单个特色映射。

特色图的大小取决于输出数据的大小,卷积操作中应用的过滤器、填充和步幅的大小。通常,随着咱们深刻网络,特色图的大小会减小,而特色图的数量会减少。特色图的大小能够用以下公式计算:

 Output_Size = (Input_Size  -  Filter_Size + 2 * Padding) / Stride + 1

这个公式十分重要,因为在计算输入时必定会用到,所以肯定要记住

来自一个卷积层的特色映射作为网络中下一层的输出数据。随着层数的减少,网络可能学习越来越简单和形象的特色。通过联合来自多层的特色,网络能够辨认输出数据中的简单模式,并做出精确的预测。

特色图可视化

这里咱们应用 TF 作为框架进行演示

 ## Importing libraries
 # Image processing library
 importcv2
 # Keras from tensorflow
 importkeras
 # In Keras, the layers module provides a set of pre-built layer classes that can be used to construct neural networks.
 fromkerasimportlayers
 # For ploting graphs and images
 importmatplotlib.pyplotasplt
 importnumpyasnp

应用 OpenCV 导入一张图像,并将其大小调整为 224 x 224 像素。

 img_size = (224, 224)
 file_name = "./data/archive/flowers/iris/10802001213_7687db7f0c_c.jpg"
 img = cv2.imread(file_name)      # reading the image
 img = cv2.resize(img, img_size)

咱们增加 2 个卷积层:

 model = keras.Sequential()
 filters = 16
 model.add(layers.Conv2D(input_shape = (224, 224, 3),filters = filters, kernel_size= 3))
 model.add(layers.Conv2D(filters = filters, kernel_size= 3))

从卷积层中获取过滤器。

 filters, bias = model.layers[0].get_weights()
 min_filter = filters.min()
 max_filter = filters.max()
 filters = (filters - min_filter) / (max_filter - min_filter)p

可视化

 figure = plt.figure(figsize= (10, 20))
 filters_count = filters.shape[-1]
 channels = filters.shape[0]
 index = 1
 for channel in range(channels):
     for filter in range(filters_count):
         plt.subplot(filters_count, channels, index)
         plt.imshow(filters[channel, :, :, filter])
         plt.xticks([])
         plt.yticks([])
         index+=1
 plt.show()

将图像输出到模型中失去特色图

 normalized_img = (img - img.min()) / (img.max() - img.min())
 normalized_img =  normalized_img.reshape(-1, 224, 224, 3)
 feature_map = model.predict(normalized_img)

特色图须要进行归一化这样才能够在 matplotlib 中显示

 feature_map = (feature_map - feature_map.min())/ (feature_map.max() - feature_map.min())

提取特色图并显示

 total_imgs = feature_map.shape[0]
 no_features = feature_map.shape[-1]
 fig = plt.figure(figsize=(10, 50))
 index = 1
 
 for image_no in range(total_imgs):
     for feature in range(no_features):
         # plotting for 16 filters that produced 16 feature maps
         plt.subplot(no_features, 3, index)
         plt.imshow(feature_map[image_no, :, :, feature], cmap="gray")
         plt.xticks([])
         plt.yticks([])
         index+=1
 plt.show()

总结

通过可视化 CNN 不同层的特色图,能够更好地了解网络在解决图像时“看到”的是什么。例如,第一层可能会学习简略的特色,如边缘和角落,而前面的层可能会学习更形象的特色,如特定物体的存在。通过查看特色图,咱们还能够辨认图像中对网络决策过程重要的区域。

https://avoid.overfit.cn/post/1132e4f9872a490e95bcbd0477d38426

作者:Ahzam Ejaz

正文完
 0