1.反向传播BP

反向传播(Backpropagation)是“误差反向传播”的简称,是一种与最优化方法,用来训练人工神经网络的常见方法。

简单来说就是:

几个人站成一排第一个人看一幅画(输入数据),描述给第二个人(隐层)……依此类推,到最后一个人(输出)的时候,画出来的画肯定不能看了(误差较大)。反向传播就是,把画拿给最后一个人看(求取误差),然后最后一个人就会告诉前面的人下次描述时需要注意哪里(权值修正)

2.卷积:convolution

从数学上讲,卷积是一种运算

卷积运算:

卷积神经网络(简单)

 

 

这个图中的计算:

左边的是输入,中间的是卷积核,右边的是输出,该图其实有算错

从输入的左上角开始扫描,大小3*3,乘中间的卷积核,一一对应,最后相加,得到对应位置的输出,然后开始扫描,向右。

卷积操作是使用一个二维的卷积核在一个批处理的图片上进行不断扫描。具体操作是将一个卷积核在每张图片上按照一个合适的尺寸在每个通道上面进行扫描。

三通道图像上的卷积过程:

卷积神经网络(简单)

 

 

 这个其实就是三通道每一个通道乘以卷积核之后三个通道的值相加,再加上偏差(有时会有)

3.代码实现卷积运算

 1 import warnings
 2 warnings.filterwarnings('ignore')
 3 import numpy as np
 4 import tensorflow as tf
 5 
 6 # 输入数据
 7 data = np.array([[2,1,0,2,3],[9,5,4,2,0],[2,3,4,5,6],[1,2,3,1,0],[0,4,4,2,8]])
 8 # 卷积核
 9 filter_ = np.array([[-1,0,1]*3]).reshape(3,3)
10 # 修改数据要求 四维(批,高,宽,通道)
11 # Given an input tensor of shape `[batch, in_height, in_width, in_channels]
12 data = data.reshape(1,5,5,1).astype(np.float32)
13 # `[filter_height, filter_width, in_channels, out_channels]`
14 filter_ = filter_.reshape(3,3,1,1).astype(np.float32)
15 # stride步数 padding: Either the `string` `"SAME"` or `"VALID"`
16 conv = tf.nn.conv2d(input=data,filter = filter_,strides=[1,1,1,1],padding ='VALID' )
17 with tf.Session() as sess:
18     print(sess.run(conv).reshape(3,3))

 

4.使用卷积处理图片

 

1 import numpy as np
2 import tensorflow as tf
3 import matplotlib.pyplot as plt
4 %matplotlib inline
5 
6 # 原始数据
7 flower = plt.imread('./flower.png')
8 plt.imshow(flower,cmap = plt.cm.gray)

 

卷积神经网络(简单)

 

 

 

 

 1 data = flower.reshape(1,332,444,1)
 2 # 浮雕效果
 3 filter_ = np.array([[-1,-1,0],[-1,0,1],[0,1,1]]).reshape(3,3,1,1)
 4 # neural network
 5 # KNN ----> nearest neighbors
 6 # CNN ---->convolution neural network:卷积神经网络
 7 conv = tf.nn.conv2d(input = data,filter = filter_,strides=[1,1,1,1],padding='SAME')
 8 with tf.Session() as sess:
 9     image = sess.run(conv).reshape(332,444)
10 plt.imshow(image,cmap = plt.cm.gray)

卷积神经网络(简单)

 

 

 

 1 data = flower.reshape(1,332,444,1)
 2 # 强调边缘
 3 filter_ = np.array([[1,1,1],[1,-10,1],[1,1,1]]).reshape(3,3,1,1)
 4 # neural network
 5 # KNN ----> nearest neighbors
 6 # CNN ---->convolution neural network:卷积神经网络
 7 conv = tf.nn.conv2d(input = data,filter = filter_,strides=[1,1,1,1],padding='SAME')
 8 with tf.Session() as sess:
 9     image = sess.run(conv).reshape(332,444)
10 plt.imshow(image,cmap = plt.cm.gray)

卷积神经网络(简单)

 

 

 

 1 data = flower.reshape(1,332,444,1)
 2 # 边缘检测
 3 filter_ = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]]).reshape(3,3,1,1)
 4 # neural network
 5 # KNN ----> nearest neighbors
 6 # CNN ---->convolution neural network:卷积神经网络
 7 conv = tf.nn.conv2d(input = data,filter = filter_,strides=[1,1,1,1],padding='SAME')
 8 with tf.Session() as sess:
 9     image = sess.run(conv).reshape(332,444)
10 plt.imshow(image,cmap = plt.cm.gray)

卷积神经网络(简单)

 

 

 

5.使用卷积使图片变得平滑,也就是更清晰

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 %matplotlib inline
 4 import tensorflow as tf
 5 
 6 plt.figure(figsize=(12,9))
 7 moon = plt.imread('./moonlanding.png')
 8 plt.imshow(moon,cmap = plt.cm.gray)
 9 
10 # 均值平滑
11 data=moon.reshape(1,474,630,1)
12 # 均值平滑,滤波
13 filter_=np.array([1/9]*9).reshape(3,3,1,1)
14 conv=tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME') 
15 with tf.Session() as sess:
16     image=sess.run(conv).reshape(474,630)
17 plt.figure(figsize=(9,6))
18 plt.imshow(image,cmap=plt.cm.gray)
19 
20 # 高斯平滑
21 data=moon.reshape(1,474,630,1)
22 # 高斯平滑,滤波
23 filter_=np.array([[1/16,2/16,1/16],[2/16,4/16,2/16],[1/16,2/16,1/16]]).reshape(3,3,1,1)
24 conv=tf.nn.conv2d(data,filter_,[1,1,1,1],'SAME') 
25 with tf.Session() as sess:
26     image=sess.run(conv).reshape(474,630)
27 plt.figure(figsize=(9,6))
28 plt.imshow(image,cmap=plt.cm.gray)

使用卷积使拖欠平滑