mnist数据集彩色图像

Below are the steps to build a model that can classify handwritten digits with an accuracy of more than 95%. While reading this article I suggest you simultaneously try the code in colab notebook. Follow the steps and observe the outputs.

以下是构建模型的步骤,该模型可以以95%以上的精度对手写数字进行分类。 阅读本文时,建议您同时尝试colab笔记本中的代码。 遵循步骤并观察输出。

You can find the complete code on GitHub as well.

您也可以在GitHub上找到完整的代码。

1.准备输入数据 (1. Prepare the input data)

Step-1 Import the required libraries

步骤1导入所需的库

import numpy as npimport kerasfrom keras.datasets import mnistimport matplotlib.pyplot as plt
  • Numpy has many functions that provide support for arrays. An image is nothing but a NumPy array containing pixels of the data points.

    Numpy具有许多对数组提供支持的功能。 图像不过是包含数据点像素的NumPy数组。

  • Keras library has many functions that make it really easy to build models.

    Keras库具有许多使构建模型变得非常容易的功能。

  • Matplotlib helps to plot images and visualize results.

    Matplotlib帮助绘制图像并可视化结果。

Step-2 Load the MNIST dataset

步骤2加载MNIST数据集

Sample of MNIST dataset containing hand-written digits.
包含手写数字的MNIST数据集样本。

Dataset- This dataset consists of 60,000 28x28 grayscale images of the 10 digits (0–9), along with a test set of 10,000 images[1]. More info can be found at the MNIST homepage.

Dataset-该数据集包括60000个28x28的10个数字(0-9)灰度图像,与测试组10,000张图像[1]的沿。 可以在MNIST主页上找到更多信息。

(x_train, y_train), (x_test, y_test)=mnist.load_data()Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11493376/11490434 [==============================] - 0s 0us/step

Step-3 Reshape the input data

步骤3重塑输入数据

#View the shape of loaded datasetx_train.shapex_test.shapey_train.shapex_test.shape

The size of input images is 78x78. But we can pass only flattened arrays in a Deep Neural Network. So let's reshape the images.

输入图像的大小为78x78 。 但是我们只能在深度神经网络中传递扁平化的数组。 因此,让我们重塑图像。

#Reshape the datasetx_test=x_test.reshape(-1,784)x_train=x_train.reshape(-1, 784)

2.建立模型 (2. Build your model)

We will build a Sequential model using Keras layers. In a sequential model output of the previous layer is treated as an input for the next layer.

我们将使用Keras层构建顺序模式 l。 在顺序模型中,将上一层的输出视为下一层的输入。

Step-4 Import the libraries required to build a model

步骤4导入构建模型所需的库

from keras.models import Sequentialfrom keras.layers import Dense
  • For building a sequential model we import Sequential class from keras.models.

    为了构建顺序模型,我们从keras.models导入顺序类。

  • We import Dense class to add dense layers in our network. In a dense layer, each neuron of a layer is connected to all the neurons of the next layer. In simple words, a dense layer is a fully connected layer.我们导入Dense类以在网络中添加密集层。 在密集层中,一层的每个神经元都连接到下一层的所有神经元。 简而言之,致密层是完全连接的层。

Step-5 Add layers to your network

步骤5将图层添加到您的网络

model=Sequential()

We created an object of Sequential class called a model. Now we will add the required layers to the model using model.add().

我们创建了一个称为模型的序列类对象。 现在,我们将使用model.add()将所需的图层添加到模型中。

model.add(Dense(units=64, activation=’relu’,input_shape= (784, )))model.add(Dense(units= 64, activation=’relu’))model.add(Dense(units= 128, activation=’relu’))model.add(Dense(units= 64, activation=’relu’))model.add(Dense(units= 10, activation=’softmax’))

In this model, we will add 5 dense layers. For each layer, we have to give values for a few parameters. The two important parameters are units and activation. However, there are many other parameters like kernal_initializer, bias_initializer, etc, which will be initialized with their default values.

