人工智能中,自动驾驶汽车是如何自动识别交通标志的?
內(nèi)容:
了解數(shù)據(jù)集。
步驟0:導(dǎo)入庫和數(shù)據(jù)集。
步驟1:數(shù)據(jù)預(yù)處理。
步驟2:數(shù)據(jù)可視化。
ConvNets背后的直覺。
步驟3:訓練模型。
步驟4:模型評估。
動機:由于特斯拉等公司在電動汽車自動化方面的努力,無人駕駛汽車正變得非常受歡迎。為了成為5級自動駕駛汽車,這些汽車必須正確識別交通標志并遵守交通規(guī)則。在識別出這些交通標志之后,它還應(yīng)該能夠適當?shù)刈龀稣_的決定。
了解數(shù)據(jù)集:
德國交通標志基準測試是在2011年國際神經(jīng)網(wǎng)絡(luò)聯(lián)合會議(IJCNN)上舉行的多類單圖像分類挑戰(zhàn)。請在此處下載數(shù)據(jù)集。數(shù)據(jù)集具有以下屬性:
https://www.kaggle.com/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign
單圖像,多分類問題
超過40個類別
總共超過50,000張圖像
大型逼真的數(shù)據(jù)庫
步驟0:導(dǎo)入庫和數(shù)據(jù)集:
在第一步中,將導(dǎo)入所有標準庫以及將作為數(shù)據(jù)和標簽存儲的數(shù)據(jù)集。導(dǎo)入Tensorflow是為了使用Keras,cv2解決計算機視覺相關(guān)的問題以及PIL處理不同的圖像文件格式。
Importing standard libraries
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns import cv2 import tensorflow as tf from PIL import ImageImporting dataset
import os data = [] labels = [] classes = 43 cur_path = os.getcwd() for i in range(classes):path = os.path.join(cur_path, 'train', str(i))images = os.listdir(path)for a in images:try:image = Image.open(path + '\\'+ a)image = image.resize((30, 30))image = np.array(image)data.append(image)labels.append(i)except:print("Error loading image")步驟1:資料預(yù)處理:
為了處理數(shù)據(jù),將使用numpy將其轉(zhuǎn)換為數(shù)組。然后,使用形狀函數(shù)驗證數(shù)據(jù)集的尺寸。然后,使用train_test_split函數(shù)以80:20的比率將數(shù)據(jù)集分為訓練和測試數(shù)據(jù)。Y_train和Y_test包含43個整數(shù)形式的類,不適合模型。因此,將使用to_categorical函數(shù)將其轉(zhuǎn)換為二進制形式。
第2步:數(shù)據(jù)可視化:
將使用imshow函數(shù)使數(shù)據(jù)集中的特定圖像可視化。該數(shù)據(jù)集中的圖像高度為30px,寬度為30px,并具有3個顏色通道。
ConvNets背后的直覺
由于卷積神經(jīng)網(wǎng)絡(luò)能夠檢測和識別圖像中的各種對象,因此在計算機視覺應(yīng)用中非常流行。
用外行的話來說,CNN基本上是一開始就具有卷積運算的完全連接的神經(jīng)網(wǎng)絡(luò)。這些卷積運算可用于檢測圖像中的定義圖案。它類似于人腦枕葉中的神經(jīng)元。ConvNets的體系結(jié)構(gòu)使用3層構(gòu)建,然后堆疊形成完整的ConvNet體系結(jié)構(gòu)。以下是三層:
1、卷積層。
2、池化層。
3、完全連接。
卷積層:卷積層是ConvNet的核心部分,它執(zhí)行所有計算量大的任務(wù)。在整個圖像中遍歷特定模式的內(nèi)核或過濾器,以檢測特定類型的特征。該遍歷的輸出將導(dǎo)致一個稱為要素圖的二維數(shù)組。該特征圖中的每個值都通過ReLU函數(shù)傳遞,以消除非線性。
池化層:該層負責減少數(shù)據(jù)量,因為它減少了計算量和處理所需的時間。有兩種類型的池化:平均值池和最大值池。顧名思義,“最大”池返回最大值,“平均”池返回內(nèi)核覆蓋的圖像部分的平均值。
完全連接:上一步收到的二維輸出數(shù)組通過展平過程轉(zhuǎn)換為列向量。該向量被傳遞到多層神經(jīng)網(wǎng)絡(luò),該網(wǎng)絡(luò)通過一系列時期學習使用Softmax函數(shù)對圖像進行分類。
步驟3:訓練模型
```python # Importing Keras Libraries from keras.models import Sequential from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout # Creating Neural network Architecture # Initialize neural network model = Sequential() # Add 2 convolutional layers with 32 filters, a 5x5 window, and ReLU activation function model.add(Conv2D(filters = 32, kernel_size = (5, 5), activation = 'relu', input_shape = X_train.shape[1:])) model.add(Conv2D(filters = 32, kernel_size = (5, 5), activation = 'relu')) # Add max pooling layer with a 2x2 window model.add(MaxPool2D(pool_size = (2, 2))) # Add dropout layer model.add(Dropout(rate = 0.25)) # Add 2 convolutional layers with 32 filters, a 5x5 window, and ReLU activation function model.add(Conv2D(filters = 64, kernel_size = (3, 3), activation = 'relu')) model.add(Conv2D(filters = 64, kernel_size = (3, 3), activation = 'relu')) # Add max pooling layer with a 2x2 window model.add(MaxPool2D(pool_size = (2, 2))) # Add dropout layer model.add(Dropout(rate = 0.25)) # Add layer to flatten input model.add(Flatten()) # Add fully connected layer of 256 units with a ReLU activation function model.add(Dense(256, activation = 'relu')) # Add dropout layer model.add(Dropout(rate = 0.5)) # Add fully connected layer of 256 units with a Softmax activation function model.add(Dense(43, activation = 'softmax')) # Summarizing the model architecture model.summary() output: _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_1 (Conv2D) (None, 26, 26, 32) 2432 _________________________________________________________________ conv2d_2 (Conv2D) (None, 22, 22, 32) 25632 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 11, 11, 32) 0 _________________________________________________________________ dropout_1 (Dropout) (None, 11, 11, 32) 0 _________________________________________________________________ conv2d_3 (Conv2D) (None, 9, 9, 64) 18496 _________________________________________________________________ conv2d_4 (Conv2D) (None, 7, 7, 64) 36928 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 3, 3, 64) 0 _________________________________________________________________ dropout_2 (Dropout) (None, 3, 3, 64) 0 _________________________________________________________________ flatten_1 (Flatten) (None, 576) 0 _________________________________________________________________ dense_1 (Dense) (None, 256) 147712 _________________________________________________________________ dropout_3 (Dropout) (None, 256) 0 _________________________________________________________________ dense_2 (Dense) (None, 43) 11051 ================================================================= Total params: 242,251 Trainable params: 242,251 Non-trainable params: 0 _________________________________________________________________ # Compile neural network model.compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"]) # Train neural network history = model.fit(X_train, Y_train_categorical, batch_size = 32, epochs = 15, validation_data = (X_test, Y_test_categorical)) Output after 15 epochs: Epoch 15/15 31367/31367 [==============================] - 98s 3ms/step - loss: 0.2169 - acc: 0.9485 - val_loss: 0.0835 - val_acc: 0.9787步驟4:模型評估:
# Ploting graph - Epoch vs Accuracy plt.plot(history.history['acc'], label='training accuracy') plt.plot(history.history['val_acc'], label='val accuracy') plt.title('Accuracy') plt.xlabel('epochs') plt.ylabel('accuracy') plt.grid() plt.legend() plt.show()準確性與時代
# Ploting graph - Epoch vs Loss plt.plot(history.history['loss'], label='training loss') plt.plot(history.history['val_loss'], label='val loss') plt.title('Loss') plt.xlabel('epochs') plt.ylabel('loss') plt.grid() plt.legend() plt.show()損失與時代
# Calculating Accuracy Score from sklearn.metrics import accuracy_score y_test = pd.read_csv('Test.csv') labels = y_test["ClassId"].values imgs = y_test["Path"].valuesdata = []for img in imgs:image = Image.open(img)image = image.resize((30,30))data.append(np.array(image))X_test = np.array(data)pred = model.predict_classes(X_test)from sklearn.metrics import accuracy_score print("Accuracy Score : ",accuracy_score(labels, pred)) Output: Accuracy Score : 0.9499604117181314歡迎大家的閱讀,如果大家有不同的意見可以發(fā)表在留言區(qū),我們一起學習,共同進步。
總結(jié)
以上是生活随笔為你收集整理的人工智能中,自动驾驶汽车是如何自动识别交通标志的?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: #AD18#PCB绘制时合并铜皮
- 下一篇: JAVA高效批量插入数据到数据库demo