日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mnist数据集彩色图像_使用MNIST数据集构建多类图像分类模型。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 np
import keras
from keras.datasets import mnist
import 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 dataset
x_train.shape
x_test.shape
y_train.shape
x_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 Sequential
from 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_initializer , bias_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_categorical
y_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.9574lidesharelideshare

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數據集彩色圖像

總結

以上是生活随笔為你收集整理的mnist数据集彩色图像_使用MNIST数据集构建多类图像分类模型。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。