【深度学习入门:基于Python的理论与实现】书本学习笔记 第七章 卷积神经网络

文章目录

  • 卷积神经网络
  • 1. 卷积神经网络的实现
    • 1.1 使用im2col
    • 1.2 池化层的实现
  • 2. CNN 的实现
    • 2.1 构建网络
    • 2.2 开始训练
  • 写在最后


注:书的源代码下载如下
书本源代码下载地址
点击右侧资源则可以下载对应的代码


卷积神经网络

1. 卷积神经网络的实现


在卷积神经网络中,我们需要考虑batch输入,则对于一个图像的输入,我们可以将其视作一个四维数组,其定义如下

import numpy as np 
x = np.random.randn(10, 1, 28, 28)
x.shape
(10, 1, 28, 28)
x[0].shape
(1, 28, 28)
x[1].shape
(1, 28, 28)

为了减少for运算,通常我们在卷积中实验一种叫im2col的方法来讲卷积输入转化为二维矩阵运算

【深度学习入门:基于Python的理论与实现】书本学习笔记 第七章 卷积神经网络_第1张图片
【深度学习入门:基于Python的理论与实现】书本学习笔记 第七章 卷积神经网络_第2张图片
【深度学习入门:基于Python的理论与实现】书本学习笔记 第七章 卷积神经网络_第3张图片

1.1 使用im2col


import sys, os
sys.path.append(os.pardir)
from common.util import im2col
x1 = np.random.rand(1, 3, 7, 7)
col1 = im2col(x1, 5, 5, stride = 1, pad = 0)
print(col1.shape)
(9, 75)
x2 = np.random.rand(10, 3, 7, 7)
col2 = im2col(x2, 5, 5, stride = 1, pad = 0) # 存了10倍数据
print(col2.shape)
(90, 75)
class Convolution:
    def __init__(self, W, b, stride = 1, pad = 0):
        self.W = W
        self.b = b
        self.stride = stride
        self.pad = pad
    
    def forward(self, x):
        FN, C, FH, FW = self.W.shape # 滤波器大小
        N, C, H, W = x.shape         # 输入大小
        # 计算输出大小
        out_h = int(1 + (H + 2*self.pad - FH) / self.stride)
        out_w = int(1 + (W + 2*self.pad - FW) / self.stride)
        
        col = im2col(x, FH, FW, se.stride, self.pad)
        col_W = self.W.reshape(FN, -1) # N个滤波器展开 * 一个滤波器参数
        out = np.dot(col, col_W) + self.b
        
        # N, H, W, C --> N, C, H, W
        out = out.reshape(N, out_h, out_w ,-1).transpose(0, 3, 1, 2)
        
        return out

1.2 池化层的实现


【深度学习入门:基于Python的理论与实现】书本学习笔记 第七章 卷积神经网络_第4张图片

class Pooling:
    def __init__(self, pool_h, pool_w, stride = 1, pad = 0):
        self.pool_h = pool_h
        self.pool_w = pool_w
        self.stride = stride
        self.pad = pad
        
    def forward(self, x):
        N, C, H, W = x.shape
        # 与卷积层相同,计算输出大小
        out_h = int(1 + (H - self.pool_h) / self.stride)
        out_w = int(1 + (W - self.pool_w) / self.stride)
        
        # Step1: 展开
        col = im2col(x, self.pool_h, self.pool_w, self.stride, self.pad)
        # 宽度必须为一个pool的元素个数
        col = col.reshape(-1, self.pool_h * self.pool_w)
        
        # Step2: 求最大值
        out = np.max(col, axis = 1)
        
        # Sep3: 转换形状
        # N, h, w, c --> N, c, h, w
        out = out.reshape(N, out_h, out_w, C).transpose(0, 3, 1, 2)
        
        return out

2. CNN 的实现


将上述的内容组合起来,构建如下所示的CNN网络

【深度学习入门:基于Python的理论与实现】书本学习笔记 第七章 卷积神经网络_第5张图片

参数

  • input_dim―输入数据的维度:(通道,高,长)
  • conv_param―卷积层的超参数(字典)。字典的关键字如下:
    • filter_num―滤波器的数量
    • filter_size―滤波器的大小
    • stride―步幅
    • pad―填充
  • hidden_size―隐藏层(全连接)的神经元数量
  • output_size―输出层(全连接)的神经元数量
  • weitght_int_std―初始化时权重的标准差

