• &4_卷积神经网络 CNN


    什么是卷积神经网络

    卷积神经网络在图片和语言识别方面能够给出更优预测结果。
    卷积神经网络最常被应用的方面是计算机的图像识别, 不过因为不断地创新, 它也被应用在视频分析, 自然语言处理, 药物发现, 等等。

    神经网络是由一连串的神经层组成,每一层神经层里面存在有很多的神经元. 这些神经元就是神经网络识别事物的关键. 每一种神经网络都会有输入输出值, 当输入值是图片的时候, 实际上输入神经网络的并不是那些色彩缤纷的图案,而是一堆堆的数字
    当神经网络需要处理这么多输入信息的时候, 也就是卷积神经网络就可以发挥它的优势的时候了.

    卷积神经网络拆开为 “卷积” 和 “神经网络”

    1. 卷积也就是说神经网络不再是对每个像素的输入信息做处理了,而是图片上每一小块像素区域进行处理, 这种做法加强了图片信息的连续性. 使得神经网络能看到图形, 而非一个点.
    2. 具体来说, 卷积神经网络有一个批量过滤器, 持续不断的在图片上滚动收集图片里的信息,每一次收集的时候都只是收集一小块像素区域, 然后把收集来的信息进行整理, 这时候整理出来的信息有了一些实际上的呈现
    3. 这时的神经网络能看到一些边缘的图片信息, 然后在以同样的步骤, 用类似的批量过滤器扫过产生的这些边缘信息, 神经网络从这些边缘信息里面总结出更高层的信息结构,比如说总结的边缘能够画出眼睛,鼻子等等. 再经过一次过滤, 脸部的信息也从这些眼睛鼻子的信息中被总结出来. 最后我们再把这些信息套入几层普通的全连接神经层进行分类, 这样就能得到输入的图片能被分为哪一类的结果了

    例子

    在这里插入图片描述
    猫的图片:长、宽、高(高指的是图片的颜色信息,黑白高只有1;若是彩色,则可能有RGB三种信息,高度为3) 三个参数

    过滤器就是图片上方不断移动的,不断在图片收集小批的像素块,收集完所有信息后,输出的值可以理解为一个高度更高,长宽更小的图片,图片中包含了一些边缘信息
    在这里插入图片描述
    以同样的步骤,再进行多次卷积,长宽再次压缩,高度增加,产生对输入图片更深的理解,将压缩的信息潜入普通的神经分析层上,就能对这些信息进行分类了
    在这里插入图片描述

    池化(pooling)

    在每一次卷积的时候, 神经层可能会无意地丢失一些信息。
    池化 (pooling) 可以解决该问题。

    1. 池化是一个筛选过滤的过程, 能将 layer 中有用的信息筛选出来, 给下一个层分析
    2. 同时也减轻了神经网络的计算负担

    也就是说在卷积的时候, 不压缩长宽, 尽量地保留更多信息, 压缩的工作就交给池化。这样的一项附加工作能够很有效的提高准确性。
    在这里插入图片描述

    常见的CNN结构

    在这里插入图片描述
    从下到上的顺序:

    1. 输入的图片(image)
    2. 经过一层卷积层 (convolution)
    3. 用池化(pooling)方式处理卷积的信息
    4. 在经过一次同样的处理
    5. 得到的第二次处理的信息传入两层全连接的神经层 (fully connected)
    6. 最后在接上一个分类器(classifier)进行分类预测

    CNN神经网络

    例:识别手写数字

    """
    View more, visit my tutorial page: https://mofanpy.com/tutorials/
    My Youtube Channel: https://www.youtube.com/user/MorvanZhou
    
    Dependencies:
    torch: 0.4
    torchvision
    matplotlib
    """
    # library
    # standard library
    import os
    
    # third-party library
    import torch
    import torch.nn as nn
    import torch.utils.data as Data
    import torchvision
    import matplotlib.pyplot as plt
    
    # torch.manual_seed(1)    # reproducible
    
    # Hyper Parameters
    EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
    BATCH_SIZE = 50
    LR = 0.001              # learning rate
    DOWNLOAD_MNIST = False
    
    
    # Mnist digits dataset
    if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
        # not mnist dir or mnist is empyt dir
        DOWNLOAD_MNIST = True
    
    # Mnist 手写数字 下载数据
    train_data = torchvision.datasets.MNIST(
        root='./mnist/',  # 保存位置
        train=True,                                     # this is training data 下载的数据是黑白的手写数字,故只有黑白两个层
        transform=torchvision.transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to  转换 PIL.Image or numpy.ndarray 成
                                                        # torch.FloatTensor (C x H x W), 训练的时候 normalize 成 [0.0, 1.0] 区间
                                                        # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
        download=DOWNLOAD_MNIST,   # 没下载就下载, 下载了就不用再下了
    )
    
    # plot one example
    print(train_data.train_data.size())                 # (60000, 28, 28)
    print(train_data.train_labels.size())               # (60000)
    plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
    plt.title('%i' % train_data.train_labels[0])
    plt.show()
    
    # Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
    # 批训练 50samples, 1 channel, 28x28 (50, 1, 28, 28)
    train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
    
    # pick 2000 samples to speed up testing 为了节约时间, 我们测试时只测试前2000个
    test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
    test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255.   # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
    test_y = test_data.test_labels[:2000]
    
    # 建立神经网络
    class CNN(nn.Module):
        def __init__(self):
            super(CNN, self).__init__()
            # 一般卷积网络的一层包括了三个东西 卷积核,激活层,pool池化层
            self.conv1 = nn.Sequential(         # input shape (1, 28, 28)
                # 卷积核就是一个过滤器
                nn.Conv2d(
                    in_channels=1,              # input height 该图片的厚度为1
                    out_channels=16,            # n_filters 输出的高度(提取出了16个该区域的特征)
                    kernel_size=5,              # filter size 过滤器的宽和高为5个像素点
                    stride=1,                   # filter movement/step 过滤器每隔多少步跳一下(指过滤器每次移动多少个像素)
                    padding=2,                  # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1 给图片一圈多加为0的数据
                ),                              # output shape (16, 28, 28)
    
                nn.ReLU(),                      # activation 激励函数
    
                # 筛选重要信息 这一步变成长宽更窄,但高度不会变的图片(pooling压缩)
                nn.MaxPool2d(kernel_size=2),    # 在 2x2 空间里向下采样 choose max value in 2x2 area, output shape (16, 14, 14)
            )
            self.conv2 = nn.Sequential(         # input shape (16, 14, 14)
                nn.Conv2d(16, 32, 5, 1, 2),     # output shape (32, 14, 14)
                nn.ReLU(),                      # activation
                nn.MaxPool2d(2),                # output shape (32, 7, 7)
            )
            self.out = nn.Linear(32 * 7 * 7, 10)   # fully connected layer, output 10 classes 由于是识别数字,因此有10个分类
    
        def forward(self, x):
            x = self.conv1(x)
            x = self.conv2(x)
            x = x.view(x.size(0), -1)           # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
            output = self.out(x)
            return output, x    # return x for visualization
    
    
    cnn = CNN()
    print(cnn)  # net architecture
    
    optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)   # optimize all cnn parameters
    loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted
    
    # following function (plot_with_labels) is for visualization, can be ignored if not interested
    from matplotlib import cm
    try: from sklearn.manifold import TSNE; HAS_SK = True
    except: HAS_SK = False; print('Please install sklearn for layer visualization')
    def plot_with_labels(lowDWeights, labels):
        plt.cla()
        X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
        for x, y, s in zip(X, Y, labels):
            c = cm.rainbow(int(255 * s / 9)); plt.text(x, y, s, backgroundcolor=c, fontsize=9)
        plt.xlim(X.min(), X.max()); plt.ylim(Y.min(), Y.max()); plt.title('Visualize last layer'); plt.show(); plt.pause(0.01)
    
    plt.ion()
    # training and testing
    for epoch in range(EPOCH):
        for step, (b_x, b_y) in enumerate(train_loader):   # gives batch data, normalize x when iterate train_loader
    
            output = cnn(b_x)[0]               # cnn output
            loss = loss_func(output, b_y)   # cross entropy loss
            optimizer.zero_grad()           # clear gradients for this training step
            loss.backward()                 # backpropagation, compute gradients
            optimizer.step()                # apply gradients
    
            if step % 50 == 0:
                test_output, last_layer = cnn(test_x)
                pred_y = torch.max(test_output, 1)[1].data.numpy()
                accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
                print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
                if HAS_SK:
                    # Visualization of trained flatten layer (T-SNE)
                    tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
                    plot_only = 500
                    low_dim_embs = tsne.fit_transform(last_layer.data.numpy()[:plot_only, :])
                    labels = test_y.numpy()[:plot_only]
                    plot_with_labels(low_dim_embs, labels)
    plt.ioff()
    
    # print 10 predictions from test data
    test_output, _ = cnn(test_x[:10])
    pred_y = torch.max(test_output, 1)[1].data.numpy()
    print(pred_y, 'prediction number')
    print(test_y[:10].numpy(), 'real number')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
  • 相关阅读:
    Python爬虫(三)
    计算机网络基本概念、OSI七层模型、TCP/IP协议模型、GNS3环境搭建(计算机网络一)
    ABP处理非结构化数据的方式:分布式文件系统minio
    Flink 报错:写入数据到流加载失败
    [附源码]Python计算机毕业设计大学生项目众筹系统
    给你一个案例怎么测试
    VUE学习笔记-1
    数据可视化素材分享 | 数十图表、无数模板
    kafka-生产者拦截器(SpringBoot整合Kafka)
    智慧水务解决方案-最新全套文件
  • 原文地址:https://blog.csdn.net/weixin_45427596/article/details/127162803