在此模型中,我们将添加5个密集层。 对于每一层,我们必须给出一些参数的值。 两个重要参数是单位激活 。 但是,还有许多其他参数,如kernal_initializerbias_initializer等,将使用其默认值进行初始化。

  • Units: refers to the number of neurons in a layer. We generally increase the number of neurons preferably in the form of 2^n. In multiclass classification number of units in the last layer is equal to a number of different classes.

    单位:指一层中神经元的数量。 我们通常以2 ^ n的形式增加神经元的数量。 在多类别分类中,最后一层中的单元数等于多个不同类别。

  • Activation: The activation function to be used in each layer. In our model, we have used Relu(Rectified Linear Unit function) in hidden layers and Softmax function in the output layer.

    激活:在每个层中使用的激活功能。 在我们的模型中,我们在隐藏层中使用了Relu(整流线性单位函数),在输出层中使用了Softmax函数。

  • input_shape: This argument is passed only for the first layer because the first layer does not know what kind of input will be given by the user. Hence we have to give it explicitly. The other layers will get the input of the same shape as the shape of the output of the previous layer.

    input_shape :此参数仅在第一层传递,因为第一层不知道用户将提供哪种输入。 因此,我们必须明确给出它。 其他层将获得与前一层输出形状相同的形状的输入。

The output is given as activation(dot(input, kernal)+bias) where activation is the element-wise activation function passed as the activation argument, kernal is a weights matrix created by the layer, and bias is a bias vector created by the layer.