2.1 构建网络


from collections import OrderedDict
# 初始化部分
class SimpleConvNet:
    def __init__(self, input_dim=(1, 28, 28), 
                 conv_param={'filter_num':30, 'filter_size':5, 'pad':0, 'stride':1},
                 hidden_size=100, output_size=10, weight_init_std=0.01):
        
        filter_num = conv_param['filter_num']
        filter_size = conv_param['filter_size']
        filter_pad = conv_param['pad']
        filter_stride = conv_param['stride']
        input_size = input_dim[1]
        
        conv_output_size = (input_size - filter_size + 2*filter_pad) / \
                            filter_stride + 1
        pool_output_size = int(filter_num * (conv_output_size / 2) *
                               (conv_output_size / 2))
        
        # 卷积核初始化
        self.params ={}
        self.params['W1'] = weight_init_std * \
                            np.random.randn(filter_num, input_dim[0], 
                                            filter_size, filter_size)
        self.params['b1'] = np.zeros(filter_num)
        
        self.params['W2'] = weight_init_std * \
                            np.random.randn(pool_output_size, hidden_size)
        self.params['b2'] = np.zeros(hidden_size)
        
        self.params['W3'] = weight_init_std * \
                            np.random.randn(hidden_size, output_size)
        self.params['b3'] = np.zeros(output_size)
        
        
        # 生成必要的层
        self.layers = OrderedDict()
        self.layers['Conv1'] = Convolution(self.params['W1'],
                                            self.params['b1'],
                                            conv_param['stride'],
                                            conv_param['pad'])
        
        self.layers['Relu1'] = Relu()
        self.layers['Pool1'] = Pooling(pool_h=2, pool_w=2, stride=2)
        self.layers['Affine1'] = Affine(self.params['W2'], self.params['b2'])
        
        self.layers['Relu2'] = Relu()
        self.layers['Affine2'] = Affine(self.params['W3'], self.params['b3'])
        self.last_layer = SoftmaxWithLoss()
        
    def predict(self, x):
        for layer in self.layers.values():
            x = layer.forward(x)
        return x
    
    def loss(self, x, t):
        y = self.predict(x)
        return self.last_layer.forward(y, t)
    
    # 误差反向传播
    def gradient(self, x, t):
        # forward
        self.loss(x, t)
        
        # backward
        dout = 1
        dout = self.last_layer.backward(dout)
        
        layers = list(self.layers.values())
        layers.reverse()
        # 逐层反向传播
        for layer in layers:
            # for循环嵌套调用
            dout = layer.backward(dout)
            
        # 设定
        grads = {}
        grads['W1'] = self.layers['Conv1'].dW
        grads['b1'] = self.layers['Conv1'].db
        grads['W2'] = self.layers['Affine1'].dW
        grads['b2'] = self.layers['Affine1'].db
        grads['W3'] = self.layers['Affine2'].dW
        grads['b3'] = self.layers['Affine2'].db
        
        return grads
    
    # 这里实现的方法和之前的差不多,书中的代码时没有说明这一段的
    def accuracy(self, x, t, batch_size=100):
        if t.ndim != 1 : t = np.argmax(t, axis=1)
        
        acc = 0.0
        
        for i in range(int(x.shape[0] / batch_size)):
            tx = x[i*batch_size:(i+1)*batch_size]
            tt = t[i*batch_size:(i+1)*batch_size]
            y = self.predict(tx)
            y = np.argmax(y, axis=1)
            acc += np.sum(y == tt) 
        
        return acc / x.shape[0]
    
    def save_params(self, file_name="params.pkl"):
        params = {}
        for key, val in self.params.items():
            params[key] = val
        with open(file_name, 'wb') as f:
            pickle.dump(params, f)

    def load_params(self, file_name="params.pkl"):
        with open(file_name, 'rb') as f:
            params = pickle.load(f)
        for key, val in params.items():
            self.params[key] = val

        for i, key in enumerate(['Conv1', 'Affine1', 'Affine2']):
            self.layers[key].W = self.params['W' + str(i+1)]
            self.layers[key].b = self.params['b' + str(i+1)]

