<?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 Jack Lin on Medium]]></title>
        <description><![CDATA[Stories by Jack Lin on Medium]]></description>
        <link>https://medium.com/@s9021025292140?source=rss-bff4bfda6a57------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*WGjWuTvCSD8vNOPBtqB6DA.jpeg</url>
            <title>Stories by Jack Lin on Medium</title>
            <link>https://medium.com/@s9021025292140?source=rss-bff4bfda6a57------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 11:38:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@s9021025292140/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[物件偵測 (Object Detection)(二)]]></title>
            <link>https://medium.com/coding-like-coffee/%E7%89%A9%E4%BB%B6%E5%81%B5%E6%B8%AC-object-detection-%E4%BA%8C-6dc482601904?source=rss-bff4bfda6a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/6dc482601904</guid>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[cnn]]></category>
            <category><![CDATA[yolo]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Jack Lin]]></dc:creator>
            <pubDate>Mon, 06 May 2019 17:13:11 GMT</pubDate>
            <atom:updated>2019-08-14T14:23:10.339Z</atom:updated>
            <content:encoded><![CDATA[<h4>徹底理解物件偵測的原理</h4><p><a href="https://medium.com/life-is-fantistic/%E7%89%A9%E4%BB%B6%E5%81%B5%E6%B8%AC-object-detection-902ee7ba758b">上一篇</a>提到使用傳統的 <strong>Sliding windows </strong>會因為需要切出大量的區域並讓每個區域獨立跑過 ConvNet ，造成<strong>極高</strong>的<strong>運算成本</strong>。</p><p>而在提到如何解決此問題前，先來看如何將 NN 的<strong>全連結層(Fully connected layers)轉換成卷積層(convolutional layers)</strong></p><h4>Turning FC layer into convolutional layers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*py9s2NZ33pJZE6NnDR7CAQ.png" /></figure><ul><li>假使你的物件偵測的演算法接受 14*14 *3 的圖片(通常不會這麼小)</li><li>用16個 5*5 的<strong>過濾器(Filter)</strong>，將 14*14 *3 的圖片轉成 10*10*16</li><li>接著用 2*2 的<strong>最大池化(Max pooling)</strong>縮減到5*5*16</li><li>然後接兩個400個單元(Unit)的<strong>全連結層(Fully connected layers)</strong></li><li>最後我們取消原本用 Softmax 輸出最大機率的類別 y，取而代之的是把 y 看成<strong>四個數值</strong>，分別對照到<strong>四個類別</strong>(前篇提到的行人、汽車、機車、背景 )的<strong>機率</strong></li></ul><p>我們要做的，就是把後面的 FC Layers 轉換成<strong>卷積層(convolutional layers)</strong>，而前面幾層的 ConvNet 架構<strong>保持原狀</strong>(如上圖第二個網路)</p><ul><li>對於第一個 FC Layer ，我們使用400個 5*5的過濾器(Filter)，則輸出的維度為 1*1*400，也就是我們不再把這400個當成一堆單元(Unit)，而是看成一個 1*1*400 的容積(Volume)</li></ul><p><strong>Note :</strong> 在意義上，這400個單元每個點都有 5*5*16 的過濾器，也就是說這400 個值每一個代表前一層5*5*16個經過某種<strong>線性函數(Linear function)</strong>的<strong>啟動值(Activation)</strong></p><ul><li>接著要做下一個卷積層，這裡使用400個 1*1 的過濾器(Filter)，則輸出的維度為 1*1*400</li><li>最後需要再使用 1*1 的過濾器(Filter)，後面再接著 Softmax 輸出1*1*4 的容積(Volume)，也就是對應類別的<strong>4個機率</strong></li></ul><p>接著就可以使用<strong>卷積化的方式實作Sliding Windows(Convolutional Implementation of Sliding Windows)</strong>，解決計算成本的問題</p><h4>Convolutional Implementation of Sliding Windows</h4><p>這個方法是基於<em>《Sermanet et al., 2014, OverFeat: Integrated recognition, localization and detection using convolutional networks》</em>這篇論文</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QNSecpqnZK82TM1nc27HAg.png" /></figure><p>假使和上述一樣，使用 <strong>Sliding Windows </strong>的 ConvNet 吃的是 14*14*3 的圖片，後面架構也皆<strong>相同</strong>，最後輸出1*1*4的容積(Volume)，而上圖為了方便只畫出2維的圖示</p><ul><li>若你的測試集圖為16*16*3(如圖黃色部分)，則在原版的 <strong>Sliding Windows </strong>會將原圖的<strong>藍色區塊</strong>餵給 ConvNet 跑過一次，將其分類為 0 或 1</li><li>接著用兩個像素(Pixel)的滑步(Stride)<strong>向右滑</strong>，如上圖<strong>綠色的區域</strong>重跑一遍 ConvNet，得到分類為 0 或 1</li><li>相同的方式跑過<strong>紅色區域</strong>後，並再進行<strong>最後一次</strong>(為了簡化圖形最後一次並沒有畫出來)完成共 <strong>4次</strong> ConvNet 的計算，最後得到<strong>4個分類</strong></li></ul><p>而在這 4次 ConvNet 的計算過程中可以發現，其中有很多的運算其實是<strong>重複</strong>的，而卷積化的 Sliding Windows 就是讓這4次 ConvNet 的正向傳播(Forward propagation)<strong>共用很多的運算</strong></p><blockquote>所以這卷積化的計算做的就是，與其把圖片的四個區域各<strong>自獨立做了共四次正向傳播</strong>，還不如把這四個<strong>整合成一次的正向傳播</strong>(Forward propagation)，利用這四塊14*14*3的共同區塊<strong>共享大量的運算</strong></blockquote><p>我們就可以把一整張圖(如28*28)使用卷積的方式，只要用<strong>一次</strong>大的 ConvNet 就能預測所有的區域，最後找到汽車的位置</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*A3i8GkN-N6Ig2BWJCqHXkw.gif" /><figcaption>Convolutional Implementation of Sliding Windows</figcaption></figure><h4>Output accurate bounding boxes</h4><p>這就是用卷積化的方式來實作 <strong>Sliding Windows</strong>，讓這整個計算過程變得<strong>更加效率</strong>，不過這個演算仍有一個<strong>弱點</strong>，也就是<strong>邊界框(</strong>Bounding Box<strong>)的位置並不會太準確</strong></p><p>當使用 <strong>Sliding Windows </strong>時，會用預設大小的 window 來跑過圖片的每一個位置，但如下圖所示，每個邊界框可能都<strong>無法</strong>剛剛好指出汽車的位置</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*EKNEjgX97vxt4Q16FI0JvA.gif" /></figure><p>為了要讓<strong>邊界框(</strong>Bounding Box<strong>)</strong>預測的位置<strong>更準確</strong>，有一個在物件偵測(Object detection)相當有名的演算法叫做 － <strong>YOLO</strong>，下一篇將進一步介紹</p><h4>References</h4><p>[1]<a href="https://arxiv.org/pdf/1312.6229.pdf"><em>《Sermanet et al., 2014, OverFeat: Integrated recognition, localization and detection using convolutional networks》</em></a></p><p>[2] <a href="https://arxiv.org/abs/1506.02640">Redmon et al., 2016</a></p><p>[3] <a href="https://arxiv.org/abs/1612.08242">Redmon and Farhadi, 2016</a></p><p>[4] <a href="https://en.wikipedia.org/wiki/Deeplearning.ai">Deeplearning.ai</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6dc482601904" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coding-like-coffee/%E7%89%A9%E4%BB%B6%E5%81%B5%E6%B8%AC-object-detection-%E4%BA%8C-6dc482601904">物件偵測 (Object Detection)(二)</a> was originally published in <a href="https://medium.com/coding-like-coffee">Coding like coffee</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[物件偵測 (Object Detection) (一)]]></title>
            <link>https://medium.com/coding-like-coffee/%E7%89%A9%E4%BB%B6%E5%81%B5%E6%B8%AC-object-detection-902ee7ba758b?source=rss-bff4bfda6a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/902ee7ba758b</guid>
            <category><![CDATA[convolutional-network]]></category>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[yolo]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Jack Lin]]></dc:creator>
            <pubDate>Wed, 01 May 2019 11:23:51 GMT</pubDate>
            <atom:updated>2019-08-14T14:21:42.030Z</atom:updated>
            <content:encoded><![CDATA[<h3>物件偵測 (Object Detection)(一)</h3><h4>徹底理解物件偵測的原理</h4><h4>What are localization and detection ?</h4><ul><li>在初學 CNN 的應用中，最常見的就是<strong>圖片分類(Image classification)</strong>，如下方中左邊的圖能夠被辨識出類別為 &quot;車子&quot;，這就是<strong>分類(Classification)</strong></li><li>而之後你要做的是讓你的 NN (Neural Network) 解決另外一個問題：<strong>分類且定位(Classification with localization)</strong>，意思是你不但要標示出這是一台車，而且還需要放一個<strong>邊界框(bounding box)</strong>，把車子所在的地方框出來，如下方中間的圖</li><li>更後面你得面對的圖片可能會有多個物件，如汽車 、機車 、公車 、路人等等，必須全部<strong>偵測(Detection)</strong>出來並且<strong>定位(localization)</strong>出它們，如下方右邊的圖</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*-0lmPPC5FbxTVbUTXLXIYg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*OvoUPv9WjJTnPYmiTLXZGA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*flc7cfxXbAFH6BJAuvcydA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HtHxkG_UxLqsKbErfKjmHA.png" /></figure><h4><strong>Classification with localization</strong></h4><p>假使我們所做的為<strong>自駕車(Self-driving car)</strong>的應用，而在一般的<strong>圖片分類(Image classification)</strong>中，你可能要辨識的類別分別為</p><ol><li><em>pedestrian</em></li><li><em>car</em></li><li><em>motorcycle</em></li><li><em>background</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*utkLdcBrMHQxoIRcD3RKxQ.png" /></figure><p>而除了經由 ConvNet 輸出預測的類別外，加入<strong>定位(Localization)</strong>後就必須包含額外四個數值 <strong><em>bx</em></strong>、<strong><em>by</em></strong>、<strong><em>bh</em></strong>、<strong><em>bw </em></strong>來表示<strong>邊界框(bounding box)</strong>，這樣一來就能利用<strong>監督式學習(Supervised Learning)</strong>來讓你的演算法輸出預測的類別以及四個參數，告訴你偵測到的物件的邊界框在哪裡</p><ul><li>定義圖片的左上的座標為<strong>(0,0)</strong>，右下角為<strong>(1,1)</strong></li><li>bx 與 by 為邊界框的<strong>中心</strong>位置，以基準點來看分別落在整張圖中的約50%與60%的位置</li><li>bh 與 bw 分別為邊界框<strong>高</strong>與<strong>寬</strong>，分別佔整張圖片高度約40%與寬度約60%</li></ul><h4>Defining the target label y</h4><p>接著，我們需定義目標標籤，在<strong>分類且定位(Classification with localization)</strong>中我們假設圖片都只有<strong>一個物件(Object)</strong>，也就是上圖四種物件中至多出現一種</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fh7HONLp49fEt7YeF3wR0Q.png" /><figcaption>Defining the target label y</figcaption></figure><p>假設有一張作為訓練資料的汽車圖做為輸入 x，則 label y 的</p><ul><li>第一維 <em>Pc</em> 為是否包含任何<strong>物件</strong> ：是→ 1 ; 不是→ 0</li><li>若 <em>Pc </em>為 0，則其他維度皆為 &quot;?&quot; (don’t cares)</li><li><em>c1、c2、c3 </em>分別為三個類別(<em>Pc</em> 為 0 代表為 background)</li></ul><p>如此以來組成的 (X,Y) 就成為了你的訓練集，並選擇你所定義的損失函數(Loss function)訓練 NN (Neural Network)，使你的 NN 輸出預測的類別與額外四個數字(bx、by、bh、bw)來指定<strong>邊界框(bounding box)</strong>完成<strong>定位(Localization)</strong></p><h4>Landmark detection</h4><p>現在知道怎麼讓 NN 輸出4個數字來指定一個物件的<strong>邊界框(bounding box)</strong>來完成<strong>定位(Localization)</strong>後，在對於更普遍的情況，我們可以讓 NN 輸出圖片中較為重要的<strong>點座標 (x,y)</strong>，也被稱作<strong>特徵點(Landmark)</strong>，作為 NN 要辨識的東西</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XfF1Cr18DAzolsC1Kdzy5w.png" /><figcaption>Landmark detection</figcaption></figure><p>假使你需要做一個可以<strong>輸出雙眼四個眼角位置</strong>的應用，你必須在左眼的左眼角標上座標 <strong>(<em>l1x </em>,<em> l1y</em>)</strong> 右眼角標上 <strong>(<em>l2x </em>,<em> l2y</em>)</strong> ，右眼以此類推，則可以修改NN 的最後一層讓他輸出4個眼角的位置；你也可以輸出嘴巴鼻子輪廓等等的<strong>座標點(特徵點)</strong>來幫助你辨認是不是<strong>人臉</strong>以及這個人的<strong>表情</strong>是開心還是不開心，又或者用32個座標來表示一個人的<strong>姿勢</strong>等等，不過這些前提是必須擁有許多經由手動標記完整特徵點的圖片來幫助我們訓練 NN</p><h4>Car Detection</h4><p>在知道了怎麼做<strong>物件定位(Object localization)</strong>與<strong>特徵點偵測(Landmark detection)</strong>後，就可以用傳統的方式進行物件偵測</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VkCO5pVIKSIDkgJBsTHPYw.png" /><figcaption>img from Andrew Ng</figcaption></figure><p>首先我們得先有一個標記好的資料集 x 和 y ，含有準確切好的車子圖片與沒有車的圖片，接著你就能訓練一個 ConvNet ，輸入一張切好的圖片，這個 ConvNet 就能夠輸出 y (1或0)代表<strong>是車</strong>或<strong>不是車</strong></p><h4>Sliding windows detection</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*LV8S7FiGktrcRPgkPlNgPw.gif" /><figcaption>img from <a href="https://towardsdatascience.com/how-do-self-driving-cars-see-13054aee2503">here</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*hF4u43vR89RuHYs63EdnXg.gif" /><figcaption>Sliding windows detection</figcaption></figure><ul><li>首先你先挑選一個視窗(window)的大小，把這個視窗所包含的圖片區域輸入到 ConvNet ，則 ConvNet 會預測這個視窗的區域是否有車，接著你會根據某個步伐<strong>滑動視窗</strong>一直重複此動作，直到滑過這張圖片的每個位置，將每個位置分類為 0 或 1，這個過程稱為<strong>一次</strong>的 <strong>Sliding window 。</strong></li><li>接著用大一點的視窗(window)重複一遍這樣的動作，而這個視窗大小必須是 ConvNet 能接受的大小，再用更大的視窗(window)重複一樣的動作，然後在有車的地方輸出 1</li></ul><p>⊙ 因為要從圖片切出非常多的區域，每個區域都要獨立跑過 ConvNet，如果你滑的步伐<strong>較大(粗略)</strong>，可以<strong>減少</strong>跑過 ConvNet 的視窗數目，但是效果可能會很<strong>差</strong>，但若滑的步伐<strong>較小(細膩)</strong>，則會有大量的視窗數目要通過 ConvNet，運算成本就會非常<strong>高</strong></p><p>⊙ 所以在 NN 興起前，大家普遍都用簡單的線性回歸運用在手動設計的特徵上進行目標檢測，因為只是一個<strong>線性函數</strong>，所以做 <strong>Sliding windows </strong>是可行的，但是使用 ConvNet 後 ，跑一次分類要花更多的時間，這樣 <strong>Sliding windows </strong>會<strong>慢到無法接受</strong>，而且你也必須使用步伐<strong>較小(細膩)</strong>的步長，否則你無法準確定位圖片中的物件</p><p>⊙ 而這個<strong>運算成本的問題</strong>在下一篇文章會提到一個有效的方法來解決</p><h4>References</h4><p>[1] <a href="https://arxiv.org/abs/1506.02640">Redmon et al., 2016</a></p><p>[2] <a href="https://arxiv.org/abs/1612.08242">Redmon and Farhadi, 2016</a></p><p>[3] <a href="https://en.wikipedia.org/wiki/Deeplearning.ai">Deeplearning.ai</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=902ee7ba758b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coding-like-coffee/%E7%89%A9%E4%BB%B6%E5%81%B5%E6%B8%AC-object-detection-902ee7ba758b">物件偵測 (Object Detection) (一)</a> was originally published in <a href="https://medium.com/coding-like-coffee">Coding like coffee</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[人臉辨識 (Face recognition)解析與實作]]></title>
            <link>https://medium.com/coding-like-coffee/%E4%BA%BA%E8%87%89%E8%BE%A8%E8%AD%98-face-recognition-cffcec53a544?source=rss-bff4bfda6a57------2</link>
            <guid isPermaLink="false">https://medium.com/p/cffcec53a544</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[face-recognition]]></category>
            <dc:creator><![CDATA[Jack Lin]]></dc:creator>
            <pubDate>Thu, 25 Apr 2019 11:06:40 GMT</pubDate>
            <atom:updated>2019-06-07T14:28:25.098Z</atom:updated>
            <content:encoded><![CDATA[<h3>人臉辨識(Face recognition) 解析與實作</h3><h4><strong>使用 Tensorflow, Keras &amp; OpenCV</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*XWmPjkZsHNsy6Xh0oAliew.jpeg" /><figcaption><a href="https://www.retailgazette.co.uk/blog/2017/08/big-brother-is-watching-why-retailers-probably-recognise-your-face/">source</a></figcaption></figure><p>來自<em>《DeepFace: Closing the gap to human-level performance in face verification》(2014)</em> [1]與<em>《FaceNet: A Unified Embedding for Face Recognition and Clustering》(2015)</em> [2]這兩篇 paper 提出的方法，此外利用OpenCV 來擷取 Webcam 的影像，並使用其提供的 Haar Cascade 分類器進行人臉檢測(Face Detection)</p><p>在 Face Recognition (人臉辨識)的問題上，通常會再進一步分成兩個種類 :</p><ul><li><strong>Face Verification (人臉驗證) :</strong></li></ul><ol><li>給予輸入 image, name/ID</li><li>輸出是否為此人</li><li>視為 1:1 matching problem</li></ol><p>e.g. 手機的人臉解鎖</p><ul><li><strong>Face Recognition (人臉辨識) :</strong></li></ul><ol><li>擁有 K 個人物的Database</li><li>給予 Input image</li><li>輸出 ID, if (image 為 K 個人物中的其中一個)</li><li>無法辨識此人, if (image 不為K個人物中任何一個)</li><li>視為 1:K matching problem</li></ol><p>e.g. 使用的人臉辨識的員工通行閘門</p><h3>Concept</h3><p>在人臉辨識 (Face Recognition) 的應用中經常要做到只靠一張照片就能辨認一個人，但深度學習(Deep Learning)的演算法在只有一筆訓練資料的情況下效果會很差，所以在人臉辨識中必須解決 <strong>One Shot Learning(單樣本學習)</strong>的問題</p><h4><strong>One Shot Learning (單樣本學習)</strong></h4><p>假定某公司內的 Database 共有4位人員的照片各一張，當有其中一位人員經過系統前的鏡頭並被捕捉到臉孔後，儘管 Database 只有一張此人的照片，系統依然能辨認出此臉孔為公司裡的員工，相反的，若不為公司內人員則無法辨識此人</p><h4><strong>Similarity Function (相似度函數)</strong></h4><p>為了達到 One Shot Learning (單樣本學習)這樣的目標，我們希望讓NN(Neural Network)去學習一個<strong>函數 d</strong></p><p><strong>d(img1, img2)</strong>：給予兩張照片，輸出這兩張照片的相異程度</p><ul><li>如果兩張照片是<strong>同一個人</strong>，則輸出一個<strong>較小</strong>的數字</li><li>如果兩張照片是<strong>不同人</strong>，則輸出一個<strong>較大</strong>的數字<br>此外，需定義一個 <strong>Hyperparameter(超參數)「τ」</strong></li><li>如果d(img1，img2)≤τ→ <strong>相同</strong></li><li>如果d(img1，img2)&gt;τ→ <strong>不同</strong></li></ul><p>如此一來就解決了人臉驗證的 1:1 matching problem，而在人臉辨識的 1:K matching problem 同樣也可使用此函數來解決 One-Shot Learning (單樣本學習)的問題</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WQCxzZ1r0bTDsA5QuhGxBQ.png" /><figcaption>找出距離小於τ的人像，並辨識出此人</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SfbvqjY1swwqgNItcN4J8g.png" /><figcaption>比對資料庫內人像的距離都大於τ，無法辨識此人</figcaption></figure><h4><strong>Siamese network (孿生網路)</strong></h4><p>使用<em>《DeepFace: Closing the gap to human-level performance in face verification》(2014)</em> [1]提出的 Siamese network 架構來達成上述 Similarity Function的效果，其實就是使用兩個常見的 ConvNet 的網路架構，這個兩個網路擁有相同的參數與權重，一樣經由 Convolution(卷積)、Pooling(池化)、Fully connected layers(全連接層)最後得到一個帶有128個數字的特徵向量(feature vector)，而這個過程稱為 encoding(編碼)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*7iBOoTvzt5eysl2RkPc0IA.png" /><figcaption>Siamese network (孿生網路)</figcaption></figure><ul><li>將兩張圖片(這裡稱x(1)與x(2))放入這兩個 ConvNet 後得出編碼的兩個特徵向量(feature vector)</li><li>為了算出兩張圖片相似度，方式為將這兩個經由編碼所獲得的128維特徵向量f(x1)、f(x2)相減並取2範數(2-Norm)的平方，這樣我們就透過Siamese network 學習出我們所想要的 Similarity Function(相似度函數)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*--QrK_vbnka5Ne36n1Kouw.png" /><figcaption>Similarity Function(相似度函數)</figcaption></figure><p><strong>Note</strong>：2範數(2-Norm)又稱為為歐基里德範數(Euclidean norm)，是以歐式距離的方式作為基礎，計算出向量的長度或大小(線性代數的內積空間經常用到)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*HmhSB7Z6iiFDuNcLexY1zQ.png" /><figcaption>2範數(2-Norm)</figcaption></figure><ul><li>總結來說，在 Siamese network 的架構我們希望能學出一種 encoding(編碼)方式，更準確來說是希望學習出參數使得我們能達成以下的目標</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EDnw9kZrAdaiH0Xuk-xn2g.png" /></figure><p>在上述的目標中，改變 ConvNet 每一層的參數就會得到不同的編碼，所以我們可以利用反向傳播(Backpropagation)來更改這些參數以達到上列的目標</p><h4>Triplet Loss (三元組損失)</h4><p>在NN(Neural Network)的訓練中，都需要一個損失函數(Loss function)作為最小化(minimize)目標，而在Face recognition的應用中為了能夠學習參數來得到良好的編碼(encoding)，《<em>FaceNet: A Unified Embedding for Face Recognition and Clustering》(2015)</em> [2]這篇論文提出一種有效的損失函數稱為 Triplet Loss (三元組損失)</p><ul><li>在 Triplet Loss 中會有 <strong>Anchor</strong>、<strong>Positive</strong>、<strong>Negative</strong> 這三種照片</li><li>Positive 為與 Anchor<strong>同個人</strong>的照片</li><li>Negative 則為<strong>不同人</strong>的照片</li><li>我們需要比較 Anchor 分別與 Positive 和 Negative 一組的兩對的照片</li><li>目標是希望 Anchor 與 Positive 的距離(編碼)<strong>較近</strong>，與 Negative 的距離(編碼)<strong>較遠</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sL5v46Jr7UDkXIJpOW1E3g.png" /></figure><p>也就是說，我們希望神經網路的參數所造成的編碼能夠使 Anchor 與 Positive的距離<strong>小於等於</strong> Anchor 與 Negative 的距離這樣的性質</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kJT7rbfL2-aq9OxfdrTliQ.png" /><figcaption>Triplet Loss</figcaption></figure><p>在上圖中，Anchor、Positive、Negative分別簡寫為A、P、N</p><ul><li>如果 f 變成<strong>零函數</strong>會將每個向量的輸出都變成零，就是所謂的<strong>trivial solutions</strong>，則0 – 0 ≤ 0 這樣就很容易滿足這個不等式，會讓NN學不到我們的目標</li><li>為了不讓 NN 將編碼學習成零函數，我們希望兩對的照片的差距不只小於等於零，還要<strong>比零還小一些</strong>，因而引進一個超參數(Hyperparameter)<strong>α</strong>，這個 α 稱為 邊距 (margin)，我們讓 ≤ 這個符號左邊的式子小於負α，習慣上會將 α 移到式子左邊</li><li>而邊距(margin) 用意即是拉開 d(A,P) 與 d(A,N) 這兩對的差距，就是把這兩對推開，<strong>盡量的遠離彼此</strong><br>e.g. 假設 margin = 0.2 ,表示若 d(A,P)=0.5 則 d(A,N) 至少0.7才符合上述的不等式，若 d(A,N) 為0.6就不符合，因為兩組的差距不夠大</li><li>Triplet Loss 的實現如下</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78d1e2fc1a7e5969cf47dedc48690cdd/href">https://medium.com/media/78d1e2fc1a7e5969cf47dedc48690cdd/href</a></iframe><h4>Loss Function (損失函數)</h4><p>Triplet Loss 定義在3張一組的圖片A、P、N上，則損失函數可以定義成：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*00gei9UxwjhZRbL6msAq-w.png" /><figcaption>Loss Function</figcaption></figure><p>這個 max 函數的用意在於，若括號的左邊項 ≤ 0則損失就為零，若左邊項 &gt; 0則損失變成大於零；而我們是希望損失越小越好，所以只要左邊項 ≤ 0不管負多少，就能把損失推向零</p><h4>Cost function (成本函數)</h4><p>將訓練資料裡一組三張圖片的損失加總起來作為整體 NN 的總成本(Total cost)，並利用 Gradient descent (梯度下降法)來去訓練 NN 最小化成本</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*Jo3_-79BZL0ilivyk17p5A.png" /><figcaption>Cost function</figcaption></figure><p><strong>Note</strong>： 假定有10000張訓練圖片，分別來自1000個不同的人(每人約10張圖片)才能構成我們的資料集，若每個人只有一張照片這樣就無法順利挑出Anchor 與 Positive，但是當 NN 訓練完成後就可以將系統用在 One-shot Learning 的問題，對於你想辨識的人，你可能只有他的一張照片也能順利辨識出此人。</p><h4>Choosing the triplets A, P, N</h4><ul><li>在訓練資料中，Triplets(三元組)樣本的選擇會是一個問題，因為在上述學習目標 d(A,P) + α ≤ d(A,N) 中，若只按照要求隨機的選擇同一個人的照片A與P 和不同人照片A與N，則這個不等式很容易就被滿足，因為隨機挑兩個人的照片有很大的機率使得 A 與 N 差異遠大於 A 與 P，這會使得 NN無法學習有效的參數</li><li>因此，要建立訓練集的話必須挑選那種<strong>很難訓練</strong>的 A, P和 N，因為目標是讓所有 Triplets(三元組)滿足 d(A,P) + α ≤ d(A,N) 這個不等式，而很難訓練的 Triplets(三元組)的意思就是你所挑選的 A,P和N會讓 d(A,P)≈ d(A,N) ，如此一來NN在學習的時候就必須花更大的力氣嘗試讓d(A,N)往上推或讓 d(A,P)往下掉 ，推開彼此以達到相隔α的邊距，這樣的效果會讓你的學習演算法更效率；反之，若隨便選會導致很多的 Triplets(三元組)都解起來很簡單，Gradient descent(梯度下降法)就不會再做任何事，因為你的NN早已把問題都做對了，這部分在<em>《FaceNet: A Unified Embedding for Face Recognition and Clustering》(2015)</em> [2]這篇論文有更詳細的說明</li></ul><h4>Face detection (人臉偵測)</h4><p>在人臉偵測的部分使用 OpenCV 的 Haar Cascade 分類器，選擇的為人臉分類器<a href="https://github.com/s90210jacklen/Real-time-Face-recognition/blob/master/haarcascade_frontalface_default.xml">haarcascade_frontalface_default.xml</a></p><h3>Usage</h3><p>完整的程式在我的<a href="https://github.com/s90210jacklen/Real-time-Face-recognition">github</a></p><ul><li>On Windows</li></ul><pre>eg: With Tensorflow as backend<br>&gt; python facenet.py</pre><h3>Result</h3><ul><li>利用 OpenCV 的 Haar Cascade 分類器進行人臉偵測(Face detection)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*LykS-3An-Px9drItoy1Jzw.png" /><figcaption>人臉偵測(Face detection)</figcaption></figure><ul><li>偵測出人臉後使用預訓練的 FaceNet 來進行 encoding 並計算距離，辨識從 Webcam 讀取的影像是否為資料庫中的人物</li></ul><p>- 若距離小於0.7則回傳資料庫內對應的人名與印出字串並發出語音”Welcome (someone), have a nice day!”</p><p>- 若不是則繼續偵測人臉並計算當下影像的編碼與距離</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*6Pe-M1RZ4W9vH3ZQ6nF4kw.png" /><figcaption>Output distance</figcaption></figure><h3>References</h3><ul><li>[1] <a href="https://research.fb.com/wp-content/uploads/2016/11/deepface-closing-the-gap-to-human-level-performance-in-face-verification.pdf">《DeepFace: Closing the gap to human-level performance in face verification》</a></li><li>[2] <a href="https://arxiv.org/pdf/1503.03832.pdf">《FaceNet: A Unified Embedding for Face Recognition and Clustering》</a></li><li>[3] <a href="https://realpython.com/face-detection-in-python-using-a-webcam/">Face Detection in Python Using a Webcam</a></li><li>[4] <a href="https://github.com/iwantooxxoox/Keras-OpenFace">OpenFace pretrained model</a></li><li>[5] <a href="https://github.com/davidsandberg/facenet">Official FaceNet github repository</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cffcec53a544" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coding-like-coffee/%E4%BA%BA%E8%87%89%E8%BE%A8%E8%AD%98-face-recognition-cffcec53a544">人臉辨識 (Face recognition)解析與實作</a> was originally published in <a href="https://medium.com/coding-like-coffee">Coding like coffee</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>