如何直接从CNN图层可视化要素地图
先决条件:-
读者知道如何生成CNN模型。
读者了解可训练的参数计算以及各个中间层的输入和输出的大小。
重要提示:-
在这里,我们只关心构建CNN模型并检查其特征图。我们不关心模型的准确性。
现在,在不浪费时间的情况下,让我们建立一个模型:
我们创建具有三个类的多类模型。
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(8,(3
tf.keras.layers.MaxPooling2D(2
tf.keras.layers.Conv2D(16,(3
tf.keras.layers.MaxPooling2D(2
tf.keras.layers.Conv2D(32,(3
tf.keras.layers.MaxPooling2D(2
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1024,activation ='relu'),
tf.keras.layers.Dense(512,activation ='relu'),
tf.keras.layers.Dense(3,activation ='softmax')
])
该模型的摘要是:
model.summary()
模型总结
正如我们在上面看到的,我们具有三个卷积层,其后是MaxPooling层,两个密集层和一个最终输出密集层。
注意:-我们需要编译和拟合模型。因此,首先运行模型,然后才能运行特征图。我没有在这里显示所有这些步骤。
要生成要素图,我们需要了解model.layers API。
访问CNN的中间层:
让我们了解如何访问CNN的中间层。
获取CNN层的名称:-
layer_names = [model.layers中图层的layer.name]
layer_names
输出为:
['conv2d',
'max_pooling2d',
“ conv2d_1”,
'max_pooling2d_1',
“ conv2d_2”,
'max_pooling2d_2',
“扁平化”
'稠密',
“ dense_1”,
'dense_2']
检查图层:
模型层
它返回图层列表,如下所示:
[<tensorflow.python.keras.layers.convolutional.Conv2D at 0x2510de50e48>,
<tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x25124b740c8>,
<tensorflow.python.keras.layers.convolutional.Conv2D在0x25124b74748>,
<tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x25124b74d48>,
<tensorflow.python.keras.layers.convolutional.Conv2D在0x25124b9e308>,
<tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x25124b9e988>,
<tensorflow.python.keras.layers.core.Flatten at 0x25124b9ee88>,
<tensorflow.python.keras.layers.core.Dense at 0x25124ba13c8>,
<tensorflow.python.keras.layers.core.Dense at 0x25124ba1988>,
<tensorflow.python.keras.layers.core.Dense at 0x25124ba1f48>]
获取CNN层的输出:-
layer_outputs = [model.layers中图层的layer.output]
这将返回?安输出对象的层。它们不是真正的输出,但是它们告诉我们将生成输出的功能。我们将把这一layer.output合并到一个可视化模型中,我们将构建该模型以提取特征图。
[<tf.Tensor'conv2d / Relu:0'shape =(None,148,148,8)dtype = float32>,
<tf.Tensor'max_pooling2d / MaxPool:0'shape =(None,74,74,8)dtype = float32>,
<tf.Tensor'conv2d_1 / Relu:0'shape =(None,72,72,16)dtype = float32>,
<tf.Tensor'max_pooling2d_1 / MaxPool:0'shape =(None,36,36,16)dtype = float32>,
<tf.Tensor'conv2d_2 / Relu:0'shape =(None,34,34,32)dtype = float32>,
<tf.Tensor'max_pooling2d_2 / MaxPool:0'shape =(None,17,17,32)dtype = float32>,
<tf.Tensor'flatten / Reshape:0'shape =(None,9248)dtype = float32>,
<tf.Tensor'dense / Relu:0'shape =(None,1024)dtype = float32>,
<tf.Tensor'dense_1 / Relu:0'shape =(None,512)dtype = float32>,
<tf.Tensor'dense_2 / Softmax:0'shape =(None,3)dtype = float32>]
为了生成特征图,我们必须构建一个可视化模型,该模型以图像为输入,并具有上述layer_outputs作为输出函数。
这里要注意的重要一点是,我们总共有10个输出,9个中间输出和1个最终分类输出。因此,我们将有9个特征图。
CNN图层中的要素地图可视化模型
feature_map_model = tf.keras.models.Model(input = model.input,output = layer_outputs)
上面的公式只是汇总了我们在开始时创建的CNN模型的输入和输出函数。
layer_outputs中总共有10个输出函数。将该图像作为输入,然后使该图像以串行方式依次通过所有这10个输出功能。
最后一个输出函数是模型本身的输出。因此,总共有9个中间输出函数,因此有9个中间特征图。
这意味着我们输入到feature_map_model的任何输入,输出将以9个特征图的形式出现。
现在,我们将准备一张图像,以将其作为上述feature_map_model的输入:-
image_path = r“来自台式机或Internet的图像路径”。img = load_img(图像路径,target_size =(150,150)) 输入= img_to_array(img)
输入= x.reshape(((1,)+ x.shape)
输入/ = 255.0
在上面的代码中,我们已将图像加载到变量“输入”中,将其转换为数组,扩展了图像的尺寸以匹配中间层的尺寸,最后,在将图像输入到层。
现在,让我们将其输入到创建的模型中:
feature_maps = feature_map_model.predict(输入)
上面的代码终于为我们生成了特征图。
我们将再次解码feature_maps内容。
现在已经生成了特征图,让我们检查每个输出的特征图的形状。
对于zip中的layer_name,feature_map(layer_names,feature_maps):print(f“ {layer_name}的形状为======= >> {feature_map.shape}”)
上面的代码将给出CNN模型中间层的层名称以及我们生成的相应要素图的形状。
形状
生成特征图的步骤:-
我们需要生成仅卷积层而不是密集层的特征图,因此我们将生成具有“ dimension = 4”的层的特征图。
对于zip(layer_names,feature_maps)中的layer_name,feature_map: 如果len(feature_map.shape)== 4
每个特征图都有n个通道,并且在特征图形状的末尾给出数字“ n”。这是特定图层中要素的数量。
例如。feature_map [0] .shape =(1
对于zip(layer_names,feature_maps)中的layer_name,feature_map:if len(feature_map.shape)== 4 #图层的要素图中的要素图像/维数
k = feature_map.shape [-1] ?
?
?#迭代特定图层的特征图以分离所有特征图像。对于我在范围(k)中:
?feature_image = feature_map [0,:,:,i
直接生成的特征图在视觉上非常暗淡,因此人眼无法正确看到。因此,我们需要对提取的特征图像进行标准化和标准化。
图像的标准化和归一化,使其可被人眼看到:-
feature_image- = feature_image.mean()
feature_image / = feature_image.std()
feature_image * = 64
feature_image + = 128
feature_image = np.clip(x,0,255).astype('uint8')
保留以上三点,让我们生成要素图,
对于zip(layer_names,feature_maps)中的layer_name,feature_map: 如果len(feature_map.shape)== 4
k = feature_map.shape [-1]
size = feature_map.shape [1]
对于我在范围(k)中:
feature_image = feature_map [0,:,:,i]
feature_image- = feature_image.mean()
feature_image / = feature_image.std()
feature_image * = 64
feature_image + = 128
feature_image = np.clip(x,0,255).astype('uint8')
image_belt [:
最后,让我们显示我们生成的image_belts:
比例= 20 / k
plt.figure(figsize =(scale * k,scale))
plt.title(layer_name)
plt.grid(False)
plt.imshow(image_belt,Aspect ='auto')
题库