5分鐘構建一個自己的無人駕駛車

心情不夠振奮,學個新技術吧!

翻譯來源:
https://www.youtube.com/watch?v=hBedCdzCoWM

發現了一個特逗兒的小伙兒,有一些5分鐘系列的視頻,介紹一些人工智能的技術,有興趣的可以去他的youtube頻道。


今天要講的是自動駕駛車是怎樣工作的,然后在一個模擬的環境中應用我們自己的自動駕駛車。

自動駕駛車再也不是只能在科幻小說里面才能看到的東西了,很多大的公司比如說谷歌,優步,豐田和福特都在研發甚至上路了。

在不久的將來,我們將會看到有越來越多的消費者可以購買自動駕駛車。

自動駕駛車是怎樣工作的呢?

當我們人類坐在車上的時候,我們需要觀察周圍的環境,同時操作我們的車,作出決定該向哪個方向轉動方向盤。

這個可以被建模成一個機器學習問題,SLAM,simutaneous localization and mapping,這就是自動駕駛車要做的事情。

無人駕駛車,會裝備GPS,一個慣性導航系統,一些傳感器。然后用GPS傳過來的地理數據來定位自己,用傳感器傳過來的數據來構造它所在環境的內部地圖,

在自己構建的這個內部地圖中找到自己的位置之后,就會尋找一個從此位置到目標地點的優化路徑,并且避免任何一個障礙物。

當它已經確定了這個最優路徑之后,就會把它分解成一系列的電機命令,這些命令會被送到汽車的致動器。

但是在實際的開車的過程中有很多問題,不僅僅是躲避障礙物,還有不同的天氣條件下如何控制速度,不同類型的道路,比如說彎路,還有遇到不同的道路標志時采取什么樣的行動。

最近有一篇論文叫 Long term Planning for Short Term 。

文中介紹了一個規劃的算法。這個算法可以使無人駕駛車在優化一個長期的目標時,做出一個即時的反應

一個應用場景實例就是在環島時,當一個汽車想要進入這個環島時,它需要做出一個是加速還是減速的決定,這個決定的長期效果就是是否成功進入環島。

通常來講,一個無人駕駛車的規劃問題,是一個強化學習問題。一個汽車是通過很多次的嘗試和犯錯,不斷的糾正錯誤,來強化它的駕駛能力的。

一個汽車觀察到周圍環境的一個狀態 s,經過判斷某個行為是好還是壞后采取行動 a,然后它會收到一個反饋,如果這個行為是對的就會獎勵它,然后重復上面這個過程。

這個過程是想要最大化這個獎勵,它依賴于將狀態映射到行動的規則,這個函數被稱作 Q,用這個函數來找到最優的規則。

但是在實際的道路上,狀況是非常復雜的,有很多車,而且這個狀態是動態的,這就不僅需要要預測自己的車的行為,還要能夠預測其他車輛的行為。

為了能夠學習出這個函數,論文的作者們構建了一個深度神經網絡。

向這個神經網絡中投入的數據包括,可預測的部分-車的速度和不可預測的部分-其他車的速度,這樣就同時學習這兩個部分。

模型里只有兩個特征,一個是 Adaptive Cruise Control,一個是 Merging Roundabouts。

有一個術語叫 End to End Learning for Self-Driving Cars,三個月前Nvidia的一個團隊發了一篇新的論文。他們在擋風玻璃上裝了三個攝像頭來獲取數據,把這些數據投入到一個卷積神經網絡中,然后自動學習特征。

用這個模型,并不需要把不同的場景明確的分解成不同的子問題。模型里的卷積神經網絡,將把從輸入數據看到的東西直接映射成轉向命令。

它會先在一個提前錄制的虛擬模擬環境中訓練,然后再由一個司機去駕駛它。他們得到一個非常好的結果,但是這個作者覺得很難區分這個神經網絡的特征提取部分和控制部分,也就很難去檢驗它們。

一個叫 George Hotz 在他的車庫里構建了自己的無人駕駛車,只用了一對手機相機,成本只有1000美元。

接下來讓我們訓練一下自己的無人駕駛車

讓它通過 Q Learning 來自動駕駛而不會碰到其它的障礙物。

篇幅有限,只貼上主要代碼一部分,邏輯請看代碼注釋的漢語部分,完整repo請看文末資源列表。

from flat_game import carmunk
import numpy as np
import random
import csv
from nn import neural_net, LossHistory
import os.path
import timeit

NUM_INPUT = 3
GAMMA = 0.9  # Forgetting.
TUNING = False  # If False, just use arbitrary, pre-selected params.