输出给出为activation(dot(input(input,kernal)+ bias) ,其中activation是作为激活参数传递的逐元素激活函数,kernal是由图层创建的权重矩阵,bias是由图层创建的偏置向量。层。

View the model summary if you want

查看模型摘要(如果需要)

model.summary()Model: "sequential" _________________________________________________________________ Layer (type)                 Output Shape              Param #    ================================================================= dense (Dense)                (None, 64)                50240      _________________________________________________________________ dense_1 (Dense)              (None, 64)                4160       _________________________________________________________________ dense_2 (Dense)              (None, 128)               8320       _________________________________________________________________ dense_3 (Dense)              (None, 64)                8256       _________________________________________________________________ dense_4 (Dense)              (None, 10)                650        ================================================================= Total params: 71,626 Trainable params: 71,626 Non-trainable params: 0 _________________________________________________________________

Step-6 Compile the model

步骤6编译模型

Now we will compile the model and give the following parameters :

现在,我们将编译模型并提供以下参数:

  • Optimizer: It optimizes the loss function. Here we will use adam optimizer.

    优化器 :优化损失函数。 在这里,我们将使用adam优化器。

  • loss: This model is performing multiclass classification so we will use categorical_crossentropy loss.

    loss :此模型正在执行多类分类,因此我们将使用categorical_crossentropy损失。

  • metrics: It defines on what basis we have to evaluate our model. Here, we will use accuracy as the metrics.

    指标 :它定义了我们必须在什么基础上评估我们的模型。 在这里,我们将准确性作为指标。

model.compile(optimizer=”adam”, loss=’categorical_crossentropy’,metrics=[‘accuracy’] )

Step-7 Convert the output vector to one hot vector

步骤7将输出向量转换为一个热向量

To know what is one-hot encoding and why it is necessary read this.

要了解什么是一键编码以及为什么有必要阅读此内容 。

from keras.utils import to_categoricaly_train=to_categorical(y_train)

3.训练模型 (3. Train your model)

Step-8 Fit the model on your training dataset

步骤8将模型拟合到您的训练数据集

Now we will fit the model on training data and save the model as ‘hist’. We have to give the values of the following parameters:

现在我们将模型拟合到训练数据上,并将模型另存为“历史”。 我们必须提供以下参数的值:

  • x: the input vector of training data.

    x :训练数据的输入向量。

  • y: output vector of training data, which was one-hot encoded.

    y :训练数据的输出向量,它是一热编码的。

  • batch_size: it is significant for large datasets. We chose 32 as the batch size which means in every iteration 32 examples will be processed.

    batch_size :对于大型数据集而言意义重大。 我们选择32作为批处理大小,这意味着在每次迭代中将处理32个示例。

  • epochs: the number of epochs

    纪元 :纪元数

  • validation_split: the ratio of total data to be used for validation.

    validation_split :用于验证的总数据的比率。

There are many other parameters but they will be initialized with their default values.

还有许多其他参数,但是它们将使用其默认值进行初始化。

hist=model.fit(x=x_train, y=y_train, batch_size=32, epochs=10, validation_split=0.2, shuffle=True)Epoch 1/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.8803 - accuracy: 0.8268 - val_loss: 0.3275 - val_accuracy: 0.9077 Epoch 2/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.2615 - accuracy: 0.9238 - val_loss: 0.2284 - val_accuracy: 0.9383 Epoch 3/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.2054 - accuracy: 0.9406 - val_loss: 0.1965 - val_accuracy: 0.9447 Epoch 4/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.1703 - accuracy: 0.9504 - val_loss: 0.1894 - val_accuracy: 0.9503 Epoch 5/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.1489 - accuracy: 0.9569 - val_loss: 0.2132 - val_accuracy: 0.9452 Epoch 6/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.1319 - accuracy: 0.9620 - val_loss: 0.1746 - val_accuracy: 0.9558 Epoch 7/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.1182 - accuracy: 0.9655 - val_loss: 0.1546 - val_accuracy: 0.9583 Epoch 8/10 1500/1500 [==============================] - 3s 2ms/step - loss: 0.1066 - accuracy: 0.9695 - val_loss: 0.1533 - val_accuracy: 0.9605 Epoch 9/10 1500/1500 [==============================] - 4s 2ms/step - loss: 0.0952 - accuracy: 0.9722 - val_loss: 0.1680 - val_accuracy: 0.9617 Epoch 10/10 1500/1500 [==============================] - 4s 3ms/step - loss: 0.0868 - accuracy: 0.9747 - val_loss: 0.1775 - val_accuracy: 0.9574
lidesharelideshare

4,测试你的模型 (4.Test your model)

Step-9 Predict outcome for any random datapoint

步骤9预测任何随机数据点的结果

Now lets test one random example from out dataset, say example no 999. To make predictions we use model.predict() function.

现在让我们从数据集中测试一个随机示例,例如999号示例。要进行预测,我们使用model.predict()函数。

model.predict_classes(x_test[999].reshape(-1,784))

The output given is 9 in my code. To check if the prediction made by your model is correct plot the data using plt.imshow().

在我的代码中给出的输出为9。 要检查模型做出的预测是否正确,请使用plt.imshow()绘制数据。

plt.imshow(x_test[999].reshape(28,28), cmap=’gray’)

Our model predicted the input digit correctly.

我们的模型正确预测了输入数字。

翻译自: https://medium.com/analytics-vidhya/implement-your-first-neural-network-8aa43c7c54a6

mnist数据集彩色图像


http://www.taodudu.cc/news/show-863754.html

相关文章:

  • bert使用做文本分类_使用BERT进行深度学习的多类文本分类
  • 垃圾邮件分类器_如何在10个步骤中构建垃圾邮件分类器
  • ai 图灵测试_适用于现代AI系统的“视觉图灵测试”
  • pytorch图像分类_使用PyTorch和Streamlit创建图像分类Web应用
  • 深度学习之对象检测_深度学习时代您应该阅读的12篇文章,以了解对象检测
  • python 梯度下降_Python解释的闭合形式和梯度下降回归
  • 内容管理系统_内容
  • opencv图像深度-1_OpenCV空间AI竞赛之旅(第1部分-初始设置+深度)
  • 概率编程编程_概率编程语言的温和介绍
  • TensorFlow 2.X中的动手NLP深度学习模型准备
  • 时间序列 线性回归 区别_时间序列分析的完整介绍(带R)::线性过程I
  • 深度学习学习7步骤_如何通过4个简单步骤为深度学习标记音频
  • 邮件伪造_伪造品背后的数学
  • 图像匹配与OpenCV模板匹配
  • 边缘计算边缘计算edge_Edge AI-边缘上的计算机视觉推理
  • arduino 入门套件_计算机视觉入门套件
  • 了解LSTM和GRU
  • 使用TensorFlow 2.0+和Keras实现AlexNet CNN架构
  • power bi_如何将Power BI模型的尺寸减少90%!
  • 使用Optuna的XGBoost模型的高效超参数优化
  • latex 表格中虚线_如何识别和修复表格识别中的虚线
  • 构建强化学习_如何构建强化学习项目(第1部分)
  • sam服务器是什么_使用SAM CLI将机器学习模型部署到无服务器后端
  • pca 主成分分析_六分钟的主成分分析(PCA)的直观说明。
  • seaborn 教程_使用Seaborn进行数据可视化教程
  • alexnet 结构_AlexNet的体系结构和实现
  • python做作业没头绪_使用Python做作业
  • 使用Python构建推荐系统的机器学习
  • DeepR —训练TensorFlow模型进行生产
  • 通化红灯_我们如何构建廉价,可扩展的架构来对世界进行卡通化!

mnist数据集彩色图像_使用MNIST数据集构建多类图像分类模型。相关推荐

  1. 运用cnn实现手写体(mnist)数字识别_实现 MNIST 手写数字识别

    1. MNIST 数据集加载 MNIST 数据集可以从MNIST官网下载.也可以通过 Tensorflow 提供的 input_data.py进行载入. 由于上述方法下载数据集比较慢,我已经把下载好的 ...

  2. python数据集划分_机器学习和数据集介绍、数据集划分、特征抽取、归一化

    机器学习介绍和数据集介绍 机器学习: 机器学习是一门多学科交叉专业,涵盖概率论知识,统计学知识,近似理论知识和复杂算法知识,使用计算机作为 工具并致力于真实实时的模拟人类学习方式,并将现有内容进行知识 ...

  3. python如何训练模型生产_手把手教你用Python构建你的第一个多标签图像分类模型(附案例)...

    你正在处理图像数据吗?我们可以使用计算机视觉算法来做很多事情: 对象检测 图像分割 图像翻译 对象跟踪(实时),还有更多-- 这让我思考--如果一个图像中有多个对象类别,我们该怎么办?制作一个图像分类 ...

  4. python图片分类技术介绍_手把手教你用Python构建你的第一个多标签图像分类模型(附案例)!...

    介绍 你正在处理图像数据吗?我们可以使用计算机视觉算法来做很多事情:对象检测 图像分割 图像翻译 对象跟踪(实时),还有更多-- 这让我思考--如果一个图像中有多个对象类别,我们该怎么办?制作一个图像 ...

  5. python图像分类_手把手教你用Python构建你的第一个多标签图像分类模型(附案例)...

    介绍 你正在处理图像数据吗?我们可以使用计算机视觉算法来做很多事情:对象检测 图像分割 图像翻译 对象跟踪(实时),还有更多-- 这让我思考--如果一个图像中有多个对象类别,我们该怎么办?制作一个图像 ...

  6. 独家 | 手把手教你用Python构建你的第一个多标签图像分类模型(附案例)

    翻译:吴金笛 校对:郑滋 本文约4600字,建议阅读12分钟. 本文明确了多标签图像分类的概念,并讲解了如何构建多标签图像分类模型. 介绍 你正在处理图像数据吗?我们可以使用计算机视觉算法来做很多事情 ...

  7. 手把手教你用Python构建你的第一个多标签图像分类模型(附案例)

    原文链接: https://www.analyticsvidhya.com/blog/2019/04/build-first-multi-label-image-classification-mode ...

  8. 使用mnist数据集_使用MNIST数据集上的t分布随机邻居嵌入(t-SNE)进行降维

    使用mnist数据集 It is easy for us to visualize two or three dimensional data, but once it goes beyond thr ...

  9. matlab朴素贝叶斯手写数字识别_基于MNIST数据集实现手写数字识别

    介绍 在TensorFlow的官方入门课程中,多次用到mnist数据集.mnist数据集是一个数字手写体图片库,但它的存储格式并非常见的图片格式,所有的图片都集中保存在四个扩展名为idx*-ubyte ...

最新文章

  1. java中普通代码块,构造代码块,静态代码块的区别及代码示例
  2. ES6之let(理解闭包)和const命令
  3. showModalDialog跨域访问的解决
  4. 基于【 centos7】一 || 安装ELK
  5. mysql实验四图书视图_[数据库实验四.doc
  6. 微软System Center Operations Manager 2012(SCOM )安装图文教程
  7. PSPNet网络要点
  8. 【flyway】flyway Migration checksum mismatch for migration
  9. ES6新特性_ES6扩展运算符的介绍---JavaScript_ECMAScript_ES6-ES11新特性工作笔记013
  10. influxdb入库mysql_InfluxDb(3)基本操作
  11. paip.防止代码命名重复的好方法
  12. 你确定你真的懂Nginx与PHP的交互?
  13. DG半离散格式的转化---基于matlab编写
  14. 最新Apicloud+Vue开发App专题完整
  15. vue3源码effect
  16. Weakly-Supervised Physically Unconstrained Gaze Estimation论文翻译
  17. Java程序朗读文字的实现,jacob.jar
  18. MySQL环境变量的配置(三)(Windows 11)
  19. be yet to用法
  20. 2017-2018-1 20162316刘诚昊 实验一 线性结构

热门文章

  1. linux基础网络设置
  2. 汉澳sinox2014x64server已经能够下载
  3. 阐述:SIP协议是什么
  4. 战痕————道具系统介绍
  5. Svn正确的使用方法
  6. linux系统安装锐捷客户端下载,Linux在宿舍里如何上网?--Fedora下锐捷802.1x客户端软件的安装和使用方法...
  7. java 基本类型 引用_java中 引用类型 和 基本类型 有何区别?
  8. 万能门店小程序_关于传统门店开发微信小程序的优势
  9. java this关键字的使用_老大:我去,你竟然还不会用 this 关键字
  10. android长截屏代码,android长截屏原理及实现代码