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

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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值