2.2 开始训练


# coding: utf-8
from common.layers import *
import sys, os
sys.path.append(os.pardir)  # 为了导入父目录的文件而进行的设定
import numpy as np
import matplotlib.pyplot as plt
from dataset.mnist import load_mnist
# from simple_convnet import SimpleConvNet
from common.trainer import Trainer

# 读入数据
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=False)

# 处理花费时间较长的情况下减少数据 
#x_train, t_train = x_train[:5000], t_train[:5000]
#x_test, t_test = x_test[:1000], t_test[:1000]

max_epochs = 20

network = SimpleConvNet(input_dim=(1,28,28), 
                        conv_param = {'filter_num': 30, 'filter_size': 5, 'pad': 0, 'stride': 1},
                        hidden_size=100, output_size=10, weight_init_std=0.01)
                        
trainer = Trainer(network, x_train, t_train, x_test, t_test,
                  epochs=max_epochs, mini_batch_size=100,
                  optimizer='Adam', optimizer_param={'lr': 0.001},
                  evaluate_sample_num_per_epoch=1000)
trainer.train()

下面因为调用了原书封装的类,所以运行起来没更新一次参数都会输出,因为这里想着和原书保持一致,所以这里就不改变原书的输出,但是输出的内容太长了,这里只保留了每个eouch的loss

train loss:2.300146098405703
=== epoch:1, train acc:0.228, test acc:0.255 ===

=== epoch:2, train acc:0.961, test acc:0.961 ===
train loss:0.11206486228087491
train loss:0.11061866190661411
train loss:0.1655503851637005

。。。
=== epoch:3, train acc:0.978, test acc:0.98 ===

=== epoch:4, train acc:0.986, test acc:0.982 ===

=== epoch:5, train acc:0.984, test acc:0.985 ===

=== epoch:6, train acc:0.993, test acc:0.988 ===

train loss:0.014062620550900698
=== epoch:7, train acc:0.992, test acc:0.978 ===

=== epoch:8, train acc:0.99, test acc:0.987 ===

=== epoch:9, train acc:0.992, test acc:0.987 ===

=== epoch:10, train acc:0.996, test acc:0.986 ===

=== epoch:11, train acc:0.997, test acc:0.991 ===

=== epoch:12, train acc:0.991, test acc:0.987 ===

=== epoch:13, train acc:0.998, test acc:0.99 ===

=== epoch:14, train acc:0.998, test acc:0.99 ===

=== epoch:15, train acc:0.999, test acc:0.991 ===

=== epoch:16, train acc:0.997, test acc:0.988 ===

=== epoch:17, train acc:0.999, test acc:0.991 ===

=== epoch:18, train acc:0.999, test acc:0.991 ===

=== epoch:19, train acc:0.993, test acc:0.984 ===

=== epoch:20, train acc:1.0, test acc:0.988 ===

=============== Final Test Accuracy ===============
test acc:0.9897

可以看到的是,对于手写数据集,我们即使使用了一个比较小的神经网络来进行训练,最终还是达到了一个比较不错的预测精度

接下来的内容我们将对结果进行绘图

import pickle
# 保存参数
network.save_params("params.pkl")
print("Saved Network Parameters!")

# 绘制图形
markers = {'train': 'o', 'test': 's'}
x = np.arange(max_epochs)
plt.plot(x, trainer.train_acc_list, marker='o', label='train', markevery=2)
plt.plot(x, trainer.test_acc_list, marker='s', label='test', markevery=2)
plt.xlabel("epochs")
plt.ylabel("accuracy")
plt.ylim(0, 1.0)
plt.legend(loc='lower right')
plt.show()

Saved Network Parameters!

【深度学习入门:基于Python的理论与实现】书本学习笔记 第七章 卷积神经网络_第6张图片


写在最后

各位看官,都看到这里了,麻烦动动手指头给博主来个点赞8,您的支持作者最大的创作动力哟!
才疏学浅,若有纰漏,恳请斧正
本文章仅用于各位作为学习交流之用,不作任何商业用途,若涉及版权问题请速与作者联系,望悉知

你可能感兴趣的:(#,《深度学习入门》笔记,深度学习(DL)之路,Python之路,python,深度学习,学习)