Try   HackMD

HowTo:使用 TensorFlow 訓練 MNIST 手寫數字辨識模型

以下教學如何在 TWCC 建立一個開發型容器,並使用 Jupyter Notebook 的工作環境,進行 MNIST (手寫數字辨識資料集)的 AI 訓練

Step 1. 登入 TWCC

Step 2. 建立開發型容器

  • 請參考 開發型容器 建立開發型容器

  • 映像檔類型請選擇 TensorFlow、映像檔選擇支援 Python 3 的版本、硬體選擇支援 1 顆 GPU 的設定即可

Step 3. 連線容器、下載訓練程式

  • 使用 Jupyter Notebook 連線容器,開啟 Terminal
  • 輸入以下指令,將 NCHC_GitHub training 程式下載至容器
git clone https://github.com/TW-NCHC/AI-Services.git

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Step 4. 進行 AI 模組訓練

  • 進入 Jupyter Notebook首頁,點進右側的點進 「AI-Services/Tutorial_One」,再點選內部的「mnist_fromScidm.ipynb」以開啟程式

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 進入程式後,點選「run」按鈕即可開始訓練

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

:information_source: 補充:MNIST資料集為經典的手寫辨識資料,相關資訊請參考:
https://scidm.nchc.org.tw/dataset/mnist

:point_right: 更多使用資訊,請參考 「資料市集」平台使用說明 :bow: :secret:

  • 訓練的結果會顯示在程式下方

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Step 5. 訓練結束,關閉開發型容器

  • 訓練結束

以下程式碼請逐步打入Notebook

  • 安裝pipenv套件
!pip install pipenv --user

  • 輸入以下指令,將 NCHC_GitHub 容器控制程式下載至容器
!git clone https://github.com/TW-NCHC/TWCC-CLI $HOME/TWCC-CLI

  • 確認pipenv安裝位置
!export PATH=$HOME/.local/bin:$PATH; whereis pipenv

  • 安裝pipenv
!export PATH=$HOME/.local/bin:$PATH; pipenv install

:high_brightness: pipenv install 問題 I

  • 出現regex問題 (看到出現很多regex)

檢查有沒有安裝 python-dev
移除方法

!sudo apt update; sudo apt install -y python-dev !pipenv install

:information_source: 這個問題可在jupyter notebook 環境解決

:high_brightness: pipenv install 問題 II

  • pip路徑問題(已安裝pip,但whereis pipenv沒有出現)
    已安裝pip,但找不到路徑

:high_brightness: pipenv install 問題 III

  • 其他問題
    做:移除pipenv重載
    移除方法:
pip uninstall pipenv # or pip uninstall pipenv --user # or sudo uninstall pipenv

確定成功

:information_source: 這裡需要輸入"Procceed (y/n)?" y
在jupyter環境做不到,所以jupyter環境裡不能解決。

沒有出現pipenv位置

whereis pipenv

確定pip位置

whereis pip

重新install pipenv (要用user)

pip install pipenv --user

pipenv install,確認pipenv位置&確認使用的pipenv有沒有一致

pipenv install whereis pipenv which pipenv

呼叫絕對位置

echo $PATH

呼叫絕對位置的pipenv

$HOME/.local/bin/pipenv


有出現表示呼叫成功

重新做 pipenv install

pipenv install

:high_brightness: pipenv install 問題 III

  • 出現 (跑pipenv run..時出現)
bash:pipenv:command not found

:information_source: 可能原因:讀錯路徑

做:移除 .local/ 路徑

移除方法(:information_source: 建議使用Terminal環境)

ls -alh rm -rf .local/


  • 接著重新install pipenv
pip install pipenv -- user

- :bulb:進入TWCC-CLI/路徑,於此路徑上 做pipenv install

cd TWCC-CLI/ pipenv install

  • 進入src/test/路徑,跑容器控制程式
cd src/test/ pipenv run python gpu_cntr.py

  • 檢視容器控制程式位置
!cd $HOME/TWCC-CLI/src/test; ls

  • 執行 gpu_cntr.py
!export PATH=$HOME/.local/bin:$PATH; cd $HOME/TWCC-CLI/src/test; pipenv run python gpu_cntr.py
  1. 第一次執行,將出現詢問API金鑰畫面

:information_source: 這裡需要輸入"API金鑰"
在jupyter環境做不到,所以jupyter環境裡不能解決。
必須回到Terminal

  • 回到Terminal,輸入以下指令
cd TWCC-CLI/ pipenv run python src/test/gpu_cntr.py
  • 輸入您的 TWCC API 金鑰 (金鑰會依計畫、使用者、使用者身份會有不同),便可成功登入

  • 選擇使用的計畫 (使用滑鼠點擊計畫或在 Answer 輸入計畫編號後,Enter)

  • 回到Jupyter Notebook,重複執行gpu_cntr.py

:bulb:查詢金鑰的方法:

  • 登入 TWCC 後,點選右上角的使用者名稱,再點選「API 金鑰管理」


  • 便可檢視您的金鑰

:bulb: 更換使用計畫的方法:

  • 執行以下指令刪除金鑰,並再次進入 TWCC CLI 環境,便可輸入其他計畫之金鑰
rm -rf ~/.twcc_data

:bulb: 注意:

  • 輸入 API 金鑰後,若遇以下語言設定問題:
    " UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-6: ordinal not in range(128)"


  • 請先依序執行以下指令,設定語言檔案,再次輸入 API 金鑰即可登入
sudo apt-get update sudo apt-get install -y locales sudo locale-gen "en_US.UTF-8" sudo update-locale LC_ALL="en_US.UTF-8" sudo export LC_ALL=en_US.UTF-8
  1. 重複執行,將出現可用提示指令
  • 列出目前存在的容器目錄,並確認欲刪除目標容器id
!export PATH=$HOME/.local/bin:$PATH; cd $HOME/TWCC-CLI/src/test; pipenv run python gpu_cntr.py list-cntr
  • 本例:欲刪除之目標容器 site_id 為 623473
  • 刪除容器
!export PATH=$HOME/.local/bin:$PATH; cd $HOME/TWCC-CLI/src/test; pipenv run python gpu_cntr.py del-cntr 623473
  • 程式下方顯示成功刪除
  • 再度確認是否刪除容器
!export PATH=$HOME/.local/bin:$PATH; cd $HOME/TWCC-CLI/src/test; pipenv run python gpu_cntr.py list-cntr
  • 沒有顯示目標刪除容器,或目標刪除容器status項顯示 Deleting,皆成功