<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mike LIn on Medium]]></title>
        <description><![CDATA[Stories by Mike LIn on Medium]]></description>
        <link>https://medium.com/@mike19931010?source=rss-99b0a070809b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gliB73LwJwZDZf-GNP3v_Q.jpeg</url>
            <title>Stories by Mike LIn on Medium</title>
            <link>https://medium.com/@mike19931010?source=rss-99b0a070809b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 12:43:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mike19931010/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[介紹 C++ 標準庫中的 nth_element]]></title>
            <link>https://medium.com/@mike19931010/%E4%BB%8B%E7%B4%B9-c-%E6%A8%99%E6%BA%96%E5%BA%AB%E4%B8%AD%E7%9A%84-nth-element-ceead03a392a?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/ceead03a392a</guid>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Fri, 15 Dec 2023 00:49:24 GMT</pubDate>
            <atom:updated>2023-12-15T00:50:23.036Z</atom:updated>
            <content:encoded><![CDATA[<p>nth_element 主要用於重新排列元素，使得給定序列中的特定位置（n-th位置）的元素被放置在如果整個序列被排序後它將會出現的正確位置上。</p><h4>功能說明</h4><ul><li>nth_element 通過部分排序算法實現其功能。它保證在 n-th 元素之前的所有元素都不大於 n-th 元素，並且在它之後的所有元素都不小於它。</li><li>此函數特別適用於只需找到<strong>中位數</strong>或其他順序統計量的場景，而不需要對整個範圍進行完全排序。</li></ul><h4>使用語法</h4><pre>nth_element(StartIterator, NthIterator, EndIterator);</pre><ul><li>StartIterator 和 EndIterator 分別指定範圍的開始和結束。</li><li>NthIterator 是指向範圍內第 n 個位置的迭代器，這是要放置正確元素的位置。</li></ul><h4>效能考量</h4><ul><li>nth_element 的效能通常比完全排序算法（如 sort）更高，尤其是在大數據集中只需要找到特定位置元素的情況下。</li></ul><h4>實際應用範例</h4><p><a href="https://leetcode.com/problems/best-meeting-point/">https://leetcode.com/problems/best-meeting-point/</a></p><p>以下是一個實際例子，展示了如何使用 nth_element 來找到一個二維點集中的中位點，以最小化到所有點的曼哈頓距離總和。這個例子是關於在一個表示朋友家的二維網格中找到最優會議點的問題。</p><p><strong>問題描述:</strong></p><ul><li>給定一個 m x n 的二進制網格 grid，其中每個 1 標記了一位朋友的家，返回最小的總旅行距離。</li><li>總旅行距離是朋友的家和會議點之間的曼哈頓距離之和。</li></ul><p><strong>C++ 程式碼實現:</strong></p><pre>class Solution {<br>public:<br>    int minTotalDistance(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {<br>        int res = 0;<br>        vector&lt;int&gt; x_coords, y_coords;<br>        for(int i = 0; i &lt; grid.size(); i++)<br>        {<br>            for(int j = 0; j &lt; grid[i].size(); j++)<br>            {<br>                if(grid[i][j] == 1)<br>                {<br>                    x_coords.push_back(i);<br>                    y_coords.push_back(j);<br>                }<br>            }<br>        }<br><br>        // Finding the median of the coordinates<br>        nth_element(x_coords.begin(), x_coords.begin() + x_coords.size() / 2, x_coords.end());<br>        nth_element(y_coords.begin(), y_coords.begin() + y_coords.size() / 2, y_coords.end());<br>        int median_x = x_coords[x_coords.size() / 2];<br>        int median_y = y_coords[y_coords.size() / 2];<br><br>        for (int i = 0; i &lt; x_coords.size(); ++i) <br>        {<br>          res += abs(x_coords[i] - median_x) + abs(y_coords[i] - median_y);<br>        }<br><br>        return res;<br>    }<br>};</pre><p>在這個範例中，`nth_element` 被用來快速找到 x 坐標和 y 坐標的中位數。這是解決這個問題的關鍵，因為曼哈頓距離之和到達最小值的點，即是這些中位數所在的點。</p><h3><strong>結論</strong></h3><p>`nth_element` 在需要快速找到一組數據中的中位數或其他順序統計量時非常有用，特別是當不需要對整個數據集進行完全排序時。它是 C++ 標準庫中的一個強大工具，適用於各種需要部分排序的應用場景。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ceead03a392a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[最長遞增子序列 — LIS(Longest Increasing Subsequence)]]></title>
            <link>https://medium.com/@mike19931010/%E6%9C%80%E9%95%B7%E9%81%9E%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97-lis-longest-increasing-subsequence-b0f9d0370bdb?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0f9d0370bdb</guid>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Wed, 13 Dec 2023 02:27:40 GMT</pubDate>
            <atom:updated>2023-12-13T02:27:40.968Z</atom:updated>
            <content:encoded><![CDATA[<h3>最長遞增子序列 — LIS(Longest Increasing Subsequence)</h3><p>「最長遞增子序列」（Longest Increasing Subsequence）演算法是一種常見的動態規劃問題，用於找出一個序列中最長的嚴格遞增子序列。這個子序列不必是連續的或唯一的。</p><h3>基本概念</h3><p>給定一個序列，比如 [10, 22, 9, 33, 21, 50, 41, 60, 80]，最長遞增子序列可能有多個，例如 [10, 22, 33, 50, 60, 80] 或 [10, 22, 33, 41, 60, 80]。</p><h3>演算法步驟</h3><ol><li><strong>初始化</strong>：創建一個陣列 dp，其中 dp[i] 表示以第 i 個元素結尾的最長遞增子序列的長度。初始時，每個元素的 dp[i] 值為 1，因為每個元素自身可以被視為長度為 1 的遞增子序列。</li><li><strong>遍歷</strong>：對於序列中的每個元素，遍歷之前的元素來更新 dp 陣列。對於每個 i（從 1 開始遍歷），遍歷所有 j &lt; i 的元素。如果發現某個 j 滿足 arr[j] &lt; arr[i]（表示可以形成遞增對），則檢查並更新 dp[i] 為 max(dp[i], dp[j] + 1)。</li><li><strong>求解</strong>：遍歷完成後，dp 陣列中的最大值即為最長遞增子序列的長度。</li></ol><h3>複雜度</h3><ul><li>時間複雜度：O(n^2)，其中 n 是序列的長度。這是因為需要雙重遍歷序列。</li><li>空間複雜度：O(n)，用於儲存 dp 陣列。</li></ul><h3>例子</h3><p>以序列 [10, 22, 9, 33, 21, 50, 41, 60, 80] 為例，演算法的步驟如下：</p><ul><li>初始化 dp = [1, 1, 1, 1, 1, 1, 1, 1, 1]</li><li>遍歷序列，更新 dp 為 [1, 2, 1, 3, 2, 4, 4, 5, 6]</li><li>最長遞增子序列的長度為 dp 陣列中的最大值，即 6。</li></ul><h3>變體</h3><p>有一種更高效的變體，使用二分搜索（Binary Search），可以將時間複雜度降低到 O(n log n)。這種方法涉及維護一個「尾部元素」的陣列，用於存儲目前已知的遞增子序列。透過二分搜索在這個陣列中找到適當的位置插入新元素，以保持遞增序列的特性，同時更新序列長度。</p><h3>Code</h3><p>O(n²):</p><pre>int LIS(vector&lt;int&gt;&amp; nums) {<br>        vector&lt;int&gt; dp(nums.size(), 1);<br>        for(int i = 1; i &lt; nums.size(); i++)<br>        {<br>            for(int j = 0; j &lt; i; j++)<br>            {<br>                if(nums[j] &lt; nums[i])<br>                {<br>                    dp[i] = max(dp[j] + 1, dp[i]);<br>                }<br>            }<br>        }<br>        return *max_element(dp.begin(), dp.end());<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0f9d0370bdb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[如何以最少硬幣達成目標金額？一個貪心算法的探討]]></title>
            <link>https://medium.com/@mike19931010/%E5%A6%82%E4%BD%95%E4%BB%A5%E6%9C%80%E5%B0%91%E7%A1%AC%E5%B9%A3%E9%81%94%E6%88%90%E7%9B%AE%E6%A8%99%E9%87%91%E9%A1%8D-%E4%B8%80%E5%80%8B%E8%B2%AA%E5%BF%83%E7%AE%97%E6%B3%95%E7%9A%84%E6%8E%A2%E8%A8%8E-046f1000118e?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/046f1000118e</guid>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Sun, 03 Dec 2023 04:22:44 GMT</pubDate>
            <atom:updated>2023-12-03T04:22:44.372Z</atom:updated>
            <content:encoded><![CDATA[<p>當你面對一堆硬幣和一個特定的目標金額時，你會怎麼選擇硬幣來達到那個金額？這不僅僅是日常生活中的問題，也是一個經典的計算機科學問題。在這篇文章中，我們將探討一種貪心算法來解決「最少硬幣添加問題」。</p><h4>問題陳述</h4><p>給定一個0索引的整數陣列coins，代表可用的硬幣面額，以及一個整數target。如果存在硬幣的一個子序列其總和為x，則稱整數x是可獲得的。問題是要添加最少數量的任何面額硬幣到陣列中，使得範圍[1, target]內的每個整數都是可獲得的。</p><p>You are given a <strong>0-indexed</strong> integer array coins, representing the values of the coins available, and an integer target.</p><p>An integer x is <strong>obtainable</strong> if there exists a subsequence of coins that sums to x.</p><p>Return <em>the</em><strong><em> minimum</em></strong><em> number of coins </em><strong><em>of any value</em></strong><em> that need to be added to the array so that every integer in the range</em> [1, target]<em> is </em><strong><em>obtainable</em></strong>.</p><p>A <strong>subsequence</strong> of an array is a new <strong>non-empty</strong> array that is formed from the original array by deleting some (<strong>possibly none</strong>) of the elements without disturbing the relative positions of the remaining elements.</p><h4>算法解釋</h4><p>這個問題的解決方案使用了一種貪心算法。以下是代碼的逐步解釋：</p><pre>class Solution {<br>public:<br>    int minimumAddedCoins(vector&lt;int&gt;&amp; coins, int target) {<br>        sort(coins.begin(), coins.end());<br>        <br>        int res = 0, i = 0;<br>        long long reach = 0;<br>        while (reach &lt; target) {<br>            if (i &lt; coins.size() &amp;&amp; coins[i] &lt;= reach + 1) {<br>                reach += coins[i++];<br>            } else {<br>                reach += reach + 1;<br>                res++;<br>            }<br>        }<br>        <br>        return res;<br>    }<br>};</pre><ol><li>排序硬幣: 我們首先對coins陣列進行排序。這樣可以保證我們從最小的硬幣開始考慮，符合貪心算法的原則。</li><li>初始化變數: res用於計數需要添加的硬幣數量，i用於追蹤目前考慮到的硬幣索引，reach用於記錄目前可達到的最大金額。</li><li>遍歷硬幣: 當reach小於target時，我們繼續尋找解決方案。如果當前硬幣可以使我們達到更高的金額，我們就將其加入到reach中。否則，我們需要添加一枚新的硬幣，使得reach增加。</li><li>達到目標: 當reach達到或超過target時，算法結束，返回res作為答案。</li></ol><h4>貪心算法的優勢</h4><p>這種方法之所以有效，是因為它在每一步都嘗試達到當前可能的最佳解。透過這種方式，我們可以確保用最少的硬幣數量達到目標金額。</p><h4>結論</h4><p>「最少硬幣添加問題」不僅在理論上有趣，也在實際應用中非常有用。透過貪心算法，我們可以找到一種高效且實用的解決方案。這種算法的美妙之處在於其簡潔性與高效性，使其成為解決此類問題的理想選擇。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=046f1000118e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[深入探索C++中的constexpr：提升代碼效率與可讀性]]></title>
            <link>https://medium.com/@mike19931010/%E6%B7%B1%E5%85%A5%E6%8E%A2%E7%B4%A2c-%E4%B8%AD%E7%9A%84constexpr-%E6%8F%90%E5%8D%87%E4%BB%A3%E7%A2%BC%E6%95%88%E7%8E%87%E8%88%87%E5%8F%AF%E8%AE%80%E6%80%A7-433424e42620?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/433424e42620</guid>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Tue, 21 Nov 2023 04:09:02 GMT</pubDate>
            <atom:updated>2023-11-21T04:09:02.309Z</atom:updated>
            <content:encoded><![CDATA[<h3>什麼是constexpr？</h3><p>在C++的世界裡，constexpr是一個關鍵字，用於標記變數、函數或物件為常量表達式。這意味著它們的值或行為必須在編譯時已知，從而允許編譯器在編譯階段進行更多的優化和檢查。這個特性不僅增強了代碼的表達能力，還有助於提高運行效率。</p><h3>constexpr的主要用法</h3><h3>1. 常量變數</h3><p>使用constexpr標記變數意味著這些變數必須在聲明時就被初始化，且初始化表達式必須是常量表達式。例如：</p><pre>constexpr int max_size = 100;</pre><p>這樣的變數在整個程序中都被視為已知的常量。</p><h3>2. 函數</h3><p>將函數定義為constexpr意味著這些函數能夠在編譯時計算其結果，只要提供的參數都是常量表達式。例如：</p><pre>constexpr int add(int x, int y) {<br>    return x + y;<br>}</pre><p>這樣的函數可以用在需要常量表達式的上下文中，如陣列大小或模板參數。</p><h3>3. 物件構造函數</h3><p>如果一個類的構造函數被標記為constexpr，則可以在編譯時創建其對象的實例。例如：</p><pre>class Point {<br>public:<br>    constexpr Point(double x, double y) : x(x), y(y) {}<br>    // ...<br>private:<br>    double x, y;<br>};<br>constexpr Point origin(0, 0);</pre><p>這對於創建不變的、在編譯時已知的物件特別有用。</p><h3>為什麼使用constexpr？</h3><p>使用constexpr有幾個重要的好處：</p><ul><li><strong>提高效率：</strong> constexpr允許編譯器在編譯時計算值，從而減少運行時的計算負擔。</li><li><strong>提高安全性與可讀性：</strong> 明確標示哪些部分的代碼是常量，有助於其他開發者理解代碼的意圖。</li><li><strong>編譯器優化：</strong> 由於編譯器可以預先計算出常量表達式的結果，因此可以進行更有效的優化。</li></ul><h3>結論</h3><p>constexpr是C++提供的一個強大工具，它不僅提升了代碼的效率，還增強了代碼的可讀性和表達能力。了解並正確使用constexpr，可以使你的C++代碼更加高效和穩健。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=433424e42620" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[如何透過三行程式碼提升C++輸入輸出效率]]></title>
            <link>https://medium.com/@mike19931010/%E5%A6%82%E4%BD%95%E9%80%8F%E9%81%8E%E4%B8%89%E8%A1%8C%E7%A8%8B%E5%BC%8F%E7%A2%BC%E6%8F%90%E5%8D%87c-%E8%BC%B8%E5%85%A5%E8%BC%B8%E5%87%BA%E6%95%88%E7%8E%87-586c62c37149?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/586c62c37149</guid>
            <category><![CDATA[c]]></category>
            <category><![CDATA[code-efficiency]]></category>
            <category><![CDATA[cplusplus]]></category>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Fri, 17 Nov 2023 04:00:01 GMT</pubDate>
            <atom:updated>2023-11-17T04:00:01.794Z</atom:updated>
            <content:encoded><![CDATA[<p>在C++編程中，輸入輸出（I/O）操作往往是效能瓶頸的一大來源。特別是在需要處理大量數據的情境，如資料分析或競賽編程，I/O效率變得尤為重要。幸運的是，有一些簡單的技巧可以幫助我們在這方面取得顯著的提升。本文將介紹三行關鍵的C++程式碼，解釋它們的作用及原理。</p><pre>ios_base::sync_with_stdio(0);<br>cin.tie(nullptr);<br>cout.tie(nullptr);</pre><h3>1. 解除C++和C標準流的同步</h3><pre>ios_base::sync_with_stdio(0);</pre><p>這行程式碼的作用是讓C++的標準輸入輸出流（如cin、cout）不再與C語言的輸入輸出流（如stdin、stdout）同步。C++標準流在預設情況下會與C的流同步，以確保可以混用C和C++的輸入輸出方法。然而，這種同步會帶來額外的性能負擔。通過設定為0，我們可以關閉這種同步，從而提升C++流的效率。但需注意，這樣做之後，不應再混用C和C++的輸入輸出方式。</p><h3>2. 解除cin和cout的綁定</h3><pre>cin.tie(nullptr);</pre><p>cin和cout在預設情況下是綁定的。這意味著在每次使用cin讀取資料前，程序會自動刷新（flush）cout中的緩衝區，以確保所有待輸出的數據被寫出。這雖然保證了數據的即時性，但也導致了許多不必要的刷新操作。透過cin.tie(nullptr);，我們可以將這兩者解綁，減少刷新操作，進而提升I/O效率。</p><h3>3. 明確化cout的獨立性</h3><pre>cout.tie(nullptr);</pre><p>這行程式碼實際上是多餘的，因為cout預設並不與其他流綁定。不過，為了清晰地表明程序的意圖，有時程序員會選擇加上這樣的語句。</p><h3>總結</h3><p>通過這三行程式碼，我們可以有效提高C++程序中的I/O效率，這對於處理大量數據的應用來說尤為重要。然而，也必須注意，一旦實施了這些優化，就不應混用C和C++的I/O操作，以免產生不可預期的行為。對於追求效能的開發者來說，這些技巧是寶貴的工具，但也需要謹慎使用。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=586c62c37149" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Meta Learning — 元學習 (上)]]></title>
            <link>https://medium.com/@mike19931010/meta-learning-%E5%85%83%E5%AD%B8%E7%BF%92-%E4%B8%8A-d6427c67ba7e?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6427c67ba7e</guid>
            <category><![CDATA[meta-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Sun, 12 Dec 2021 05:43:06 GMT</pubDate>
            <atom:updated>2021-12-12T05:43:06.656Z</atom:updated>
            <content:encoded><![CDATA[<h3>Meta Learning — 元學習 (上)</h3><p>This is a note of this youtube video: <a href="https://youtu.be/xoastiYx9JU">https://youtu.be/xoastiYx9JU</a></p><p>今天我們來介紹一下最近很紅的“元”方面的東西，那時候看到這個meta的時候就覺得很有興趣，今天終於有時間能把這個技術大概了解整理一下。</p><p>Meta-x 的意思是 x about x, 換句話說 meta-learning 就是 learn about learn，也就是學習如何學習。而 meta-verse 就是 universe about universe, 可以說是在宇宙中的宇宙。</p><p>先回頭講到ＭＬ的基本三個步驟：</p><ul><li><strong>找到一個function with unknown</strong>: 比如說定義好一個NN, 裡面有很多需要學習的 parameters, 定義好哪些參數需要被學習</li><li><strong>定義 loss function</strong>: 找到所需要可以偏微分的 loss function</li><li><strong>Optimize 這個 function: </strong>根據訓練資料跑出來的 loss，去最小化這個 loss</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c1LjvmttVyz_SgZaBdaDyg.png" /></figure><p>Machine Learning 基本的架構是首先先選一個Learning algorithm, 而這個是人手動調整 hyper-parameter 後產生的。再根據產生出來的 classifier 去做訓練，optimize 成最後的模型。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X92h8GEPofzu8w-x4YtHRA.png" /></figure><p>那我們可不可以讓機器學習說“怎麼產生模型”呢？這就是 Meta-Learning 的主要構想了，讓機器自己去學習該產生怎樣的模型，包含層數、架構、parameters、…等都可以讓機器自己去決定。</p><p>在 Meta-Learning 中一樣可以分成三個部分：</p><ul><li>定義 function: 定義好要學習的東西, learnable components</li><li>Define loss function for learning algorithm</li><li>Optimize it to get the minimum loss</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4-XECHdf5Ytk5s02Wf4TWg.png" /><figcaption>Step 1: 定義好learnable componets</figcaption></figure><h3><strong>Step 2:</strong></h3><p>定義 loss function. 這邊假設要訓練一個二元分類器，所以採用了 cross-entropy 當作是 loss function. 當訓練後的 loss 是小的話，代表機器學習後產生的 classifier 是好的，也就代表這個 learning how to learn 的 model 是好的。反之，如果 loss 太大，代表這個 create classifier 的 model 是不好的。</p><p>Loss 是算出所有 task loss 的總和，又叫做 Total loss.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7o4hAaDFo93LHLQhJWQkVQ.png" /></figure><p>這邊比較 tricky 的部分是，在 ML 訓練的時候一般來說只會使用 training data 去做訓練，Testing data 不參與訓練。而在 meta-learning 在 training 的時候是 training data 當作是訓練模型的 data，而 testing data 是用於 evaluate 產生的 classifier 並產生 loss 的 data。</p><p>那 Meta Learning train 完後要怎麼評估呢？ 答案就是使用 Testing Task. 在 Training 的時候使用 Training Task 裡面的 Training data and Testing data 去做訓練，等到模型訓練完畢後，再使用 Testing Task 裡的 Training data 去讓模型產生出 Classifier. 並用 Testing Task 中的 Testing data 去評估模型的好壞。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CxZrj651wbt8TIMugrO_1w.png" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fsheetsu.com%2Ftables%2Fec9e911f8f&amp;dntp=1&amp;display_name=Sheetsu&amp;url=https%3A%2F%2Fsheetsu.com%2Ftables%2Fec9e911f8f&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=sheetsu" width="700" height="220" frameborder="0" scrolling="no"><a href="https://medium.com/media/272d191c6c8dcc680e6247badc3e771c/href">https://medium.com/media/272d191c6c8dcc680e6247badc3e771c/href</a></iframe><h3>Step 3: Optimization</h3><p>我們定義好了 learning algorithm 之後，目標是找到 <a href="https://zh.wiktionary.org/wiki/%CF%86%CE%B9#Greek">φ</a> 讓整個 loss 是最小的。這邊就要用到你所知道的 optimization 技術，如果可以偏微分的話，常用的 Gradient Descent 就很好用。那如果不行的話，就用 RL 硬 train 一發。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2x4ArQEXkqV5YyPLcOzS4w.png" /></figure><p>這邊做的小總結，目前大致了解 Meta-learning 的目的跟步驟，整個的 framework 如下圖。分別有兩群的 Tasks, 我們感興趣的地方是 Testing Task, 我要訓練一個模型，當我丟我想要的 task 的時候，這個模型會自動幫我產生最好的分類模型，我只要丟一點 Testing task 裡面的 training data 就可以有很好的結果。這也是 meta-learning 的一個應用： 小樣本學習。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6K98nEGmbgXqCFTlyvcPNw.png" /></figure><h3>Machine Learning vs Meta Learning</h3><p><strong>Goal:</strong></p><ul><li>Machine Learning — Find a function f, input is an image and output its label.</li><li>Meta Learning — Find a function <strong>F</strong> that finds a function <strong>f. </strong>Input some training tasks to find a function which can find the best classifier</li></ul><p><strong>Training Data:</strong></p><ul><li>Machine Learning — One task, using training data. <strong>Within-task Training.</strong></li><li>Meta Learning —Multi-task, using training tasks which include training data for update parameters and testing data to calculate loss. <strong>Across-task-training.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L3TUb1E9VDbIYPBMLq3T5g.png" /></figure><p><strong>Testing Data:</strong></p><ul><li>Machine Learning — One task, using testing dataset to evaluate model. <strong>Within-task Testing.</strong></li><li>Meta Learning — Multi-task, using testing tasks which include training data for create model and testing data to evaluate model. <strong>Across-task Testing. Also include within-task training to create best classifier.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kfIeqamcoWb00GC-dKJE7Q.png" /></figure><p><strong>Loss:</strong></p><ul><li>Machine Learning — Sum over training data in single task.</li><li>Meta Learning — Sum over testing data in all training tasks.</li></ul><p>To calculate loss of meta learning for one tasks needs one within-task training and one within-task testing. So it’s a huge computation in meta learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fcSWshxC2cF_vRUc1MR4nw.png" /></figure><p>But there are still something similar in both. What you know about ML can usually apply to meta learning.</p><ul><li>Over-fitting on training tasks</li><li>Getting more training tasks to improve performance</li><li>Task augmentation</li><li>You still need to adjust hyperparameters when learning a learning algorithm</li><li>Development task</li></ul><h3>Summary</h3><p>這篇文章大概介紹了 Meta learning 的構想以及步驟，跟比較一下與傳統 Machine learning 的差異。簡單來說，就是我們人工設定調整的模型有可能不是最好的架構與參數，讓機器學習怎麼產生最好的架構就是 meta learning 的主要想法。有點像是工業革命的時候用機器取代大量人力，現在要用機器人取代操控機器的人，說不定以後又會用機器去取代設定或製造機器人的人…這樣 loop 下去。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6427c67ba7e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Resize image using Cuda NPP]]></title>
            <link>https://medium.com/@mike19931010/resize-image-using-cuda-npp-81b16bbe6351?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/81b16bbe6351</guid>
            <category><![CDATA[npp]]></category>
            <category><![CDATA[cuda]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Sat, 11 Dec 2021 03:46:22 GMT</pubDate>
            <atom:updated>2021-12-11T03:46:22.055Z</atom:updated>
            <content:encoded><![CDATA[<p>In real world coding implementation, opencv usually used as an image processing library. But it is way too slow to implement these jobs on CPU instead of GPU. OpenCV also provide GPU version, but in ROS system, it can’t both exist two version of OpenCV.</p><p>So implemented in CUDA is my first choice to speedup my image processing module and is easily build in ROS system.</p><p>Let’s get into code: <strong>cpp</strong> and <strong>cmake</strong> file.</p><h3>cpp</h3><pre> #include &lt;opencv2/opencv.hpp&gt;<br> #include &quot;cuda_runtime.h&quot;<br> #include &quot;device_launch_parameters.h&quot;<br> #include &quot;npp.h&quot;<br> #include &quot;nppi_data_exchange_and_initialization.h&quot;<br> #include &quot;opencv/cv.hpp&quot;<br> <br> void cudaResize(cv::Mat in, cv::Mat&amp; out, int w, int h)<br> {<br>     int outSize = w * h * sizeof(uchar3);<br> <br>     int inW = in.cols;<br>     int inH = in.rows;<br>     int inSize = inW * inH * sizeof(uchar3);<br> <br>     NppiSize srcSize = {inW, inH};<br>     NppiRect srcRoi = {0, 0, inW, inH};    <br>     NppiSize dstSize = {w, h};<br>     NppiRect dstRoi = {0, 0, w, h};<br>     NppiSize finalSize = {w, h};<br> <br>     uchar3* d_src = NULL;<br>     uchar3* d_dst = NULL;<br>     uchar3* d_final = NULL;<br> <br>     cudaMalloc((void**)&amp;d_src, inSize);<br>     cudaMalloc((void**)&amp;d_dst, outSize);<br>     cudaMalloc((void**)&amp;d_final, outSize);<br>     cudaMemcpy(d_src, in.data, inSize, cudaMemcpyHostToDevice);<br> <br>     // Resize using NPP<br>     nppiResize_8u_C3R((Npp8u*)d_src, inW*3, srcSize, srcRoi,<br>                         (Npp8u*)d_dst, w*3, dstSize, dstRoi,<br>                         NPPI_INTER_LINEAR);<br>     <br>     int BGROrder[3];<br>     BGROrder[0] = 2, BGROrder[1] = 1, BGROrder[2] = 0;<br>     // Swap BGR to RGB<br>     nppiSwapChannels_8u_C3R((Npp8u*)d_dst, w*3,<br>                             (Npp8u*)d_final, w*3,<br>                             finalSize, BGROrder);<br> <br>     cv::Mat tmpImg(h, w, CV_8UC3);<br>     cudaMemcpy(tmpImg.data, d_final, outSize, cudaMemcpyDeviceToHost);<br>     out = tmpImg;<br>     // Free memory<br>     cudaFree(d_src);<br>     cudaFree(d_dst);<br>     cudaFree(d_final);<br> }</pre><h3>cmake</h3><pre>cmake_minimum_required(VERSION 2.8.12)<br> project(cudaResize)<br> <br> find_package(CUDA)<br> find_package(OpenCV REQUIRED)<br> <br>  add_executable(cudaResize cudaResize.cpp)<br> <br>  target_compile_definitions(cudaResize PUBLIC -DGPU -DCUDNN)<br> <br>  target_include_directories(cudaResize PRIVATE<br>   ${CUDA_INCLUDE_DIRS}<br>   ${OpenCV_INCLUDE_DIRS}<br>   )<br> <br>  target_link_libraries(cudaResize<br>   ${OpenCV_LIBS}<br>   ${CUDA_LIBRARIES}<br>   ${CUDA_CUBLAS_LIBRARIES}<br>   ${CUDA_curand_LIBRARY}<br>   ${CUDA_TOOLKIT_ROOT_DIR}/lib64/libnppisu.so<br>   ${CUDA_TOOLKIT_ROOT_DIR}/lib64/libnppig.so<br>   ${CUDA_TOOLKIT_ROOT_DIR}/lib64/libnppidei.so<br>   ${CUDA_TOOLKIT_ROOT_DIR}/lib64/libnppial.so<br>   )</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81b16bbe6351" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transformer 筆記]]></title>
            <link>https://medium.com/@mike19931010/transformer-%E7%AD%86%E8%A8%98-4f19abc87354?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f19abc87354</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[encoder-decoder]]></category>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Sat, 27 Nov 2021 09:35:07 GMT</pubDate>
            <atom:updated>2021-11-27T09:37:56.377Z</atom:updated>
            <content:encoded><![CDATA[<p>這是台大教授影片：<a href="https://www.youtube.com/watch?v=n9TlOhRjYoc">https://www.youtube.com/watch?v=n9TlOhRjYoc</a> 的整理筆記</p><p>主要是前幾篇有寫到Tesla在影像投影跟fusion中有使用到transformer，今天來一探究竟。</p><p>Transformer 主要被應用在sequence to sequence的task裡面，舉凡像是翻譯，語音辨識，演講即時翻譯，看圖說故事等。同樣可以應用在chatbot上面，輸入是一個句子，輸出一個回答…and so on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uUyKEtgCz-bBs24n4DgfZA.png" /></figure><p>Question answering 同樣可以被視為一種seq-to-seq的應用，像是給出一段文章要求機器給書重點整理，或者抓出關鍵字，甚至給出幾個問題要求機器回答。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0bK23i2D_EO8Pmsm76b8fw.png" /></figure><p>影片中有說到，<strong>對多數NLP任務而言，往往為這些任務客製化模型，會得到更好的結果</strong>。這邊舉例是瑞士刀，雖然可以應用在很多場景，卻還是對於特定情況使用特定工具會來的有效率。</p><p>這裡有一個範例，是google2014發的paper，Grammar as a Foreign Language <a href="https://arxiv.org/abs/1412.7449">https://arxiv.org/abs/1412.7449</a>。裡面也是使用這種seq to seq的方法來判斷英文句子結構（文法分析）。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EjlInp1TMd6Nmgdujr52hA.png" /></figure><p>同樣的這樣的模型也在2020年被應用在object detection上面，End-to-End Object Detection with Transformers(DETR) (<a href="https://arxiv.org/abs/2005.12872">https://arxiv.org/abs/2005.12872</a>)。雖然效果不是state-of-the-art，不過給了影像辨識領域一個新的思考模式，很期待未來這個方法能打敗用了很久的anchor方式的偵測模型。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dXt6vkTB_vqkaCbdbFmEYQ.png" /></figure><p>Seq-to-Seq model 是由 encoder-decoder 模型而組成。下圖是原始transformer 論文裡面 Encoder 部分的圖片。</p><h3>Encoder</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1R3N5ew0iX9c6R_4sMY0Ag.png" /></figure><p>可以看到右邊很多花花綠綠的block，接下來會針對右邊做介紹。可以看到右圖有個Nx表示有很多個這種block組成一個encoder，而一個block內含有很多個layers, 李弘毅老師的影片裡面給了很詳細的解釋。首先最下面是seqence的所有內容，先做一次self-attention讓不同時間的資訊可以共享，再來做 residual 這也是很多論文會用到的方法，注意residual是addition跟yolov4的那種concat是不一樣的。之後做layer normalization 來防止某些features影響過大，後面接了一個FC with residual同樣是特徵萃取，再接上layer normalize. 這樣可以搭配上圖來看流程了，首先是multi-head attention -&gt; addition and normalize -&gt; FC -&gt; addition and normalize 這樣拆解起來就蠻清楚的。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6SifAI_0ZAIMgiDAbryAoA.png" /></figure><h3>Decoder</h3><p>Decoder的目標就是把encoder所萃取出來的資訊收集起來，然後產生我們所需要的output. 可以看到首先把Encoder的結果給送到Decoder，然後輸入一個起始點給Decoder請它開始產生句子。之後的每個輸出都當作是t+1的輸入，就這樣持續下去到機器輸出end為止。這種方式稱作Autoregressive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kpeWzuoi-TlrOQtm_jb1Qw.png" /></figure><p>下圖是論文中Encoder-Decoder的架構圖，可以看到很大部分是一樣的，而且decoder裡面的layer其實encoder中都有提到作用了。不同的地方在於一開始的<strong>Masked Multi-head Attention</strong> 與中間 Multi-Head Attention的箭頭從Encoder輸入。其實很簡單，Masked與原本的差別在於我們並<strong>沒有辦法事先知道所有的output</strong>，只能一步一步的產生，所以時間t的輸入就只會是0…t-1而沒有t+1之後的資訊。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1x7FAp8Rf83GLj1QwxXHvA.png" /></figure><h3>AT vs NAT</h3><p>看到這邊可能會想說，那可不可以不要花時間一個一個產生output, <strong>一次丟進去就產生一個完整的句子</strong>就好。NAT — non autoregressive 這是一種這樣的方法。NAT輸入變成是一連串的begin，輸出就是一個句子。那斷句的方式有<strong>兩種</strong>，一種是讓decoder輸出很長的output, 在end後面輸出的單子就捨棄不用。第二種是訓練一個predictor去預測句子的長度。而影片中提到NAT的方法通常都不會比AT的方法來的好，需要用很多tricks才有辦法達到與AT相同的準確度。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8-fMlO7jqBeX71nbc2qF7Q.png" /></figure><h3>Encoder and Decoder 的傳遞</h3><p>接下來講到中間這層是怎麼運作的，要怎麼傳遞萃取完的資訊到decoder中。這邊可以看到在中間那層有三個箭頭，其中兩個是來自encoder而一個是來自decoder。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5UMx9pMuLzL91SxC9JRUkw.png" /></figure><p>下圖就是Cross Attension所做的事情。首先encoder會輸入一個seq再輸出一個seq，而decoder一開始會輸入一個begin token然後輸出一個向量，再乘上linear transform變成一個query，然後這個query去跟key計算attention的分數再normalize之後得到a1&#39;,a2&#39;,…,an’. 再跟v1, v2, vn做加權後相加，得到v。再把v當作是FC的輸入，然後就這樣一直重複下去。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7xQ3cFhubpRiRdkC2nCR1w.png" /></figure><p>然而在Transformer原文裡面是用Encoder最後一層的輸出當decoder的輸入。不過也不一定要這樣，可以自己設計用全部Encoder每一層的輸出當輸入或其他方法都可以。</p><h3>Training</h3><p>那inference的時候知道怎麼運作了，來看一下怎麼train吧。這邊等decoder算出答案之後，去計算prediction and ground truth的cross entropy然後去minimize 它。注意這邊不管decoder預測什麼，都要<strong>拿ground truth當作產生新單詞的input</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9kfpvxB1CUJWRIkGb5sk-g.png" /></figure><p>這邊就有一個問題產生了，當我訓練的時候都是給正確答案當成輸入，但真正inference的時候不是，這樣變成真正使用的時候當機器不小心判斷錯誤，又把這個輸出當成輸入，後面的結果就會錯下去。正所謂<strong>一步錯步步錯</strong>。那我們該怎麼解決呢？其實很直觀，就在訓練的時候<strong>故意把一些錯誤的字當成輸入</strong>，期待機器可以學習這些錯誤的字並把結果導正回來。這個問題又叫做<strong>exposure bias</strong>. 用的方法稱作<strong>scheduled sampling</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kVS4GbQiJCkihJQ9YAujDw.png" /></figure><h3>Tips</h3><p>最後講到一些訓練seq to seq時的tips.</p><p>總共是這4種：</p><ul><li>Copy Mechanism</li><li>Guided Attention</li><li>Beam search</li><li>Optimizing evaluation metrics</li></ul><p><strong>Copy Mechanism</strong></p><p>把輸入的某些詞彙，當作是輸出。比如說chatbot, 當使用者輸入：你好我是某某某。這時候機器就可以直接複製名字，輸出：你好，某某某。在讓機器讀取文章並輸出摘要時特別好用，像是做摘要時很多的詞彙都是從文章裡擷取出來的，所以這個方法就會變得讓機器不用再去分析那些專有名詞。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SLvRmZA5KyC4tVXbLSEKfg.png" /></figure><p><strong>Guided Attention</strong></p><p>經過一些實驗他們發現，有些情況會導致模型中的attention並不是我們想像的那樣，比如說speech recognition產生一個句子的時候一般來說是先看前面的聲音再看後面的聲音。可以訓練出來的結果像下圖下面那樣是亂跳的。而Guided Attention的目的就是希望機器在計算Attention scores的時候是照我們所期望的方式去產生。有興趣的話google: Monotonic Attention 或是 Location-aware attention.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9aZfGBJXN4QofrWaIw1qqA.png" /></figure><p><strong>Beam Search</strong></p><p>在之前decoder都是選擇max(A,B)，這樣就變成選到紅色這條路。可是綠色才是the best one. 所以beam search就是為了防止這樣的問題產生。應用在不同輸入會造成輸出不同的情況。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PBiOfDiFAe0PUvWN7XUdLQ.png" /></figure><p><strong>Optimizing evaluation metrics</strong></p><p>這邊提到說在訓練時我們用的都是cross entropy去判斷正不正確，可是實際上判斷一個句子對錯這這邊是用BLEU score (<a href="https://www.youtube.com/watch?v=-UqDljMymMg">https://www.youtube.com/watch?v=-UqDljMymMg</a>)</p><p>而minimize cross entropy並不一定可以minimize BLEU score。所以當選擇的時候，讓valid時使用BLEU score然後選擇分數最高的模型當作是訓練出來做好的模型。而BELU score沒辦法微分，當然也不能gradient descent。</p><p>這邊老師教了一個口訣：『如果不知道怎麼optimize的話，用reinforcement learning 硬 train 一發就對了！』</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*548NXvzEl4zoEUHyrlY2aA.png" /></figure><p><strong>總結</strong>：transformer 是在 NLP 領域非常popular 的論文，有很多論文都是基於這篇而寫出來的，而且已經在2020年應用在image的detection上面，相信未來在single frame or sequence frames(videos)的應用都是讓人很興奮的。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f19abc87354" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tesla AI Day — Labeling]]></title>
            <link>https://medium.com/@mike19931010/tesla-ai-day-labeling-2e4859df1541?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e4859df1541</guid>
            <category><![CDATA[aritificial-intelligence]]></category>
            <category><![CDATA[autonomous-driving]]></category>
            <category><![CDATA[tesla]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data]]></category>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Thu, 25 Nov 2021 17:47:43 GMT</pubDate>
            <atom:updated>2021-11-25T17:47:43.150Z</atom:updated>
            <content:encoded><![CDATA[<h3>Tesla AI Day — Labeling</h3><p>This is a briefly introduction of <a href="https://www.youtube.com/watch?v=j0z4FweCy4M">https://www.youtube.com/watch?v=j0z4FweCy4M</a></p><p>Now we moving to the spirit of neural network — data.</p><p>四年前一開始他們外包所有的資料標記給其他廠商，除了response time很長，無法實時溝通，最重要的是標記品質參差不齊。所以他們現在是in-house在做資料標記，hire 1000 個標記人員全力去做標記，這可以讓溝通變得快速，也能讓資料更統一進而讓機器學得更好。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wps-cEqfZwNCUVKXIalCIg.png" /></figure><p>可以看到他們建立了一個非常系統化的資料標記group, 這邊有哪些資料被標記、哪些人標記的、總數有多少都是被視覺化出來的。這樣可以大幅減少engineer去QA這些標記資料的時間，也讓整個流程的效率大幅提高。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zBDGnGRRpkjANIpYqacazg.png" /></figure><p>在四年前的標記都是使用影像去做標記，標記的方式是segmentation的方法，可以看到道路邊界、車道線、紅綠燈、物體都是用這種方式去做標記的。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1NFKkz35PRAU1zMvsmX7Xg.png" /></figure><p>但他們很快的不再在影像上去做標記了，而是建立了一個三維甚至四維的標記工具。首先把二維的多個影像結合起來形成向量空間，並在向量空間上去做標記，標記的結果會自動project回去每個影像當中。不得不說真的很厲害。更厲害的是，這些標記是在向量空間上完成的，我們可以把向量空間想成HD map，當標記完一個frame之後，後面的連續影像基本就不用重新標記，這省去了很大部分的標記時間。</p><p>接下來講到因為人跟機器各有所長，要怎麼樣跟機器合作，達到auto-labeling的效果。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jFkjLoZxtCuUDY74J4PKzw.png" /></figure><p>這邊是一個短影片clip的流程，一開始會從他們的實驗車或是真正tesla使用者的車擷取下來，有影像、gps、imu、odemetry的資料。之後給到後端機器做前幾篇講到的NN, 有segmentation, depth estimation, point matching…。最後產生vector space上的所有資訊再提供機器做訓練。這邊沒有提到有沒有使用到人工label，但我猜最後一階段是有的。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*smFbFgYv84GfYuI2CeiUiA.png" /></figure><p>這邊用到了一篇paper中的技術：Implicit Neural Representations with Periodic Activation Functions（<a href="https://arxiv.org/pdf/2006.09661.pdf">https://arxiv.org/pdf/2006.09661.pdf</a>）這篇論文主要在說重構一個物體，並讓表面夠連續光滑，不會像之前很多投影的方法是坑坑疤疤的。</p><p>目的是為了要重構出道路的所有資訊。輸入是很多很多二維的點，輸出是這些點的height, lane line, curb …道路上的資訊。再去求cross-entropy loss去minimize 實際標記後reproject回影像的資料與image space預測出來資料的loss。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rz9Mgd_5_W84IIBy98iLug.png" /></figure><p>他們實際demo了一個影片，看起來非常穩定而且連續。不管是道路或是線都偵測的很好，而這些資料一樣是可以透過ＮＮ投影到向量空間上的。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e-eCRBloui8mU0Gr9U9YSQ.png" /></figure><p>他們利用了這個方式投影到向量空間中並構建了一張地圖，而且不止應用在一台車上面。當這個區域有很多tesla跑過，就可以建構一張非常完整的vector space map。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EfDoRR3k6GBhnJvC7KFi8g.png" /></figure><p>而這張圖就是透過很多不同的車建構起來的地圖（標線的部分）。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BSj1t4lvQCMTmn4JlvSDYQ.png" /></figure><p>接下來他們利用同樣的方法去產生假的點雲資料，如上圖能像lidar一樣對於不同的障礙物產生不同的反射，可以一眼就知道這些平面來自不同的材質。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5lVEJ-sYExe6juqgAQlBag.png" /></figure><p>另一個應用是可以用segmentation建構一個辨識環周物體的場景。在這個場景中會有物體類別、位置、速度、加速度、軌跡、未來可能的軌跡。看起來非常流暢，而且有些物體occlusion後還能有同一個tracking ID，看起來是用前幾篇提到到video based的方法去做訓練的。這邊有趣的是目測超過50公尺以外的物體就沒有偵測了，不知道是故意的還是real-time與accuracy的trade-off。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JXm8ASag7nku4C3hOaSAyA.png" /></figure><p>用了這些方法全部弄在一起變成了這樣，有物體segment，有類lidar的結果及地面標線，就能提供planning做很大部份的決策了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pgKpigEFfdDlH9LlQ94wjg.png" /></figure><p>最後提到了一個pure camera solution會遇到的問題，也是他們在四年前遇到的問題。當前面的車經過揚起雪的時候，攝影機會霧濛濛看不清楚，這種時候在四年前是偵測不到的。不過透過現在的flow與自動標記的方式，他們在短短一個禮拜內標記了10k的clips，經過訓練已經可以像右圖一樣穩定的偵測前車了。算是花了相當短的時間就處理了這個問題，體現了這個flow的效率跟完整性。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ssa0dL_ieNWFwijfOT36hw.png" /></figure><p>最後給上了一張看上去像是實驗車收集的圖，其實是模擬出來的，很誇張的光影及真實度，也是下一章要講的重點。</p><p>結論：也難怪Tesla會自己掀開底牌，甚至連哪篇paper用幾層都說出來了。這個團隊的數量跟系統化不是其他公司一朝一夕可以達到的，加上這種類似遊戲公司的模擬系統，技術護城河已經建起來了，很期待看Tesla以後的發展。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e4859df1541" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tesla Ai Day part 2]]></title>
            <link>https://medium.com/@mike19931010/tesla-ai-day-part-2-83e4d54c7c46?source=rss-99b0a070809b------2</link>
            <guid isPermaLink="false">https://medium.com/p/83e4d54c7c46</guid>
            <category><![CDATA[path-prediction]]></category>
            <category><![CDATA[lstm]]></category>
            <category><![CDATA[rnn]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[autonomous-driving]]></category>
            <dc:creator><![CDATA[Mike LIn]]></dc:creator>
            <pubDate>Tue, 16 Nov 2021 17:59:39 GMT</pubDate>
            <atom:updated>2021-11-16T17:59:39.273Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a briefly note of this video: <a href="https://youtu.be/j0z4FweCy4M">https://youtu.be/j0z4FweCy4M</a></p><p>上一篇是有關於spatial的物件偵測，包含一開始使用單顆相機，再來應用到多顆相機之中，還有把多顆相機在二維空間轉移到向量空間中的方法。</p><p>Okay, Spatial部分處理完了，接下來處理 Temporal 的部分了。Tesla提到，在實際的case中，有些部分會需要用到時序性的模型去做處理。影片分析已經變成是下個時代的標配了，隨著ＧＰＵ越來越powerful, 時序性的模型有突破性的發展加上object detection似乎已經到了一個極限，時序性的偵測變成是下一代的兵家必爭之地。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j5aJoE30EJ18CbOp7oNWjA.png" /></figure><p>Slide 中提到，這邊是他們覺得加入時序性模型可以解決的問題。一、是否能夠知道物體的速度加速度，從而知道物體未來可能的運動，抑或是能否知道物體後面遮擋住的地方還有一個物體存在。二、如果可以持續追蹤路面上或路邊的號誌，將會對於行車資訊有很大的幫助。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l48rhBoLbyT2NYvM0wUJDg.png" /></figure><p>這是Tesla在加入時序性模型之後的overview. 可以看到在上一篇提到的multiple camera fusion 後加上了一個queue去儲存資料，可以看到在 multi-camera features 時是 20x80x256, 到 queue 時變成 20x80x256x60。</p><p>另外，這邊加入了一個IMU取得自身速度與方向，我猜目的為用其他物體的相對速度去推估絕對速度，並投影這些物體的軌跡，path prediction 到向量空間中。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b4PPYsXOlCd5UWCgRc4inw.png" /></figure><p>這邊展示了這個queue的內部細節。裡面包含了ego kinematics from IMU, multi-camera features from former layer and positional encoding。有關positional encoding 並沒有提到。而這邊提到了一個問題，多久要 push and pop 一次，這邊寫說 27ms. 具體沒有提到這個magic number怎麼產生的，可能實務上的try and error吧。除了Temporary queue以外，還有一個根據距離push and pop 的 Space-based queue, 目的為儲存之前所有的資訊，例如目前的車道是左轉道還是直行。而為什麼要用space based？因為在停等紅燈時，如果單純用時間做queue會讓很久以前的資訊pop掉，等到綠燈時早就忘了之前的資訊，因此使用了space-based queue. (push every one meter)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9RtcnsmRQZpxIsp1GD4T5Q.png" /></figure><p>他們 survey 了很多種可能性，從3D convolution model 到 transformer 的變形與很多人很愛在時序性資料上使用的RNN, LSTM 去做。最後他們選的Spatial RNN model as video module.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y6BN5j5OvPmQu_WCSwbBzA.png" /></figure><p>可以看到這邊用的是bird view的方式做使用，這在自動駕駛算是大家都會用的一個view, 因為物體的高度不太會影響結果。這邊選用LSTM based RNN model, 因為有使用IMU的關係加上車子本身只能識別某些範圍內的資訊，因此在更新時lstm只會更新附近的資訊保留其他偵測不到的地方。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yUlinzzwvO6PMFbRttpTRg.png" /></figure><p>這邊展示出了hidden state of RNN, 可以看到每個feature map 被賦予不同的任務，像是偵測中心點、路面邊緣、標線…等。也有提到說LSTM可以學習當附近有車阻擋路面資訊時，不去更新路面資訊，而是等到遮擋的車子開走後再重新開始更新路面資訊，這就是forget gate的功勞。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z8hFHz3z--gh4_xddbAQ7w.png" /></figure><p>介紹完怎麼實作之後緊接著是實驗結果，可以看到這是透過spatial rnn 訓練後的模型跑出來的結果，可以透過偵測路面標線自己建立一個類似於hd map的空間，而且這是單一車子所產生出來的，很難想像如果有成千上萬的車子可以產生出多精確的類HD map.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UNggep-sBG3riX4xJDexeQ.png" /></figure><p>而他們繼續把這樣的video based module 應用在object detection上，很直觀的可以解決物體被遮擋的問題。因為object detection by frame當物體被擋住時根本就不可能知道後面還有物體。而用rnn的話，之前偵測的結果會被記錄下來，當被遮擋住時LSTM會知道什麼時候要keep住之前的資訊，這就讓整個偵測模型更加的robust. 所以當物體遮蔽時，橘色by frame偵測完全偵測不到而藍色則是利用了之前的資訊將物體保留在該在的地方。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EBQaYs6VltwnR_qZgzZZfA.png" /></figure><p>Tesla 繼續的把這個方法應用在深度偵測及速度估算上，可以看到綠色為ground truth (by radar), 而橘色是之前single timestamp 的估測，藍色為加入影像模組後的估算結果。可以看到藍色已經幾乎是完全貼齊綠色了。難怪已經可以把radar給拔掉了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Ro9xEJKo1WmTk-v2H2gFw.png" /></figure><p>整個end-to-end架構如左圖。底層是raw image然後透過rectify layer去處理distortion, 之後個別接上NN去extract features. 第二層使用了transformer去整合每個相機的資訊並且投影到向量空間上。再加上一個queue裡面包含每27ms更新的temporary queue與每1公尺更新的 space-based queue。之後加上video module which is implemented by RNN. 最後是每個task自己的head去處理自己專注的事情。這邊不一樣的是一開始的架構圖給的三個head是object detection, traffic light tasks, lane prediction. 這邊右邊的demo video看起來是lane prediction, image semantic segmentation, depth estimation.</p><p>總結來說，真得太厲害了。真的是把neural network用到極致了，所有問題一律用NN來處理，本來覺得tesla終究要投入lidar懷抱，現在看起來離pure camera solution已經不遠了。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83e4d54c7c46" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>