2020机器学习卷积神经网(1)

作者: zidea | 来源:发表于2020-01-31 20:55 被阅读0次
machine_learning.jpg

感谢李宏毅老师的机器学习,部分内容参考了李宏毅老师的讲义

卷积神经网络

今天我们回头看一看卷积神经网,告诉我们是可以重新设计神经网络结构.说到深度学习我们不仅就会想起卷积神经网络和循环神经网。卷积神经网络活跃在计算机视觉,而循环神经网络活跃在自然语言处理领域。

卷积神经网络动机

现在在图像识别上我们都会采用 CNN ,卷积神经网络针对图像识别中出现问题给出了解决方案。卷积神经网络应该是全连接神经网的简化版,通过一些小技巧来减少参数数量。

图像辨识一些特征

我们看图像辨识上有哪些特点,让我们可以利用这些特点来实现减少参数目标

naruto.png

我们如何判断图中是鸣人,他是有黄头发,然后带有表示火影标志,脸上有三道,这些特征都是我们判断他是鸣人而不是佐助的特征。有时候我们判断忍者卡通人物是火影而不是风影。我们只需要判断图形中一部分区域图案就可以判断他是火影而不是风影。所有我们神经元不需要看整张图,只看一部分就可以判断出他是来自火影。

sasuke.jpg
import matplotlib.pyplot as plt
import cv2
import numpy as np

%matplotlib inline
naruto_path = "images/naruto.png"
sasuke_path = "images/sasuke.jpg"
naruto_img = cv2.imread(naruto_path)
naruto_img = cv2.cvtColor(naruto_img, cv2.COLOR_BGR2RGB)
sasuke_img = cv2.imread(sasuke_path)
sasuke_img = cv2.cvtColor(sasuke_img, cv2.COLOR_BGR2RGB)
plt.subplot(2, 2, 1)
plt.imshow(sasuke_img)
plt.subplot(2, 2, 2)
plt.imshow(naruto_img)
plt.show()
output_4_0.png

同一特征不同位置

我们对比看佐助和鸣人图片,他们火影标志出现在这个图片不同位置。

sasuke_big_path = "images/sasuke_big.jpg"
sasuke_big_img = cv2.imread(sasuke_big_path)
sasuke_big_img = cv2.cvtColor(sasuke_big_img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(12, 6.5))
plt.subplot(2, 2, 1)
plt.imshow(sasuke_big_img)
plt.subplot(2, 2, 2)
sasuke_big_img_downsampling = cv2.pyrDown(sasuke_big_img)
plt.imshow(sasuke_big_img_downsampling)
plt.show()
output_6_0.png

我们看上对图片进行降采样,我们发现上面两张图片并没有什么区别。我们简单总结一下上面主要说了几点

  • 我们通过提取局部特征来推断分类,也就是说明我们只看图片区部而无需看整张就可以提取特征
  • 用于辨识特征的局部图像可能出现图像不同位置
  • 对于图片进行降采样

卷积核

无论是在机器学习还是在计算机视觉,卷积核都发挥重要作用,例如对图像特征提取,图像模糊处理都少不了卷积核。我们今天通过代码带大家一起认识卷积核。

conv_001.png

定义一个矩阵通过行和列定义像素值,然后就是定义一个3 \times 3的卷积核,我们卷积核从左向右从上向下进行按步骤移动。

img = np.array(([
    [1,0,0,0,0,1],
    [0,1,0,0,1,0],
    [0,0,1,1,0,0],
    [1,0,0,0,1,0],
    [0,1,0,0,1,0],
    [0,0,1,0,1,0]
]))
# print(img.shape)
kernel_01.png
kernel_one = np.array([
    [1,-1,-1],
    [-1,1,-1],
    [-1,-1,1]
])
a = np.array([
    [1,0,0],
    [0,1,0],
    [0,0,1]
])
print(np.sum(a*kernel_one))
3
kernel_one_size = kernel_one.shape[0]
# print(img.shape)
# rows = img.shape[0] - (img.shape[0] / kernel_one_size)
# print(rows)
# print(rows)
feature_map_one = np.zeros((4,4),dtype=int)
for row in range(4):
    for col in range(4):
        arr = img[row:(row+3),col:(col+3)]
        conv = np.sum(arr*kernel_one)
        feature_map_one[row,col] = conv
      
kernel_res_01.png
feature_map_one
array([[ 3, -1, -3, -1],
       [-3,  1,  0, -3],
       [-3, -3,  0,  1],
       [ 3, -2, -2, -1]])
kernel_two = np.array([
    [-1,1,-1],
    [-1,1,-1],
    [-1,1,-1]
])
feature_map_two = np.zeros((4,4),dtype=int)
for row in range(4):
    for col in range(4):
        arr = img[row:(row+3),col:(col+3)]
        conv = np.sum(arr*kernel_two)
        feature_map_two[row,col] = conv
feature_map_two
array([[-1, -1, -1, -1],
       [-1, -1, -2,  1],
       [-1, -1, -2,  1],
       [-1,  0, -4,  3]])
