当前位置: 首页 > news >正文

4_卷积神经网络

卷积神经网络CNN

  • 什么是卷积神经网络
    • 例子
    • 池化(pooling)
    • 常见的CNN结构
  • 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')

相关文章:

  • [C++基础]-入门知识
  • Java8-19新特性(附官网传送门)
  • 加解密相关
  • 【TypeScript教程】# 6:使用webpack打包ts代码
  • Pytorch环境配置(anaconda安装+独显+CUDA+cuDNN)
  • Incorrect string value: ‘\xE6\x9D\x91\xE4\xB8\x8A...‘ for column ‘name‘错误解决
  • git --- git撤销commit(未push)
  • python爬虫的防盗链
  • 【UI自动化】实现C站三连功能
  • 网络规划与部署—ACL命名实验
  • MATLAB-随机森林实现数据回归分析预测
  • SQL考试练习题及全部答案
  • 【C++】内联函数、auto、范围for循环,nullptr
  • Mybatis(第一篇)
  • nodejs+vue+elementui旅游资源网站python-java景点门票预订网站php
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 【140天】尚学堂高淇Java300集视频精华笔记(86-87)
  • 2017-08-04 前端日报
  • 78. Subsets
  • Elasticsearch 参考指南(升级前重新索引)
  • ES2017异步函数现已正式可用
  • JSONP原理
  • python 学习笔记 - Queue Pipes,进程间通讯
  • select2 取值 遍历 设置默认值
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • 【云吞铺子】性能抖动剖析(二)
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • ​你们这样子,耽误我的工作进度怎么办?
  • #FPGA(基础知识)
  • #vue3 实现前端下载excel文件模板功能
  • (1)Nginx简介和安装教程
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (a /b)*c的值
  • (floyd+补集) poj 3275
  • (WSI分类)WSI分类文献小综述 2024
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • **PHP二维数组遍历时同时赋值
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .NET CORE Aws S3 使用
  • .NET Core工程编译事件$(TargetDir)变量为空引发的思考
  • .NET MVC第三章、三种传值方式
  • .NET 动态调用WebService + WSE + UsernameToken
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .Net环境下的缓存技术介绍
  • .Net语言中的StringBuilder:入门到精通
  • [2016.7.Test1] T1 三进制异或
  • [3300万人的聊天室] 作为产品的上游公司该如何?
  • [Angularjs]asp.net mvc+angularjs+web api单页应用
  • [AUTOSAR][诊断管理][ECU][$37] 请求退出传输。终止数据传输的(上传/下载)