机器学习基础(十四)卷积神经网络——图像处理

1. 卷积神经网络原理—简单图像卷积操作

from skimage import data, io
import numpy as np
import  matplotlib.pyplot as plt

# 手写实现灰度图的卷积操作过程

moon = data.moon()
camera = data.camera()
io.imshow(camera)
io.show()

H, W = camera.shape  # 原图的高度和宽度
print(H,W)
filter = np.array([[1, 0],
                   [0, -1]])  # 卷积核
HH, WW = filter.shape  # 卷积核的高度和宽度
stride = 1   # 步长
pad = 0  # 扩展,补全,在图像外部补全0行及0列

OH = (H+pad*2-HH)//stride + 1
OW = (W+pad*2-WW)//stride + 1

outImage = np.zeros(shape=(OH, OW))

for i in range(OH):
    for j in range(OW):
        outImage[i][j] = int(np.sum(filter * camera[i*stride : i * stride + HH, j * stride : j * stride + WW]))

print(outImage.shape)

plt.imshow(outImage/255, plt.cm.gray)
plt.show()

在这里插入图片描述
在这里插入图片描述

2. 三维卷积运算

import numpy as np
import matplotlib.pyplot as plt
from skimage import io, data

# 手写实现彩色图像的卷积操作
coffee = data.coffee()
print(coffee.shape)
plt.imshow(coffee)
plt.show()

# 彩色图像的卷积和卷积效果
H, W, C = coffee.shape  # 原图的高度和宽度
print(H, W, C)
filter = np.array([[[1, 0],
                   [0, -1]],
                   [[1, 0],
                    [0, -1]],
                   [[1, 0],
                    [0, -1]]])  # 卷积核3 * 2 * 2
filter2 = np.array([[[1, 0],
                   [0, -1]],
                   [[1, 0],
                    [0, -1]],
                   [[1, 0],
                    [0, -1]]])  # 卷积核3 * 2 * 2
filter3 = np.array([[[1, 0],
                   [0, -1]],
                   [[1, 0],
                    [0, -1]],
                   [[1, 0],
                    [0, -1]]])  # 卷积核3 * 2 * 2
CC, HH, WW = filter.shape  # 卷积核的高度和宽度
stride = 1   # 步长
pad = 0  # 扩展,补全,在图像外部补全0行及0列

OH = (H+pad*2-HH)//stride + 1
OW = (W+pad*2-WW)//stride + 1

outImage = np.zeros(shape=( OH, OW, C))

for i in range(OH):
    for j in range(OW):
        outImage[i][j][0] = np.sum(coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 0] * filter[0] + \
                            coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 1] * filter[1] + \
                            coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 2] * filter[2])
        outImage[i][j][1] = np.sum(coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 0] * filter2[0] + \
                            coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 1] * filter2[1] + \
                            coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 2] * filter2[2])

        outImage[i][j][2] = np.sum(coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 0] * filter3[0] + \
                            coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 1] * filter3[1] + \
                            coffee[i * stride:i * stride + HH, j * stride:j * stride + WW, 2] * filter3[2])
print(outImage.shape)
outImage = outImage.astype(np.int32)
plt.imshow(outImage)
plt.show()

在这里插入图片描述
卷积层的意义就是找到一个更优的卷积核处理图像,使得图像特征可以更好地被提取出来,便于后续的分类。

3. 最大池化—修改图像大小

import numpy as np
import matplotlib.pyplot as plt
from skimage import io, data

# 手写实现最大池化
coffee = data.coffee()
print(coffee.shape)

pool_size = (2, 2)
stride = 2
H, W, C = coffee.shape
OH = H // 2
OW = W // 2
print(OH, OW)
outImage = np.zeros(shape = (OH, OW, C), dtype=np.float64)
for i in range(OH):
    for j in range(OW):
        outImage[i][j][0] = np.max(coffee[i * stride : i * stride + pool_size[0], j * stride : j * stride + pool_size[1], 0])

        outImage[i][j][1] = np.max(coffee[i * stride : i * stride + pool_size[0], j * stride : j * stride + pool_size[1], 1])

        outImage[i][j][2] = np.max(coffee[i * stride : i * stride + pool_size[0], j * stride : j * stride + pool_size[1], 2])
        pass
    pass
plt.imshow(outImage.astype(np.int32))
plt.show()

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
神经网络(Convolutional Neural Network,CNN)是一种机器学习算法,其主要应用于图像识别、计算机视觉和模式识别等领域。CNN模型的设计灵感来源于科学家们对于生物视觉系统的研究。该算法的核心概念是通过卷层、池化层和全连接层的组合,对输入的图像进行特征提取和分类。 在CNN中,卷层是该模型的主要组成部分之一。通过定义一组卷核(或过滤器),卷层可以对输入的图像进行滤波操作,将原始图像中的特定特征(例如边缘和纹理)提取出来,并生成一系列特征图。这些特征图可以被认为是对原始图像进行不同尺度和方向的特征提取。 在经过卷层之后,通常会接着使用池化层来进行下采样操作。池化层的主要目的是减小特征图的尺寸,同时保留重要的特征信息。最常见的池化操作是最大池化,它通过从特定区域选择最大值来减小特征图的尺寸。 最后,经过卷层和池化层的多次迭代后,最后会以全连接层作为输出层,进行分类任务。全连接层的每个节点都与前一层的所有节点相连接,主要用于将最后一层的特征进行整合,并根据特征进行分类或回归。 相比于传统机器学习算法,CNN在处理图像任务方面具有更好的性能。这是因为卷层可以通过共享权重和局部连接的方式进行参数的共享,大大减少了需要训练的参数数量,并且能够有效处理图像的平移不变性。此外,卷神经网络还可以通过堆叠多个卷层和全连接层来构建深层网络模型,从而进一步提高模型的性能。 总而言之,卷神经网络是一种强大的机器学习算法,特别适用于图像识别和计算机视觉任务。通过卷层、池化层和全连接层的组合,CNN可以有效地提取图像中的特征,并进行分类或回归等任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值