Try   HackMD

Google Colab相關設定

colaboratory使用自訂資料檔案的方法

tags: Colab Python

說明

程式入門者對程式環境設定一直是個不小的坑與門檻,如果有頻繁在各種桌機、筆電、OS系統執行Python程式,設定環境費時費力。Colab是以雲端網頁呈現的Python筆記本形式,類似Jupyter Notebook,但開啟時其實是幫你開啟一個Linux虛擬機,加上可以調用免費GPU算力,可以輕鬆達到跨平台使用的目的,也不只是可用於資料科學領域,對於程式日常開發而言也是好幫手。

優缺點比較

  • 優點
    • 使用雲端空間,不用自己的電腦空間。
    • 可免費用Colab的GPU算力,執行深度學習速度有感。
    • 可以跳過Mac、Win的各種坑,設定省心。
    • 手機平板也可以執行,但建議只用來看成果,因為容易斷線且優化待加強。
  • 缺點
    • 佔用Google雲端硬碟容量,有些學校有提供學生免費申請Gmail教育帳號,使用空間無限大。如需付費資訊參閱Google說明
    • Colab設定其實也有不少坑,下一篇會分享實用的填坑技巧。
    • 雲端服務一定要有網路。

如何開啟Colab筆記本?

從Google搜尋Colab新增或開啟檔案

  • colab關鍵字餵狗(Google)後出現的Google Colab即是,歡迎頁面目前已經改為繁體中文介面,或直接依提示視窗選擇用範例、Google雲端硬碟、GitHub或本機上傳。
    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 →

從Google雲端資料夾開啟Colab

  1. 至您的雲端資料夾,如果有任何副檔名是.ipynb的檔案,就可以用Colab服務開啟此筆記本。
  2. 以滑鼠右鍵點選*.ipynb檔案,選擇開啟工具,如果先前有「連結」過,就會有「Google Colaboratory」的選項
    • 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 →

從超連結開啟Colab

  • 如果在Google雲端硬碟的.ipynb沒有出現像上圖的Colab選項,可以進行以下步驟:
    • 點選「連結更多應用程式」,搜尋Colab並連結Colab。

      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 →

      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 →

    • 如果連結Colab仍未切換開啟目標*ipynb檔案,很有可能是您本來就尚未登入google,請記得先登入google帳戶唷。

常用相關配置

更改為GPU運算

Colab有意提供GPU運算資源給願意投入深度學習領域的開發者,如需使用免費GPU資源可依下方式更換。但如果您的程式未用到GPU資源,目前Golab版本會在右下方提示建議您改回CPU運算(當然也可以不改,目前)。

  • 點選「執行階段」-「變更執行階段類型」
    • 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 →
  • 選擇變更為GPU運算
    選單下方有提示"如要發揮Colab的最大效益,請避免在不必要的情況下使用GPU"。(如非深度學習其實沒必要使用GPU喔)
    • 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 →
  • 查看GPU資訊
    • !nvidia-smi
      嘿嘿這次要到Tesla P100,以前常開到Tesla K80,Google也有不斷升級超佛心!
      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 →

更改為英文版(選用)

  • 建議改成英文版,原因有2:
    • 預設繁體中文翻譯不見得看得懂。
    • 繁體中文目前排版不是等間距字元!!(希望以後會改)
      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 →

Colab 操作指南

  • 當您 Google 搜尋 Colab ,歡迎頁面介紹了該應用的用途與多種範例。

    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 →

  • 文字欄位使用 MarkDown 語法編輯, 程式碼欄位可輸入程式,按下 Play按鍵或Shift + Enter,執行結果會在程式碼下方顯示。

    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 →

  • 側邊欄目前有三個功能

    1. 目錄,目錄的產生來自您文字欄位的 MarkDown 語法設定的# 大標# 次標,另外您文字 Cell 也可以依照您的標題次序展開/收合。
      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 →
    2. 程式碼片段,Colab 有許多奇技淫巧,在此處您可以搜尋、閱讀解說、將範例插入程式碼區塊。建議可以花時間逐一瀏覽,讓腦海有這些用途概念以便伺機採用。
      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 →
    3. 檔案,是我覺得最精彩的功能,前面提到每個 Colab 其實是 Google 幫您開了雲端虛擬 Linux 主機,如果您有 Linux 基礎則您運用時如虎添翼,終端機(Terminal)指令只要在前方加入!+指令 即可,例如!pip install Flask,並且支援 !cp!mv!git clone 等指令。上傳/下載檔案也可直接在此側分頁操作,或設計程式介面在Colab的Cell供使用者操作(程式碼片段的分頁有範例)。
      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 →
  • 更多功能仍請參閱歡迎頁面,下方很多範例有關機械學習/深度學習,有興趣可以開啟執行,或許會開啟您邁向AI領域之路。

