顯示具有 機器學習 標籤的文章。 顯示所有文章
顯示具有 機器學習 標籤的文章。 顯示所有文章

2025年5月21日 星期三

模型是根據句中的哪段話判斷分類結果

 

#Transformer #BERT # Self -Attention  #Explainable AI  #LIME  #SHAP 

在NLP中的文章分類或句子的情緒判別中,模型分類結果符合預期,但如何得知到底是這句話或文章那句話或詞語決定了分類的結果 ? 


LIME 執行的結果


若使用的是Transformer 的BERT., 那可以透Attention weight distribution , 得知那模型主要關注了那一個字。但若分類模型不是BERT 這種具有Self -Attention 的機制,那作法可以用模型可解釋性 (Explainable AI) 的方法,,如LIME 或 SHAP ,他們會用分類模型結果去建立一個簡線性模型, 如Logistic Regression 的這種架構,Y=w1x1+w2x2+w3x3+....如此便可以透過這些權重值w1,w2,... 去了解每個字的重要性。









2024年12月16日 星期一

判別式AI與生成式AI

 

生成式AI的崛起與發展

生成式AI(Generative AI)在2022年底隨著ChatGPT的問世而廣受關注。ChatGPT展現出近乎自然的人類對話能力,讓許多人驚嘆AI技術的進展。如今,ChatGPT的功能已不僅限於文字對話,更可解讀圖片、PDF文件分析,並能提供內容摘要與深度分析。市場上類似的工具還包括Microsoft Copilot、Claude和Notebook LLM等。除了文字生成,AI技術也延伸至音樂創作和圖像生成領域。 在圖像生成領域已有非常成熟的平台,例如 Stable Diffusion、Midjourney和DALL-E等。

判別式AI與生成式AI的本質差異

相較於2019年主流的判別式AI(Discriminative AI)——專注於圖像分類或文本分類等任務,生成式AI面對的是更具挑戰性的問題。判別式AI主要解決P(y=k|x)的問題,即在已知條件x下,預測標籤y為k的機率,而無需了解x的整體分布。

生成式AI則致力於估計P(x),即從觀察到的樣本x1, x2, x3...中推測整體的機率分布。這個任務的複雜度遠超過判別式AI。理解P(x)分布的重要性在於:如果我們能找到一個近似分布Q(x),使其接近真實分布P(x),那麼從Q(x)中採樣得到的新樣本x將與真實數據具有相似的特徵。

舉例來說,在人臉生成的應用中,即使生成的面孔並不存在於原始訓練數據集中,但由於其符合真實人臉的分布特徵,因此看起來自然且真實,難以與真實人臉區分。這正是生成式AI的強大之處。


