一. 概述
本文將介紹一套非常好用的一個免費資源,由 Google 所提供的 Colab 這項雲端服務。
延續上一章節的理念,此系列的目的為工具書導向,這裡將持續介紹一些額外的第三方軟體技巧應用,比如說如何連接 Google Drive 來存取檔案,如何利用 Google sheet 來讀寫資料,以及該如何利用 Github 來建立更想大的備份代碼倉儲等等應用技巧。如下圖文章架構圖所示,此架構圖隸屬於 i.MX8M Plus 的方案博文中,並屬於 Third Party 軟體資源的 Google Colab 密技大公開 之部分,目前章節介紹 “Colab 第三方應用”。
若新讀者欲理解更多人工智慧、機器學習以及深度學習的資訊,可點選查閱下方博文
大大通精彩博文 【ATU Book-i.MX8系列】博文索引
Colab 系列博文-文章架構示意圖
二. Google Colab 應用技巧
1. Google Drives 應用 :
Google Drive可搭配 PyDrive 套件進行驗證、上傳與下載文件。而進階應用可搭配 Google Resource Manager 進行更高層度的雲端數據管理,請參考此連結。
官方網站 : https://pythonhosted.org/PyDrive/#
下列附上 程式儲存格的代碼(灰底) 以及 運行結果(灰底後的圖示),複製貼上至 Colab 即可使用 !!
(1) 掛載 Google Drive
from google.colab import drive
drive.mount('/gdrive')
(2) Google Drive - 上傳檔案
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default() # 給予授權
drive = GoogleDrive(gauth)
uploaded = drive.CreateFile({'title': 'Sample file.txt'}) # 建立檔案
uploaded.SetContentString('Sample upload file content') # 設定資料
uploaded.Upload() # 上傳
print('Uploaded file with ID {}'.format(uploaded.get('id'))) # 取得上傳文件的 id
(3) Google Drive - 載入檔案
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default() # 給予授權
drive = GoogleDrive(gauth)
file_id = 'REPLACE_WITH_YOUR_FILE_ID' #設定上傳文件的 id
downloaded = drive.CreateFile({'id': file_id})
print('Downloaded content "{}"'.format(downloaded.GetContentString()))
(4) Google Drive - 寫入檔案
with open('/gdrive/My Drive/foo.txt', 'w') as f:
f.write('Hello Google Drive!')
!cat '/gdrive/My Drive/foo.txt'
(5) Google Colab – 上傳檔案
從本機檔案系統上傳檔案
from google.colab import files
uploaded = files.upload("/gdrive/filename")
(6) Google Colab – 下載檔案
將檔案下載至本機檔案系統
from google.colab import files
df.to_csv("/gdrive/filename")
files.download("/gdrive/filename")
2. Google Sheets 應用 :
Google Drive可搭配 gspread 套件進行驗證、上傳與下載文件。
官方網站 : https://docs.gspread.org/en/latest/
(1) Google Sheets - 寫入資料 / 儲存檔案
from google.colab import auth
import gspread
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gc = gspread.authorize(GoogleCredentials.get_application_default()) #給予授權
sh = gc.create('A new spreadsheet') #創立一個新檔案
worksheet = gc.open('A new spreadsheet').sheet1 #選取工作表
cell_list = worksheet.range('A1:C2') #選取工作欄位
import random
for cell in cell_list:
cell.value = random.randint(1, 10) # 撰寫變數
worksheet.update_cells(cell_list) #上傳/儲存資料,將放置於 Google Drive
(2) Google Sheets - 載入資料
from google.colab import auth
import gspread
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gc = gspread.authorize(GoogleCredentials.get_application_default()) #給予授權
worksheet = gc.open(' A new spreadsheet ').sheet1 #從 Google Drive 中,讀取資料
rows = worksheet.get_all_values()
import pandas as pd
pd.DataFrame.from_records(rows)
3. Github 應用 :
Github 是知名的版本控制代管平台,提供組織建立和存取代管、圖表與代碼顯示等等功能。廣受開發者愛戴,已擁有超過 4 千萬的使用者並成為世界上最大的代碼存放網站,於 2018 年由微軟收購。
Google Colab 可以無痛銜接至 Github 上,進行更有效的版本管理。
設定方式如下 :
Step 1 : 開啟新的 Github Repository 倉儲
Step 2 : 於 Colab 中,點選 “在 GitHub 中儲存副本”即可。
4. Cloudflare 應用 :
Cloudflare 是一套知名的分佈式式域名解析服務,藉此應用可讓 VS Code 遠端操作 Colab 的資源。
應用方式如下 :
Step 1 : 下載 Cloudflare 的執行檔
請點選此連結,並下載相應的作業系統之二進制檔案。
Step 2 : 開啟 Colab 筆記本,取得 SSH 的連結位置
安裝套件 :
!pip install colab_ssh --upgrade
取得代理伺服器 SSH 位置 :
from colab_ssh import launch_ssh_cloudflared, init_git_cloudflared
launch_ssh_cloudflared(password="Password")
Step 3 : 開啟 VS Code 設定遠端連線,即可啟用
按下 F1 , 設定 Remote SSH : Open SSH Configuration File
Host nations-radical-cialis-luke.trycloudflare.com
HostName %h
User root
Port 22
ProxyCommand D:\COLAB\cloudflared-windows-amd64.exe access ssh --hostname %h
按下 F1 , 執行 Remote SSH Connect to Host
選擇 SSH 遠端連線 > Linux > 輸入帳密即可完成連線
三. 結語
本文主要目的是推廣 Colab 的實用性為主,其用意是希望讀者可以將此系列博文當作一套工具書來查閱,來達到快速應用之目的。本文介紹了如何透過 Google Drive 來達到上傳檔案、載入檔案等目的,以及透過 Google Sheet 來讀寫資料。接下來,我們也介紹了 Cloudflare 來實現遠端連線至 Colab 中,並透過 VSCode 的軟體介面來操作。或者也可直接用 SSH 進行連線。最後一篇文章,將利用 Colab 訓練出物件識別的模型,並直接應用至 i.MX8M Plus 的開發板中 !! 幫助讀者可以快速訓練出屬於自己的 AI 模型 !! 敬請期待 !!
四. 參考文件
[1] 官方文件 - Colaboratory 官網
[2] 第三方文件 -鳥哥的首頁
如有任何相關 Colab 技術問題,歡迎至博文底下留言提問 !!
接下來還會分享更多 Colab 的技術文章 !!敬請期待 【ATU Book-i.MX8 系列 - Colab 】 !!
評論