美文网首页
教程:如何直接从可视化CNN layers中的特征

教程:如何直接从可视化CNN layers中的特征

作者: 致Great | 来源:发表于2021-01-05 22:24 被阅读0次

简介

我们把神经网络比作眼睛,我们看看卷积神经网络(CNN)能够观察到什么:

[站外图片上传中...(image-d0c56d-1609856640469)]

基础条件:-

  • 读者知道如何构建CNN模型。

  • 读者了解可训练的参数计算以及各个中间层的输入和输出的大小。

注意:在这里,我们只关心构建CNN模型并观察其特征图(feature map),我们不关心模型的准确性。

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
import os
import numpy as np
import matplotlib.pyplot as plt

现在,在不浪费时间的情况下,让我们建立一个CNN模型:

model=tf.keras.models.Sequential([
    
    tf.keras.layers.Conv2D(8,(3,3),activation ='relu', input_shape=(150,150,3)),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(16,(3,3),activation ='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(32,(3,3),activation ='relu'),
    tf.keras.layers.MaxPooling2D(2,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')    
 ])

该模型的summary是:

model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 148, 148, 8)       224       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 74, 74, 8)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 72, 72, 16)        1168      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 36, 36, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 34, 34, 32)        4640      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 17, 17, 32)        0         
_________________________________________________________________
flatten (Flatten)            (None, 9248)              0         
_________________________________________________________________
dense (Dense)                (None, 1024)              9470976   
_________________________________________________________________
dense_1 (Dense)              (None, 512)               524800    
_________________________________________________________________
dense_2 (Dense)              (None, 3)                 1539      
=================================================================
Total params: 10,003,347
Trainable params: 10,003,347
Non-trainable params: 0
_________________________________________________________________

正如我们在上面看到的,我们具有三个卷积层,其后是MaxPooling层,两个全连接层和一个输出全连接层。


相关文章

网友评论

      本文标题:教程:如何直接从可视化CNN layers中的特征

      本文链接:https://www.haomeiwen.com/subject/wjlgoktx.html