cnn卷积神经网络应用_卷积神经网络(CNN):应用的核心概念
cnn卷積神經(jīng)網(wǎng)絡(luò)應(yīng)用
In this tutorial, we’ll work through the core concepts of convolutional neural networks (CNNs). To do this, we’ll use a common dataset — the MNIST dataset—and a standard deep learning task—image classification
在本教程中,我們將研究卷積神經(jīng)網(wǎng)絡(luò)(CNN)的核心概念。 為此,我們將使用一個(gè)通用的數(shù)據(jù)集-MNIST數(shù)據(jù)集-和一個(gè)標(biāo)準(zhǔn)的深度學(xué)習(xí)任務(wù)-圖像分類
The goal here is to walk through an example that will illustrate the processes involved in building a convolutional neural network. The skills you will learn here can easily be transferred to a separate dataset.
這里的目標(biāo)是通過(guò)一個(gè)示例來(lái)說(shuō)明構(gòu)建卷積神經(jīng)網(wǎng)絡(luò)所涉及的過(guò)程。 您將在此處學(xué)習(xí)的技能可以輕松地轉(zhuǎn)移到單獨(dú)的數(shù)據(jù)集中。
So let’s go deeper…pun intended.
因此,讓我們更深入……雙關(guān)語(yǔ)。
建立 (Setup)
In this step, we need to import Keras and other packages that we’re going to use in building our CNN. Import the following packages:
在這一步中,我們需要導(dǎo)入Keras和其他將用于構(gòu)建CNN的軟件包。 導(dǎo)入以下軟件包:
Sequential is used to initialize the neural network.
順序用于初始化神經(jīng)網(wǎng)絡(luò)。
Conv2D is used to make the convolutional network work with images.
Conv2D用于使卷積網(wǎng)絡(luò)處理圖像。
MaxPooling2D is used to add the pooling layers.
MaxPooling2D用于添加池化層。
Flatten is the function that converts the pooled feature map to a single column, which is then passed to the fully connected layer.
Flatten是將合并的要素映射轉(zhuǎn)換為單列,然后將其傳遞到完全連接的層的功能。
Dense adds the fully connected layer to the neural network.
密集將完全連接的層添加到神經(jīng)網(wǎng)絡(luò)。
Reshape is used to change the shape of the input array
重塑用于 改變輸入數(shù)組的形狀
導(dǎo)入數(shù)據(jù)集 (Import the Dataset)
Next, we’ll import the mnist dataset from Keras. We load in the training set and the testing set. We have to scale our data so that it will be compatible with our network. In this case, we scale the images by dividing them by 255. This will ensure that the array values are numbers between 0 and 1.
接下來(lái),我們將從mnist導(dǎo)入mnist 數(shù)據(jù)集 。 我們加載訓(xùn)練集和測(cè)試集。 我們必須擴(kuò)展數(shù)據(jù),以使其與我們的網(wǎng)絡(luò)兼容。 在這種情況下,我們通過(guò)將圖像除以255來(lái)縮放圖像。這將確保數(shù)組值是介于0和1之間的數(shù)字。
mnist = tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()x_train, x_test = x_train / 255.0, x_test / 255.0
validation_data = x_test, y_test
可視化數(shù)字 (Visualize a digit)
We can visualize a single digit using Matplotlib. This is important so that we can see a sample of what the digits in the dataset look like.
我們可以使用Matplotlib可視化一位數(shù)。 這很重要,因此我們可以看到數(shù)據(jù)集中的數(shù)字樣本。
import matplotlib.pyplot as pltimage = x_train[2]fig = plt.figureplt.imshow(image, cmap=’gray’)plt.show()建立 (Setup)
In this step, we need to import Keras and other packages that we’re going to use in building the CNN. Import the following packages:
在這一步中,我們需要導(dǎo)入Keras和其他將用于構(gòu)建CNN的軟件包。 導(dǎo)入以下軟件包:
Sequential is used to initialize the neural network.
順序用于初始化神經(jīng)網(wǎng)絡(luò)。
Conv2D is used to make the convolutional network that deals with the images.
Conv2D用于制作處理圖像的卷積網(wǎng)絡(luò)。
MaxPooling2D layer is used to add the pooling layers.
MaxPooling2D層用于添加池化層。
Flatten is the function that converts the pooled feature map to a single column that is passed to the fully connected layer.
Flatten是將合并的要素映射轉(zhuǎn)換為傳遞到完全連接的圖層的單個(gè)列的功能。
Dense adds the fully connected layer to the neural network.
密集將完全連接的層添加到神經(jīng)網(wǎng)絡(luò)。
Reshape for changing the shape of the input array
重塑用于改變輸入陣列的形狀
from tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,Reshape
導(dǎo)入數(shù)據(jù)集 (Import the Dataset)
Next, we’ll import the mnist dataset from Keras. We load in the training set and the testing set. We’ve seen previously that we have to scale our data. In this case, we scale the images by dividing by 255. This will ensure that the array values are numbers between 0 and 1.
接下來(lái),我們將從Keras導(dǎo)入mnist數(shù)據(jù)集。 我們加載訓(xùn)練集和測(cè)試集。 之前我們已經(jīng)看到我們必須擴(kuò)展數(shù)據(jù)。 在這種情況下,我們將圖像除以255進(jìn)行縮放。這將確保數(shù)組值是介于0和1之間的數(shù)字。
mnist = tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()x_train, x_test = x_train / 255.0, x_test / 255.0
validation_data = x_test, y_test
可視化數(shù)字 (Visualize a Number)
We can visualize a single digit using Matplotlib.
我們可以使用Matplotlib可視化一位數(shù)。
import matplotlib.pyplot as pltimage = x_train[2]fig = plt.figureplt.imshow(image, cmap=’gray’)plt.show()Machine learning is rapidly moving closer to where data is collected — edge devices. Subscribe to the Fritz AI Newsletter to learn more about this transition and how it can help scale your business.
機(jī)器學(xué)習(xí)正Swift向收集數(shù)據(jù)的地方(邊緣設(shè)備)靠近。 訂閱Fritz AI新聞通訊以了解有關(guān)此過(guò)渡及其如何幫助您擴(kuò)展業(yè)務(wù)的更多信息 。
建立網(wǎng)絡(luò) (Building the Network)
Before we start building the network, let’s confirm the shape of the training data. This is important because the shape will be needed by the convolution layer. We’ll use this shape later to reshape the size of the input data.
在開(kāi)始構(gòu)建網(wǎng)絡(luò)之前,讓我們確認(rèn)訓(xùn)練數(shù)據(jù)的形狀。 這很重要,因?yàn)榫矸e層將需要該形狀。 稍后我們將使用此形狀來(lái)調(diào)整輸入數(shù)據(jù)的大小。
x_train.shape(60000, 28, 28)We kick off by initializing the sequential model. Then we pass in our layers as a list. The first layer is the reshape layer for changing the shape of the input data. We already know that each image has a size of 28 by 28. We also pass 1 to the reshape layer to ensure that the input data has a single color channel.
我們通過(guò)初始化順序模型開(kāi)始。 然后,我們將圖層作為列表傳遞。 第一層是用于改變輸入數(shù)據(jù)形狀的重塑層。 我們已經(jīng)知道每個(gè)圖像的尺寸為28 x28。我們還將1傳遞給重塑圖層,以確保輸入數(shù)據(jù)具有單個(gè)顏色通道。
Next, we create the convolution layer. This layer takes in a couple of parameters:
接下來(lái),我們創(chuàng)建卷積層。 該層接受幾個(gè)參數(shù):
filters — the number of output filters in the convolution, i.e the number of feature detectors. 32 is a common number of feature detectors used.
過(guò)濾器 -卷積中輸出過(guò)濾器的數(shù)量,即特征檢測(cè)器的數(shù)量。 32是常用的特征檢測(cè)器。
kernel_size — the dimensions of the feature detector matrix. This is the width and height of the convolution window.
kernel_size —特征檢測(cè)器矩陣的尺寸。 這是卷積窗口的寬度和高度。
input_shape — the shape of the input data. Since we included a reshape layer, we don’t need to pass this.
input_shape —輸入數(shù)據(jù)的形狀。 由于我們包含了一個(gè)重塑層,因此我們不需要傳遞它。
activation — the activation function to be uses. The ReLu activation function is a common choice.
激活 -要使用的激活功能。 ReLu激活功能是常見(jiàn)的選擇。
The next item on the list is to add the pooling layer. In this step, the size of the feature map is reduced by taking the maximum value during the pooling process. 2 by 2 is a commonly-used pool size. This will reduce the size of the feature map while maintaining the most important features needed to identify the digits.
列表上的下一項(xiàng)是添加池化層。 在此步驟中,通過(guò)在合并過(guò)程中取最大值來(lái)減小特征圖的大小。 2 x 2是常用的池大小。 這將減少要素圖的大小,同時(shí)保留識(shí)別數(shù)字所需的最重要的要素。
After that, we flatten the feature maps. This will convert the feature maps into a single column.
之后,我們將要素圖展平 。 這將轉(zhuǎn)換 要素映射到單列。
The results obtained above are then passed to the dense layer. The parameters passed to this layer are the number of neurons and the activation function. In this case, those are 128 and ReLu, respectively.
然后將上面獲得的結(jié)果傳遞到致密層 。 傳遞給該層的參數(shù)是神經(jīng)元數(shù)量和激活函數(shù)。 在這種情況下,它們分別是128和ReLu。
Finally, we created our output layer. 10 is used here, because our dataset has 10 classes. We’ll use the softmax activation function because classes are mutually exclusive—an image can’t be, for example, classified as numbers 0 and 1 at the same time. If the case was otherwise, we’d use the sigmoid activation function.
最后,我們創(chuàng)建了輸出層 。 這里使用10,因?yàn)槲覀兊臄?shù)據(jù)集有10個(gè)類。 我們將使用softmax激活功能,因?yàn)? 類是互斥的-例如,圖像不能同時(shí)分類為數(shù)字0和1。 如果不是這種情況,我們將使用S型激活函數(shù) 。
model = Sequential([Reshape((28, 28, 1)),Conv2D(filters=64,kernel_size=(3,3),activation=’relu’),MaxPooling2D(pool_size=(2,2)),Flatten(),Dense(128, activation=’relu’),Dense(10, activation=’softmax’)])編譯網(wǎng)絡(luò) (Compiling the Network)
We now need to apply gradient descent to the number. This is done at the compile stage. We pass the following parameters:
現(xiàn)在,我們需要對(duì)數(shù)字應(yīng)用梯度下降 。 這是在編譯階段完成的。 我們傳遞以下參數(shù):
optimizer — the type of gradient descent to use; in this case, we’ll use stochastic gradient descent
優(yōu)化器 -要使用的梯度下降類型; 在這種情況下,我們將使用隨機(jī)梯度下降
loss — the cost function to use. We use sparse_categorical_crossentropy because the classes are integer-encoded. Had we performed one-hot encoding, we’d have to use the categorical cross-entropy loss function.
損失 —使用的成本函數(shù)。 我們使用sparse_categorical_crossentropy 因?yàn)檫@些類是整數(shù)編碼的。 如果我們執(zhí)行了一次熱編碼,則必須使用分類交叉熵?fù)p失函數(shù)。
metrics — the evaluation metrics to monitor; in this case, accuracy
指標(biāo) —要監(jiān)視的評(píng)估指標(biāo); 在這種情況下,準(zhǔn)確性
安裝CNN (Fitting the CNN)
Next, we fit the training set to the network. epochs is the number of rounds the data will pass through the network. validation_data is the test dataset that will be used to evalutate the performance of the model during training.
接下來(lái),我們將訓(xùn)練集適合網(wǎng)絡(luò)。 epochs是數(shù)據(jù)將通過(guò)網(wǎng)絡(luò)的輪數(shù)。 validation_data是測(cè)試數(shù)據(jù)集,將用于評(píng)估訓(xùn)練期間模型的性能。
model.fit(x=x_train,y=y_train,epochs=5,validation_data=validation_data)評(píng)估網(wǎng)絡(luò) (Evaluating the Network)
We can now check the performance of the network. We do so using the evaluate function, passing in the test set. The first item in the result produced is the loss, and the second is the accuracy.
現(xiàn)在,我們可以檢查網(wǎng)絡(luò)的性能。 我們使用evaluate函數(shù)來(lái)傳遞測(cè)試集。 產(chǎn)生的結(jié)果中的第一項(xiàng)是loss ,第二項(xiàng)是accuracy 。
model.evaluate(x_test,y_test)313/313 [==============================] - 2s 8ms/step - loss: 0.1116 - accuracy: 0.9668
[0.1115519180893898, 0.9667999744415283]
檢查預(yù)測(cè) (Checking the Predictions)
Let’s now see if our network is able to correctly identify a digit. We kick off by making predictions:
現(xiàn)在讓我們看看我們的網(wǎng)絡(luò)是否能夠正確識(shí)別數(shù)字。 我們通過(guò)做出預(yù)測(cè)開(kāi)始:
predictions = model.predict(x_test)For example, let’s check the digit at index 2 in the test set.
例如,讓我們檢查測(cè)試集中索引2處的數(shù)字。
image = x_test[2]fig = plt.figureplt.imshow(image, cmap=’gray’)plt.show()This is clearly the digit one. Let’s compare that with the prediction made for digit 1.
這顯然是數(shù)字。 讓我們將其與數(shù)字1的預(yù)測(cè)進(jìn)行比較。
import numpy as npnp.set_printoptions(suppress=True)
predictions[2]*100array([ 0.00077352, 99.63969 , 0.07214971, 0.03056707, 0.00606834,
0.00301009, 0.02507676, 0.11558129, 0.10332227, 0.00378496],
dtype=float32)
From the predictions below, we can see that the digit 1 — at index 1 in the prediction array— has the highest prediction.
從下面的預(yù)測(cè)中,我們可以看到數(shù)字1(在預(yù)測(cè)數(shù)組的索引1處)具有最高的預(yù)測(cè)。
0.00077352 — digit 0 prediction
0.00077352 —數(shù)字0預(yù)測(cè)
99.63969 — digit 1 prediction
99.63969 —數(shù)字1的預(yù)測(cè)
0.07214971 — digit 2 prediction
0.07214971-數(shù)字2預(yù)測(cè)
0.03056707 — digit 3 prediction
0.03056707 —數(shù)字3預(yù)測(cè)
0.00606834 — digit 4 prediction
0.00606834 —數(shù)字4預(yù)測(cè)
0.00301009 — digit 5 prediction
0.00301009-數(shù)字5預(yù)測(cè)
0.02507676 — digit 6 prediction
0.02507676 —數(shù)字6的預(yù)測(cè)
0.11558129 — digit 7 prediction
0.11558129-數(shù)字7預(yù)測(cè)
0.10332227 — digit 8 prediction
0.10332227 —數(shù)字8預(yù)測(cè)
0.00378496 — digit 9 prediction
0.00378496 —數(shù)字9預(yù)測(cè)
We can check another prediction by plotting the digit at index 0 in the testing set. Let’s try number 7.
我們可以通過(guò)在測(cè)試集中的索引0處繪制數(shù)字來(lái)檢查另一個(gè)預(yù)測(cè)。 讓我們嘗試數(shù)字7。
image = x_test[0]fig = plt.figure
plt.imshow(image, cmap=’gray’)
plt.show()
Next, we’ll look at the prediction at index 0 to see if it’s also number 7. Clearly, 7 has the highest prediction.
接下來(lái),我們將看一下索引0處的預(yù)測(cè),看看它是否也是7。顯然,7具有最高的預(yù)測(cè)。
import numpy as npnp.set_printoptions(suppress=True)predictions[0]*100array([ 0.00032742, 0.00000064, 0.00919174, 0.09352361, 0.00000077, 0.00045173, 0.00000015, 99.88985 , 0.00224745, 0.00440894], dtype=float32)結(jié)論 (Conclusion)
Well done for making it this far! In this article, we’ve used CNN core concepts to develop a working convolutional neural network. We have seen the steps you’ll need to take, and we’ve evaluated our trained model. Now you can try this out with a different dataset to see if you can reproduce the network we built here.
到現(xiàn)在為止做得很好! 在本文中,我們使用了CNN核心概念來(lái)開(kāi)發(fā)有效的卷積神經(jīng)網(wǎng)絡(luò)。 我們已經(jīng)看到了您需要采取的步驟,并且已經(jīng)評(píng)估了我們訓(xùn)練有素的模型。 現(xiàn)在,您可以使用其他數(shù)據(jù)集進(jìn)行嘗試,以查看是否可以重現(xiàn)我們?cè)诖颂帢?gòu)建的網(wǎng)絡(luò)。
Editor’s Note: Heartbeat is a contributor-driven online publication and community dedicated to exploring the emerging intersection of mobile app development and machine learning. We’re committed to supporting and inspiring developers and engineers from all walks of life.
編者注: 心跳 是由貢獻(xiàn)者驅(qū)動(dòng)的在線出版物和社區(qū),致力于探索移動(dòng)應(yīng)用程序開(kāi)發(fā)和機(jī)器學(xué)習(xí)的新興交集。 我們致力于為各行各業(yè)的開(kāi)發(fā)人員和工程師提供支持和啟發(fā)。
Editorially independent, Heartbeat is sponsored and published by Fritz AI, the machine learning platform that helps developers teach devices to see, hear, sense, and think. We pay our contributors, and we don’t sell ads.
Heartbeat在編輯上是獨(dú)立的,由以下機(jī)構(gòu)贊助和發(fā)布 Fritz AI ,一種機(jī)器學(xué)習(xí)平臺(tái),可幫助開(kāi)發(fā)人員教設(shè)備看,聽(tīng),感知和思考。 我們向貢獻(xiàn)者付款,并且不出售廣告。
If you’d like to contribute, head on over to our call for contributors. You can also sign up to receive our weekly newsletters (Deep Learning Weekly and the Fritz AI Newsletter), join us on Slack, and follow Fritz AI on Twitter for all the latest in mobile machine learning.
如果您想做出貢獻(xiàn),請(qǐng)繼續(xù)我們的 呼吁捐助者 。 您還可以注冊(cè)以接收我們的每周新聞通訊(《 深度學(xué)習(xí)每周》 和《 Fritz AI新聞通訊》 ),并加入我們 Slack ,然后繼續(xù)關(guān)注Fritz AI Twitter 提供了有關(guān)移動(dòng)機(jī)器學(xué)習(xí)的所有最新信息。
翻譯自: https://heartbeat.fritz.ai/convolutional-neural-networks-cnns-core-concepts-applied-3c12e60e2c40
cnn卷積神經(jīng)網(wǎng)絡(luò)應(yīng)用
總結(jié)
以上是生活随笔為你收集整理的cnn卷积神经网络应用_卷积神经网络(CNN):应用的核心概念的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 季度怎么划分,季报和年报的区别
- 下一篇: 国债指数