JavaShuo
《吴恩达深度学习》04卷积神经网络(第1周卷积神经网络)
时间 2020-12-27
标签
深度学习理论学习
04. 卷积神经网络
第1周 卷积神经网络
1.1 计算机视觉
计算机视觉问题
(1)图像分类
(2)目标检测
(3)风格迁移
大规模图片上的深度学习
(1)以往的图片往往大小是
64
×
64
×
3
64\times 64 \times 3
6
4
×
6
4
×
3
的,即总特征数为12288。
(2)若图片大小增加到
1000
×
1000
×
3
1000 \times 1000 \times 3
1
0
0
0
×
1
0
0
0
×
3
,则总特征数增加到3百万。
1.2 边缘检测示例
计算机视觉问题
(1)人脸检测距离
垂直边缘检测
[
3
0
1
2
7
4
1
5
8
9
3
1
2
7
2
5
1
3
0
1
3
1
7
8
4
2
1
6
2
8
2
4
5
2
3
9
]
∗
[
1
0
−
1
1
0
−
1
1
0
−
1
]
=
[
−
5
−
4
0
8
−
10
−
2
2
3
0
−
2
−
4
−
7
−
3
−
2
−
3
−
16
]
\left[\begin{matrix} 3 & 0 & 1 & 2 & 7 & 4 \\ 1 & 5 & 8 & 9 & 3 & 1 \\ 2 & 7 & 2 & 5 & 1 & 3 \\ 0 & 1 & 3 & 1 & 7 & 8 \\ 4 & 2 & 1 & 6 & 2 & 8 \\ 2 & 4 & 5 & 2 & 3 & 9 \end{matrix}\right] * \left[\begin{matrix} 1 & 0 & -1\\ 1 & 0 & -1\\ 1 & 0 & -1 \end{matrix}\right] = \left[\begin{matrix} -5 & -4 & 0 & 8 \\ -10 & -2 & 2 & 3 \\ 0 & -2 & -4 & -7 \\ -3 & -2 & -3 & -16 \end{matrix} \right]
⎣
⎢
⎢
⎢
⎢
⎢
⎢
⎡
3
1
2
0
4
2
0
5
7
1
2
4
1
8
2
3
1
5
2
9
5
1
6
2
7
3
1
7
2
3
4
1
3
8
8
9
⎦
⎥
⎥
⎥
⎥
⎥
⎥
⎤
∗
⎣
⎡
1
1
1
0
0
0
−
1
−
1
−
1
⎦
⎤
=
⎣
⎢
⎢
⎡
−
5
−
1
0
0
−
3
−
4
−
2
−
2
−
2
0
2
−
4
−
3
8
3
−
7
−
1
6
⎦
⎥
⎥
⎤
编程实现
(1)python:conv_forward
(2)tensorflow:tf.nn.conv2d
垂直边缘检测的直观解释
1.3 更多边缘检测内容
垂直和水平边缘检测
(1)垂直
[
1
0
−
1
1
0
−
1
1
0
−
1
]
\left[\begin{matrix} 1 & 0 & -1\\ 1 & 0 & -1\\ 1 & 0 & -1 \end{matrix}\right]
⎣
⎡
1
1
1
0
0
0
−
1
−
1
−
1
⎦
⎤
(2)水平
[
1
1
1
0
0
0
−
1
−
1
−
1
]
\left[\begin{matrix} 1 & 1 & 1\\ 0 & 0 & 0\\ -1 & -1 & -1 \end{matrix}\right]
⎣
⎡
1
0
−
1
1
0
−
1
1
0
−
1
⎦
⎤
(3)正负边缘
正边缘指从亮到暗的过渡边缘。
学习边缘检测
(1)Sobel滤波器(更鲁棒)
[
1
0
−
1
2
0
−
2
1
0
−
1
]
\left[\begin{matrix} 1 & 0 & -1\\ 2 & 0 & -2\\ 1 & 0 & -1 \end{matrix}\right]
⎣
⎡
1
2
1
0
0
0
−
1
−
2
−
1
⎦
⎤
(2)Scharr滤波器
[
3
0
−
3
10
0
−
10
3
0
−
3
]
\left[\begin{matrix} 3 & 0 & -3\\ 10 & 0 & -10\\ 3 & 0 & -3 \end{matrix}\right]
⎣
⎡
3
1
0
3
0
0
0
−
3
−
1
0
−
3
⎦
⎤
(3)可以将矩阵元素作为学习的参数。
1.4 Padding
Padding
(1)若有一张图片大小为
n
×
n
n \times n
n
×
n
,滤波器大小为
f
×
f
f \times f
f
×
f
,则卷积结果大小为
(
n
−
f
+
1
)
×
(
n
−
f
+
1
)
(n-f+1)\times (n-f+1)
(
n
−
f
+
1
)
×
(
n
−
f
+
1
)
。
(2)缺点:每次卷积图像会变小;边角的像素只在一次卷积中涉及到,中部的像素点被多次采用。
(3)解决方法:在原始图像外围填充一圈像素点。设填充像素点个数为
p
p
p
,则原始图像变为
(
n
+
2
p
)
×
(
n
+
2
p
)
(n+2p)\times(n+2p)
(
n
+
2
p
)
×
(
n
+
2
p
)
,因此卷积结果大小变为
(
n
+
2
p
−
f
+
1
)
×
(
n
+
2
p
−
f
+
1
)
(n+2p-f+1)\times(n+2p-f+1)
(
n
+
2
p
−
f
+
1
)
×
(
n
+
2
p
−
f
+
1
)
Valid卷积和Same卷积
(1)Valid卷积:不填充
(2)Same卷积:填充,使得卷积结果和原始图像大小相同
通常情况下,
f
f
f
是奇数。
1.5 卷积步长
步幅卷积
(1)之前的卷积过程,步幅为1。
(2)若输入为
n
×
n
n \times n
n
×
n
,滤波器为
f
×
f
f \times f
f
×
f
,填充大小为
p
p
p
,步幅为
s
s
s
,则输出结果为
⌊
n
+
2
p
−
f
s
+
1
⌋
×
⌊
n
+
2
p
−
f
s
+
1
⌋
\lfloor\frac{n+2p-f}{s}+1\rfloor \times \lfloor\frac{n+2p-f}{s}+1\rfloor
⌊
s
n
+
2
p
−
f
+
1
⌋
×
⌊
s
n
+
2
p
−
f
+
1
⌋
互相关和卷积
机器学习(深度学习)领域中的卷积,和数学领域的卷积有所区别,和数学领域中的互相关更一致。
数学领域中的卷积运算,首先要对滤波器进行水平和垂直翻转,然后再进行计算。
1.6 三维卷积
RGB图像卷积
(1)原始图像尺寸:高
×
\times
×
宽
×
\times
×
通道数
(2)卷积核尺寸:高
×
\times
×
宽
×
\times
×
通道数(与原始图像相同)
(3)输出结果:高
×
\times
×
宽(二维)
多过滤器
用于检测不同目标,如垂直边缘、水平边缘等。此时输出结果不再是二维的,和过滤器数量有关。
1.7 单层卷积网络
单层举例
符号说明
若
l
l
l
层为卷积层,则
(1)
f
[
l
]
f^{[l]}
f
[
l
]
过滤器规模
(2)
p
[
l
]
p^{[l]}
p
[
l
]
填充像素数量
(3)
s
[
l
]
s^{[l]}
s
[
l
]
步长
(4)
n
C
[
l
]
n^{[l]}_C
n
C
[
l
]
过滤器数量
(5)每个过滤器的大小为
f
[
l
]
×
f
[
l
]
×
n
C
[
l
]
f^{[l]}\times f^{[l]} \times n_C^{[l]}
f
[
l
]
×
f
[
l
]
×
n
C
[
l
]
(6)激活函数
a
[
l
]
a^{[l]}
a
[
l
]
为
n
H
[
l
]
×
n
W
[
l
]
×
n
C
[
l
]
n_H^{[l]}\times n_W^{[l]}\times n_C^{[l]}
n
H
[
l
]
×
n
W
[
l
]
×
n
C
[
l
]
(7)权重
f
[
l
]
×
f
[
l
]
×
n
C
[
l
]
×
n
C
[
l
]
f^{[l]}\times f^{[l]} \times n_C^{[l]}\times n_C^{[l]}
f
[
l
]
×
f
[
l
]
×
n
C
[
l
]
×
n
C
[
l
]
(8)偏差
n
C
[
l
]
n_C^{[l]}
n
C
[
l
]
(9)输入:
n
H
[
l
−
1
]
×
n
W
[
l
−
1
]
×
n
C
[
l
−
1
]
n^{[l-1]}_H \times n^{[l-1]}_W\times n^{[l-1]}_C
n
H
[
l
−
1
]
×
n
W
[
l
−
1
]
×
n
C
[
l
−
1
]
(10)输出:
n
H
[
l
]
×
n
W
[
l
]
×
n
C
[
l
]
n^{[l]}_H \times n^{[l]}_W\times n^{[l]}_C
n
H
[
l
]
×
n
W
[
l
]
×
n
C
[
l
]
,其中
n
H
[
l
]
=
⌊
n
H
[
l
−
1
]
+
2
p
[
l
]
−
f
[
l
]
s
[
l
]
+
1
⌋
n^{[l]}_H=\lfloor\frac{n_H^{[l-1]}+2p^{[l]}-f^{[l]}}{s^{[l]}}+1\rfloor
n
H
[
l
]
=
⌊
s
[
l
]
n
H
[
l
−
1
]
+
2
p
[
l
]
−
f
[
l
]
+
1
⌋
,
n
W
[
l
]
n^{[l]}_W
n
W
[
l
]
类似.
1.8 简单卷积网络示例
1.9 池化层
1.10 卷积神经网络示例
1.11 为什么使用卷积?