# 卷积介绍

# 卷积神经网络

卷积神经网络 (Convolutional Neural Network) CNN
大部分的图片并不是正正方方摆好的,会有些偏移,这时候全连接的计算机视觉就不太好使了。
他是识别物品的特征,来判断物品的;

image-20201210200313433

从图像当中取到的像素,通过过滤器,与过滤器(Filter)相乘,最后相加,得到一个新的像素集;

不同的 filter 会有不同的效果

image-20201210200607255

image-20201210200657614

每次卷积完了之后还要再做一个 Max Pooling,他的作用是增强图像的特征,如下是取最大值,最后就剩下一个 2x2 的矩阵

image-20201210200734124

image-20201210200907168

Max Pooling 以后数据减少了,但是特征增强了。卷积的材料可以看 https://bit.ly/2UGa7uH

# 卷积神经程序

import tensorflow as tf
from tensorflow import keras
fasion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fasion_mnist.load_data()
model = keras.Sequential()
#Conv2D 二维的卷积层,64 个过滤器,每个过滤器 3X3 的像素,
model.add(keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)))
#Maxpooling 2x2,四个像素,
model.add(keras.layers.MaxPooling2D(2,2))
model.add(keras.layers.Conv2D(64,(3,3),activation='relu'))
model.add(keras.layers.MaxPooling2D(2,2))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128,activation=tf.nn.relu))
model.add(keras.layers.Dense(10,activation=tf.nn.softmax))
train_imges_scaled=train_images/255
model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
#reshape ,-1 是不管无视行数
model.fit(train_imges_scaled.reshape(-1,28,28,1),train_labels,epochs=5)

# 卷积网络结构

model.summary()
---console---
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 64)        640  
//输入是28x28,但是filter去掉了两个像素,64filter
//640 = (3*3+1)*64  
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 64)        0    //尺寸缩小一半。没有调整参数所以Param=0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 64)        36928  //再卷积,又去掉两个像素
//36928=(3*3*64+1)*64
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0       //尺寸继续锁校
_________________________________________________________________
flatten (Flatten)            (None, 1600)              0       //展平数据,
_________________________________________________________________
dense (Dense)                (None, 128)               204928  //
_________________________________________________________________
dense_1 (Dense)              (None, 10)                1290      
=================================================================
Total params: 243,786
Trainable params: 243,786
Non-trainable params: 0
_________________________________________________________________
#读取 MODEL 的每个层
layer_outputs = [layer.output for layer in model.layers]
#将 input 和 output 放在一起,构成一组对象
activation_model = tf.keras.models.Model(inputs=model.input,outputs=layer_outputs )
#用在一张图片上,预测结果
pred = activation_model.predict(test_images[0].reshape(1,28,28,1))
len(pred)
#输出 7,有 7 个层的输出
pred[0].shape
#(1,26,26,64) 
pred[0][0,:,:,1]
#第 0 层,所有的行,所有的列,filter

第 0 层,conv2d,filter=1

第 1 层,max_pooling2d,filter=1
image-20201210215457034
第 2 层,conv2d_1,filter =1
image-20201210215623822
第 3 层,max_pooling2d_1,filter=1
image-20201210215648245

第 4 层,flatten
image-20201210215701092
第 5 层,dense
image-20201210215709794
第 6 层,dense_1,一共 6 层,输出预测结果
image-20201210215719873

第 0 层,filter=50,不同 filter 的样子
image-20201210215805919

-->