# 訓練一個神經網絡,有一些hyper parameters
def train_net(model, params):   

    filename = params_to_filename(params)
    
    # 定義變量:觀察數據
    observe = 1000  # Number of frames to observe before training.
    epsilon = 1
    train_frames = 1000000  # Number of frames to play.
    batchSize = params['batchSize']
    buffer = params['buffer']

    # 定義變量:位置
    # Just stuff used below.
    max_car_distance = 0
    car_distance = 0
    t = 0
    data_collect = []
    replay = []  # stores tuples of (S, A, R, S').

    loss_log = []

    # 創建一個新的游戲實例
    # Create a new game instance.
    game_state = carmunk.GameState()

    # 得到這個實例的第一個狀態
    # Get initial state by doing nothing and getting the state.
    _, state = game_state.frame_step((2))

    # 用一個timer來追蹤
    # Let's time it.
    start_time = timeit.default_timer()

    # 當我們開始建立experience replay時
    # Run the frames.
    while t < train_frames:

        t += 1
        car_distance += 1

        # 我們會更新位置變量,然后依據狀態隨機選擇行為
        # Choose an action.
        if random.random() < epsilon or t < observe:
            action = np.random.randint(0, 3)  # random
        else:
            # 如果這個隨機變量在我們的限制條件之外,我們會得到每個行為的Q值,
            # 來幫我們找到最優的決策
            # Get Q values for each action.
            qval = model.predict(state, batch_size=1)
            action = (np.argmax(qval))  # best

        # 如果它是有效的,我們會得到一個reward
        # Take action, observe new state and get our treat.
        reward, new_state = game_state.frame_step(action)

        # Experience replay storage.
        replay.append((state, action, reward, new_state))

        # 當它結束觀察游戲和建立經驗回放時,會開始訓練采樣記憶experience replaying,得到訓練值
        # If we're done observing, start training.
        if t > observe:

            # If we've stored enough in our buffer, pop the oldest.
            if len(replay) > buffer:
                replay.pop(0)

            # Randomly sample our experience replay memory
            minibatch = random.sample(replay, batchSize)

            # Get training values.
            X_train, y_train = process_minibatch(minibatch, model)

            # 然后訓練神經網絡模型
            # Train the model on this batch.
            history = LossHistory()
            model.fit(
                X_train, y_train, batch_size=batchSize,
                nb_epoch=1, verbose=0, callbacks=[history]
            )
            loss_log.append(history.losses)

        # 然后更新狀態
        # Update the starting state with S'.
        state = new_state

        # Decrement epsilon over time.
        if epsilon > 0.1 and t > observe:
            epsilon -= (1/train_frames)

        # 當car dies,
        # We died, so update stuff.
        if reward == -500:
            # Log the car's distance at this T.
            data_collect.append([t, car_distance])

            # Update max.
            if car_distance > max_car_distance:
                max_car_distance = car_distance

            # Time it.
            tot_time = timeit.default_timer() - start_time
            fps = car_distance / tot_time

            # Output some stuff so we can watch.
            print("Max: %d at %d\tepsilon %f\t(%d)\t%f fps" %
                  (max_car_distance, t, epsilon, car_distance, fps))

            # 記錄距離,重啟
            # Reset.
            car_distance = 0
            start_time = timeit.default_timer()

        # 每25000 frames保存一下模型和weights
        # Save the model every 25,000 frames.
        if t % 25000 == 0:
            model.save_weights('saved-models/' + filename + '-' +
                               str(t) + '.h5',
                               overwrite=True)
            print("Saving model %s - %d" % (filename, t))

    # Log results after we're done all frames.
    log_results(filename, data_collect, loss_log)

下面是訓練的結果,然后看一下在模擬環境中是如何work的,可以看到它可以通過強化學習和神經網絡,不斷地躲避障礙物:

來,把你的小汽車放回宇宙吧


其他資源:
The code in the video is here:
https://github.com/llSourcell/Self-Dr...

Paper 1: Long term Planning for Short Term Prediction
[http://arxiv.org/pdf/1602.01580v1.pdf(http://arxiv.org/pdf/1602.01580v1.pdf)

Paper 2: End-to-End Learning for Self-Driving Cars
[https://arxiv.org/pdf/1604.07316v1.pdf(https://arxiv.org/pdf/1604.07316v1.pdf)

More on Reinforcement Learning:
http://www2.hawaii.edu/~chenx/ics699r...
https://www.quora.com/Artificial-Inte...
http://www2.econ.iastate.edu/tesfatsi...


我是 不會停的蝸牛 Alice
85后全職主婦
喜歡人工智能,行動派
創造力,思考力,學習力提升修煉進行中
歡迎您的喜歡,關注和評論!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容