如何讓您的Google雲端硬碟檔案與Colab連結?

方法1. 先看懶人包

  • 以下為執行碼複製貼上,將os.chdir()改您的Google雲端硬碟資料夾位址即可)
#Colab資料夾設定方法
#!pip install google.colab #如未安裝取消註解後執行
import os

from google.colab import drive
drive.mount('/content/drive')

##出現提示欄進行授權

os.chdir('/content/drive/My Drive/{你的google雲端硬碟資料夾位址}') #切換該目錄
os.listdir() #確認目錄內容

細部說明

  • Colab在說明中有提到,當你開啟Colab互動環境,其實是開啟了一個虛擬機,你可以透過與雲端硬碟連結。

  • 另外Colab的提示只有下方兩行,執行後授權Colab與你的google雲端硬碟帳戶連結。

    ​​​​from google.colab import drive
    ​​​​drive.mount('/content/drive')
    
  • 過程中會出現等待您的授權碼,請點選連結,複製授權碼貼回Colab等待的對話框即可。

    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 →

  • 進一步讓Colab能存取你指定的google雲端硬碟路徑,你需要執行以下程式碼:

    ​​​​import os
    ​​​​os.chdir('/content/drive/My Drive/{你的google雲端硬碟資料夾位址}') 
    
  • 要確定是否順利切換到指定目錄,可以用os.listdir()檢查目錄之下檔案是否如預期,如果看到自己的資料夾目錄代表成功。

    ​​​​os.listdir() #確認目錄內容
    
  • 注意事項:如果Colab要從外部下載安裝第三方套件(如Talib),記得先安裝好Talib再切換到自己的雲端硬碟目錄

方法2. 直接點圖示完成連線 (NEW!)

  • 因為 Google Colab 為雲端服務持續更新,介面更新也不會特別更您說,目前的最新狀態是: 側邊欄的 Google 雲端硬碟圖示點下去,確認您授權操作Google 雲端硬碟,就大功告成了!
    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 →

Colab繪圖如何正常顯示繁體中文?

懶人包

# Colab 進行matplotlib繪圖時顯示繁體中文 # 下載台北思源黑體並命名taipei_sans_tc_beta.ttf,移至指定路徑 !wget -O taipei_sans_tc_beta.ttf https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download !mv taipei_sans_tc_beta.ttf /usr/local/lib/python3.7/dist-packages/matplotlib//mpl-data/fonts/ttf from matplotlib.font_manager import FontProperties import matplotlib.pyplot as plt # 自定義字體變數 myfont = FontProperties(fname=r'/usr/local/lib/python3.7/dist-packages/matplotlib/mpl-data/fonts/ttf/taipei_sans_tc_beta.ttf') # !!!!後續在相關函式中增加fontproperties=myfont屬性即可!!!!

說明

matplotlib繪圖要顯示中文需要修改相關設定,經過一番苦心找到解法,並且使用開源且可商用的「台北黑體」(感謝 翰字鑄造JT Foundry)。

  1. 下載指定字體置於colab指定位置
    2. 以!wget下載字體
    3. 存置Colab路徑: /usr/local/lib/python3.7/dist-packages/matplotlib/mpl-data/fonts/ttf
  2. 各種文字輸出的函數加入fontproperties屬性,如:
    ​​​​plt.title("自訂標題",fontproperties=myfont) 
    ​​​​plt.ylabel("自訂y軸",fontproperties=myfont) 
    ​​​​plt.xlabel("自訂x軸",fontproperties=myfont) 
    ​​​​ax.annotate('註解說明',fontproperties=myfont)
    ​​​​ax.set_title('自訂標題',fontproperties=myfont)
    ​​​​ax.set_xlabel('自訂x軸',fontproperties=myfont)
    ​​​​ax.set_ylabel('自訂y軸',fontproperties=myfont)
    ​​​​複製cell並執行即可
    

參考