# AI 環境 ## 連線至(容器內的)伺服器終端機 ### Windows 可以透過 PieTTY 或是 VSCode。 - [PieTTY 教學](https://www.youtube.com/watch?v=8jCcEI74XOY&index=1&list=PLaDzWgOj0-mly-JVlSgV-kcWMaPFdhNn1) - [VS Code 遠端連線教學](https://xenby.com/b/221-%E6%95%99%E5%AD%B8-%E4%BD%BF%E7%94%A8-visual-studio-code-%E9%80%8F%E9%81%8E-ssh-%E9%80%B2%E8%A1%8C%E9%81%A0%E7%AB%AF%E7%A8%8B%E5%BC%8F%E9%96%8B%E7%99%BC) ### Mac 打開任何終端機程式輸入以下指令, 請參考 [Linux 的 SSH 安全加密連線指令使用教學、設定檔配置範例](https://blog.gtwang.org/linux/ssh-command-tutorial-and-script-examples/)。 ```shell ssh root@[CONTAINER_IP] -p [CONTAINER_SSH_PORT] ``` :::info 這次的伺服器是使用安裝 ubuntu 18.04 的容器, 連線進去後使用者會是 `root`,擁有最高權限。 一般使用者如果需要取得最高權限的話, 下面許多指令需要透過 `sudo` 執行。 [Linux 的 root 與 sudo 介紹](https://blog.gtwang.org/linux/sudo-su-command-tutorial-examples/) ::: ## 事前準備 ```shell= # 更新 ubuntu 套件的資料 apt-get update apt-get dist-upgrade # 安裝所需的 ubuntu 套件 apt-get install vim tmux -y # vim: 是文字編輯器 # tmux: 是終端機管理程式 ``` ### vi vi 是一個強大的文字編輯器,在 Linux 環境中很多人使用。 請參考 [vi 簡介](https://mropengate.blogspot.com/2015/07/vim-ch1-vim.html)。 以下介紹 vi 的使用方法: - 執行 ```shell= vi # 直接執行 vim vi [FILE_NAME] # 執行 vim 並開啟一個檔案 ``` - 一般模式 - 進入 vi 後預設會是一般模式,一般模式下可以進行存檔、離開等操作,但不能編輯文字。 - 編輯模式 - 在一般模式下按 `i` 即可進入編輯模式,可輸入及編輯文字,編輯完成之後按 `esc` 可以回到一般模式。 - 複製及貼上文字 - Windows - PieTTY 預設的複製文字是將文字選取起來,貼上的話則是按 `滑鼠右鍵`,與一般使用電腦的 `Ctrl+C` `Ctrl+V` 不太一樣。 - Mac - 在 Mac 終端機中使用原本的 `Command+C` `Command+V` 即可。 - 存檔 - 在一般模式下按 `:w` `Enter` 後即可存檔。 - 離開: - 在一般模式下按 `:q` `Enter` 後即可離開編輯器。 ### tmux 在電腦上如果有很多事情要同時處理,我們會習慣開多個視窗,或是分割畫面來進行多工。 `tmux` 就是終端機的多工工具,它也可以讓程式在背景執行,幫助我們提升工作效率。 請參考 [Linux tmux 終端機管理工具使用教學](https://blog.gtwang.org/linux/linux-tmux-terminal-multiplexer-tutorial/)。 以下介紹 `tmux` 的使用方法: - 執行 ```shell= tmux # 建立一個新的 session tmux new -s [SESSION_NAME] # 建立一個指定名稱的 session tmux ls # 查看目前現有的 sessions tmux attach -t [SESSION_NAME] # 開啟指定的 session ``` - session, window, pane - session\ 當執行 `tmux` 之後,會開啟一個新的 session, session 的概念與虛擬桌面相似, 你可以將相關的工作放在同一個 session 裡, 並為這個 session 命名,方便管理不同專案。 - window\ 每個 session 裡可以開啟多個 windows,相當於開啟了多個視窗, 每個視窗都可以做不一樣的事情。 - pane\ 每個 window 又可以分割成多個 panes,就是分割畫面的概念, 把同個畫面切割成很多區塊去進行不同的事情。 - 簡單的操作指令 - session - `Ctrl+B` `D`: 將目前的 session 移至背景執行,不會關閉正在 session 中執行的程式。 - 將所有 windows 關閉時,整個 session 即結束。 - window - `Ctrl+B` `C`: 建立新視窗(**c**reate) - `Ctrl+B` `N`: 切換至下個視窗(**n**ext) - `Ctrl+B` `P`: 切換至前一個視窗(**p**revious) - `Ctrl+B` `&`: 關閉目前的視窗 - pane - `Ctrl+B` `%`: 垂直分割視窗 - `Ctrl+B` `"`: 水平分割視窗 - `Ctrl+B` `方向鍵`: 切換至指定方向的 pane - `Ctrl+B` `X`: 關閉目前的 pane ## 安裝 Python ```shell apt-get install python3-dev -y ``` 執行以上指令可以安裝 Python 的執行環境(runtime), 這表示系統具備以下兩種功能: 1. Python console 在終端機輸入 `python3`,即可進入如下的 Python 的互動式環境: ```shell= $ python3 Python 3.6.9 (default, Dec 8 2021, 21:08:43) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ``` 可以在裡頭執行 Python 語法,例如: ```shell=4 >>> a = 1 >>> b = 2 >>> a + b 3 >>> ``` Python 的語法可以參考 [Python入門詳細介紹](https://medium.com/python4u/hello-python-509eabe5f5b1), 按下 `Ctrl-D` 或是執行 `exit()` 可以離開 Python console。 2. 執行 Python 程式 與 console 的單步執行不同,你也可以直接執行包含多個指令的 Python 程式,步驟如下: 1. 使用 vi 編輯 `test.py`。 ```shell vi test.py ``` 2. 在 vi 中輸入以下程式碼並儲存離開。 ```python= a = 1 b = 2 print(a + b) ``` 3. 執行 `test.py`。 ```shell= $ python3 test.py 3 ``` :::info 上述操作相當於只使用最陽春的 Python 直譯器,配合 vi 編輯器來開發。 也有適合 Python 的整合開發環境(integrated development environment, IDE), 可以參考[使用 VS code 建置環境並執行python程式](https://ithelp.ithome.com.tw/articles/10212365)。 執行環境與整合開發環境之間的區別請參考[認識 IDE 整合開發環境](https://makerpro.cc/2015/08/what-is-ide/)。 ::: ## 套件管理 ```shell apt-get install python3-pip -y ``` 執行以上指令可以安裝 Python 的套件管理程式 `pip`, 請參考[套件管理工具 pip 指令用法](https://learn.markteaching.com/python-pip/), 以下介紹 `pip` 的使用方法: ```shell= pip3 install --upgrade pip # 更新 pip pip3 install [PACKAGE_NAME] # 安裝套件 ``` 先不用急著安裝所需的套件,配合下面虛擬環境的功能,可以更有效的管理 Python 開發環境。 ## 虛擬環境 透過建立虛擬環境,每個專案可以使用不同的虛擬環境來建置該專案所需的套件,彼此互相獨立。 詳細介紹請參考 [Python 的虛擬環境 (virtual environment) 使用介紹](https://docfunc.com/posts/33/)。 以下介紹虛擬環境的使用方法: 1. 安裝 `virtualenv` 套件。 ```shell pip3 install virtualenv ``` 2. 建立名為 `VENV_NAME` 的虛擬環境。 ```shell virtualenv -p python3 [VENV_NAME] ``` 3. 進入虛擬環境,這時可以開始安裝專案所需套件、執行 Python 程式。 ```shell= $ source [VENV_NAME]/bin/activate # 進入虛擬環境 # 成功進入後,可以看到命令提示字串前面出現 (VENV_NAME) (VENV_NAME) $ pip install [PYTHON_PACKAGES] ``` :::info 進入虛擬環境後,就不一定要使用 `python3`、`pip3`, 更建議透過 `python`、`pip`, 使用在建立虛擬環境時指定的 Python 版本。 ::: 4. 離開虛擬環境 ```shell= (VENV_NAME) $ deactivate # 成功離開後,命令提示字串前面的 (VENV_NAME) 會消失 $ ``` ## AI 專案中常用的 Python 套件 ```shell pip install scikit-learn tensorflow torch torchvision matplotlib ``` - [sklearn](https://scikit-learn.org/stable/) - [tensorflow](https://www.tensorflow.org/) - [keras](https://keras.io/) (包含在 `tensorflow` 套件裡) - [pytorch](https://pytorch.org/) (通常也會安裝 `torchvision`) - [matplotlib](https://matplotlib.org/) ## 練習:建立適合 AI 專案的 Python 虛擬環境,在其中執行 AI 程式 這邊選了兩個經典的資料集,並附上對應的程式: :::info 以下會使用到的範例程式都放在 Github 上, Github 是個免費開源的平台,使用者可以將專案上傳到 Github 中來做版本控管以及協作。 而其中 Git 是拿來做版本控制的工具,Github 就是 Git 推出的代管服務平台。 Git 以及 Github 的介紹請參考 [Git 與 GitHub 介紹,軟體版本控制基本教學](https://tw.alphacamp.co/blog/git-github-version-control-guide)。 ::: 請執行以下指令安裝 Git 以及下載範例程式 ```shell= apt-get install git # 安裝 git clone https://github.com/mbilab/AI-environment.git # 下載範例程式 ``` 下載完成後 `ls` 查看目錄,範例程式即在 `AI-environment` 資料夾裡,`cd` 進入後即可看到本次課程會使用到的程式。 ```shell= # 下載完成後 $ ls AI-environment [VENV_NAME] $ cd AI-environment $ ls LICENSE README.md iris_sklearn.ipynb iris_sklearn.py mnist_keras.py mnist_pytorch.ipynb mnist_pytorch.py ``` 1. [MNIST](https://nickyuu.github.io/2017/07/04/01/) 是灰階的手寫數字辨識,圖像資料,適合深度學習,可以使用 `pytorch` 或是 `keras`。 ```shell= python mnist_pytorch.py # pytorch python mnist_keras.py # keras ``` 2. [Iris](https://blog.yeshuanova.com/2018/10/dataset-iris/) 是鳶尾屬花朵分類問題, 花瓣花萼長寬的結構化資料,適合傳統機器學習,可以使用 `sklearn`。 ```shell python iris_sklearn.py ``` :::info 如果你安裝完所需套件並成功執行程式後,可以透過以下步驟管理套件/處擬環境, 請參考 [pip3 freeze & requirements.txt](https://learn.markteaching.com/python-pip/#pip_freeze_%3E_%E5%88%97%E5%87%BA%E5%AE%89%E8%A3%9D%E7%9A%84%E5%A5%97%E4%BB%B6): 1. 將目前環境中安裝的套件版本記錄至 `requirements.txt`。 ```shell pip freeze > requirements.txt ``` 2. 在其他環境/電腦依照 `requirements.txt` 安裝套件。 ```shell pip install -r requirements.txt ``` ::: ## Jupyter Notebook / Lab Jupter Notebook 是一個介於 console 與編輯器之間的開發環境, 透過直譯式的特性,達到高互動的執行結果,方便資料的視覺化呈現, Jupyter Lab 則是它的加強版。 - [Jupyter Notebook 完整介紹、安裝及使用說明](https://medium.com/ai-for-k12/jupyter-notebook-%E5%AE%8C%E6%95%B4%E4%BB%8B%E7%B4%B9-%E5%AE%89%E8%A3%9D%E5%8F%8A%E4%BD%BF%E7%94%A8%E8%AA%AA%E6%98%8E-846b5432f044)。 - [JupyterLab 資料分析必備 IDE 完全指南](https://www.gushiciku.cn/pl/23Lf/zh-tw) 以下介紹 Jupyter Notebook 的使用方法: ### 安裝 ```shell pip3 install notebook ``` ### 啟動 ```shell= jupyter notebook --allow-root jupyter notebook --allow-root --ip 0.0.0.0 --port 8000 # 指定 ip、port ``` ### 登入 #### token 登入 正常啟動後看到以下畫面: ```shell= [I 06:20:12.583 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret [I 06:20:12.891 NotebookApp] Serving notebooks from local directory: /root [I 06:20:12.891 NotebookApp] Jupyter Notebook 6.4.8 is running at: [I 06:20:12.891 NotebookApp] http://7172a415c7a3:8000/?token=2312f53142b7d9b0140d74f0bd3984097e350298c4fd3d87 [I 06:20:12.891 NotebookApp] or http://127.0.0.1:8000/?token=2312f53142b7d9b0140d74f0bd3984097e350298c4fd3d87 [I 06:20:12.892 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 06:20:12.897 NotebookApp] No web browser found: could not locate runnable browser. [C 06:20:12.897 NotebookApp] To access the notebook, open this file in a browser: file:///root/.local/share/jupyter/runtime/nbserver-1471-open.html Or copy and paste one of these URLs: http://7172a415c7a3:8000/?token=2312f53142b7d9b0140d74f0bd3984097e350298c4fd3d87 or http://127.0.0.1:8000/?token=2312f53142b7d9b0140d74f0bd3984097e350298c4fd3d87 ``` 打開網頁後會要求輸入 token,輸入 `token=` 後面的那一串亂碼即可登入。 #### 密碼登入 使用此種方式不用每次都要複製 token 才能登入,方法是在啟動前先執行以下指令設定密碼: ```shell= $ jupyter notebook password Enter password: # 輸入你要的密碼,這裡不會明碼顯示出來 Verify password: # 再輸入一次 [NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json ``` 設定完成後,再次啟動後即可使用設定的密碼登入。 ### 關閉 在終端機按下 `Ctrl+C` 後會確認是否要結束,按下 `Y` `Enter` 即可關閉。 ### 範例程式 - mnist_pytorch.ipynb - iris_sklearn.ipynb :::info Jupyter Lab 的使用方法大同小異。 ```shell pip3 install jupyterlab # 安裝 jupyter-lab --allow-root # 啟動 jupyter-lab --allow-root --ip 0.0.0.0 --port 8000 # 指定 ip、port ``` 登入、密碼設置及關閉方法皆與 Jupyter Notebook 相同。 ::: ## 在 Jupyter Notebook / Lab 中使用虛擬環境 請參考[幫 Notebook 切換虛擬環境](https://weirenxue.github.io/2021/08/17/jupyter_notebook_venv/), 方法如下: ```shell= # 假設 `VENV_AI` 是想在 Jupyter Notebook / Lab 中看到的虛擬環境 $ source [VENV_AI]/bin/activate (VENV_AI) $ pip install ipykernel (VENV_AI) $ python -m ipykernel install --user --name=[VENV_AI] ``` 之後再啟動 Jupyter Notebook / Lab , 就可以看到剛才新增的 `VENV_AI` 了。 ## 在 VSCode 中使用虛擬環境以及 Jupyter Notebook - [VSCode 設定虛擬環境 Virtual Env](https://pythonviz.com/vscode/visual-studio-code-virtual-environment-setup/) - [VSCode Jupyter Notebook 互動編程](https://pythonviz.com/colab-jupyter/visual-studio-code-jupyter-notebook-integration/) ## Jupyter Hub Jupyter Hub 是 Jupyter Notebook 的多人連線版本, 請參考 [JupyterHub](https://jupyter.org/hub)。 以下介紹 Jupyter Hub 的使用方法: - 安裝 ```shell= apt-get install curl curl -fsSL https://deb.nodesource.com/setup_17.x | bash - apt-get install nodejs -y pip3 install jupyterhub # 通常在虛擬環境以外,直接安裝在整個伺服器上 npm install -g configurable-http-proxy ``` - 啟動 ```shell= jupyterhub jupyterhub --ip 0.0.0.0 --port 8000 # 指定 ip、port ``` - 啟動後,打開網頁會看到輸入帳號密碼的畫面。 但是因為目前容器內的 ubuntu 只有 `root`,沒有其他帳號,所以沒有辦法登入。 可以新建立一個帳號,測試是否能正常登入。 ```shell= # 建立一個叫做 `test` 的使用者 $ adduser test Adding user `test' ... Adding new group `test' (1000) ... Adding new user `test' (1000) with group `test' ... Creating home directory `/home/test' ... Copying files from `/etc/skel' ... Enter new UNIX password: # 輸入密碼,這裡不會以明碼出現 Retype new UNIX password: # 再次確認 passwd: password updated successfully Changing the user information for test Enter the new value, or press ENTER for the default # 設定帳號資料,可以一直按 `Enter` 就好 Full Name []: Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Y ``` 建立帳號之後再次啟動、打開網頁,輸入剛才建立的帳號密碼,即可成功登入。