naruto_path = "images/naruto.png"
naruto_img = cv2.imread(naruto_path)
naruto_img_gray = cv2.cvtColor(naruto_img, cv2.COLOR_BGR2GRAY)
print(naruto_img_gray)
print(naruto_img_gray.shape)
[[133 133 133 ... 135 136 137]
 [134 134 134 ... 136 140 144]
 [133 133 133 ... 142 150 156]
 ...
 [104 105 104 ... 147 145 144]
 [118 119 119 ... 162 160 160]
 [146 147 146 ... 178 175 176]]
(180, 180)
print(naruto_img_gray.shape)
out_height= naruto_img_gray.shape[0] - 3 +1
out_width= naruto_img_gray.shape[1] - 3 +1
print(out_height,out_width)

naruto_img_gray_filter_one = np.zeros((out_height,out_width),dtype=int)

for row in range(out_width):
    for col in range(out_height):
        arr = naruto_img_gray[row:(row+3),col:(col+3)]
        conv = np.sum(arr*kernel_one)
        naruto_img_gray_filter_one[row,col] = conv
# naruto_img_gray_filter_one
plt.subplot(2, 2, 1)
plt.imshow(naruto_img_gray_filter_one,cmap='gray')
plt.subplot(2, 2, 2)
naruto_img = cv2.cvtColor(naruto_img, cv2.COLOR_BGR2RGB)
plt.imshow(naruto_img)
plt.show()
(180, 180)
178 178
output_22_1.png
print(naruto_img_gray.shape)
out_height= naruto_img_gray.shape[0] - 3 +1
out_width= naruto_img_gray.shape[1] - 3 +1
print(out_height,out_width)

naruto_img_gray_filter_two = np.zeros((out_height,out_width),dtype=int)
naruto_img = cv2.imread(naruto_path)
for row in range(out_width):
    for col in range(out_height):
        arr = naruto_img_gray[row:(row+3),col:(col+3)]
        conv = np.sum(arr*kernel_two)
        naruto_img_gray_filter_two[row,col] = conv
# naruto_img_gray_filter_one
plt.subplot(2, 2, 1)
plt.imshow(naruto_img_gray_filter_two,cmap='gray')
plt.subplot(2, 2, 2)
naruto_img = cv2.cvtColor(naruto_img, cv2.COLOR_BGR2RGB)
plt.imshow(naruto_img)
plt.show()
(180, 180)
178 178
output_23_1.png
naruto_img_gray_filter_two

array([[-400, -399, -402, ..., -411, -419, -424],
       [-399, -400, -401, ..., -422, -437, -447],
       [-398, -400, -400, ..., -437, -459, -476],
       ...,
       [-340, -347, -346, ..., -413, -394, -400],
       [-332, -338, -338, ..., -408, -441, -440],
       [-366, -371, -367, ..., -364, -438, -487]])




---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-129-12580f596815> in <module>
----> 1 naruto_img_gray_filter_two = naruto_img_gray_filter_two.reshape((1,))
      2 naruto_img_gray_filter_two


ValueError: cannot reshape array of size 31684 into shape (1,)

最后希望大家关注我们微信公众号


wechat.jpeg

相关文章

  • 2020机器学习卷积神经网(1)

    卷积神经网在当下流行深度学习框架如 tensorflow mxnet 都提供 API 很容就能够实现卷积神经网,但...

  • 2020机器学习卷积神经网(1)

    感谢李宏毅老师的机器学习,部分内容参考了李宏毅老师的讲义 卷积神经网络 今天我们回头看一看卷积神经网,告诉我们是可...

  • 2020机器学习卷积神经网(2)

    准备数据 用 numpy.zeros 创建 的矩阵作为卷积核集合,这里 2 表示有两个卷积核,卷积核大小都是 ...

  • 从Keras开始掌握深度学习-3 实现卷积神经网络

    前言 关于卷积神经网络的详细内容可以参考我前面写的文章:从零开始机器学习-18 CNN:卷积神经网络在了解了卷积网...

  • [机器学习入门] 李宏毅机器学习笔记-11(Convolutio

    [机器学习入门] 李宏毅机器学习笔记-11(Convolutional Neural Network;卷积神经网络...

  • 2020 时序分析(5)

    深度学习遍布与机器学习各个领域,特别是处理时间序列和空间问题,分别有循环神经网和卷积神经网络两大主力。既然循环神经...

  • 3种常见神经网络

    1.卷积网络(convolutional neural network) 卷积神经网络简单理解 从字面上卷积神经网...

  • 2020 机器翻译 (1)

    注意力机制 参考《动手学深度学习》参考《李宏毅老师机器学习》 相关参考资料 2020机器学习循环神经网(1) 20...

  • 卷积神经网(1)

    卷积核组 我们把这样 7 卷积核放在一起就是卷积核组,我们现在第一个卷积核看起,这个卷积核应该是高斯偏导核,用于检...

  • 白话机器学习 - 感知机

    在机器学习里面,有一大类叫做神经网络。神经网络里面,大名鼎鼎的有卷积神经网络CNN,循环神经网络RNN。其中,卷积...

网友评论

    本文标题:2020机器学习卷积神经网(1)

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