生成式AI的技術發展史

  • Shift from GANs to Diffusion Models for image generation
  • Evolution from autoregressive models to large language models

  • 2022: Stable Diffusion released as open-source

    2023

    • GPT-4 released with multimodal capabilities
    • Claude (Anthropic) and Google's PaLM demonstrate advanced reasoning
    • Improved versions of Stable Diffusion (XL, 3)
    • Llama and Llama 2 released by Meta
    • Gemini announced by Google












    2022年12月31日 星期六

    佈署AI模型於Web 上


    目標: 用瀏覽器上傳圖案到Web後端進行預測, 並將模型預測結果回傳至瀏覽器。


    #teachable machine #Flask  #Keras 

    #Install required packages
    pip install flask==2.1.1
    pip install Pillow==9.1.0
    pip install tensorflow==2.3.1
    pip install keras==2.4.3






    專案目錄結構如下, 其中model 資料匣 要放置AI 模型檔及Label 文字檔。凡應用是上傳圖片,由模型預測分類結果的, 都可以套用本專案程式架構。在不想改程式碼, 就直接模型檔名改成keras_model.h5 ,類別的文字檔改成 labels.txt。 


     執行主程式 main.py 後, 會載入模型同時啓動Web Server (Flask) 



    P.S. 此處 model 資料匣的所放的AI模型, 是直接使用 Teachable Machine 來建立模型。為了方便套用至其他的應用程式, 從teacbable machine 複製來的程式碼, 將其程式架構改寫為 Class 的方式,會比較好用。而且主程式會顯得很簡潔也會易於日後程式維護。aimodel.py 程式碼參考如下:



     

    2021年11月10日 星期三

    How to save and load fine-tuned model?



     如何儲存Fine-tune BERT model 的網路權重及架構?

    Custom mask layers require a config and must override get_config  ...........


    1. ) 若pre-trained bert model 只是用來作為sentence embedding 的話,. 那就只儲存後面自己接的網路架構..不用整個儲存(即不必含BERT model), 因此可忽略載入pre-trained bert model . 那就回到原本傳統的model.save() , load_models('xxxx.h5')


    2.) pre-trained bert model 串接自己網路架構, 一起訓練, 如果是這種的, 就用方法2

    只存weight (model.save_weights), 用原本model 架構去產生一個空的new_model, 然後new_model.load_weights

    model.save_weights('my_model_weights.h5')
    ...
    new_model = <build your model with your model building code>

    new_model.load_weights('my_model_weights.h5')








    2021年2月28日 星期日

    物件偵測 (#1/5): MS COCO的資料集格式




    在進行AI圖像上, 常會看到MS COCO的資料集,

    COCO 的圖片資料集,提供3種標注檔:object instances(用於物件偵測), person keypoints(人的關鍵點,用於姿態識別)以及 image captions(圖像標題, 5 captions per image) , 每種標注類型都有相應的json 檔。標注檔也分好了訓練集、驗證集。


    with open(annotation_file, "r") as f:
        data = json.load(f)
        annotations=data["annotations"]
        images=data["images"]
        categories=data["categories"]
        
        
        
    print(f"Number of images: {len(annotations)}")
    print(f"Number of images: {len(images)}")
    print(f"Number of images: {len(categories)}")

    The COCO dataset has been downloaded and extracted successfully.
    Number of images: 36781
    Number of images: 5000
    Number of images: 80

    images[60] => 取出index 60 這張圖的資訊

    {'license': 1,
     'file_name': '000000360661.jpg',
     'coco_url': 'http://images.cocodataset.org/val2017/000000360661.jpg',
     'height': 480,
     'width': 640,
     'date_captured': '2013-11-18 21:33:43',
     'flickr_url': 'http://farm4.staticflickr.com/3670/9709793032_f9ee4f0aa2_z.jpg',
     'id': 360661}

    annotations[60] => 取出index 60 annotations資訊

    {'segmentation': [[267.51,
       222.31,
       292.15,
       222.31,
       291.05,
       237.02,
       265.67,
       237.02]],
     'area': 367.89710000000014,
     'iscrowd': 0,
     'image_id': 525083,
     'bbox': [265.67, 222.31, 26.48, 14.71],
     'category_id': 72,
     'id': 34096}


    annotation{
        "id": int,    
        "image_id": int,
        "category_id": int,
        "segmentation": RLE or [polygon],
        "area": float,
        "bbox": [x,y,width,height],
        "iscrowd": 0 or 1,
    }


    每一張圖片會有一個image_id, 而一張圖可能包含一個以上的單一物件或群物件. 針對每一個物件,
    不論是單一物件或群物件, 都會用一個annotation來表現物件內容.. 一張圖會有多個annotation, 即多個物件







    annotation{
     "id": int, ==> 物件id
     "image_id": int, ==> 所屬的圖片
     "category_id": int, ==>此物件的類別id
     "segmentation": RLE or [polygon], ==> 單一物件或一群物件的區域描述
     "area": float, ==> 物件區域的Pixel總數
     "bbox": [x,y,width,height], ==> bounding box的座標
     "iscrowd": 0 or 1, ==> 0: 單一物件, 1: 一群物件 (如:一群觀眾) 
    }


    其中"segmentation": 若為單一物件, 則是以一個多邊形的座標點 [X1,Y1,X2,Y2, ....] 來描述此物件的區塊位置.
    若是一群物件的區域描述, 如要描述一群蘋果,則會用Mask的方式來描述,如下圖所示。






    一群物件的區域描述, 即iscrowd=1, 則segmentation的內容為

    {'counts': [671, 10, 2, 2, 4, 22, 6, 31, 1, 11, 1, 10, 379, 16, 1, 25, 5, 55, 378, 43, 4, 55, 378, 44, 3, 55, 378, 44, 3, 55, 378, 44, 3, 55, 378, 44, 3, 55, 378, 44, 4, 54, 379, 29, 1, 16, 1, 54, 380, 28, 2, 15, 2, 53, 382, 23, 6, 15, 1, 8, 21, 1, 3, 3, 5, 12, 384, 20, 8, 16, 40, 12, 384, 16, 14, 15, 40, 10, 386, 10, 21, 14, 40, 8, 388, 8, 22, 15, 41, 3, 393, 3, 25, 15, 465, 15, 465, 15, 466, 14, 467, 13, 468, 12, 469, 10, 471, 8, 474, 3, 983, 7, 472, 9, 470, 11, 454, 6, 1, 20, 452, 28, 451, 30, 449, 31, 448, 33, 447, 34, 446, 35, 445, 35, 445, 35, 445, 35, 445, 36, 445, 36, 445, 35, 447, 33, 450, 30, 450, 30, 450, 12, 1, 17, 451, 10, 3, 16, 452, 8, 6, 13, 455, 4, 12, 8, 474, 3, 50865, 6, 459, 6, 8, 11, 454, 27, 452, 29, 450, 31, 448, 32, 448, 32, 448, 32, 448, 32, 448, 32, 448, 32, 448, 31, 450, 29, 452, 20, 2, 4, 456, 7, 1, 3, 1, 4, 7174, 6, 2, 6, 4, 14, 447, 34, 445, 36, 443, 38, 442, 38, 442, 38, 442, 38, 442, 38, 442, 38, 442, 38, 443, 36, 445, 34, 446, 6, 19, 6, 450, 3, 478, 1, 42714, 6, 473, 8, 471, 10, 469, 15, 465, 18, 462, 19, 461, 21, 459, 22, 458, 24, 456, 26, 455, 25, 456, 24, 458, 22, 461, 18, 463, 16, 466, 3, 5, 3, 3840, 7, 463, 20, 459, 22, 457, 27, 448, 33, 446, 35, 437, 44, 435, 46, 433, 47, 432, 48, 432, 48, 432, 48, 432, 48, 432, 30, 4, 14, 432, 29, 7, 12, 432, 29, 8, 10, 433, 29, 9, 8, 435, 13, 1, 1, 2, 10, 12, 3, 439, 12, 6, 7, 456, 10, 471, 3, 3, 2, 474, 1, 478, 2, 477, 3, 476, 4, 476, 9, 470, 11, 469, 12, 468, 13, 467, 14, 7, 1, 458, 23, 457, 23, 457, 23, 458, 22, 459, 21, 461, 19, 463, 18, 462, 19, 461, 20, 1, 9, 450, 33, 447, 34, 446, 36, 444, 37, 443, 38, 443, 38, 443, 37, 445, 35, 450, 7, 2, 21, 459, 21, 460, 20, 461, 19, 463, 3, 3, 10, 471, 8, 474, 3, 18209, 1, 479, 2, 478, 3, 477, 4, 476, 5, 475, 6, 474, 7, 474, 10, 471, 8, 474, 4, 5302, 7, 447, 4, 26, 4, 445, 6, 1, 15, 11, 3, 443, 29, 6, 3, 442, 32, 4, 2, 442, 38, 442, 38, 442, 38, 442, 38, 442, 38, 442, 37, 444, 16, 1, 18, 446, 8, 1, 3, 6, 3, 4, 8, 449, 3, 22, 4, 46076, 6, 468, 13, 466, 15, 461, 20, 459, 21, 458, 22, 457, 23, 457, 23, 457, 23, 457, 23, 457, 22, 458, 21, 459, 20, 460, 20, 461, 19, 462, 18, 463, 17, 462, 18, 461, 19, 460, 19, 461, 19, 461, 19, 461, 19, 461, 19, 461, 19, 461, 19, 461, 18, 463, 16, 465, 8, 1, 3, 470, 4, 31194, 12, 9, 6, 452, 29, 445, 36, 443, 38, 441, 39, 435, 45, 434, 46, 427, 53, 426, 54, 425, 55, 424, 55, 425, 54, 426, 53, 427, 51, 429, 25, 1, 24, 430, 22, 5, 22, 210], 'size': [480, 640]}






    References:




    尚有其他訓練影像的標記格式, 如PASCAL VOC 以XML格式儲存, TensorFlow Object Detection 以.csv 儲存, 而Darknet (Yolo) 以.txt 儲存

    2020年10月5日 星期一

    AI邊緣運算實作: TensorFlow Lite for MCU




    AI邊緣運算


    隨著物聯網與人工智慧發展,工作負載開始由雲端移轉至終端,AI也隨之進入到嵌入式系統及物聯網終端裝置中。在終端或所謂邊緣裝置這類的超低功耗微處理器上所實現的深度學習,被稱呼為微型深度學習。然而在MCU本身運算速度就不快,記憶體空間也有限的情況下,AI模型也不能太大,因此要能確保AI 效能,同時兼顧低功耗、成本與縮短開發時間,都是研發人員所必須面對的挑戰。

    什麼是TensorFlow Lite

    TensorFlow Lite for MCU正是專為邊緣裝置設計的TensorFlow模型預測框架,是TensorFlow的精簡版本,讓開發者可以在物聯網與嵌入式裝置中部署微型機器學習模型。

    AI模型佈署於微控制器,需要那些知識 ?

    首先,必須先知道TensorFlow Lite for MCU 在實作上會涉及Deep Learning 觀念及MCU開發技術,然而這是兩個截然不同的技術領域的人, 故要能有效整合出好的AI系統,必須能同時理解這兩邊的技術。簡而言之,你會需要對於Deep Learning 有一定的概念、還要有模型優化(如Quantization)、TensorFlow Lite的框架以及MCU程式開發的相關知識。技術不能說很難,但對於初學者,沒有人引導,是有困難度的,因為開發環境包含AI環境與MCU的BSP環境都不太熟悉的情況下,產生的錯誤常常搞不清楚問題在那 。

    在板子跑AI和我在電腦跑AI有何差別?

    在電腦上跑AI,對於輸入資料多半是.csv或圖片,以檔案方式存在。 然而在邊緣運算上, 輸入的資料通常是來自硬體的感測器,因此要能正確且即時的對資料預處理並且轉換成模型所須要的x輸入型狀(x input shape)才能代入模型。但資料抓取上要如何切成一個剛好完整的x 來進行推論呢? 例如聲音訊號是連續波, 不可能每次都剛好切出一段完整的聲音丟入模型,而得到很好的正確率。還有,像是感測器訊號也可能有雜訊產生,也會造成模型誤判。所以在系統上,需要有一個處理的機制來讓我們的AI系統達到穏定。

    在板子上讀取硬體訊號,作為模型輸入的資料 $x$

    板子上的感測器,可能像是G-Sensor 、Microphone 這類的。接著我們要讀出感測訊號並觀察訊號。務必確認感測訊號是正確的,因為不對的$x$, 就會是GIGO (Garbage In Garbage Out)。
    然後對原始訊號做訊號處理 (即數據的前處理) ,獲得訊號的特徵向量 $x'$ 後 ,再餵入模型 $\hat{y}=f(x')$

    在Sparkfun edge board 讀取G-Sensor X, Y,Z 的變化


    在Sparkfun edge board 讀取Microphone 左右聲道變化


    Tiny DL Demo
    底下Demo 幾個AI邊緣運算分別在圖像、聲音、感測訊號的應用

    2020年9月7日 星期一

    win10安裝 Anaconda +Tensorflow2.0

    一. 建議使用Google Colab 執行keras/tensoflow 的程式,因為這是最適合初學者的操作環境, 不僅可以省去很多套件安裝的麻煩, 還可以擁有免費的GPU/TPU 計算資源可以使用.




    二.  Win10 安裝 Anaconda + Python 3.7+ Tensorflow2.x


    1. ) 先下載Anaconda 3.x    並安裝 (基本上只要一直按"下一步",即可完成安裝)



    2.) 利用Anaconda 建置一個虛擬環境, 並在此虛擬環境下安裝所需的套件

    先在Windows 開始選擇, 找到 “Anaconda Prompt”, 並開啓 [Anaconda Prompt]




     
    接著執行以下指令

    #新增虛擬環境(取名為tf2.0)

    conda create -n tf2.0 python=3.7

    #啓用虛擬環境並安裝TF2.0 (穏定版本)

    conda activate tf2.0

    pip install tensorflow==2.0.0-beta1

    pip uninstall numpy

    pip install numpy==1.16.2

    pip install matplotlib


    #在虛擬環境tf2.0安裝其他套件 (如果有需要的話)

    conda install jupyter


    #在虛擬環境tf2.0, 進入python 互動環境(REPL)並檢查TF版本

    python

    import tensorflow as tf

    tf.__version__





    #移除conda虛擬環境tf2.0

    conda env remove --name tf2.0

    =======其他=============

    # 將tensorflow 2.0 更新到目前最新版本(非stable 版本, 如果有必要的話才更新)
    pip install tf-nightly --upgrade


    2020年6月1日 星期一

    multivariate Gaussian distribution


    一維的高斯分佈(或常態分佈) $X \sim N(\mu,\sigma^2 )$

    PDF:  p(x)


    二維的高斯分佈 $ N\sim(\mu,\sum ) $
    PDF:  p(x,y)



     
    MultivariateNormal.png


    Many sample points from a multivariate normal distribution with  and , shown along with the 3-sigma ellipse, the two marginal distributions, and the two 1-d histograms.

    μ ∈ Rk — location
    Σ ∈ Rk × k — covariance 







    References:
    1. WiKi-Multivariate normal distribution
      https://en.wikipedia.org/wiki/Multivariate_normal_distribution
    2. 吳恩達-機器學習(9)-異常檢測、協同過濾https://www.itread01.com/content/1545204306.html
    3. Andrew Ng
      https://www.coursera.org/learn/machine-learning?action=enroll#syllabus
    4. Python for Covvriance
      https://hadrienj.github.io/posts/Preprocessing-for-deep-learning/

    2019年6月14日 星期五

    訓練自己的偵測物件--YOLO


      YOLO的訓練模型框架

    1.安裝Darkflow
    2.先使用prebuild YOLO模型
    3.訓練新的YOLO模型
    • 準備訓練資料 (圖片與xml檔): 利用LabelImg 工具框選物件及標注label
    • 修改YOLO模型參數,如class數量, filter數目
    • 定義label名稱
    • 開始訓練,相關命令列參數: model , load , gpu,…
    • 儲存模型(weight .pb & metafile .meta), 相關命令列參數: --savepb
    • 載入儲存的模型(.pb & .meta)
    • 利用python 測試YOLO模型


    1.安裝Darkflow

    git clone https://github.com/thtrieu/darkflow.git

    python setup.py build_ext --inplace

    在windows上要先安裝MS visual studio才能編譯安裝

    Flowing the graph using flow

    python flow --help  => 若能成功執行,表示己順利安裝成功

    2.先使用prebuild YOLO模型


    #測試YOLO darkflow

    python flow --imgdir sample_img/ --model cfg/tiny-yolo-voc.cfg --load bin/tiny-yolo-voc.weights

    --imgdir: 指定要倒入YOLO模型的圖檔目錄
    --model : 指定網路結構
    --load: 載入權重檔; 若為-1 則從ckpt/ 從上一次記錄的權重檔繼續train

    3.訓練新的YOLO模型

    • 準備訓練資料 (圖片與xml檔): 利用LabelImg 工具框選物件及標注label

      安裝LabelImg 工具:

      在Anaconda terminal 輸入: 

      git clone https://github.com/tzutalin/labelImg
      cd lableimg
      python lableimg.py    ==> 出現畫面如下
      安裝時可能會需要這些套件:
      conda install pyqt=5
      pip install resources
      pip install requests
      pip install staty
      pip install lxml

      接著就開始框選圖片中的物件並label其名稱 (名字不要有特殊符號, -,_@,#&^...)

      每一張圖片可以有標記多個物件.而每一張圖片也對應著一個和圖檔相同檔名的.xml檔,此xml記錄著此圖片中每個物件座標及其label名稱


    XML檔案的內容

    當你用lableimg標記完之後, 大概像下圖這樣。一個xml檔記錄著一個對應的圖檔. 若xml中有20個,則圖檔就會至少有20個被對應  
    圖檔位置: train/orchid/images

    XML檔位置: train/orchid/annotations

    • 修改YOLO模型參數,如class數量, filter數目

    1.) 通常你會使用fine tune方式, 即使用既有的yolo network model (.cfg) 與weight (.weights)作為你訓練新模型的基本架構, 然後再修改成你想要偵測的類別項目

    用既有的yolo network model (.cfg) ==> 所以你會從cfg/ copy 一份新的cfg並稍微修改一下檔名.  
    例如: cp tiny-yolo-voc.cfg tiny-yolo-voc-xxxx.cfg

    在複製出來的cfg檔中,修改網路的最後2層, 主要有兩個2個地方, 一個是分類數classes, 另一個是filter的數量
       
    ...

    [convolutional]  
    size=1
    stride=1
    pad=1
    filters=40     #在倒數第2個 [convolutional], 修改filters 數量
    activation=linear

    [region]
    anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
    bias_match=1
    classes=3   #偵測的物件有3類
    coords=4
    num=5  # anchors  box的數量 (一個anchors會有confidence、座標、在每個類別的機率)
    softmax=1


    ...
           
    filters =num*(classes+5) , 若classes=3 , num(自訂)=5, filter=40


    • 定義label名稱
    修改 darkflow/labels.txt, 一行一個label,  行數要和class數量一樣 (名字不要有特殊符號, -,_@,...)
      
    redline
    red
    white
    • 開始進行YOLO模型訓練

    # Fine tuning tiny-yolo-voc from the original one

    python flow  --model cfg/tiny-yolo-voc-orchid.cfg --load bin/tiny-yolo-voc.weights  --train --annotation train/orchid/annotations --dataset train/orchid/images


    --train : 表示要進行training
    --model : 指定網路結構
    --load: 載入權重檔; 若為-1 則從ckpt/ 從上一次記錄的權重檔繼續train
    --dataset : 指出training data
    --annotation: 框選object 的資訊 (.xml)
    --labels: labels.txt 分類名稱. 每一行代表分類名稱


    說明: 

      1.) --load  tiny-yolo-voc.weights ==> 會載入tiny-yolo-voc.weights 做為你的初始權重, 因此得去找找到對應的tiny-yolo-voc.cfg 如此才能知道如何將權重載入到網路中, 而你所自訂的tiny-yolo-voc-xxxx.weights  和原本tiny-yolo-voc.cfg 基本上前面都相同, 只有後面2層不同,所以後面的2層不會載入權重. 

    原文說明如下

    When darkflow sees you are loading tiny-yolo-voc.weights it will look for tiny-yolo-voc.cfg in your cfg/ folder and compare that configuration file to the new one you have set with --model cfg/tiny-yolo-voc-3c.cfg. In this case, every layer will have the same exact number of weights except for the last two, so it will load the weights into all layers up to the last two because they now contain different number of weights.


    2.)
    在你訓練過程中, darkflow會將不斷的weight存在 ckpt/目錄中, 所以你可以隨時從最後一次記錄的權重檔繼續train下去 (--load -1 ),而不用每次都重來..(重來很花時間的!)

    python flow  --model cfg/tiny-yolo-voc-orchid.cfg --load -1 --train --annotation train/orchid/annotations --dataset  train/orchid/images

    • 將最後的權重檔儲存為tensorflow格式 .pb & meta file 

    #Saving the lastest checkpoint to protobuf file
    python flow --model cfg/tiny-yolo-voc-orchid.cfg --load -1 --savepb

    built_graph/輸出2個檔案,到時候可拿此訓練好的模型來做物件偵測與識別

    tiny-yolo-voc-orchid.pb
    tiny-yolo-voc-orchid.meta


    • 測試模型

    1.) 用darkflw指令測試訓練好的模型
    ## Forward images in sample_img for predictions based on protobuf file

    python flow --pbLoad built_graph/tiny-yolo-voc-orchid.pb --metaLoad built_graph/tiny-yolo-voc-orchid.meta --imgdir train/orchid/images




    2.) 利用python 測試YOLO模型









    References:

    https://github.com/thtrieu/darkflow

    Full Example Code 下載


    2018年8月11日 星期六

    VS2017 設定Tesseract-OCR的編譯環境





    Tesseract是一個光學字元識別引擎,支援多種作業系統。

    [Include 目錄] (增加一項)


    [程式庫目錄] 


    [其他相依性] 


    [C/C++ 前置處理器] 前置處理器定義






    設定完成後, 執行上課範例, 可以看見原始影像為TAW-8686.jpg 然後看看Tesseract識別引擎的效果. 理論上"乾淨"的圖識別的效果應該100%正確!




    但實際上取得的車牌影像不會如此乾淨, 車牌會有污點、影像對比度可能也不足(光線影響)、拍攝角度不對以及有其他的文字、符號等等., 都得再經過影像處理的手法重新把影像"惡搞"後,才能丟入OCR去做後續的文字分析識別


    台灣的車牌


    影像經過二值化處理,將原始影影像轉換成"黑白"影像, 至於Threshold value 怎麼選, 這就是學問所在啦~


    若想要用自己的識別引擎去分析文字或一些特定的符號,也可以自己去訓練。訓練的方法可以用現在很夯的AI方法如Deep Learning  CNN來訓練模型,像是 MINST 手寫數字辨識 這種資料集(Data Set) 模型可以讓你識別出0~9的手寫數字。

    不過在做真正的影像識別的應用,通常得經過一些影像處理的技巧, 例如進行二值化 (Thresholding)、閾值分析、影像去雜訊、影像模糊、影像強化、影像縮放、色彩空間轉換及影像切割等,才餵進到識別引擎,以獲得較高的辡別率。因此,對影像進行預處理,總是避免不了~