cnn卷积神经网络应用_卷积神经网络(CNN):应用的核心概念
cnn卷積神經網絡應用
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
在本教程中,我們將研究卷積神經網絡(CNN)的核心概念。 為此,我們將使用一個通用的數據集-MNIST數據集-和一個標準的深度學習任務-圖像分類
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.
這里的目標是通過一個示例來說明構建卷積神經網絡所涉及的過程。 您將在此處學習的技能可以輕松地轉移到單獨的數據集中。
So let’s go deeper…pun intended.
因此,讓我們更深入……雙關語。
建立 (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:
在這一步中,我們需要導入Keras和其他將用于構建CNN的軟件包。 導入以下軟件包:
Sequential is used to initialize the neural network.
順序用于初始化神經網絡。
Conv2D is used to make the convolutional network work with images.
Conv2D用于使卷積網絡處理圖像。
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是將合并的要素映射轉換為單列,然后將其傳遞到完全連接的層的功能。
Dense adds the fully connected layer to the neural network.
密集將完全連接的層添加到神經網絡。
Reshape is used to change the shape of the input array
重塑用于 改變輸入數組的形狀
導入數據集 (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.
接下來,我們將從mnist導入mnist 數據集 。 我們加載訓練集和測試集。 我們必須擴展數據,以使其與我們的網絡兼容。 在這種情況下,我們通過將圖像除以255來縮放圖像。這將確保數組值是介于0和1之間的數字。
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
可視化數字 (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可視化一位數。 這很重要,因此我們可以看到數據集中的數字樣本。
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:
在這一步中,我們需要導入Keras和其他將用于構建CNN的軟件包。 導入以下軟件包:
Sequential is used to initialize the neural network.
順序用于初始化神經網絡。
Conv2D is used to make the convolutional network that deals with the images.
Conv2D用于制作處理圖像的卷積網絡。
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是將合并的要素映射轉換為傳遞到完全連接的圖層的單個列的功能。
Dense adds the fully connected layer to the neural network.
密集將完全連接的層添加到神經網絡。
Reshape for changing the shape of the input array
重塑用于改變輸入陣列的形狀
from tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,Reshape
導入數據集 (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.
接下來,我們將從Keras導入mnist數據集。 我們加載訓練集和測試集。 之前我們已經看到我們必須擴展數據。 在這種情況下,我們將圖像除以255進行縮放。這將確保數組值是介于0和1之間的數字。
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
可視化數字 (Visualize a Number)
We can visualize a single digit using Matplotlib.
我們可以使用Matplotlib可視化一位數。
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.
機器學習正Swift向收集數據的地方(邊緣設備)靠近。 訂閱Fritz AI新聞通訊以了解有關此過渡及其如何幫助您擴展業務的更多信息 。
建立網絡 (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.
在開始構建網絡之前,讓我們確認訓練數據的形狀。 這很重要,因為卷積層將需要該形狀。 稍后我們將使用此形狀來調整輸入數據的大小。
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.
我們通過初始化順序模型開始。 然后,我們將圖層作為列表傳遞。 第一層是用于改變輸入數據形狀的重塑層。 我們已經知道每個圖像的尺寸為28 x28。我們還將1傳遞給重塑圖層,以確保輸入數據具有單個顏色通道。
Next, we create the convolution layer. This layer takes in a couple of parameters:
接下來,我們創建卷積層。 該層接受幾個參數:
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.
過濾器 -卷積中輸出過濾器的數量,即特征檢測器的數量。 32是常用的特征檢測器。
kernel_size — the dimensions of the feature detector matrix. This is the width and height of the convolution window.
kernel_size —特征檢測器矩陣的尺寸。 這是卷積窗口的寬度和高度。
input_shape — the shape of the input data. Since we included a reshape layer, we don’t need to pass this.
input_shape —輸入數據的形狀。 由于我們包含了一個重塑層,因此我們不需要傳遞它。
activation — the activation function to be uses. The ReLu activation function is a common choice.
激活 -要使用的激活功能。 ReLu激活功能是常見的選擇。
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.
列表上的下一項是添加池化層。 在此步驟中,通過在合并過程中取最大值來減小特征圖的大小。 2 x 2是常用的池大小。 這將減少要素圖的大小,同時保留識別數字所需的最重要的要素。
After that, we flatten the feature maps. This will convert the feature maps into a single column.
之后,我們將要素圖展平 。 這將轉換 要素映射到單列。
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.
然后將上面獲得的結果傳遞到致密層 。 傳遞給該層的參數是神經元數量和激活函數。 在這種情況下,它們分別是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.
最后,我們創建了輸出層 。 這里使用10,因為我們的數據集有10個類。 我們將使用softmax激活功能,因為 類是互斥的-例如,圖像不能同時分類為數字0和1。 如果不是這種情況,我們將使用S型激活函數 。
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’)])編譯網絡 (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:
現在,我們需要對數字應用梯度下降 。 這是在編譯階段完成的。 我們傳遞以下參數:
optimizer — the type of gradient descent to use; in this case, we’ll use stochastic gradient descent
優化器 -要使用的梯度下降類型; 在這種情況下,我們將使用隨機梯度下降
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.
損失 —使用的成本函數。 我們使用sparse_categorical_crossentropy 因為這些類是整數編碼的。 如果我們執行了一次熱編碼,則必須使用分類交叉熵損失函數。
metrics — the evaluation metrics to monitor; in this case, accuracy
指標 —要監視的評估指標; 在這種情況下,準確性
安裝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.
接下來,我們將訓練集適合網絡。 epochs是數據將通過網絡的輪數。 validation_data是測試數據集,將用于評估訓練期間模型的性能。
model.fit(x=x_train,y=y_train,epochs=5,validation_data=validation_data)評估網絡 (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.
現在,我們可以檢查網絡的性能。 我們使用evaluate函數來傳遞測試集。 產生的結果中的第一項是loss ,第二項是accuracy 。
model.evaluate(x_test,y_test)313/313 [==============================] - 2s 8ms/step - loss: 0.1116 - accuracy: 0.9668
[0.1115519180893898, 0.9667999744415283]
檢查預測 (Checking the Predictions)
Let’s now see if our network is able to correctly identify a digit. We kick off by making predictions:
現在讓我們看看我們的網絡是否能夠正確識別數字。 我們通過做出預測開始:
predictions = model.predict(x_test)For example, let’s check the digit at index 2 in the test set.
例如,讓我們檢查測試集中索引2處的數字。
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.
這顯然是數字。 讓我們將其與數字1的預測進行比較。
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.
從下面的預測中,我們可以看到數字1(在預測數組的索引1處)具有最高的預測。
0.00077352 — digit 0 prediction
0.00077352 —數字0預測
99.63969 — digit 1 prediction
99.63969 —數字1的預測
0.07214971 — digit 2 prediction
0.07214971-數字2預測
0.03056707 — digit 3 prediction
0.03056707 —數字3預測
0.00606834 — digit 4 prediction
0.00606834 —數字4預測
0.00301009 — digit 5 prediction
0.00301009-數字5預測
0.02507676 — digit 6 prediction
0.02507676 —數字6的預測
0.11558129 — digit 7 prediction
0.11558129-數字7預測
0.10332227 — digit 8 prediction
0.10332227 —數字8預測
0.00378496 — digit 9 prediction
0.00378496 —數字9預測
We can check another prediction by plotting the digit at index 0 in the testing set. Let’s try number 7.
我們可以通過在測試集中的索引0處繪制數字來檢查另一個預測。 讓我們嘗試數字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.
接下來,我們將看一下索引0處的預測,看看它是否也是7。顯然,7具有最高的預測。
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)結論 (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.
到現在為止做得很好! 在本文中,我們使用了CNN核心概念來開發有效的卷積神經網絡。 我們已經看到了您需要采取的步驟,并且已經評估了我們訓練有素的模型。 現在,您可以使用其他數據集進行嘗試,以查看是否可以重現我們在此處構建的網絡。
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.
編者注: 心跳 是由貢獻者驅動的在線出版物和社區,致力于探索移動應用程序開發和機器學習的新興交集。 我們致力于為各行各業的開發人員和工程師提供支持和啟發。
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在編輯上是獨立的,由以下機構贊助和發布 Fritz AI ,一種機器學習平臺,可幫助開發人員教設備看,聽,感知和思考。 我們向貢獻者付款,并且不出售廣告。
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.
如果您想做出貢獻,請繼續我們的 呼吁捐助者 。 您還可以注冊以接收我們的每周新聞通訊(《 深度學習每周》 和《 Fritz AI新聞通訊》 ),并加入我們 Slack ,然后繼續關注Fritz AI Twitter 提供了有關移動機器學習的所有最新信息。
翻譯自: https://heartbeat.fritz.ai/convolutional-neural-networks-cnns-core-concepts-applied-3c12e60e2c40
cnn卷積神經網絡應用
總結
以上是生活随笔為你收集整理的cnn卷积神经网络应用_卷积神经网络(CNN):应用的核心概念的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 季度怎么划分,季报和年报的区别
- 下一篇: 卷积神经网络结构_卷积神经网络