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()