Chao-Kuei Hung
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 拿自己的資料訓練 yolov3 ## 一、 轉檔及備料 本節動作可在本機執行。 轉換標籤檔格式: 1. 下載 [Isabek/xml2txt](https://github.com/Isabek/XmlToTxt) 2. 查看 requirements.txt 發現只需要安裝一個相依套件: ```pip3 install --user declxml``` 3. 把 out/ 目錄清空。 4. 測試: python3 xmltotxt.py -xml xml/ -out out/ 5. 拿自己的資料轉檔時, 請注意 「Please, add 'bucket' to classes.txt file.」 之類的訊息。 可以這樣撈出所有類別名稱 (例如上述的 bucket), 順便統計每個類別的案例數: ```perl -ne 'print "$1\n" if m#<name>(\w+)</name>#' *.xml | sort | uniq -c | sort -n``` 然後貼到 classes.txt 去。 規律整理大量資料時, [regular expressions](https://www.cyut.edu.tw/~ckhung/b/re/) 超好用。 6. 至於 「Missing required array "objects" at annotation」 這樣的錯誤, 是因為某些相片的對應 xml 檔裡面沒半個標示物件的長方形框座標。 應該要把對應的 jpg 圖片刪掉: 在標籤目錄執行 ```ls | perl -pe 's/\.txt//' | sort > ~/a``` 又在圖片目錄執行 ```ls | perl -pe 's/\.jpg//' | sort > ~/b``` 最後在家目錄執行: ```comm -3 a b | perl -pe 's/$/.jpg/' > c``` 然後就可以回圖片目錄執行 ```rm $(cat ~/c)``` 訓練資料拆成 1:5 (這部分建議晚一點再回頭來做): 1. 進到圖片目錄底下: ```ls | perl -ne 'print if $. % 6 == 0' > ~/valid.txt``` 然後把 == 改成 != 再跑一次, 改產生 ~/train.txt。 用這個方式可以把所有檔案分成 1/6 加 5/6。 有了檔案清單, 就可以用 ```cp $(cat ~/train.txt) 目的地``` 把圖檔放到適當的目錄。 2. 幫 train.txt 每列前面加上適當路徑, 例如: ```perl -pe 's#^#/d/cdc/train/images/#' ~/train.txt > /tmp/train.txt``` 對於 valid.txt 也比照辦理。 ## 二、試車 以下第二步開始, 都在 google colab 裡執行。 建議使用 [cyut 的 gmail 帳號](http://web.cyut.edu.tw/p/406-1000-20327,r30.php?Lang=zh-tw) 這樣儲存空間可能會比較大。 1. 到 [ckhung/yolov3-ult](https://github.com/ckhung/yolov3-ult) 裡面, 把 transfer_cfg/transfer.ipynb 上傳到 google drive 去。 2. 根據你自己的目錄與檔案配置, 略微修改 ```ln -s '/content/drive/My Drive/...' ...``` 那三句。 建立這三個捷徑之後, 下面各個指令與設定檔的路徑寫起來比較簡單、 大家可以一致。 3. 一直執行到 ```python3 detect.py``` 那一格。 4. 從 google drive 裡, 到 /c/output 底下查看結果。 建立捷徑時請注意: google drive 的儲存空間不支援 symbolic link (不可以是捷徑所在位置)。 Symbolic link 只能從 runtime 虛擬機的臨時硬碟下的目錄指出去。 執行 detect.py 時, 如果遇到 load_darknet_weights 出現 RuntimeError: shape [...] is invalid for input of size ... 有可能是因為 weights/download_yolov3_weights.sh 下載回來的 xxx.weights 檔壞掉了, 導致權重矩陣與模型設定 (xxx.cfg) 不符。 這時需要改成手動下載。 順便一提: 一個用到 argparse 的 python 程式, 它的每個選項的預設值是多少? 參考 [這個問答](https://stackoverflow.com/questions/12151306/argparse-way-to-include-default-values-in-help), 可以在 ```parser = argparse.ArgumentParser()``` 那一句裡加上這個參數: ```formatter_class=argparse.ArgumentDefaultsHelpFormatter``` 這樣執行時 -h (help) 選項會印出預設值。 如果你有租用 linux host (有自己固定的 IP), 建議可以架設 [VNC 服務](https://newtoypia.blogspot.com/2014/05/vnc.html), 從那裡開瀏覽器連到 google colab 做事, 這樣才可以從家裡、 宿舍、 等等各地連到同一個 runtime 接續先前的部分訓練成果。 ## 三、 遷移學習 根據 [這個問答](https://github.com/ultralytics/yolov3/issues/106) (請在頁面內尋找「realize」) 當我們自己的資料的 class 個數小於等於 80 時, 其實可以直接拿現成的權重矩陣來做遷移學習。 這種做法效果不佳, 但最簡單, 不需要改 .cfg 檔。 呼叫 ultralytics 版的 train.py 時, 只需要加上 --transfer 選項, 就會試著載入既有的權重矩陣, 並且用這種方法訓練。 照理來說, 訓練資料應該分為 training 與 validation 兩部分, 各佔約 5/6 與 1/6 。 可是看程式碼好像並沒有用到 validation 。 這個選項 --data 就像是粽子串的頭, 那個設定檔裡有好幾個路徑, 例如 train= 後面的路徑裡面再有一堆路徑指向訓練圖片。 而 backup= 後面的路徑則是訓練過程中各階段的權重暫存檔。 每隔多久存一次權重檔? 在命令列上以 --bkevery 指定。 如果太常存檔, 你的 google drive 容量會很快用爆掉。 GPU 記憶體不足? 我覺得 [這個問答](https://github.com/eriklindernoren/PyTorch-YOLOv3/issues/40) 的提問者講得比較有道理,不過回答者說降低 batch size 就好, 程式作者也同意, 那就照辦吧: 在命令列上指定 --batch size 16 或更小的數字。 第一次的 "Reading labels" 超級慢。 第二次以後就快多了。 訓練一段時間後, 如果中斷, 下次再執行時, 可以加上 --resume。 這樣它會去找上次最後留下的 last.pt 從那一個時間點接續著訓練。 但是如果訓練已完成, 又想要再 --resume , 程式會出錯。 此時可以把某個備份的 .pt 檔複製成為 last.pt 等於倒帶回那個時間點重算。 完整指令範例: ```python3 train.py --bkevery 10 --resume --transfer --batch-size 16 --data transfer_cfg/transfer-data.txt``` ## 四、 整個砍掉重練 重要參考資料: [Train Custom Data](https://github.com/ultralytics/yolov3/wiki/Train-Custom-Data) 我們的積水容器資料集只有 13 個類別, 所以需要修改模型檔。 按照上述連結 [5. Update .cfg file] 那一節改好之後存檔, 比方說叫做 yolov3-spp-cdc.cfg 好了。 訓練時, 在 train.py 的命令列上要加上 -cfg 參數, 指定符合資料集的模型。 完整指令範例: ```python3 train.py --bkevery 10 --resume --batch-size 16 --cfg cfg/yolov3-spp-cdc.cfg --data from0_cfg/from0-data.txt``` 但是! 後來我把程式改壞掉了,找不到正確的版本可以上傳,所以目前 github 上面的版本無法從零開始訓練。 反正先前成功時,發現這個版本就算砍掉重練,效果也不好。 算了,改天再找別的版本來改。 ## 五、 更多參考連結 1. 本文最重要參考資料: [ultralytics/yolov3 的 Example: Transfer Learning](https://github.com/ultralytics/yolov3/wiki/Example:-Transfer-Learning) 2. 這篇解釋不少觀念,但沒講軟體版本等等細節: [建立自己的YOLO辨識模型 – 以柑橘辨識為例](https://chtseng.wordpress.com/2018/09/01/%E5%BB%BA%E7%AB%8B%E8%87%AA%E5%B7%B1%E7%9A%84yolo%E8%BE%A8%E8%AD%98%E6%A8%A1%E5%9E%8B-%E4%BB%A5%E6%9F%91%E6%A9%98%E8%BE%A8%E8%AD%98%E7%82%BA%E4%BE%8B/) 3. 這篇詳盡說明各個參數的意義: [Training YOLOv3 : Deep Learning based Custom Object Detector](https://www.learnopencv.com/training-yolov3-deep-learning-based-custom-object-detector/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully