給程式設計師的實用深度學習 2022,第 2 課 — Jeremy Howard
本頁標題、字幕、摘要與閱讀筆記為已儲存的 AI 譯文,可能有誤。時間戳與來源一致,可展開查看原始字幕,並對照原影片核對。
Jeremy Howard 逐步示範如何使用筆記本、本機與 Kaggle GPU,以及為程式設計師打造的 fastai 工作流程,來訓練與部署實用的深度學習模型。
逐字稿來源:創作者上傳至 YouTube 的字幕。AI 標點由 Vidleaf 加入;原始措辭與時間戳錨點均保留。原始逐字稿可在下方取得。
AI 生成摘要
本課程因大學場地預約問題而從家中錄製,內容涵蓋課程後勤與學生專案,之後再進入將模型部署到生產環境。免費書籍可透過 fastbook 儲存庫或 course.fast.ai 取得,每章結尾都有測驗,forums.fast.ai 則為每堂課設有官方主題討論。早期的專案包括受損車輛分類器、鬍子偵測器,以及一個已在生產環境運作的食物分類器。在生產環境方面,筆記本以 ddg 取代 bing,因為 Bing API 需要 SDK 金鑰。講者建議先訓練再清理資料,說明調整尺寸的選擇以及 aug_transforms 作為資料擴增,並指出由於僅約四個微調週期,擴增可能其實並非必要。混淆矩陣顯示哪些案例較難處理,plot_top_losses 則呈現損失最高的項目,而高損失可能來自答錯卻很有信心,或答對卻缺乏信心;ImageClassifierCleaner 會依損失排序項目,以便將標錯的項目重新標記或刪除。實務建議包括在 4GB GPU 上關閉未使用的筆記本,因為 GPU 無法置換 RAM。在部署方面,重點是搭配 Gradio 的 HuggingFace Spaces,並訓練一個寵物分類器,以 learn.export 匯出,再以 load_learner() 載入,而 Gradio 需要一個函式,回傳類別對應機率的字典,且機率為一般浮點數。依賴 GPU 的步驟可使用 Colab 與 Kaggle,而預測可在本機執行,JavaScript 網頁應用程式則可透過 GitHub Pages 免費託管。
由逐字稿生成。請對照原始影片檢查連結的段落。
含時間戳的章節摘要
第二課:課程後勤與學生專案
課程設定與書籍資源
- [0:00] 第二課因大學行政場地預約問題而從家中錄製。
- [0:57] 本課程搭配的書籍完全免費,可透過 fast.ai 的 fastbook 儲存庫或 course.fast.ai 取得,書中還包含神經網路歷史等額外內容。
- [1:45] 每章結尾都有測驗;建議在看影片前先閱讀第二章測驗,並在看完後完成它。
測驗網站與論壇資源
- [2:07] Radek 建立了 aiquizzes.com,這是一個為本書提供全新精選測驗題目的網站,採用重複式學習技巧。
- [2:38] course.fast.ai 提供筆記本與 Kaggle 連結,而 forums.fast.ai 則為每堂課設有官方主題,其中的資訊比精簡的課程教材更為詳細。
- [3:40] 官方主題文章底部的「summarize this topic」按鈕只會顯示獲得最多贊同的回覆,例如 Sanyam 與 Tanishq 的回覆。
來自「Show Us What You've Made」的學生專案
將網頁應用程式部署到正式環境
從模型到正式環境網頁應用程式
- [6:20] 講者表示,有一天觀眾或許能建立自己的網頁應用程式並將其部署到正式環境。
- [6:27] 講者澄清「有一天」指的就是今天,並主動表示要示範如何立刻做到。
- [6:40] Suvash 發佈的主題被形容為幸運的巧合,因為那正好就是今天選定的主題。
將模型部署到正式環境的第一步
資料清理與 Jupyter 筆記本操作
從定義問題到資料清理
- [7:00] 在定義問題、尋找並蒐集資料之後,下一步就是資料清理。
- [7:14] 本書第 2 章涵蓋資料清理,可從課程在 Colab 中開啟,或從本機複製的版本開啟。
- [7:35] course.fast.ai 說明如何執行這些筆記本,而第 2 章則是關於將東西部署到正式環境。
- [7:46] 按下 Shift + Enter 即可執行儲存格。
Jupyter 筆記本擴充功能與目錄
- [8:20] 「Navigate」選單預設不會出現;它來自透過 pip 安裝 Jupyter 筆記本擴充功能並重新啟動 Jupyter。
- [8:45] Colab 已經有目錄,因此這個擴充功能主要用於本機環境,安裝後會出現 Nbextensions 分頁。
- [8:57] 在 Nbextensions 中點選目錄(2)會提供方便好用的導覽列。
可折疊標題與鍵盤導覽
- [9:07] 可摺疊標題擴充功能會加入控制項,用來關閉與開啟各區段。
- [9:20] 右方向鍵會移到區段結尾,左方向鍵會移到區段開頭,讓你在各區段之間移動更輕鬆。
- [9:35] 再按一次左鍵會關閉區段,按右鍵則會開啟。
用於圖片搜尋的 Bing API
- [9:48] 本書使用 Bing API 來搜尋圖片。
以 DDG 取代 Bing,以及 Python 文件查詢技巧
在 Notebook 中從 Bing 改用 DDG
- [9:58] Notebook 將「bing」替換為「ddg」,因為 Bing API 需要取得 SDK 金鑰。
- [10:05] 講者開玩笑說,深度學習最難的部分是搞懂 Bing Azure 網站並把它處理好,而 ddg 則不需要這樣做。
- [10:21] Notebook 會分享在課程網站的論壇上,唯一真正的變更是把 bing 換成 ddg 並移除金鑰。
沿用上週的熊搜尋範例
查詢 Python、fastai 與 PyTorch 的資訊
在清理資料與影像擴增之前先訓練
在清理資料之前先訓練模型
- [12:48] 有別於一般建議先清理資料,講者提出先訓練模型再清理資料,並會稍後說明原因。
- [13:10] 使用 DataBlock 取得用於訓練的資料載入器,而 show_batch 會像之前一樣顯示批次。
調整大小選項:壓縮、裁切與填補
- [13:37] 調整大小可以用字串或類似列舉的選項來指定,而壓縮會產生非常細長的熊,同時保留整張圖片可見。
- [14:12] 裁切只保留影像中央以獲得更好的長寬比,但會遺失部分內容;而用零(黑色)填補則能保留整張影像與正確的長寬比。
- [14:33] 不同的調整大小選擇可能產生不同品質的模型,但差異通常不大,因此不必太過擔心。
RandomResizedCrop 與資料擴增
- [14:49] RandomResizedCrop 每次都會產生影像的不同部分,這對於在非常大的掃描檔上執行許多輪訓練很有用。
- [15:11] 在論壇上,有人想在非常大的文件掃描檔中區分法文與德文,而建議的 RandomResizedCrop 方法稍微奏效。
- [15:51] 每次從同一張影像取得不同圖片稱為資料擴增,而 aug_transforms 會套用旋轉、壓縮、扭曲、重新上色與飽和度變化。
- [16:32] 若要訓練超過約五到十輪,通常建議使用 RandomResizedCrop 與 aug_transforms,並將 mult 留空,而非設為 2。
擴增在記憶體中進行,而非產生副本
- [17:03] 回應 Alex 的問題時,講者釐清影像並未被複製多份;每一輪都會讀取每張影像,並在訓練期間即時於 RAM 中扭曲、裁切與重新上色。
- [17:32] 電腦上不會儲存任何副本,但模型實際上會看到幾乎無限多份略有不同的副本。
以四輪微調進行的訓練執行
以更多輪數訓練更久
執行額外的輪數
- [18:24] 執行更多輪被描述為好方法,結果回報為低於百分之三。
訓練熊分類器並讀取其混淆矩陣
在 4GB 的筆記型電腦 GPU 上訓練
類別標籤的混淆矩陣
- [18:59] 混淆矩陣只有在標籤是類別時才有意義,它會顯示模型犯了哪些類別錯誤。
- [19:15] 此時模型有兩次把實際的灰熊誤認為黑熊,也有兩次把實際的黑熊誤認為灰熊。
- [19:27] 模型從未把泰迪熊判斷錯誤,這很合理,因為泰迪熊看起來和這兩種熊都相當不同。
使用混淆矩陣找出困難的類別
- [19:39] 混淆矩陣通常能讓人實際感受到哪些案例是困難的。
- [19:43] 在本書與課程所使用的寵物資料集中,不同品種的混淆矩陣會顯示哪些品種難以辨識。
- [20:00] 講者閱讀了維基百科頁面以及關於辨識這些品種的寵物育種報告,因為它們太難了,連專家都覺得難以分辨。
- [20:10] 從課程中,講者了解到黑熊與灰熊比想像中更難分辨,因此講者甚至不打算嘗試。
下一步:分類解讀
- [20:21] 模型建立完成後,下一個值得展示的有趣內容是分類解讀。
使用 plot_top_losses 與 ImageClassifierCleaner 清理影像
解讀 plot_top_losses 的輸出
- [20:42] plot_top_losses 會顯示損失最高的項目,而損失是對每一筆資料評估後所得的模型品質衡量指標。
- [21:03] 如果模型預測錯誤且非常有信心,損失就很糟;顯示順序為預測、實際、損失、機率。
- [21:16] 在範例中,模型在實際為黑熊時預測為灰熊,且有 96% 的信心,因此專家需要檢查究竟是模型還是標籤有誤。
- [21:41] 有些高損失項目其實是正確的,例如灰熊對灰熊與泰迪熊對泰迪熊,因為模型的信心只有 66% 或 72%。
- [22:01] 糟糕的損失可能來自預測錯誤卻有信心,也可能來自預測正確卻缺乏信心。
使用 ImageClassifierCleaner 清理訓練集
- [22:14] fast.ai 的 ImageClassifierCleaner 可用來清理資料集中標籤錯誤的項目。
- [22:24] ImageClassifierCleaner 會執行模型,這就是為什麼要傳入 "learn"。
- [22:42] 選擇泰迪熊之後,它會顯示訓練集或驗證集中被標記為泰迪熊的項目,並依損失排序,也就是依信心排序。
- [23:00] 對於分類錯誤的項目,您可以選擇正確的類別,或者如果它根本不該存在,就點擊刪除。
- [23:14] 一些困難的範例包括兩隻泰迪熊靠在一起、一隻看起來很奇怪像袋熊的熊,以及一隻在背景中很難看清的熊;如果前幾個都沒有問題,這組資料大概就沒問題。
清理驗證集並套用變更
- [23:44] 驗證集也用同樣的方式清理,刪除那些實際上不是熊或泰迪熊的項目。
- [24:20] 清理器會儲存一份已變更項目的清單和一份已刪除項目的清單。
- [24:26] 執行這個儲存格會刪除標記為刪除的索引所對應的檔案,並將標記為變更的項目移到新的資料夾。
在清理資料之前先建立模型
GPU 記憶體管理與影片學習方式
GPU 記憶體限制與置換
- [25:23] 只有 4GB 的 GPU 時,關閉並停止 notebook 以釋放記憶體非常重要。
- [25:37] 一般 RAM 滿了會讓電腦把 RAM 置換到硬碟,但 GPU 無法置換。
- [26:00] 當 GPU 的 RAM 用完時就沒戲唱了,所以要關閉未使用的 notebook,並且在 GPU 上一次只執行一件事,以避免記憶體耗盡。
觀看程式教學影片的建議方式
使用 HuggingFace Spaces 與 Gradio 部署模型
部署選項:Voilà 與 HuggingFace Spaces 的比較
- [27:18] 清理資料之後,本書使用 Voilà 將模型投入正式環境,講者認為這相當不錯。
- [27:29] 講者認為現今大多數人會覺得 HuggingFace Spaces 更有用,而今天的重點是 Gradio,它是可與之搭配使用的工具之一。
Tanishq Abraham 的部落格文章作為課程教材
- [27:42] 雖然這本書沒有相關章節,但課程助教 Tanishq Abraham 寫了一篇完整的部落格文章,涵蓋了今天課程的所有內容,並從論壇和課程頁面連結過去。
- [28:07] 講者讚揚 Tanishq 是 fastai 社群中最樂於助人、最有毅力、最體貼且最有耐心的人之一,並分享了一張他小時候作為神童與 Conan 的合照。
建立 HuggingFace Space
搭配 Spaces 使用 Git 與 GitHub Desktop
Git 作為 Spaces 背後的機制
- [30:14] Spaces 透過 Git 運作,這是大多數軟體開發者都很熟悉的工具,不過有些資料科學家可能不熟悉。
- [30:24] Git 被描述為一個非常有用的工具,但講者並未詳細介紹,而是直接快速示範如何使用它。
GitHub Desktop 的優點
- [30:38] Git 可以透過 GitHub Desktop 來使用,講者稱它相當棒。
- [30:46] 即使是透過主控台使用 Git 的人,大概也應該考慮 GitHub Desktop,因為有些事情用它處理起來快得多也容易得多。
透過 GitHub Desktop 還原誤提交的內容
從終端機使用 Git 與 Linux
各作業系統的終端機選項
- [31:19] Git 主要從主控台或終端機使用,Linux 和 Mac 使用者已經內建了。
- [31:30] Windows 使用者可以從 Microsoft Store 取得 Windows Terminal,每當需要終端機時講者都會使用它。
透過 WSL 在 Windows 上執行 Ubuntu
- [31:59] 在 Windows 上,講者建議使用 Ubuntu,因為 Windows 可以執行完整的 Linux 環境。
- [32:15] 安裝 WSL 的流程包括:搜尋 WSL 安裝、以系統管理員身分執行 PowerShell、貼上指令、等待約五分鐘,然後重新開機。
講者為何偏好 Windows 搭配 Linux
將 Gradio 應用程式部署到 HuggingFace Spaces
在終端機中複製入門範例儲存庫
- [33:37] 一旦有了終端機,你就可以複製貼上指令,而不必照著逐步點擊的說明操作。
- [33:55] 貼上並執行提供的指令會建立一個新目錄,該目錄一開始是空的。
- [34:10] 接著說明會要求你建立一個包含指定內容的檔案。
在編輯器中開啟資料夾
- [34:24] 在 Windows 上,輸入「explorer.exe .」會於目前目錄開啟檔案總管。
- [34:42] 在 Mac、Linux 或 Windows 上,輸入「code .」會開啟 Visual Studio Code 並載入該資料夾。
- [34:58] VS Code 被形容為很棒的 IDE,值得花幾分鐘看教學。
- [35:04] 你建立一個 app.py 檔案,內容為說明所指定的內容。
將檔案提交到 HuggingFace Spaces
- [35:30] 儲存後,必須將檔案提交到 HuggingFace Spaces。
- [35:37] 在 Visual Studio Code 中,你可以點擊輸入提交訊息、按下打勾圖示,就會將提交傳送到 HuggingFace Spaces。
測試建置完成的 Gradio 應用程式
- [35:54] 回到 huggingface.co/spaces/jhp00/minimal,網站大約需要一分鐘建置。
- [36:15] 建置完成的網站有一個 Gradio 介面,包含文字輸入與文字輸出,會執行 greet() 函式並回傳「Hello {name}」。
- [36:40] 以 Tanishq 這個名字進行測試,顯示出輸入所產生的輸出。
- [36:53] 這個應用程式不算很令人興奮,但它現在是一個在正式環境中運行的應用程式。
邁向深度學習模型的下一步
- [37:00] 下一步是把這個簡單的應用程式變成一個在正式環境中運行的深度學習模型。
訓練並匯出 fastai 寵物分類器
在 Kaggle 中建構狗/貓分類器
- [37:10] 必須先取得一個深度學習模型,基本上就是得先訓練一個。
- [37:29] Kaggle 和 Colab 的範例做的是同一件事:建立一個狗或貓的分類器。
- [37:52] Kaggle 已經安裝了 fastai,但筆記本會先安裝它,以確保使用最新版本。
- [38:03] 寵物資料集透過 is_cat 標註函式載入至 ImageDataLoaders,這被描述為執行 DataBlocks 的簡便方式。
- [38:13] 建立學習器並進行微調,使用先前見過的步驟。
Kaggle 編輯檢視與閱讀檢視
- [38:22] 每個 Kaggle 筆記本都有編輯檢視和閱讀檢視,而共享的筆記本任何人都可以在閱讀檢視中閱讀。
- [38:42] 訓練大約花了五分鐘;Kaggle 的 GPU 比大多數現代 GPU 稍慢,但仍然夠快。
匯出訓練好的模型
在 Colab 上重複相同工作流程
- [39:12] Colab 版本使用相同的步驟:pip install、is_cat、資料、ImageDataLoaders、好玩用的 show_batch,然後建立學習器並匯出。
將 Kaggle 模型匯出至 HuggingFace Spaces
從 Kaggle 下載已儲存的模型
將模型移入 Spaces 應用程式目錄
- [39:57] 下載的模型必須複製到與 HuggingFace Spaces 應用程式相同的目錄中。
- [40:13] 在終端機中,Mac 上輸入「open .」或 Windows 上輸入「explorer.exe .」會在該目錄中開啟 Finder 或檔案總管,以便將檔案貼到那裡。
在 Windows 上存取 Linux 檔案的提示
- [40:36] 在 Windows 上,將 Linux 家目錄釘選到快速存取,即可快速存取 Linux 檔案。
- [40:51] Mac 上不需要這個變通方法,因為檔案是整合的,而在 Windows 上它們就像兩台獨立的機器。
將模型放入測試 Space
- [41:04] 建立了一個名為 testing 的 Space,並將下載的「model.pkl」貼到其中。
使用已儲存的 fastai learner 進行預測
載入已儲存的 learner 與所需函式
- [41:41] 筆記本一如往常匯入 fastai 與 Gradio,並複製標記時所使用的完全相同 is_cat 定義。
- [41:49] 標記時所使用的任何外部函式都必須包含在內,因為 learner 會參照它們,儘管它並不會儲存它們的原始碼。
- [42:43] 不訓練 learner,而是以儲存的檔案名稱呼叫 load_learner(),並傳回與訓練結束時完全相同的 learner。
pickle 化的 learner 如何還原
- [43:03] 訓練好的 learner 被凍結成 pickle 檔案,儲存到磁碟,傳輸到電腦,然後載入並解除凍結。
- [43:23] 解除 pickle 化的 learner 可用於任何用途,而它的方法之一就是 .predict 方法。
預測速度與輸出格式
建立 Gradio 貓狗分類器介面
調整 predict 輸出以配合 Gradio
- [44:28] Gradio 需要一個可呼叫的函式,因此 predict 會回傳預測字串、其索引,以及狗與貓的機率。
- [44:45] Gradio 預期一個字典,將每個可能類別(狗或貓)對應到其機率,而這是以 zip 組成的 dict 來建立。
- [45:12] Gradio 目前無法處理 PyTorch 張量或 Numpy 陣列,因此每個值都必須轉換成一般的 float。
- [45:32] 對狗狗圖片呼叫 classify_image() 會回傳一個字典,其中狗為 definitely,貓為 definitely not。
建立並啟動 Gradio 介面
- [45:51] Gradio 介面指定了要呼叫的函式、影像輸入,以及標籤輸出,構成一個非常簡單的介面。
- [46:16] 提供的範例包括一張狗、一張貓,以及一張 dunno 圖片。
- [46:27] 啟動後,介面會在講者自己筆電上的 URL 執行,尚未進入正式環境,而且回應基本上是即時的。
dunno 範例與重新訓練的模型
- [46:55] dunno 圖片現在回傳 100,而不是平常大約 50-50,講者稱這很令人失望。
- [47:13] 講者六歲的女兒透過 Google 圖片找到那張狗與貓混合的圖片,而先前的模型給它幾乎正好 50-50。
- [47:53] 當天重新訓練的模型現在確信牠是貓,可能是因為訓練排程略有不同或多了一個 epoch,而講者說他不該訓練那麼久。
停止介面與下一步
將筆記本匯出成 app.py 並部署到 Gradio
以 hash pipe export 標記要匯出的儲存格
- [48:35] 所需的程式碼必須複製到 Python 腳本中,而僅用於檢查或實驗的儲存格則可以省略。
- [48:54] 講者在每個包含最終腳本所需資訊的儲存格頂端寫上「hash pipe export」來標記它們。
使用 nbdev 的 notebook2script 產生 app.py
- [49:13] 在筆記本底部,講者從 nbdev 匯入了 notebook2script,並以筆記本名稱執行它來建立 app.py。
- [49:32] 這種做法適合需要腳本而非筆記本的工具,例如 HuggingFace Spaces;複製貼上到文字檔也同樣可行。
- [49:42] 講者偏好這個方法,因為所有實驗都能在筆記本中進行,並由底部單一儲存格匯出結果。
- [49:54] 輸出檔名 app.py 是由筆記本頂端的 default_exp 設定所決定。
透過 Git push 部署並在正式環境中測試
訓練時長、匯出模型與 GPU 使用
選擇 epoch 數量
- [52:14] 隨著訓練進行,錯誤率會改善,因此問題在於是否要增加 epoch 數量。
- [52:26] 目前這輪使用三個 epoch 再加一個來起步,這樣是否足夠取決於使用者。
- [52:32] 錯誤率為百分之一時,講者感到滿意,但更多資料增強與更長的訓練還能再改善。
- [52:47] 如果訓練得夠久,錯誤率其實會開始變差,這會在後續課程中說明。
- [52:58] 你可以訓練到結果夠好為止、訓練到耐心、時間或運算資源用盡為止,或訓練到錯誤率開始變差為止。
從 Colab 下載匯出的模型
- [53:20] 在 Colab 中,匯出模型後開啟檔案瀏覽器即可看到該檔案。
- [53:30] 點擊下載不會立即開啟儲存對話框,而是出現一個進度圓圈。
- [53:43] 視模型大小而定,進度圓圈可能要跑幾分鐘,瀏覽器才會提示你儲存檔案。
訓練與預測所需的 GPU
使用 fastsetup 設定 Python 與 Jupyter
從 GitHub 複製 fastsetup
- [54:52] GitHub 上的 fastsetup Git 儲存庫讓設定 Python 與 Jupyter Notebook 變得簡單直接。
- [55:03] 前往 Github 的 fastai/fastsetup,即可透過 Code 按鈕取得整個儲存庫,並用 Github Desktop 開啟來複製它。
使用 setup-conda.sh 安裝 Python
- [55:44] 複製之後,以 ./setup-conda.sh 執行 setup-conda.sh 檔案即可安裝 Python。
- [56:05] 在 Linux 或 Mac 上,不應使用系統既有的 python,因為電腦需要它,而亂動它總是會導致災難。
- [56:32] 你會想要自己專屬的開發版 Python,搭配最新版本與函式庫,而基於 conda 的發行版幾乎肯定是最佳選擇。
- [56:49] 目前建議的發行版是 mambaforge,它會在你重新開啟終端機後加入用於安裝套件的 mamba 指令。
使用 mamba 安裝 fastai 與 nbdev
- [57:23] 根據 docs.fast.ai,正確的指令是 conda install -c fastchan fastai,但應將 conda 換成 mamba,因為它快得多:mamba install -c fastchan fastai。
- [57:52] 這會安裝所有需要的東西,包括 Pytorch、Numpy 與 fastai。
- [58:11] 用同樣方式安裝 nbdev 是選用的,但也會一併安裝 Jupyter 等項目。
啟動 Jupyter Notebook
- [58:42] 輸入 jupyter notebook 會啟動伺服器,點擊超連結即可開啟課程中所示的介面。
GPU 環境設定中 mamba 與 pip 的比較
- [59:17] pip 幾乎能做到 mamba 能做的任何事,但有了 GPU 時,pip 通常不會安裝成能在 GPU 上運作的版本,還需要許多其他惱人的安裝步驟,這就是建議使用 mamba 的原因。
在 HuggingFace Sp 上使用 Gradio 與 Streamlit 部署模型
課程檢查清單進度
- [59:54] 檢查清單確認空間、基本介面、git 設定,以及 conda 或 mamba 設定都已完成。
- [1:00:26] 狗狗對貓貓、匯出的學習器,以及 nbdev 都已經順利完成,沒有任何問題。
Mamba 對比 conda
- [1:00:04] Mamba 和 conda 是同樣的東西,但 mamba 是快得多的版本。
- [1:00:16] conda 從 mamba 借鏡的加速功能正在開發中,所以 conda 未來某個時間點可能又會變得可用,但就目前而言 conda 實在太慢,不應該使用。
Gradio 在原型開發上的彈性
- [1:00:50] 講者覺得相當酷的是,可以免費提供一個真正能運作的模型給任何想使用的人。
- [1:01:08] Gradio 透過各種小工具,對網站外觀提供了相當合理的彈性。
- [1:01:22] Gradio 的彈性不算驚人,但已經夠用,而且它真的就只是用來做原型開發。
Streamlit 作為替代平台
- [1:01:36] Streamlit 是目前 HuggingFace Spaces 支援的另一個主要平台。
- [1:01:44] Streamlit 比 gradio 更有彈性,但上手沒那麼容易,而且它主要也是用來打造原型。
超越原型
使用 JavaScript 搭配 HuggingFace Spaces API
模型預測的 API 端點
- [1:02:13] 應用程式的模型預測由 HuggingFace Spaces Gradio 處理,可以寫一個 JavaScript 應用程式來與它溝通。
- [1:03:27] 有一個 URL 端點在上面執行模型;把影像資料傳給這個 URL 會回傳一個字典,做的事和 UI 相同,但以 API 函式的方式呈現。
對 JavaScript 前端工作的反應
- [1:02:41] 前端工程師可能會很興奮,因為他們現在只要寫任何程式碼,就能創造出世界上任何東西。
- [1:02:52] 許多資料科學家可能會擔心,因為 JavaScript 不在他們的技能清單裡。
- [1:03:07] 講者建議不要太害怕 JavaScript,並指出其中一個選項是把模型交給懂 JavaScript 的同事來建立介面。
使用 curl 呼叫 API
建置 JavaScript 寵物分類網頁應用程式
單一檔案寵物品種分類器示範
- [1:04:56] 講者建立了一個名為「tinypets」的網站,雖然基本但功能可用,並提供了幾個範例。
- [1:05:17] 在單一檔案範例中,模型執行完整的寵物品種分類,而不只是狗與貓的區分,使用的是訓練來分類品種的模型,這部分下週會進一步討論。
- [1:05:41] 由於這個示範是 JavaScript 應用程式,因此沒有限制能做什麼,即使結果本身並不特別令人驚豔。
HTML 中的最小 JavaScript 流程
- [1:06:07] 基本步驟是為照片建立 import,並新增一個事件監聽器,在照片變更時呼叫 read 函式。
- [1:06:19] read 函式會建立檔案讀取器、讀取檔案,並在載入完成時呼叫 loaded。
- [1:06:30] loaded 函式會抓取 API 路徑,該路徑是從範例複製貼上,只是改用完整的 pets 模型。
- [1:06:53] 程式碼接著取得 JSON,從資料中取出標籤與信心分數,並設定 HTML。
- [1:07:02] 講者指出,這些 JavaScript 概念對非 JavaScript 使用者來說是新的,但並不比 Python 難,只是另一種要學的語言。
多檔案分類擴充
- [1:07:24] 多檔案版本讓使用者可以選取多張圖片,例如一隻紐芬蘭犬、一隻布偶貓、一隻巴吉度獵犬和一隻貓,並一次全部分類。
- [1:07:47] 那隻貓顯然被分類為孟加拉貓,紐芬蘭犬也被辨識出來。
- [1:07:57] 多檔案程式碼並沒有長很多:它取得所有檔案、將它們映射到 read,並附加每個結果。
結合兩個模型的社群範例
建置並託管簡單的 JavaScript 網頁應用程式
在本機執行單一檔案的 JavaScript 應用程式
- [1:09:52] 最基本的版本是單一 HTML 原始檔,可以儲存後在 Visual Studio Code 中開啟,或在檔案總管或 Finder 中按兩下開啟。
- [1:10:28] 這個檔案無須安裝任何軟體就能當作可運作的應用程式執行,因為瀏覽器本身就是完整的執行環境,還包含除錯器。
- [1:10:46] 這個應用程式會呼叫外部的 Hugging Face 端點,因此全部都能在電腦上本機執行。
透過 FastPages 託管於 GitHub Pages
- [1:11:06] GitHub Pages 免費託管網站,而且因為它只是 JavaScript,在那裡運作得很好。
- [1:11:22] 講者認為,建立 GitHub Pages 網站最簡單的方式是 FastPages,這是 fast.ai 的專案。
- [1:11:43] 設定過程包括點擊連結以產生副本、為它命名並建立儲存庫,另外大約還有兩個步驟,約需五分鐘。
- [1:12:24] 設定完成後會得到一個空網站,你可以用 GitHub Desktop 或 Visual Studio 開啟,或在終端機貼上指令開啟,然後儲存 HTML 檔案並推送回 GitHub。
主題與 Jekyll 設定
- [1:12:49] FastPages 會顯示所建立網站的連結,其外觀可以用主題來變更。
- [1:13:03] config.yml 檔案可讓你挑選主題;講者沒有特別理由地選了一個叫「alembic」的主題。
- [1:13:18] GitHub Pages 使用 Jekyll,所以任何 Jekyll 主題都能用,儲存的檔案會自動出現在網站和檔案清單中。
- [1:13:38] 每個 GitHub Pages 檔案頂端都需要「front meta」:三條破折號、標題、版面配置,再三條破折號。
分支並自訂網站
- [1:14:18] 若要製作不同版本,請前往 fast.ai/tinypets 並點擊 Fork 建立自己的副本;講者是在 jph00 帳號下這麼做的。
- [1:14:38] 講者在 config.yml 中把主題改成「pages-themes/hacker」。
- [1:14:47] 分支之後,你必須前往 Settings、點擊 Pages,並使用「Master Branch Root Save」啟用預設關閉的 GitHub Pages。
- [1:15:09] 換上不同的主題後,jph 的 Tiny Pets 資訊相同,但看起來更有駭客風格。
零成本、無需繁重工具即可開始
自然語言處理簡介
即將介紹的 NLP 主題與方法
前 3 段雙語對照預覽 · 免消耗額度
以下展示前 3 段已儲存繁體中文譯文與英文原版字幕的逐段對照(零額度消耗):
大家好。歡迎來到第二課。感謝各位回來……這裡的環境有點變化,我們大學出了點「行政問題」——有人把我們的教室訂走了——所以我現在在家裡的書房進行。很抱歉我背後少了些裝飾。我其實對這堂課超級超級興奮。感覺像是回到非常早期的時候,因為我們要做一些非常新、非常酷的東西,這些……
Hi everybody. Welcome to lesson two. Thanks for coming back… slight change of environment here, we had a bit of an “administrative issue” at our university — somebody booked our room — so I'm doing this from the study at home. so sorry about the lack of decorations behind me. I'm actually really really pumped about this lesson. It feels like going back to what things were like in the very early days, because we're doing some really new, really cool stuff, which…
你知道的……這些東西以前真的沒出現在這類課程裡。所以我超級超級興奮。非常感謝各位在第一課之後回來,希望回來是值得的。我想……我想你們會愛上它的!我是,對,我真的很期待。現在請記得,這門課搭配著書,所以務必跟著看。我的意思是,不是要你照著書一步步跟著做,因為我們會用不同方向涵蓋類似的內容……不過,也請讀一讀那本書,並記得那本書也完全免費提供。你可以到 fast.ai 的 fastbook 儲存庫看那些 notebook,或透過 course.fast.ai 在那裡閱讀——例如透過 Colab。
you know… stuff that hasn't really been in courses like this before. So, I'm super super excited. So thanks a lot for coming back after lesson one, and I hope it's worth you coming back. I think… I think you're gonna love it! I am, yeah, I'm really excited about this. Now remember that, you know, the course goes with the book, so be sure that you're following. I mean, not following along in the book, because we're covering similar things in different directions…. but, like, read the book as well, and remember the book is entirely available for free as well. You can go to the fast.ai fastbook repo to see the notebooks, or through course.fast.ai you can read it there — for example, through Colab.
另外也請記得,那本書……我的意思是……書裡有很多我們在課程中沒涵蓋的東西,你知道的,一些我覺得相當有趣的內容,關於神經網路的歷史,其中有些還包含很有意思的個人故事,你讀到這裡就會知道。每章末尾都有測驗。記得,在看影片之前先讀測驗題是個不錯的主意,所以如果你想先讀第二章的測驗,你知道的,然後再回來,這主意不錯。接著請確保你在看完影片、讀完書的第二章之後,能夠完成那個測驗。
And also remember that the book… I mean… the book has a lot of stuff that we didn't cover in the course like, you know, stuff I find pretty interesting, about the history of neural networks, some of which has some really interesting personal stories actually, as you'll read here. And at the end of each chapter there is a quiz. Remember it's not a bad idea before you watch the video to read the quiz, so if you want to read the Chapter Two quiz, you know, and then come back, that's not a bad idea. Then make sure that you can do the quiz after you've watched the video, and you've read chapter two of the book.
[0:00] 大家好。歡迎來到第二課。感謝各位回來……這裡的環境有點變化,我們大學出了點「行政問題」——有人把我們的教室訂走了——所以我現在在家裡的書房進行。很抱歉我背後少了些裝飾。我其實對這堂課超級超級興奮。感覺像是回到非常早期的時候,因為我們要做一些非常新、非常酷的東西,這些……
[0:41] 你知道的……這些東西以前真的沒出現在這類課程裡。所以我超級超級興奮。非常感謝各位在第一課之後回來,希望回來是值得的。我想……我想你們會愛上它的!我是,對,我真的很期待。現在請記得,這門課搭配著書,所以務必跟著看。我的意思是,不是要你照著書一步步跟著做,因為我們會用不同方向涵蓋類似的內容……不過,也請讀一讀那本書,並記得那本書也完全免費提供。你可以到 fast.ai 的 fastbook 儲存庫看那些 notebook,或透過 course.fast.ai 在那裡閱讀——例如透過 Colab。
[1:28] 另外也請記得,那本書……我的意思是……書裡有很多我們在課程中沒涵蓋的東西,你知道的,一些我覺得相當有趣的內容,關於神經網路的歷史,其中有些還包含很有意思的個人故事,你讀到這裡就會知道。每章末尾都有測驗。記得,在看影片之前先讀測驗題是個不錯的主意,所以如果你想先讀第二章的測驗,你知道的,然後再回來,這主意不錯。接著請確保你在看完影片、讀完書的第二章之後,能夠完成那個測驗。
[2:07] 上週我沒提到的一件事,是 Radek——我上週提過他——還寫了一個很酷的東西,叫做 aiquizzes.com,這是一個專門針對這本書的測驗網站。它其實運用了重複式學習技巧,確保你永遠不會忘記。所以,請務必去看看 aiquizzes.com。裡面全都是全新的題目,跟書裡的題目不一樣,而且整理、編排得非常好。所以也去看看 aiquizzes.com 吧。記得,除了 course.fast.ai 之外,還有 forums.fast.ai。course.fast.ai 是你要去取得所有 notebook 連結、Kaggle 相關東西等等的地方。你在 forums.fast.ai 上也會發現,每一課都有一個官方主題,裡面有你需要的一切資訊。一般來說論壇上會有稍微多一點的資訊,我們盡量讓課程
[3:09] 保持精簡,而論壇則比較詳細。所以如果你發現——在這個情況下你要找的是第二課的官方主題,但這裡是目前為止的第一課官方主題。從第一課的官方主題來看,在我錄製之後才過幾天——我們甚至還沒正式推出課程,所以目前只有跟著直播做的人——就已經有很多回覆了,這可能會讓人相當吃不消。
[3:40] 所以要注意,我的貼文底部有一個按鈕寫著「總結這個主題」。如果你按下它,就只會看到最多人按讚的回覆,這是確保你掌握重點內容的絕佳方式。所以,按鈕就在那裡,按下之後看起來就像這樣。你只會得到……你只會得到來自 fast.ai 傳奇人物如 Sanyam 和 Tanishq 的熱門內容。希望你會覺得這是使用論壇的實用方式。
[4:14] 這週很酷的一件事是,我……如先前承諾的,發了「秀出你的作品」的貼文,而且已經有很多人貼文了。這張截圖是我幾天前拍的,已經遠遠超過 39 則回覆了,如果我……如果我記得沒錯的話。我很難決定要分享哪些,因為它們全都太棒了,所以我其實決定走簡單的路,就挑了最前面的幾個。所以我只打算給你們看最早貼出的那幾個,因為它們全都太棒了。所以第一個,非常非常第一個貼出的,是一個受損車輛分類器……看起來運作得相當不錯。而且我很喜歡創作者 Matt 對這件事所說的,就是……你知道,哇,執行這段程式碼有點令人不安,我還不太懂,但我就是照做。所以我想,是啊,Matt 做得好,就只是……
[5:14],光是動手去做就對了。這就是起步的方式,一切都會變得合理,別擔心。很高興看到下一篇貼出的其實是 fast pages 裡的部落格文章。— 很高興看到 —,所以只是描述一些東西、他們這週跑的一些實驗,以及他們發現了什麼。下一篇是超棒的鬍子偵測器,如果我理解正確的話,主要是因為從 bird 變成 beard 很容易,只要把一個字母改成兩個字母,而這個在找出留鬍子的男士方面做得非常好。非常棒。然後這一個是……
[5:51] 又更上一層樓了,這是一個完整的正式上線網路應用程式,用來分類食物,這算是額外加分。看來那個討論串現在已經有 80 則回覆了,謝謝 Sanyam,非常酷。所以,你知道的,顯然……所以這其實是 Suvash 做的,他上這些課程已經。好幾年了,我想。所以你知道的,有一天你也可能可以做出你自己專屬的。網路應用程式並把它正式上線。而我說有一天,更確切地說……就是今天!我其實。現在就要示範怎麼做!所以 Suvash 把它貼上去其實是個很幸運的巧合,因為那正好就是我們今天要選的主題。
[6:46] 那麼,我們要怎麼把模型正式上線呢?第一步是……嗯……你其實已經做完第一步了,對吧?第一步是……第一、二、三、四步……是弄清楚。你想解決什麼問題、弄清楚怎麼找到它的資料、去蒐集一些資料等等。那麼在你拿到資料之後,接下來的第一步是什麼?下一步是。資料清理,如果你翻到書的第 2 章,我現在就來把它打開……
[7:23] 這就是那本書……你可以直接從課程在 Colab 裡打開它,或者如果你。已經把它複製到你的電腦上,或隨便怎樣,你都可以……你都可以在那裡做。所以記住,course.fast.ai 會帶你完整了解怎麼執行,這些 notebook,所以你可以看到第 2 章全都是在講把東西正式上線,而這裡是第 2 章。好,那麼記住,我們按 Shift + Enter 來執行儲存格,好嗎?……來執行它們。而我們要前往書中開始清理資料的那個部分。
[7:59] 所以我會點一下 Navigate,然後我們往下到這裡……蒐集資料……找到了!所以我們可以先快速複習一下。順帶一提,我要提一下,很多人問我,我用來這麼快速輕鬆地在 Jupyter notebook 裡穿梭的小技巧是什麼。其中一個很棒的就是你等等會看到的這個 navigate 選單,它其實預設。不會出現。所以如果你安裝一個叫做 Jupyter Notebook Extensions 的東西 - Jupyter……
[8:30] Notebook……擴充功能……所以你只要……你只要「pip install」它們,照著指示操作,然後重新啟動 Jupyter。顯然這……順帶一提,Colab 已經有目錄功能了,所以這只是給例如你在本機使用時用的,然後你會看到這裡出現這個「Nbextensions」的東西,如果你點「Table of Contents (2)」,就會給你這個方便的導覽列。另一個我很喜歡的是這個叫可折疊標題的功能,就是它讓我在這裡有這些好用的小東西,可以關閉和展開。而且其實這還不是最棒的部分。對我來說最棒的是,如果我按右方向鍵,它會跳到某個章節的結尾,如果我按左方向鍵,它會跳到某個章節的開頭。所以就像,如果我想在各章節之間移動,我只要按上左、下右、下右,非常方便,
[9:35] 而且如果你在這裡再按一次左鍵,它就會把它關起來。在這裡再按一次右鍵,就會展開。所以這就是可折疊標題。總之,幾個非常方便的東西,我們等一下今天還會多談很多關於設定你的 notebook 的事。好,你會注意到的一件事是,在書裡我們用 Bing API 來搜尋圖片。我已經直接動手把「bing」換成「ddg」了,因為 Bing API 需要取得 SDK 金鑰,老實說,深度學習裡最難的事大概就是搞清楚 Bing Azure 網站,然後把它弄好。ddg 不用!所以基本上完全一樣……(而且你可以……)
[10:21] 我也會在課程網站的論壇上分享這個 notebook,但我基本上只是把 bing 換成 ddg,然後把金鑰拿掉。所以接著,就像我們上週做的,我們可以搜尋東西,在書裡我們做了一個熊偵測器,因為我寫的時候,我當時的幼兒很喜歡我幫她辨識泰迪熊,而我當然不希望她不小心抱到灰熊,所以我們在這裡展示如何搜尋灰熊,就像上週一樣。
[10:56] 一個會迴圈處理灰熊、黑熊和泰迪熊的東西,就像上週一樣。把失敗的去掉,就像上週一樣。而論壇上有幾個人問過的一個問題是?我要怎麼找到基本上任何 Python 或 fastai 或 pytorch 東西的更多資訊。書裡這裡有幾個提示。一個是,如果你在任何函式名稱後面加上兩個問號,你其實會得到它完整的原始碼,同樣地,如果你加一個問號,你會得到簡短的……
[11:41] 你知道的……一點點資訊。如果你安裝了 nbdev(我想你需要的是 nbdev),那麼你可以輸入 doc,這會給你或許最重要的是直接連到文件的連結,在那裡你可以找到更多資訊,而且通常也會有範例。另外這裡還有原始碼的連結,如果你想看的話……
[12:13](我們用控制項來做這件事,好嗎)……原始碼的連結,這樣你就可以跳來跳去。注意在 GitHub 的原始碼裡,你可以點擊某些東西並跳到它們的定義。所以這是一種很方便的方式,可以跳來跳去,確切了解發生了什麼事。好的,所以有很多很棒的取得協助的方式。但我向你承諾的是,我們現在要來清理資料。所以我要告訴你一件你可能會覺得非常驚訝的事。在你清理資料之前,你先訓練一個模型。
[12:55] 現在,我知道這聽起來跟你可能聽過上千次的說法完全相反,那就是你先……(建立/你/訓練)……你先清理資料,然後再訓練你的模型。但我要給你看一件非常了不起的事。首先我們要訓練一個模型,你馬上就會明白為什麼。所以,要訓練一個模型,就跟之前一樣,我們用 DataBlock 來取得我們的資料載入器——書裡有很多關於這裡發生什麼事的資訊。就是這樣……然後我們就可以像往常一樣呼叫 show_batch 來看看它們。
[13:33] 書裡這裡有個小側欄,我快速提一下,是關於我們可以調整大小的不同方式。我想我們上週簡短提過……我們可以擠壓……上週我用的是字串,你可以用字串,或者用我們這種列舉類的東西。你可以看到用擠壓,你可能會得到一些非常瘦的熊,對吧?所以這是熊的實際大小、形狀。這裡它變瘦了,但你可以看到現在我們有了牠所有的幼熊……(牠們叫 clubs 嗎?對,熊崽)……所以它擠壓了,以確保我們能看到整張圖片。這裡也是同一隻。這隻原本在畫面外,我們擠壓了它,這傢伙現在看起來瘦得很奇怪,但我們能看到整個東西。所以那就是擠壓!
[14:12] 或者這裡這個是裁切。它只裁切出影像的中心,所以我們得到更好的長寬比,但會失去一些東西。這是為了讓我們能得到正方形影像。另一種做法是我們可以用「填補」。所以你可以用各種不同的東西來填補。如果你用零填補,也就是黑色,你可以看到這裡現在我們有了整張影像和正確的長寬比。所以那是我們可以做的另一種方式,你知道的,不同的情況會產生不同品質的模型。你可以都試試看,通常不會造成太大的差別。
[14:44] 所以我不會太擔心這件事。不過我還是要告訴你一個非常有趣的東西,叫做 RandomResizedCrop。所以與其說 resize,我們可以說 RandomResizedCrop。如果我們這麼做,你會看到我們每次都會得到影像的不同部分。所以,在這週……這週有人在論壇上問……我試著……(這是個非常有趣的想法,結果它稍微奏效了,)他們想要辨識法文和德文的圖片。所以顯然這不是你會用的正常做法,但只是做點實驗,(我愛實驗,所以,)他們有非常大的文件掃描檔,他們想只透過看圖片來判斷那是法文還是德文,而他們說圖片太大了?該怎麼辦?我說,用 RandomResizedCrop,這樣你就能抓取影像的不同部分,這非常棒,因為你可以跑很多很多
[15:44] 個 epoch,每次得到稍微不同的圖片。所以這是個非常好的技巧。而這種每次從同一張影像得到不同圖片的想法叫做「資料擴增」。同樣地,我不會深入講太多資料擴增的細節,因為書裡有,但我只會快速地在這裡指出,如果你使用這個叫做「aug_transforms」的東西,也就是擴增轉換,而這裡我把它們乘以二,所以我把他們弄得超級大,讓你可以更容易看到。你可以看到這些泰迪熊被旋轉、被壓扁、被扭曲、被重新上色、被調整飽和度,所有這些東西都是為了讓每張圖片都不同。一般來說,如果你要訓練超過大約五到十個 epoch,而大多數時候你大概都會想這麼做,除非你有一個超級簡單的問題要解決,你大概會想用 RandomResizedCrop 和這些「aug_transforms」。不要把 mult=2 放進去,
[16:49] 就讓它空著。我只是把它放在那裡,讓你可以更清楚地看到它們。所以我們觀眾裡的 Alex 問了一個有趣的問題,那就是:「這是……這是把影像複製很多次嗎?……做這樣的事?……還是這樣的事?」答案是「不,我們不是在複製影像。實際發生的是,那張影像……所以每個 epoch,每張影像都會被讀取。而這裡發生的是……是……有點像是……在記憶體裡,在 RAM 裡,這個……影像正在被「扭曲」,對吧?它正在被……我們在裁切它、重新上色等等。所以這是模型訓練期間即時發生的過程。所以,你的電腦上不會儲存任何副本,但實際上幾乎就像是存在無限多張稍微不同的副本,因為那就是模型最終看到的東西。所以,希望這樣說有道理,Alex,還有其他所有人,
[17:52],所以這是個好問題!好的,我們已經有了……我們要。使用 RandomResizeCrop,我們要使用擴增轉換,這樣我們就能從中得到 DataLoaders,然後就可以繼續訓練我們的模型。大約需要一分鐘。在這個例子中,我們只做了四個週期的微調,我們會在課程後面談到為什麼這裡有五個,但主要就是四個週期的微調。所以我們可能真的不需要 RandomResizedCrop 和 aug_transforms,因為週期太少了,但你知道,如果你想跑更多週期,這是個好方法。低於百分之三!錯誤率!那很好!好的,還記得我說過我們要在清理之前先訓練一個模型嗎?
[18:36] 好的,那我們就繼續訓練它。所以趁它訓練的時候……它正在我的筆記型電腦上跑,那台只有 4GB 的 GPU,相當。基本,但足以開始了。趁它訓練的時候,我們來看看下一個。所以我們要看的第一件事是混淆矩陣,而混淆矩陣是……只有當你的標籤是類別時才有意義,對吧!
[19:09] 而它說的是你犯了什麼類別錯誤。所以這顯示出我們目前得到的模型……有兩次實際上是一隻灰熊。而它以為是黑熊,還有兩次,實際上是一隻黑熊,而它以為是灰熊,還有零次,它把泰迪熊弄錯,這很合理對吧,因為泰迪熊看起來跟這兩者都相當不同。在很多情況下當你看這個,它會讓你真正感覺到,像是好吧,哪些是困難的?對吧?所以舉例來說如果你用我們在書和課程中經常。玩的 pets 資料集,這個不同品種的寵物分類 [**] 矩陣,你知道,真的會告訴你哪些是難以辨識的,而我實際上還去,像是,讀了維基百科頁面,還有寵物育種報告關於如何辨識。這些特定類型,因為它們太難了,甚至專家都覺得困難。
[20:10] 而我在做這門課程時實際學到的一件事是……黑熊和灰熊。牠們比我想像中還要難分辨得多……所以我根本不打算嘗試!但我要展示給你看,這個模型真正有趣的地方在於,現在我們已經建立了這個分類解讀。物件,我們用它來做混淆矩陣,我們可以說 plot_top_losses。我們可以說繪製最高損失,這非常有趣。它做的事情是,告訴我們「損失」最高的地方。現在如果你還記得上一課的內容,損失就是那個衡量指標——衡量我們的模型有多好——我們在每次跑完一筆資料後所計算的。如果我們預測錯誤,而且還非常有信心,損失就會是「糟糕」的。
[21:11] 關於那個預測。所以這裡有個例子,我們預測了……(這裡的順序是:預測、實際、損失、機率)。……我們預測是灰熊,但實際上是黑熊,而我們96%確定(我們的模型)牠是灰熊。現在,我對熊的了解不夠多,無法判斷是模型犯了錯,還是這張照片。真的是灰熊……但所以,專家顯然會回去檢查這些……
[21:41] 對吧?現在,你會注意到這裡有幾個……它有灰熊/灰熊、泰迪熊/泰迪熊……牠們其實。是正確的,對吧?那為什麼這個損失是「糟糕」的?當它是正確的時候?原因是……因為它不是很確定。它只有66%的確定度,對吧?所以這裡有個泰迪熊……它只有72%的確定度。對吧?所以你可能會有糟糕的損失,要嘛是錯誤卻有信心,要嘛是正確卻。沒信心。現在這之所以非常有幫助,是因為我們現在可以使用一個叫做 fast.ai ImageClassifierCleaner 的工具,來清理我們資料集中標記錯誤的項目。
[22:24] 所以當我們使用 ImageClassifierCleaner 時,它實際上會執行我們的模型。這就是為什麼我們把「learn」傳給它,對吧?而我提到過我對黑熊和灰熊了解不多,但我對。泰迪熊了解很多,所以我會選泰迪熊。如果我點擊泰迪熊,它現在會顯示。訓練集中(你可以選訓練或驗證)所有被標記為泰迪。熊的東西,而這裡非常重要的是:它們按損失排序,所以它們按確定度排序,對吧?所以我可以只滑過前幾個,檢查它們是否正確,對吧?喔,這裡有個錯誤,對吧?所以當我找到一個被錯誤收集的,我可以選擇把它……如果它在錯誤的類別,我可以選擇正確的類別……或者,如果它根本不該在那裡,我就點擊刪除。所以在這裡我會繼續點擊刪除,對吧?
[23:14] 所以你可以看到,有些東西之所以困難是有原因的,例如這裡有兩隻泰迪熊,我想這會讓人困惑,因為它不常看到這種情況。這一隻看起來有點奇怪,幾乎像隻袋熊。這裡的泰迪熊實在太多了。這一隻可能只是比較難從背景中看出來,但除此之外,這些看起來都沒問題。……好,所以我們就只看前幾個,如果你在第一年沒看到任何問題,那你大概就沒問題了。這樣就清理完我們的訓練集了,我們也來清理驗證集吧。這就是那個它處理起來有困難的,我不知道為什麼它對那個有困難,但就這樣吧,我們快速捲動瀏覽一下……
[23:59] 好,我不太確定那是隻熊,所以我乾脆直接把它刪掉。它是某種泰迪……但你知道,這是個問題。好,那也不是泰迪熊……所以你懂這個概念了吧?那麼在我們做完那件事之後,它會做的是……清理器現在已經儲存了我們變更過的清單,以及我們刪除的清單,所以我們現在可以繼續執行這個儲存格。那會逐一處理我們指定要刪除的所有索引清單,並刪除那些檔案。然後它會逐一處理我們設定要變更的所有項目,並把它們移到新資料夾,就是這樣,完成了!
[24:45] 所以,這不只是適用於影像模型的東西。它其實……是幾乎沒有人知道並使用的一項非常強大的技術,也就是:在你開始清理資料之前,總是先建立一個模型,找出你的資料中哪些東西難以辨識,並找出模型能幫你發現資料問題的地方。然後當你看到它們時,你大概會說「好,我看到我們遇到的這類問題了」,而你可能會找到更好的方式來蒐集下一批資料集,或者你可能會找到方法來自動化部分清理工作等等。
[25:23] 好,那就是資料清理,而由於我只有 4GB 的 GPU,這對我來說非常重要。要關閉並停止,因為那會釋放記憶體。所以重要的是要知道……在你的電腦上,你的一般 RAM 其實不太會滿,因為如果你用掉太多 RAM,會發生的情況是,你的電腦反而會開始(這叫做)置換,基本上就是把那些 RAM 存到硬碟上以便之後使用。GPU 無法置換。
[26:00] 顆 GPU,當它們的記憶體用完時,就結束了,你就完了。所以你必須確保關閉任何正在使用 GPU 但你沒在用的筆記本。而且真的在 GPU 上一次只使用一個東西,否則你幾乎肯定會記憶體不足。所以我們開始看到最初幾個紅色出現了。所以記得要發問,而就黃色部分來說,重要的是要知道,當你觀看影片時,我不是要你執行所有這些程式碼,好嗎?這個想法是大概看一遍,然後再回去暫停,你知道,隨著你進行。或者你可以直接停下來、試一試、停下來、試一試。我非常喜歡——而且很多學生也非常喜歡——觀看這些影片的方式,其實是完整看一遍而不碰鍵盤,先了解影片大概在講什麼,然後再回到開頭看一遍並跟著做。這樣一來,在每個時間點你都知道你正在
[27:00] 做什麼,你知道接下來會發生什麼。這其實可以幫你節省一些時間。這是有點不尋常的方式,因為很明顯,像真實生活中的講座你不能這樣做,你不能把教授倒帶讓他們再說一遍,但在這裡這是個好方法。所以現在我們已經清理了資料,我們要如何把它投入生產?嗯,在書中我們使用一個叫做 Voilà 的東西。它……它相當不錯!但其實,我認為你們大多數人現在可能會覺得更有用的東西,是一個叫做 HuggingFace Spaces 的東西,而且有幾件事你可以搭配它使用。我們今天要來看一個叫做 Gradio 的東西。
[27:42] 書裡沒有關於這個的章節,但那不重要,因為 Tanishq Abraham——他其實是這門課的助教之一——寫了一篇很棒的部落格文章,涵蓋了我們今天要講的幾乎所有內容。所以論壇和課程頁面上都有那篇文章的連結,所以這就像是書中那一章的對應內容,如果你喜歡這樣說的話。如果我不停下來好好表揚 Tanishq 一番,我會覺得過意不去,原因有兩個:第一,他是 fastai 社群中最樂於助人的人之一,他已經在這裡好幾年了,非常堅毅、體貼又有耐心;還有因為我有他一張幾年前和 Conan 的精彩照片,當時他是著名的神童,所以現在你知道著名的神童長大後會怎樣——他們成為更有名的 fastai 社群成員和深度學習專家。
[28:41] 所以你們一定要看看這段影片,他在裡面講笑話給 Conan 聽。我想他現在其實還只有 18 而已,這應該不是很多年前的事。非常感謝 Tanishq,感謝你在社群裡的所有協助。也很抱歉用你那張九歲時的照片讓你尷尬了——其實我並不抱歉,哈哈。好,現在重點是,要做 Gradio 和 HuggingFace Spaces,其實起步很容易。好,我們從 HuggingFace Spaces 頁面開始——我們在課程的論壇裡有放連結。我們要把模型推上正式環境,也就是拿我們訓練好的模型,基本上把它複製到這台 HuggingFace Spaces 伺服器,然後為它寫一個使用者介面。
[29:29] 那麼……開始吧!建立新的 space。好,所以你可以直接繼續,說,好……很明顯你要先註冊……整個東西都是免費的。基本上我在這整門課裡示範的所有東西,你都可以免費做到,這是好消息。好,給它取個名字,建立一個最簡單的就好。我一律使用 Apache 授權,因為這代表其他人可以非常輕鬆地使用你的作品,但你不用太擔心專利問題。就像我說的,有幾種不同的產品可以搭配使用,我們要用 Gradio,也是免費的。
[30:04] 如果你把它設為公開,就可以分享出去,這對學生來說一向是好主意,特別是……真的能逐步累積你的作品集。好,我們完成了。我們建立了一個 space。接下來要做什麼?Spaces 是透過 Git 運作的,現在大多數軟體開發者對 Git 都會非常熟悉。有些資料科學家可能不熟。所以 Git 是非常、非常有用的工具。我不打算詳細講它,但我們來快速了解一下怎麼使用它,好嗎?現在,你可以透過一個叫做 Github Desktop 的東西來用 Git,它其實相當棒,甚至那些透過主控台使用 Git 的人,可能也該考慮用 Github Desktop,因為有些事情在裡面就是快得多、容易得多,事實上我今天跟我朋友 Hamil 聊,我說,喔,救命,我不小心把這兩個東西提交進去了,還原最簡單的方法是什麼,而他以前在 Github 工作過,
[31:09] 我本來以為他會拿出什麼花俏的主控台指令,結果他說:「喔,你應該用 GitHub Desktop,直接點一下就好。」喔!這主意真棒。所以那……那確實很實用。但大多數時候,我們還是從主控台、從終端機來使用 Git。如果你是 Linux 或 Mac 使用者,你本來就有終端機,非常直接,不用擔心。如果你是 Windows 使用者,我有個好消息,現在的 Windows 有個很棒的終端機,叫做 Windows Terminal,你可以從 Microsoft Store 取得,所以其實每次你看到我在用終端機,我用的就是那個 Windows Terminal,運作得非常好。天曉得你為什麼會想要這樣,配上這些荒唐的顏色,不過,就是這樣。
[31:54] 那麼,你想在終端機裡跑什麼呢?顯然,如果你用的是 Linux 或 Mac,你已經有設定好的 shell 了。在 Windows 上,你幾乎肯定會想用 Ubuntu。所以 Windows,信不信由你,其實可以執行完整的 Linux 環境,而要這麼做,只要輸入一行指令,就是這個……所以,如果你去(直接 Google 搜尋 WSL install)以系統管理員身分執行 PowerShell,貼上那個指令,等大約五分鐘後重新開機,就完成了。
[32:30] 你現在有完整的 Linux 環境了。我提到這件事的原因之一是,我接下來要示範怎麼在你自己的機器上做事,所以這算是稍微進階一點的極客操作,有些資料科學家可能比較不熟悉。所以你知道,別對終端機感到擔心,你會……我想你會……發現它真的很有幫助,而且比你想的還要不嚇人。我特別要說,像我自己,我選擇用 Windows,因為我能用到所有好用的 Windows 圖形介面應用程式,可以在螢幕上畫圖、做簡報,同時也有完整的 Linux 環境。而且那個 Linux 環境會用到我的 GPU 等等,所以對我來說,我的第一選擇是用 Windows,第二選擇——其實差別不大,我真的很喜歡——是用 Linux。Mac 稍微難一點,但仍然可用。有些東西在 Mac 上比較棘手,但你應該沒問題。
[33:37] 好,所以,不管你手上有什麼,到這個階段你已經有一個終端機可用了。因此,在你的終端機裡……(使用終端機的一大好處,就是你不用遵循一大堆「點這裡、點這裡、點這裡」的指示。你只要複製貼上就好。)所以我現在就……你只要複製這段,然後切換到你的終端機,貼上去,再執行它,做完之後,你會發現你現在有了一個目錄。這個新目錄一開始是空的,而他們會告訴你:好,請建立一個檔案,把這些內容放進去。好,那麼,當我們在 Windows 上的 Linux 環境裡,或在 Mac 的終端機裡,或不管在哪裡,你要怎麼建立一個含有那些內容的檔案呢?在 Windows 上,你只要輸入「explorer.exe .」,它就會在這裡開啟檔案總管;或者更好的做法是,在 Mac、Linux 或 Windows 上……
[34:42] 所以,沒錯,不管你是用哪一種電腦,你只要輸入「code .」,它就會彈出 Visual Studio Code 並開啟你的資料夾。接著你就可以直接……(如果你以前沒用過 VS Code,真的很值得花幾分鐘讀一些教學,它是一個很棒的 IDE。)……然後你就可以照他們說的,建立一個 app.py 檔案,一個 app.py 檔案,裡面放進他們要你放的內容。就在這裡,在這裡。
[35:22] 好,我們快到了。你現在可以儲存它,接著你需要提交它,提交到 Gradio……(不是 Gradio……)提交到 HuggingFace Spaces。一個很簡單的方法就是,直接在 Visual Studio 裡,點這裡,它會給你一個可以輸入訊息的地方,你按下打勾,它就會幫你送到 HuggingFace Spaces。做完之後,你就可以回到……回到你之前所在的那個完全相同的網站:「huggingface.co/spaces/jhp00/minimal」,而你現在會發現——建立你的網站大約需要一分鐘。而且它……
[36:15] 它正在建立的網站會有一個 Gradio 介面,包含一個文字輸入、一個文字輸出,並且會對輸入執行一個叫做「greet()」的函式,而我的「greet()」函式會回傳「Hello {name}」。所以這就是它要做的事。現在開始了,我們來試試看!我們來跟 Tanishq 打聲招呼。我記他的名字拼法常常記不太牢,我想應該是這樣拼。
[36:50] 這樣就完成了,你可以看到它為我們的輸入產生了輸出。所以這不是一個很令人興奮的應用程式,但平心而論,我們現在有一個在生產環境中運行的應用程式了,對吧?現在,我告訴過你我們會有一個深度學習模型在生產環境中運行,所以現在我們必須邁出下一步,也就是把它變成一個深度學習模型。好的,首先我們需要一個深度學習模型。取得深度學習模型有幾種不同的方式,但基本上我們得訓練一個。所以,我準備了幾個範例:一個 Kaggle 範例和一個 Colab 範例。也許我會快速給你看這兩個。它們會是一樣的東西。我就是要建立一個狗或貓的分類器。好的,這是我們的 Kaggle 模型,我會點擊編輯,這樣你就能實際看到它在編輯檢視中的樣子。
[37:52] 現在,Kaggle 已經安裝了 fastai,但我總是把這行放在最前面,以確保我們用的是最新版本。然後顯然要匯入一些東西,我們……所以我們要取得 pets 資料集、一個用來檢查是否為貓的函式,那是我們 ImageDataLoaders 的標註函式——記住這只是 DataBlocks 的另一種寫法,就像一個小簡寫。然後我們建立我們的 learner,並對它進行微調。好的,這些都是我們以前見過的東西。所以在 Kaggle 中,每個 notebook 都有一個編輯檢視(就是你剛才看到的)和一個閱讀檢視。
[38:27] 所以你可以分享你的 notebook——如果你想的話。然後任何人都可以閱讀閱讀檢視,如你所見。所以在這裡,你可以看到,它顯示了我運行時發生了什麼。所以我訓練了它,花了……你知道,Kaggle 上的 GPU 比大多數現代 GPU 慢一些,但仍然夠快,我的意思是,花了五分鐘。最後這裡有一個你以前沒見過的部分,就是我用了 learn.export 並給它一個名稱。
[39:02] 現在,那會建立一個包含我們訓練好的模型的檔案,而且……就這樣而已!建立這個檔案是你唯一需要 GPU 的地方,對吧?所以你在 Kaggle 或 Colab 上做這件事。所以這裡在 Colab 上完全一樣……你可以看到「pip install」、is_cat、我們的資料、ImageDataLoaders,所以我這裡也有一個 show_batch,只是為了好玩。建立我的 learner 然後匯出。所以,在我們等待的時候,我可能會直接去運行它。
[39:36] Kaggle 有個好處是,一旦你執行完並儲存,就可以切到資料分頁,基本上你儲存的任何東西都會出現在這裡,你看,就是這個:model.pickle,對吧?所以現在我可以直接下載它,它就會下載到我的下載資料夾。接著我需要把它複製到我 HuggingFace Spaces 應用程式所在的同一個目錄。現在,我的 HuggingFace Spaces 應用程式正在終端機中開啟,在 Mac 上你可以輸入「open .」,在 Windows 上你可以輸入「explorer.exe .」,這樣就會在該目錄中開啟你的 Finder 或檔案總管。然後你就可以把你下載的那個東西貼到這個目錄裡。順帶一提,在 Windows 上我會做一件我覺得很有幫助的事,就是我實際上會把我……我在 Linux 的家目錄抓過來
[40:43] 然後把它釘選到我的快速存取,這樣我在 Windows 上就能隨時直接跳到我的 Linux 檔案。這在 Mac 上其實不太需要擔心,因為一切算是整合在一起的,但在 Windows 上它們就像是兩台獨立的機器。好,那麼……我們來做……我建立了一個叫做 testing 的 Space,我下載了我的「model.pkl」並把它貼到 testing 裡。所以現在我們需要知道,要怎麼對已儲存的模型做預測?
[41:23] 所以我們有一個 notebook 來做這件事……好,我們有一個 notebook 來做這件事,那麼……我要帶你了解如何使用我們訓練好的模型來做預測。hash pipe 有幾個奇怪的地方,我等一下會說明,現在先忽略它們。所以我們照常匯入 fastai。我們像之前一樣匯入 Gradio。然後我們複製進來和之前完全相同的 is_cat 定義。這很重要,你在標註時用到的任何外部函式,都必須一併包含在這裡,因為那個 learner 會參照那些函式,對吧?它會儲存……那個 learner 會儲存你模型的一切,但它不會存函式的原始碼,所以你需要自己保留那些函式。
[42:12] 那麼,我們來試著執行這個。舉例來說,我剛剛抓了……(你可能在我的檔案總管裡看到了)我剛剛在那裡放了一張狗的圖片。所以我們可以從那隻狗建立一個 Python 影像函式庫的影像,把它縮小一點,這樣它就不會佔滿我整個螢幕,然後這裡就有一張狗的圖片。那麼,我們要如何預測那是狗還是貓呢?嗯,非常簡單,我們只要……不是訓練一個學習器,而是使用 load_learner()。我們傳入儲存的檔案名稱,然後它就會回傳一個學習器。這個學習器跟你訓練完成時得到的學習器完全相同。所以我們在這裡,這就是 Colab,對吧?我們剛剛一直在訓練一個學習器,所以在最後,有一個已經訓練好的學習器,於是我們……我們把它凍結在時間裡,這叫做 pickle 檔案——這是 Python 的概念。它就像一個凍結的物件,我們把它存到磁碟,我們把它傳輸,
[43:17] 傳到我們的電腦,而我們現在已經載入它,我們現在已經把它解凍了。這是我們解 pickle 後的學習器,我們現在可以隨心所欲地使用它。所以其中一件事……學習器擁有的方法之一是 dot predict 方法,所以如果我執行它,你可以看到,即使在我的筆記型電腦上,它基本上也是瞬間完成。事實上我們可以看到它花了多久時間。如果你——在 Jupyter 裡,以百分比開頭的東西叫做 magics,它們是 Jupyter 的特殊功能,舉例來說,有一個東西可以看某件事花了多久時間。
[43:51] 就是這樣,好的,所以它花了 54 毫秒才判斷出這不是貓。所以它回傳兩件事:它是貓嗎?(以字串表示);它是貓嗎?(以零或一表示),然後是它是狗的機率,以及它是貓的機率。所以「它是貓嗎?」的零(假)和一(真)的機率,所以肯定是狗。我們現在想要建立一個 Gradio 介面,基本上包含這些資訊。
[44:28] 所以 Gradio 要求我們給它一個函式,它會去呼叫,所以這是我們的函式。我們要呼叫 predict,而它如我們所說回傳三件事:預測結果是字串;它的索引;以及它是狗還是貓的機率。而 Gradio 想要的是,它想要取回一個字典,包含每個可能類別——在這個例子中是狗或貓——以及每一個的機率。所以,如果你以前沒做過太多 Python,zip 的 dict 可能是你沒見過的東西。
[45:04] 非常實用的小技巧,很值得一看。如果你以前沒看過 map 也一樣,總之就在這裡。目前 Gradio 有一點稍微惱人的地方,就是它不處理 Pytorch Tensor,你可以看到這裡 Pytorch 回傳的不是普通數字,而是 Tensor。它甚至不是回傳 Numpy 陣列。事實上 radio 也無法處理 Numpy,所以我們必須把全部東西都改成普通的 float,這就是這裡在做的事,它把每一個都轉成 float。所以,舉例來說,如果我現在用我們那張狗狗圖片呼叫 classify_image(),我們會拿回一個字典:狗,絕對是;貓,絕對不是。
[45:45] 現在我們有了這些,就可以繼續建立 Gradio 介面。Gradio 介面就是這樣:我們要說,嗯,你要呼叫哪個函式來取得輸出、輸入是什麼——在這個例子裡我們說,喔,輸入是一張圖片——。所以去看看 Gradio 文件,它可以是各種東西,例如網路攝影機拍的照片、文字,或你知道的各種東西。給它一個它要放進去的形狀,輸出只會是一個標籤,所以我們要建立非常非常簡單的介面。我們也可以提供一些範例,所以這裡有一隻狗、一隻貓,還有一個「不知道」——我等一下會告訴你那是什麼——你會在這裡看到有一隻狗、一隻貓,還有一個「不知道」。
[46:27] 所以我一啟動它,它就說,好,現在跑在這個 url 上。所以如果我打開它;你現在可以看到,我們就像 Suvash 一樣,有我們自己的——還沒上正式環境,但跑在我們自己的機器上——分類器。所以來檢查一下:狗,你可以點它並上傳一張,或直接選擇範例……它們在這裡!所以它跑在我的筆電上,基本上瞬間完成。我真的得跟你講講這裡這位的故事,這就是那個「不知道」。
[47:01] 送出,等等,為什麼它顯示 100?通常這裡會顯示像 50 50。真掃興,這個模型……把我的整個故事都搞砸了。上次我訓練這個模型,然後在……「不知道」上跑它,它說,它說幾乎正好是 50 50。而我們找到這張圖片的方式是……我給我六歲的女兒看,她說:「爸爸你在做什麼?」,我說,我在寫程式,「你在寫什麼程式?」喔,你知道的,狗貓分類器。她看了一下,她的第一個問題是:「我可以借用你的鍵盤一下嗎」,然後她跑去 Google,她說:狗混貓叫什麼。好像根本沒有狗混貓這種東西。總之,她跑到圖片分頁,找到這張圖片,然後她說:「你看,有一隻狗混貓。」
[47:47] 她說:「在那個爸爸上跑它!在那個上跑它!」然後我跑了,結果就像:50-50。它完全不知道那是狗還是貓。現在,這個我今天才重新訓練的模型,現在它很確定那是貓。所以就是這樣,我想我用了稍微不同的訓練排程之類的,或者,多給了它一個 epoch,總之,那是一張狗貓圖,但顯然它是貓。我猜它是貓。它大概是對的。我不該訓練它那麼久。好,所以這就是我們的介面。現在,它其實正在執行,所以你其實得按一下開始按鈕才能讓它停止執行,否則你在 notebook 裡什麼其他事都做不了。好,所以現在我們得把它變成一個 python 腳本。所以,把它變成 python 腳本的一個方法,就是把你需要的所有東西複製貼上到一個 python 腳本裡。
[48:42] 我需要把你需要的這整份東西的所有這些部分複製貼上到一個 python 腳本裡,所以,舉例來說,我們不需要這個——這只是要檢查某個東西,我們不需要這個——這只是在實驗。這只是在實驗。我們會需要這個,對吧?所以,我做的事是……我逐一檢查,並在每個儲存格的最上方寫上「hash pipe export」,那些儲存格含有我最後腳本裡會需要的資訊,然後——所以那些就是步驟,對吧?然後,在最下面這裡,我匯入了一個叫做 notebook2script 的東西,來自 nbdev,如果我執行它,並傳入這個 notebook 的名稱,它就會幫我建立一個叫做 app.py 的檔案,裡面含有那個腳本。所以這是個不錯、簡單的方法——就像當你在處理那些期待腳本而非 notebook 的東西時,HuggingFace Spaces 就是這樣。如果你喜歡,直接複製貼上到文字檔也沒問題,
[49:42] 但我真的很喜歡這種做法,因為這樣我就能在 notebook 裡做我所有的實驗,做完之後,我只要在底部有個儲存格,執行一下就能匯出它。它怎麼知道要叫它 app.py?那是因為最上方有個特別的東西 default export:default_exp,它說明了要建立什麼 python 檔名。所以那只是我用的一個小技巧。所以現在我們有了 app.py。我們需要把它上傳到 Gradio,該怎麼做?你只要……你只要把它推送到 Git。所以……你可以用 Visual Studio Code 來做,或者你可以輸入「git commit」。
[50:28] 然後執行「git push」。完成之後,……如果我們把 minimal 改成 testing,……我想這個應該還在執行中,我之前的模型——因為我沒有推送它,這樣一來,我們就能看到我們那隻瘋狂的狗貓。好的,所以就在這裡,你可以看到它在正式環境中執行。所以現在這是任何人都能——,如果你把它設為公開——,任何人都可以到這裡查看你的模型,他們也可以上傳。那麼,這是我的狗狗。
[51:02] 沒錯,絕對是狗。貓,對,我想我可能少訓練了一、兩個 epoch,所以它比較沒那麼有把握。對,絕對是貓。狗貓……嘿,狗貓……嗯,還是認為它絕對是貓。唉,真奇怪。好的,那麼,這就是……好的,那麼,這就是把一個簡單模型送上正式環境的例子。論壇上、社群裡有幾個問題。
[51:49] 好,有人問:「pytorch 模型和 fastai learner 有什麼不同?」好,沒問題,我們很快就會講到。不確定會是哪一課,可能是這一課或下一課。然後另一個人問的,基本上是在問:「我們要訓練多少個 epoch?」所以,當你訓練模型時,錯誤率,如你所見,會逐漸改善。
[52:21] 所以問題是:我應該再多跑一些嗎?我應該增加 epoch 的數量嗎?這裡是跑三個 epoch,對吧?這是我的一開始的三個 epoch 再加一個。你看,這取決於你,對吧?我的意思是,對這個來說,這裡顯示:有百分之一的錯誤率。我……我可以接受百分之一的錯誤率,你知道,如果你希望它更好,那你可以使用更多資料擴增,並訓練更久。如果你訓練得夠久,就像我們很快就會學到的,然後……也許下一課。如果你訓練得夠久,你的錯誤率其實會開始變差。而且,你會看到,我們會學到為什麼。所以基本上,對,你可以訓練到它夠好為止,或直到你失去耐心、或時間、或運算資源用完,或直到你……或直到錯誤率開始變差。
[53:14] 好,喔,然後在 Colab 裡,你要怎麼取得你的模型?在 Colab 裡你只需要,在你匯出之後,如果你進入它的檔案瀏覽器,你其實會在這裡看到它,對吧?然後你可以點擊下載。這有點奇怪,它不會像跳出一個方框問你要下載到哪裡,而是會跳出這種進度圓圈,所以取決於它有多大等等,可能要花幾分鐘。一旦那個圓圈填滿,瀏覽器的東西就會終於跳出來說,好,你可以儲存了。好,那就是你怎麼……
[53:57],這才是你實際取得模型的方式。所以如你所見,真正需要 GPU 的步驟,你可以使用這些完全免費的資源:Colab、Kaggle,還有其他我們會在後續課程談到的。然後其他所有事情你都可以在自己的電腦上完成,包括預測,預測很快,對吧?所以,除非你要做上千次預測,否則你真的不需要為此使用 GPU。好,開始了,現在它要求我儲存。好,現在一個大問題是,我們需要……在我們的電腦上執行它……我們需要電腦上跑著 python 和 Jupyter Notebooks,那你要怎麼做?
[54:41],因為這正是人們經常遇到各種麻煩的地方……「我想辦法要讓這一切運作起來。」好消息是,我們其實有個東西能讓這件事變得非常非常簡單。它叫做 fastsetup。你真正需要的其實只有其中一個部分,讓我示範給你看……它……它……其實是 Github 上的一個 Git 儲存庫。Github 是大多數 Git 儲存庫所在的地方。所以如果你到 Github 的 fastai/fastsetup,你就會看到它。接著你可以直接在這裡點 code 來取得整個儲存庫,如果你安裝了 Github Desktop,就點 open with Github Desktop,如你所見,它會跳出視窗說:好,我準備好幫你儲存了,於是我點 clone,它就會複製一份。這樣就行了……
[55:39],所以基本上,一旦你 clone 下來,你會發現裡面有個檔案叫做 setup-conda.sh……你知道,細節其實不太重要,它相當短,但那就是會幫你安裝 python 的東西。所以到那個時候,你只要執行「./setup-conda.sh」,它就會執行這個安裝程式。現在,如果你用的是 Linux 或 Mac,你的機器上已經有 python 了。別用那個 python!!
[56:14],原因是那個 python 叫做系統 python,它是你的電腦用來做電腦相關事情用的,對吧?它其實……它其實是必需的。你不會想去動它,我跟你保證,就像……它總是導致災難,總是!你要的是你自己的開發用 python 版本,它也會確保你有最新版本,以及所有你想要的函式庫,而對你來說,迄今最好的幾乎肯定會是這些以 conda 為基礎的 python 發行版,所以如果你執行 setup conda,你就會得到我們推薦的那個。而我們目前推薦的叫做 mambaforge。所以基本上,一旦你執行它,你會發現你現在——關掉終端機再重新打開——你會發現你現在多了一個指令,叫做 mamba,而 mamba 讓你安裝東西。
[57:10] 所以一旦你執行完,就能輸入「mamba install fastai.」。這會……其實我應該提一下,更詳細一點說明如何正確安裝。如果我們到 docs.fast.ai 的 installing 頁面。對,好,我們其實要執行的是「conda install -c fastchan fastai」。那就直接複製貼上。喔抱歉,不是這樣……然後,另一件我要說的是,不要用 conda,把 conda 換成 mamba,因為現在它快得多。所以是「mamba install -c fastchan fastai.」。
[57:52] 現在,這會安裝你所需的一切。它會安裝 Pytorch、安裝 Numpy、安裝 fastai……諸如此類,對吧?然後……很明顯我已經裝好了。接著另一件你會想做的是安裝 nbdev,這樣你就能對 nbdev 做完全相同的事。你不一定要這樣做,對吧?只是……但它會順便幫你安裝 Jupyter 以及其他東西。
[58:28] 所以到那個時候,你就能使用 Jupyter 了。Jupyter 的運作方式是——你可以在這裡看到。這是我的……(我先把它關掉,這樣我們可以重新開始。)基本上,要使用 Jupyter,你只要輸入「jupyter notebook」,好嗎?當你執行它,它會說:好,我們現在為你執行一個伺服器,所以如果你點那個超連結,就會跳出這個,好嗎?這正是你一直看到我在用的東西。好,希望這樣足以讓你開始使用 Python 和 Jupyter Notebook……
[59:17] 人們安裝軟體的另一種方式,是用所謂的 pip 而不是 mamba。幾乎任何你能用 mamba 做的事,也都能用 pip 做,但如果你有 GPU,pip 一般不會安裝能在你 GPU 上運作的東西,你得安裝很多其他東西,這很麻煩,所以我才會建議大家用 mamba,不過其他情況你可以用 pip。還是有點紅。請再告訴我們能怎麼幫你。
[59:54] 好,那我們來看看這些步驟進行得如何。我忘了這裡有這些步驟提醒我自己。我們建立了 space,打勾。我們建立了基本介面,打勾。好,我們設定好 git,設定好 conda,或 mamba。mamba 和 conda 是同樣的東西,mamba 只是快得多的版本,我們會在課程網站上留一些筆記,因為目前他們其實正在把 mamba 的加速功能納入 conda。所以某個時候,也許再用 conda 就沒問題了。目前 conda 太慢了,所以別用它。
[1:00:26] 好,我們已經完成狗與貓的分類,沒問題。是的,所以我們也可以來看看寵物品種(嗯對,我們會稍微看一下)。好,我們已經用過匯出的學習器,沒問題。我們用過 nbdev,沒問題。喔!好……試試看 API……好,這很有意思。所以我想大家應該都能同意,希望如此,這挺酷的,我們可以免費提供給任何想用的人一個真正可運作的模型,而且你知道,有了 gradio,其實在你能怎麼讓網站看起來的樣子這方面,有相當合理的彈性,你知道,就是用這些不同的元件。
[1:01:22] 它沒有到非常彈性,但已經夠彈性了,可以說……它真的就只是用來做原型,對吧。所以 gradio 有很多元件和東西可以用。目前 HuggingFace Spaces 支援的另一個主要平台叫做 streamlit。streamlit 我會說比 gradio 更有彈性——上手沒那麼容易,但你知道,它大概就是那個不錯的中間選擇,所以也是很好的東西,你知道,同樣主要是用來建立原型。但到了某個時候,你會想要建立的不只是原型。你想要建立一個應用程式,對吧,而我在 HuggingFace Spaces 的 gradio 裡很喜歡的一點是……這裡下面有個按鈕「view the api」。所以我們其實可以建立任何我們想要的應用程式,而關鍵在於那個東西,
[1:02:27] 真正替我們做模型預測的會由 HuggingFace Spaces Gradio 來處理,對吧,然後我們可以寫一個 Javascript 應用程式去和它溝通。現在這裡會有兩種反應……任何做過一些前端工程的人,會說「喔太棒了,我現在真的可以在世界上建立任何東西,因為我只要寫任何程式碼就能做到」,他們會很興奮。而很多資料科學家可能會說「喔糟了……我完全不知道怎麼用 javascript,它不在我的,你知道,技能清單裡。」
[1:03:07] 所以這又是我要說的地方:聽著,不要太害怕 Javascript。我的意思是,顯然這裡一個選項就是直接說「嘿我有一個模型」,然後把它丟給你那位懂 Javascript 的朋友,說請幫我建立一個 Javascript 介面。但讓我先讓你感受一下這其實有多不難。所以這裡有一個……端點。現在有一個網址正在跑我們的模型。如果你傳一些資料給它……一張圖片……一些圖片資料……到這個……到這個網址,它就會回傳字典。所以它會做和這個介面完全相同的事,但以 API 的形式,以我們可以呼叫的函式形式。
[1:03:58] 所以這裡有像是如何呼叫它的範例……比如說,我其實可以……讓我用我們剛才那個最小介面,示範一下 API 的用法……因為這樣會簡單一些。所以如果我點 curl 然後複製那個……複製那個……再貼上。你看那裡……(喔,傳入 hello world 不是個好例子)……如果我傳入 Tanishq 看看,來看看用他的名字 Tanishq 會怎樣。
[1:04:37] 你看這裡它回傳「Hello Tanishq!」。這就是這些 API 的運作方式,對吧。所以我們可以用 JavaScript 來呼叫 API,這裡也有一些範例。所以我做了一個網站,這就是我的網站「tinypets」。在這個網站上,如你所見,它不是最漂亮 amazing 的東西,但它就是個網站。這是個開始,對吧,然後上面這裡我有一些範例,來吧,單一檔案,點一下,選擇檔案,點一下……而在這個範例裡,我其實在做完整的寵物分類。所以我其實訓練了一個模型來分類品種,這我們下週會再談,而不只是分狗和貓。所以我們挑一個特定的品種……然後我們執行它……
[1:05:41] 喔!就在那裡了!現在這不是很了不起,對吧,但重點是這現在是個 JavaScript 應用程式。意思是我們對於能做什麼沒有限制。然後我們來看看那個 HTML……就這樣!它輕鬆就能塞進一個畫面裡,對吧,而基本步驟並不誇張,對吧,基本上就是……我們為照片建立一個 import,我們加一個事件監聽器,說當你變更照片時就呼叫 read 函式,read 函式說建立一個 file reader,讀取檔案,當你載入完成時,呼叫 loaded,然後 loaded 說……fetch 那個……現在那個路徑……是……
[1:06:39] 那個路徑!對!只是我們做的是完整的寵物版本。所以這基本上就是從他們的……從他們的範例複製貼上來的。然後抓取 JSON,然後從資料裡抓第一個東西……信心分數……標籤,然後設定 HTML。所以如你所見,這就像是(好吧)如果你以前沒用過 JavaScript,這些都是新東西,對吧,但它們並不……它並不比 Python 難,對吧,它就只是……它就只是另一個、另一個要學的語言。所以從這裡你就可以開始往上建構了,對吧!
[1:07:20] 所以舉例來說,我們做了一個多檔案版本。所以在多檔案版本裡,讓我示範給你看……多檔案……選擇……所以我們現在可以點幾個……所以我們有了一隻紐芬蘭犬、一隻布偶貓、一隻巴吉度獵犬,還有某種貓。我不太算是愛貓的人。所以我們選了四個檔案,砰!它們全都被分類了!顯然牠是孟加拉貓!(我是不懂啦)這是我們的紐芬蘭犬。
[1:07:52] 所以這是多檔案版本,如果你看程式碼,其實沒有多太多,對吧,現在只是做……它取得所有檔案並把它們映射到讀取,然後逐一附加,所以程式碼根本沒多多少。而你可能在我們網站上看到,還有幾個範例,是社群在這週內自己建立的版本。所以這裡這個是……我想這是……對……這是來自 gradio 某位成員的……他們稱之為「認識你的寵物」。所以如果我選一隻寵物……我有點……我真的很喜歡這個,因為它實際上結合了兩個模型。首先它會說,喔!這是巴吉度獵犬,然後讓我輸入並詢問關於牠的事。所以我可以看到,「喔,牠有什麼樣的尾巴?……」搜尋,所以現在這會呼叫一個自然語言處理模型,
[1:08:56] 它會詢問關於這個……這是彎曲的軍刀狀尾巴。就是這樣!「牠需要什麼保養?」所以同樣地,像這裡你可以大概看到(喔)巴吉度獵犬的耳朵必須經常內外清潔。所以這就像是結合模型。所以你可以看到,這是只用一個……一個現成介面做不到的事。接著我想指出的是,我們是如何建立這個網站的?我示範了如何建立 HTML 檔案,但你要怎麼建立那些檔案,又怎麼把它們變成一個網站?那就看這個!讓我們……這裡是……這裡是……
[1:09:52] 這是我們最基本版本的原始碼,好!所以我可以直接儲存這個……就是這樣……好,所以我們可以用 Visual Studio Code 開啟它……然後……我們其實可以做的,是我們可以……這裡它就在檔案總管或 Mac 的 & Finder 裡。我可以直接雙擊它,它就在這裡了!這是個能運作的應用程式!!
[1:10:32] 所以你可以看到,我不需要在電腦上安裝任何軟體就能使用 JavaScript 應用程式,對吧!它就是單一檔案……我只要在瀏覽器裡執行它……瀏覽器就是我們完整的執行環境。它有除錯器。它什麼都有。所以在這裡你可以(對)在這裡你可以看到它……它只是呼叫這個外部的 hacking faces 端點,所以我可以坐在電腦前完成所有事。所以一旦我的 HTML 檔案在電腦上、在 VSCode 裡運作良好,我要怎麼把它放到網路上,讓其他人也能使用?
[1:11:06] 同樣地,這整件事都是免費的。有個很酷的東西叫 github pages,基本上會幫你託管網站。而且因為它只是 JavaScript,它……它全都會運作得很好!在我看來,建立 github pages 網站最簡單的方法,是使用一個叫 fastpages 的東西,那是 fast.ai 的東西!然後……基本上你要做的……就是照著設定流程走。
[1:11:43] 所以首先它會做它的……我們就直接看一遍……它說點擊這個連結來產生一份副本。所以我點擊連結,好的,沒問題,給它一個名稱……(我盡量把一切都設為公開!我總覺得這是個好習慣……你不一定要這樣做)……建立儲存庫,產生中,好的,然後基本上還有兩個步驟,大約要花五分鐘。我們沒有五分鐘,所以我會直接展示我已經建好的那個,也就是「fast.ai/tinypets」。所以一旦完成,你基本上會得到這個空白的網站,同樣地,你只要點「code」「open with github desktop」,或「open with visual studio」,隨便哪個都行……那就「open with github desktop」……
[1:12:37] 或者你可以複製 & 貼到你的終端機……然後,其中任何一種方式都會把整套東西弄到你的電腦上。你可以在那裡儲存你的 HTML 檔案……再推回 github。而你會發現我們……我們……FastPages 會顯示為你建立的網站連結。現在,為你建立的這個網站,你可以用一種叫做「主題」的東西讓它看起來隨你喜歡。所以你會看到它建立了一個叫做 config.yml 的檔案,你可以在那裡挑選主題。所以在這個例子裡,我挑了一個叫做「alembic」的主題,沒什麼特別的理由。
[1:13:18] 所以 Github Pages 使用一種叫做「Jekyll」的東西,因此任何「Jekyll」主題基本上都能運作。所以我挑選了這個主題,結果就是,當我現在把東西存進這個儲存庫時,它們就會自動出現在這個網站上,而檔案也會自動出現在上面的這個清單裡。所以,如果你看我的 index,那就是首頁……整個檔案就只有這樣,對吧!唯一稍微奇怪的地方是,每個 Github Pages 檔案的開頭都必須有三條橫線、title、layout,再三條橫線。這叫做「front meta」。所以,一旦你這麼做……然後儲存,它就會出現在你的網站上。所以,我接著還做了另一件事,我當時想,好吧……fast.ai 建立了這個網站固然很好,但我其實不太喜歡它看起來的樣子。我想做一個不同的版本。
[1:14:18] 沒問題。你可以前往「fast.ai/tinypets」並點擊「Fork」。當你點擊「Fork」時,它會建立你自己的副本。所以,我在我的個人帳號 jph00 底下做了這件事。你看!現在我有了自己的版本,現在我可以在這裡進行修改。所以我做了一些修改。其中一個修改是:我前往 config.yml,並把主題改成「pages-themes/hacker」。所以一旦你 fork 之後,有一件事你必須做,而這通常是 FastPages 會幫你處理的,就是你必須前往「Settings」,點擊「Pages」,並實際啟用「Github Pages」。基本上,預設是關閉的,所以在這裡你只需要把它開啟。使用「Master Branch. Root Save」,然後它會顯示「沒問題,已準備好發布」。所以我修改了 config.yml 檔案,指向不同的主題。所以,如果你現在看……jph 的 Tiny Pets……
[1:15:17] 它不一樣了!好嗎?它有一樣的資訊,但更有「駭客味」,因為 jph00 是個認真的駭客,從他的網站你就看得出來。總之,你看,這只是對 Javascript、網站這類世界的一個非常簡短的體驗,但我想讓你感受到,你知道,你不需要任何錢。你不需要任何 IDE。你知道,你其實不需要寫太多程式碼就能開始撰寫自己的網路應用程式,而且多虧了 hugging face spaces,你知道,他們甚至會幫你託管你的模型,你只需要有那個神奇的魔法字串,當作呼叫用的東西。好!
[1:16:07] 那麼……下線了:駭客 Jeremy Howard。非常感謝你的收看,在下一堂課,我們要深入探討一些自然語言處理。我們會做類似的事情,但我們會用語言而不是圖片來做。我們會深入底層,看看這些模型實際上如何運作。我們會學習隨機梯度下降之類的東西,我們甚至可能得稍微複習一點微積分。希望我說了那個「微」字沒有把你嚇跑。
[1:16:38] 我們下次見!謝謝大家!再見!
查看英文原始字幕
[0:00] Hi everybody. Welcome to lesson two. Thanks for coming back… slight change of environment here, we had a bit of an “administrative issue” at our university — somebody booked our room — so I'm doing this from the study at home. so sorry about the lack of decorations behind me. I'm actually really really pumped about this lesson. It feels like going back to what things were like in the very early days, because we're doing some really new, really cool stuff, which…
[0:41] you know… stuff that hasn't really been in courses like this before. So, I'm super super excited. So thanks a lot for coming back after lesson one, and I hope it's worth you coming back. I think… I think you're gonna love it! I am, yeah, I'm really excited about this. Now remember that, you know, the course goes with the book, so be sure that you're following. I mean, not following along in the book, because we're covering similar things in different directions… but, like, read the book as well, and remember the book is entirely available for free as well. You can go to the fast.ai fastbook repo to see the notebooks, or through course.fast.ai you can read it there — for example, through Colab.
[1:28] And also remember that the book… I mean… the book has a lot of stuff that we didn't cover in the course like, you know, stuff I find pretty interesting about the history of neural networks, some of which has some really interesting personal stories actually, as you'll read here. And at the end of each chapter there is a quiz. Remember it's not a bad idea before you watch the video to read the quiz, so if you want to read the Chapter Two quiz, you know, and then come back, that's not a bad idea. Then make sure that you can do the quiz after you've watched the video, and you've read chapter two of the book.
[2:07] Something I didn't mention last week is there's also a very cool thing that Radek — who I mentioned last week — has written, called aiquizzes.com which is a site specifically for quizzes about the book. And it actually uses repetitive based learning techniques to make sure that you never forget. So, do check out aiquizzes.com. It's all brand new questions, they're different to the ones in the book, and they're really nicely curated, and put together. So check out aiqquizzes.com as well. Remember, as well as course.fast.ai, there's also forums.fast.ai. So course.fast.ai is where you want to go to get, you know, links to all the notebooks, and kaggle stuff, and all that stuff. You'll also find on forums.fast.ai every lesson has an official topic, you know, with all the information you'll need. Generally there'll be a bit more info on the forums, we try to keep the course
[3:09] lean and mean, and the forums are a bit more detailed. So if you find — in this case you are gonna look for the Lesson Two official topic but here's the Lesson One official topic so far. So from the Lesson One official topic, already, after just a few days since I recorded it — we haven't even launched the course so it's just the people doing it live — there's already a lot of replies, and that can get pretty overwhelming.
[3:40] So be aware that there's a button at the bottom of my post that says summarize this topic. And if you hit that, then you'll just see the most upvoted replies, and that's a really good way to just make sure that you hit on the main stuff. So there's the button, and here's what it looks like after you hit it. You'll just get… you'll just get the upvoted stuff from fast.ai legends like Sanyam and Tanishq. So hopefully you'll find that a useful way to use the forum.
[4:14] So one of the cool things about this week is I… as promised, put up the “show us what you've made” post, and already a lot of people have posted. I took this screenshot a few days ago, it's way above 39 replies already, if I'm… if I remember correctly. I had a lot of trouble deciding which ones to share because they're all so good, so I've actually decided to kind of, you know, go the easy route, and I just picked the firsts. So I'm just going to show you the first ones that were posted because they're all so good. So the first, the very very first one to be posted is a damaged car classifier… so that worked out pretty well — it looks like. And I really liked what Matt, the creator, said about this, is that… you know, wow, it's a bit uncomfortable to run this code, I don't really understand yet but I'm just doing it. And so I'm like, yeah, good on you Matt for just…
[5:14] for just doing it. That's the way to get started, it's all going to make sense, don't worry. Very nice to see that the next one posted was actually a blog post in fast pages — very nice to see — so just describing some stuff, some experiments that they ran over the week and what did they find. Next one was the amazing beard detector which if I understand correctly was mainly because it's very easy to get from bird to beard by just changing one letter to two, and this is doing a very good job of finding gentlemen with beards. Very nice. And then this one is…
[5:51] another level again, it's a whole in-production web app to classify food, which is kind of like extra credit . Apparently we're up to 80 replies now in that thread, thank you Sanyam, very cool. So, you know, obviously… so this was actually created by Suvash who's been doing the courses for a few years now, I believe. And so you know, one day you too might be able to create your very own web app and put it in production. And when I say one day, more specifically… today! I'm actually going to show you how to do this right now! So it's actually quite a lucky coincidence that Suvash put this up there because it's exactly the topic that we're going to pick today.
[6:46] So, how do we go about putting a model in production? Step one, is… well… you've kinda done step one, right? Step one is… step one, two, three, four… is to figure out what problem you want to solve, figure out how to find the data for it, go gather some data, and so forth. So what's the kind of first step after you've got your data? The next step is data cleaning, and if you go to Chapter 2 of the book, which I'm going to go ahead and open up now…
[7:23] So here is the book… so you can open it in Colab directly from the course, or if you've cloned it to your computer, or whatever, you can… you can do it there. So remember, course.fast.ai will run you through exactly how to run these notebooks, and so you can see Chapter 2 is all about putting stuff in production, and so here is Chapter 2. All right, and so remember we hit Shift + Enter to run cells, okay? …to execute them. And so we're going to go to the part of the book where we start cleaning the data.
[7:59] So I'll click on Navigate, and we'll go down here… gathering data… there we are! So we could do a quick bit of revision first. By the way, I will mention, a lot of people ask me what are the little tricks I use for getting around a Jupyter notebook so quickly and easily. One of the really nice ones as you'll see is this navigate menu, which actually doesn't appear by default. So if you install something called Jupyter Notebook Extensions - Jupyter…
[8:30] Notebook… Extensions… and so you just… you just “pip install” them, follow the instructions and then restart Jupyter. Obviously this… Colab already has a table of contents by the way so this is just if you're using something local, for example, then you'll see here that this “Nbextensions” thing will appear, and if you click on “Table of Contents (2)” that gives you this handy navigation bar. The other thing I really like is this one here called collapsible headings, and that's the one which gives me these nice little things here, to close, and open up. And actually that's not even the best part. The best part for me is if I hit the right arrow it goes to the end of a section, and if I hit the left arrow it goes to the start of a section. So it's like, if I want to move around sections I just press up left, down right, down right, very handy,
[9:35] and if you hit left again when you're here it 'll close it up. Hit right again here, open it up. So that's collapsible headings. Anyway, a couple of really handy things, and we'll be talking a lot more about getting your notebook set up today in a moment. Okay, so one thing you'll notice is in the book, we use the Bing API for searching images. I've just gone ahead, and replaced “bing” with “ddg” because the Bing API requires getting an SDK key which honestly, it’s like the hardest thing in deep learning is figuring out the Bing Azure website, and getting that sorted out. ddg doesn't! So it's basically exactly the same… (and you can…)
[10:21] I'll share this notebook as well, on the course website in the forum but all I've basically done is I've replaced bing with ddg, and got rid of the key. So then, just like we did last week, we can search for things, and so in the book we did a bear detector because at the time I wrote it my then toddler was very interested in me helping identify teddy bears, and I certainly didn't want her accidentally cuddling a grizzly bear, so we show how here how we can search for grizzly bears, just like last week.
[10:56] Something that loops through grizzly bears, black bears, and teddy bears, just like last week. Get rid of the ones that failed, just like last week. And one thing a few people have asked on the forum is how do i find out more information about basically any python or fastai or pytorch thing. There's a few tips here in the book. One is that if you put a double question mark next to any function name you'll actually get the whole source code for it, and by the same token if you put a single question mark you'll get a brief…
[11:41] you know… a little bit of information. If you've got nbdev installed (I think it's nbdev you need) then you can type doc, and that will give you perhaps most importantly a link straight to the documentation where you can find out more information and generally there will be examples as well. And also a link here to the source code, if you want to…
[12:13] (let's do that with a control okay) …a link to the source code and that way you can jump around. And notice that in github in the source code you can click on things and jump to their definition. So it's kind of a nice way of skipping around to understand exactly what's going on. Okay, so lots of great ways of getting help. But what I promised you is that we're going to now clean the data. So I'm going to tell you something that you might find really surprising. Before you clean the data, you train a model.
[12:55] Now, I know that's going to sound really backwards to what you've probably heard a thousand times, which is that first you… ~(build/you/train) …you clean your data and then you train your model. But I'm going to show you something really amazing. First we're going to train a model and you'll see why in a moment. So to train a model, just like before, we use a DataBlock to grab our data loaders — there's lots of information here in the book about what's going on here. There we go… and so then we can call show_batch to see them as per usual.
[13:33] There's a little side bar here in the book, I'll quickly mention, which is about the different ways we can resize. I think we briefly mentioned it last week… we can squish… last week I used a string, you can use a string or this kind of enum like thing that we have. You can see with a squish you can end up with some very thin bears, right? So this is the real size, the shape, of the bear. Here its become thin but you can see now we've got all of its cubs… (are they called clubs? yeah bear cubs) …so it squished it to make sure we can see the whole picture. Same one here. This one was out of the picture, we squished it and this guy now looks weirdly thin, but we can see the whole thing. So that's squishing!
[14:12] Or else this one here is cropping. It's cropped out just the center of the image so we get a better aspect ratio but we lose some stuff. This is so we can get square images. And the other approach is we can use “pad.” And so you can pad with various different things. If you pad with zeros, which is black, you can see here now we've got the whole image and the correct aspect ratio. So that's another way we can do it and you know, different situations, you know, result in different quality models. You can try them, all it doesn't normally make too big a difference.
[14:44] So I wouldn't worry about it too much. I'll tell you one though that is very interesting is RandomResizedCrop. So instead of saying resize, we can say RandomResizedCrop. And if we do that you'll see we get a different bit of an image every time. So, during the week… this week somebody asked on the forum… I'm trying to… (this is a really interesting idea, which it turned out worked slightly) they wanted to recognize pictures of French and German texts. So obviously this is not the normal way you would do that, but just for a bit of experiment (and I love experiments, so) they had very big scans of documents and they wanted to figure out whether it was french or german just by looking at images and they said the pictures were too big what should I do? I said, use RandomResizedCrop and that way you'd grab different bits of the image, and this is very nice because you could run lots and lots
[15:44] of epochs and get slightly different pictures each time. So this is a very good technique. And this idea of getting different pictures each time from the same image is called “data augmentation.” And again, I'm not going to go into too much detail about data augmentation, because it's in the book, but i'll just quickly, you know, point out here that if you use this thing called “aug_transforms” so augmentation transforms, and here I have multiplied them by two – so I've made them super big so you can see them more easily. You can see that these teddies are getting turned, and squished, and warped, and recolored, and saturated, all this stuff to make every picture different. And generally speaking if you're training for more than about five or ten epochs, which you'll probably want to do most of the time unless you've got a super easy problem to solve, you'll probably want to use RandomResizedCrop and these “aug_transforms”. Don't put the mult=2,
[16:49] just leave that empty. I'm just putting it there so you can see them more clearly. So I've got an interesting question here from Alex in our audience, which is: “Is this… is this copying the image multiple times? …doing something like this? …or something like this?” and the answer is “no we're not copying the image. What happens is that image… so each epoch, every image gets read. And what happens here is though… is… is kind of… in memory, in RAM, this… the image is being “warped”, right? it's being… we're cropping it and recoloring it, and so forth. So it's a real time process that's happening during model training. So, there's no copies being stored on your computer, but effectively it's almost like there's infinitely slightly different copies because that's what the model ends up seeing. So, I hope that makes sense Alex, and everybody else,
[17:52] so that's a great question! Okay, so we've got… we're going to use RandomResizeCrop, we're going to use augmentation transforms, so that we can get at DataLoaders from that and then we can go ahead and train our model. It takes about a minute. In this case we only did four epochs of fine tuning, and we'll talk about why there's five here later in the course, but four main epochs of fine tuning. So we probably didn't really need RandomResizedCrop and aug_transforms because there's so few epochs, but you know, if you want to run more epochs this is a good approach. Under three percent error! that's good! Okay, so remember I said we're going to train a model before we clean?
[18:36] Okay so let's go ahead and train it. So while that's training… so it's running on my laptop, which only has a 4GB GPU, it's pretty basic, but it's enough to get started. While that's training we'll take a look at the next one. So the first thing we're going to look at is the confusion matrix and the confusion matrix is something that… it only is meaningful for when your labels are categories, right!
[19:09] And what it says is how/what category errors are you making. And so this is showing that the model that we've got at this point… there were two times when there was actually a grizzly bear and it thought it was a black bear, and there was two times when there was actually a black bear, and it thought it was a grizzly bear, and there was no times that it got teddies wrong, which makes sense right because teddies do look quite different to both. In a lot of situations when you look at this, it'll kind of give you a real sense of, like okay, well what are the hard ones? right? So for example if you use the pets data set that we quite often play with in the book and the course, this classification [**] matrix for different breeds of pets, you know, really shows you which ones are difficult to identify, and I've actually gone in, and like, read wikipedia pages, and pet breeding reports about how to identify these particular types, because they're so difficult, and even experts find it difficult.
[20:10] And one of the things I've learned from doing the course actually is… black bears and grizzly bears are much harder to pick apart than I had realized... so I'm not even going to try! But I'll show you, the really interesting thing we can do with this model, is that now we've created this classification interpretation object, which we use for confusion matrix, we can say plot_top_losses. We can say plot top losses, and this is very interesting. What it does is, it tells us the places where the “loss” is the highest. Now if you remember from the last lesson, the loss is that measurement – of how good our model is – that we take after each time we run through an item of data. A loss will be “bad” if we predict wrongly, and we're very confident
[21:11] about that prediction. So here's an example where we predicted… (here's the order here: prediction, actual loss, probability) …where we predicted grizzly and it was actually a black, and we were 96% sure (our model was) that it's a grizzly. Now, I don't know enough about bears to know whether the model made a mistake or whether this actually is a picture of a grizzly bear… but so, an expert would obviously go back and check those out…
[21:41] right? Now, you'll notice a couple here... it's got grizzly/grizzly, teddy/teddy… they're actually correct, right? So why is this loss “bad”? When it's correct? And the reason is… because it wasn't very confident. It was only 66% confident, right? So here's a teddy… it's only 72% confident right? So you can have a bad loss, either by being wrong and confident, or being right and unconfident. Now the reason that's really helpful, is that now we can use something called the fast.ai ImageClassifierCleaner to clean up the ones that are wrongly labeled in our data set.
[22:24] So when we use the ImageClassifierCleaner it actually runs our models. That's why we pass it “learn”, right? And I mentioned that I don't know much about black bears and grizzly bears but I do know a lot about teddy bears, so I'll pick teddy bears. And if I click teddy bears it's now showing me all the things in the training set (you can pick training or valid) that were marked as teddy bears and here's what's really important: they're ordered by loss, so they're ordered by confidence, right? So I can scroll through just the first few and check they're correct, right? And oh, here's a mistake, right? So when I find one that was wrongly gathered I can either, put it…if it's in the wrong category, I can choose the correct category… or, if it shouldn't be there at all, I click delete. So here I'll go ahead and click delete, right?
[23:14] So you can see some reasons that some of these are hard, like for example here's two teddies, which is just, I guess, confusing since it doesn't see that often. This one here is a bit weird looking, it looks almost like a wombat. This is an awful lot of teddies. This one maybe is just a bit hard to see from the background, but these, otherwise, they look fine… Fine, so we just look through the first few and if you don't see any problem or problems in the first year, you're probably fine. So that's cleaned up our training set, let's clean up our validation set as well. So here's that one it had trouble with, I don't know why it had trouble with that one but so be it and we'll have a quick scroll through…
[23:59] Okay I'm not really sure that's a bear so I'm just going to go ahead and delete it. It's a teddy something… but, you know, it's a problem. Okay, that's not a teddy either… So you see the idea, right? So after we've done that, what that does is… the cleaner has now stored a list of the ones that we changed and the list of the ones we deleted, so we can now go ahead and run this cell. And so that's going to go through a list of all of the indexes that we said to delete, and it will delete those files. And we'll go through all the ones we set to change, and it will move them to the new folder, there we go, done!
[24:45] So, this is like, not just something for image models. It's just… it's actually… a really powerful technique that almost nobody knows about and uses, which is, before you start data cleaning, always build a model to find out what things are difficult to recognize in your data and to find the things that the model can help you find data problems. And then as you see them, you'll kind of say “okay, I see the kinds of problems we're having” and you might find better ways to gather the next data set, or you might find ways to kind of automate some of the cleaning and so forth.
[25:23] Okay, so, that is data cleaning and since I only have a 4GB GPU it's very important for me to close and halt, because that will free up the memory. So it's important to know… on your computer your normal RAM doesn't really get filled up, because if you use up too much RAM, what will happen is that instead your computer will start (it's called) swapping, which is basically to save that RAM onto the hard disk to use it later. GPUs can't swap.
[26:00] GPUs, when they run out of RAM, that's it, you're done. So you need to make sure that you close any notebooks that are using the GPU that you're not using and really only use one thing at a time on the GPU, otherwise you're almost certainly run out of memory. So we've got the first few reds starting to appear. So remember to ask and, in terms of the yellows, it's important to know, as you watch the video, I'm not asking you to run all this code, okay? The idea is to kind of watch it, and then go back and pause, you know, as you go along. Or you can just stop, try, stop, try. The approach I really like — and a lot of students really like — for watching these videos is to actually watch the entire thing without touching the keyboard, to get a sense of what the video is about, and then go back to the start and watch it again and follow along. That way, at every point you know what it is you're
[27:00] doing, you know what's going to happen next. That can actually save you some time. It's a bit of an unusual way because, obviously like, real life lectures you can't do that, you can't rewind the professor and get them to say it again, but it's a good way to do it here. So now that we've cleaned our data, how are we going to put it into production? Well in the book we use something called Voilà. And it's… it's pretty good! But there's actually something that I think most of you are probably going to find a lot more useful nowadays, which is something called HuggingFace Spaces, and there's a couple of things you can use with that. We're going to look at something called Gradio today.
[27:42] And there isn't a chapter about this in the book but that doesn't matter because Tanishq Abraham — who's actually one of the TAs in the course — has written a fantastic blog post about really everything we're going to cover today. So there's a link to that from the forum and from the course page, so this is like the equivalent of the chapter of the book, if you like. And I would be remiss if I didn't stop for a moment and call out Tanishq in a big way for two reasons: the first is he is one of the most helpful people in the fastai community, he's been around quite a few years, incredibly tenacious, thoughtful and patient; and also because I have this fantastic picture of him a few years ago with Conan when he was a famous child prodigy, so now you know what happens to famous child prodigies when they grow up – they became even more famous fastai community members and deep learning experts.
[28:41] So you should definitely check out this video of him telling jokes to Conan. I think he's still only 18 actually, this is probably not that many years ago. So thank you very much Tanishq for all your help in the community and sorry for embarrassing you with that picture of you as a nine-year-old — I'm not really, haha. Okay, now, the thing is for doing Gradio and HuggingFace Spaces, well, it's easy enough to start. Okay, we start over here on the HuggingFace Spaces page — which we've linked to from the forum in the course. And we're going to put a model in production, where we're going to take the model we trained, and we are going to basically copy it to this HuggingFace Spaces server and write a user interface for it.
[29:29] So… let's go! Create new space. Okay, so, you can just go ahead and say, all right… so obviously you sign up… the whole thing's free. Basically everything I'm showing you in this entire course, you can do for free, that's the good news. Okay, so, give it a name, just create something minimal. I always use the Apache license because it means other people can use your work really easily but you don't have to worry too much about patents. As I say there's a few different products you can use with it, we're going to use Gradio, also free.
[30:04] If you make it public then you can share it, which is always a good idea when you're a student, particularly to… to really be building up that portfolio. Okay, so we're done. We've created a space. Now, what do we do next? Well, Spaces works through Git, now, most software developers will be very familiar with Git. Some data scientists might not be. And so Git is a very, very useful tool. I'm not going to talk about it in detail, but let's kind of quickly learn about how to use it, right? Now, Git, you can use it through something called Github Desktop which is actually pretty great and even people who use Git through the console should probably be considering using Github Desktop as well, because some things are just much faster and easier in it, in fact I was talking to my friend Hamil today and I was like, oh, help, I've accidentally committed this two things by mistake, what's the easiest way to revert it, and he used to work at Github
[31:09] and I thought he was going to have some fancy console command and he's like “oh, you should use Github Desktop and you can just click on it.” Oh! That's a great idea. So that's… that's useful. But most of the time we do use Git from the console, from the terminal. If you're a Linux user or a Mac user you've already got a terminal, very straightforward, no worries. If you're a Windows user I've got good news, nowadays Windows has a terrific terminal. It's called Windows Terminal, you get it from the Microsoft Store, so in fact every time you see me using a terminal, I'm actually using that Windows Terminal, works very well. God knows why you'd want it to, with all these ridiculous colors but, there you go.
[31:54] Now, what do you want to be running inside your terminal? Obviously, if you're in Linux or Mac you've already got a shell set up. In Windows, you almost certainly want to use Ubuntu. So Windows, believe it or not, can actually run a full Linux environment and to do it, is right typing a single line, which is this… So, if you go to (just google for WSL install) run PowerShell as administrator, paste that command, wait about five minutes reboot, you're done.
[32:30] You now have a complete Linux environment. Now, one of the reasons I'm mentioning this is, I'm going to show you how to do stuff on your own machine now, and, so, this is like going to a bit of an extra level of geekery, which some data scientists may be less familiar with. So you know, don't be worried about the terminal, you're gonna.. I think you're gonna… find it really helpful and much less scary than you expect. And, I particularly say, like for me, I choose to use Windows and that's because I get, you know, all the nice Windows GUI apps and I can draw on my screen, and do presentations, and I have a complete Linux environment as well. And that Linux environment uses my GPU and everything, so for me, you know, my first choice is to use Windows, my second choice, by not very much, really like it, would be to use Linux. Mac is a little bit harder, but it's still usable. Some things are a little bit trickier on Mac but, you should be fine.
[33:37] Okay, so, whatever you've got, at this point you've now got a terminal available. And so, in your terminal… (one of the really nice things about using a terminal is you don't have to follow lots of instructions about click here, click here, click here. You just copy and paste things.) So I'm just going to… you just copy this, and you go over to your terminal, and you paste it in, and you run it, and after you do that, you'll find that you've now got a directory. And so that new directory initially is empty and they tell you, okay, go ahead and create a file with this in it. Okay, so, how do you create a file with that in it, when we're in here, in our Linux environment on Windows, or in the terminal on Mac, or whatever? Well, all you do in Windows, if you just type “explorer.exe .”, it'll open up explorer here, or better still, on either Mac, or Linux, or Windows…
[34:42] So, yeah, regardless of what computer (type of computer on), you can just type “code .” and it will pop up Visual Studio Code and open up your folder. And so then you can just go ahead and… (if you haven't used VS Code before it's really well worth taking a few minutes to read some tutorials, it's a really great IDE) …and so you can go ahead and create an app.py file, like they tell you to, app.py file, containing what they told you to put in it. Here it is, here.
[35:22] All right, we're nearly there. So you can now go ahead and save that, and then you need to commit it, to Gradio… (not Gradio…) to HuggingFace Spaces. So one really easy way is just, in Visual Studio itself, you can just click here, and that'll give you a place where you type a message, and you hit tick, and it'll send it off to HuggingFace Spaces for you. So once you've done that, you can then go to… back to… the exact same website you're in before: “huggingface.co/spaces/jhp00/minimal”, and what you'll find now is that — it'll take about a minute, to build your website. And it's…
[36:15] the website it's building is going to have a Gradio interface with a text input, a text output, and it's going to run a function called “greet()” on the input, and my function called “greet()” will return “Hello {name}”. So that's what it's going to do. Now there it goes, let's try it! We'll say hello to Tanishq. I'm not always very good at remembering how to spell his name, I think it's like that.
[36:50] And there you go, so you can see it's put the output, for our input. So not a very exciting app but we now have, to be fair, an app running in production, right? Now, I told you we'd have a deep learning model running in production so now we have to take the next step which is to turn this into a deep learning model. All right, so, first we're going to need a deep learning model. And there's a few different ways we can get ourselves a deep learning model, but basically we're gonna have to train one. So, I've got a couple of examples: I've got a Kaggle example and a Colab example. Maybe I'll quickly show you both. They're gonna be the same thing. And I'm just going to create a dog or a cat classifier. Okay, so here's our Kaggle model, I'll click on edit so you can actually see what it looks like in edit view.
[37:52] Now, Kaggle already has fastai installed but I always put this first, just to make sure we've got the latest version. And obviously import stuff, we… So we're going to grab the pets data set, a function to check whether it's a cat, that's our labeling function for our ImageDataLoaders — remember this is just another way of doing DataBlocks, it's like a little shorthand. And we create our learner, and we fine tune it. Okay, so, that's all stuff we've seen before. So, in Kaggle every notebook has an edit-view (which is what you just saw) and a reader-view.
[38:27] And so you can share your notebook — if you want to. And then anybody can read the reader-view, as you see. And so, here, you can see, it shows you what happened when I ran it. And so, I trained it, it took… you know, the GPUs on Kaggle are a bit slower than most modern GPUs but they're still fast enough, I mean, it takes five minutes. And there's one bit at the end here, which you haven't seen before, which is I got learn.export and I give it a name.
[39:02] Now, that's going to create a file containing our trained model, and… that's the only thing! Creating this file is the only thing you need a GPU for, right? So you do that on Kaggle, or on Colab. So here's exactly the same thing on Colab… you can see “pip install”, is_cat, our data, ImageDataLoaders, so I've got a show_batch here as well, just for fun. Create my learner and then export. So, while we wait I might go ahead and just run that.
[39:36] One nice thing about Kaggle is once you've run it and saved it, you can then go to the data tab and, here is basically anything you've saved, it's going to appear here, and here it is: model.pickle, right? So now I can go ahead and download that and that will then be downloaded to my downloads folder. And then I need to copy it into the same directory that my HuggingFace Spaces App is in. Now my HuggingFace Spaces App is currently open in my terminal, on Mac you can type “open .” or in Windows you can type “explorer.exe .” and that'll bring up your finder or explorer in that directory. And so then you can just paste that, that thing you downloaded into this directory. Something by the way, in Windows I do, which I find really helpful is, I actually grab my… my home directory in Linux
[40:43] and I pin it to my Quick Access and that way I can always jump in Windows straight to my Linux files. Not really something you have to worry about on Mac because it's all kind of integrated, but on Windows they're like, kind of like, two separate machines. Okay, so… Let's do… so, I created a Space called testing and I downloaded my “model.pkl” and I pasted it into testing. So now we need to know how do we do predictions on a saved model?
[41:23] So we've got a notebook for that… Okay, so we've got a notebook for that and… so I'm going to take you through how we use a model that we've trained to make predictions. There's a few funny things with hash pipe which I'll explain in a moment, just ignore those for now. So we import fastai as usual. We import Gradio as we did before. And we copy in the exact same is_cat definition we had before. That's important, any external functions that you used in your labeling, need to be included here as well because that learner refers to those functions, okay? It saves... that learner saves everything about your model, but it doesn't have the source code of the function, so you need to keep those with you.
[42:12] So, let's try running this. So, for example, I just grabbed… (as you might have seen in my explorer) I just popped a dog picture there. And so we can create a python image library image from that dog, turn it into a slightly smaller one so it doesn't overwhelm my whole screen and there is a picture of a dog. So, how do we make predictions of whether that's a dog or a cat? Well, it's very simple, all we do is… instead of training a learner, we use load_learner(). We pass in the file name that we saved, and that returns a learner. This learner is exactly the same as the learner you get when you finish training. So here we are, here's Colab, right? We've just been training a learner, so at the end of that, there's a learner that's been trained, and so we… We kind of froze it in time, something called a pickle file — which is a python concept. It's like a frozen object, we saved it to disk, we transferred
[43:17] it to our computer, and we've now loaded it, and we've now unfreezed thought it. This is our unpickled learner and we can now do whatever we like with that. So one of the things that… One of the methods that a learner has is a dot predict method, so, if I run it, you can see, even on my laptop, it's basically ~(instance) instant. In fact we can see how long it took. If you — in Jupyter things that start with percent are called magics, they're special Jupyter things, so for example, there's a thing to see how long something takes.
[43:51] There you go, okay, so it took 54 milliseconds to figure out that this is not a cat. So it's returning two things: is it a cat? (as a string); is it a cat? (as a zero or one), and then the probability that it's a dog, and the probability that it's a cat. So the probability of zero (false) and one (true) of is it a cat? so definitely a dog. So we now want to create a Gradio interface which basically has this information.
[44:28] So Gradio requires us to give it a function that it's going to call, so here's our function. So we're going to call predict, and that returns as we said three things: the prediction is a string; the index of that; and the probabilities of whether it's a dog or a cat. And what Gradio wants, is it wants to get back a dictionary containing each of the possible categories — which in this case is dog or cat — and the probability of each one. So, if you haven't done much python before, a dict of a zip may be something you haven't seen.
[45:04] Very handy little idiom well worth checking out. Ditto if you haven't seen map before, anyway, here it is. One slightly annoying thing about Gradio at the moment is that it doesn't handle Pytorch Tensors, you can see here Pytorch is not returning normal numbers, it's returning Tensors. It's not even returning Numpy Arrays. In fact radio can't handle Numpy either, so we have to change everything just to a normal float, so that's all that this is doing, it's changing each one to a float. So, for example, if I now call classify_image() with our doggy image, we get back a dictionary of a dog, yes definitely; cat, definitely not.
[45:45] So now we've got all that we can go ahead and create a Gradio interface. So Gradio interface is something where we say, well, what function do you call to get the output, what is the input — in this case we say, oh, the input is an image — and so check out the Gradio docs, it can be all kinds of things like a webcam picture, or a text, or you know, all kinds of things. Give it a shape that it's going to put it into, the output's just going to be a label so we're going to create very, very simple interface. And we can also provide some examples, and so there's a dog, a cat, and a dunno — which I'll tell you about in a moment — which you'll see here there's a dog, and a cat, and a don't know.
[46:27] So once I launch it, it says, okay, that's now running on this url. So if I open that up; you can see now, we have just like Suvash, we have our own — not yet in production, but running on our own box — classifier. So let's check: dog, so you can click it and upload one, or just choose the examples… Here they are! So it's running on my own laptop, basically instant. And I really have to tell you the story about this guy here, this is the dunno.
[47:01] Submit, wait, why is it saying 100? Normally this says like 50 50. That's a bummer, this model's got… messed up my whole story. So last time I trained this model, and I ran it on the… dunno, it said, it said like it's almost exactly 50 50. And the way we found this picture is… I showed my six-year-old daughter, she's like: “what are you doing dad?”, like, I'm coding, “what are you coding?” Oh, you know, dog cat classifier. She checks it out and her first question is: “can I take your keyboard for a moment”, and she goes to google, and she's like: what is a dog mixed with a cat called. Like, there's no such thing as a dog mixed with a cat. Anyway, she goes to the images tab, and finds this picture, and she's like: “look there's a dog mixed with a cat”.
[47:47] She said: “run it on that dad!, run it on that!” And I ran it and it was like: 50-50. It had no idea if it's a dog or a cat. Now, this model I just retrained today, now it's sure it's a cat. So there you go, I think I used a slightly different training schedule or something, or gave it an extra epoch, anyway, so that's a dog-cat, but apparently it's a cat. I guess it is a cat. It's probably right. I shouldn't have trained it for as long. Okay, so there's our interface. Now, that's actually running, so you actually have to click the start button to stop it running so otherwise you won't be able to do anything else in your notebook. Okay, so, now we have to turn that into a python script. So, one way to turn it into a python script would be to copy and paste into a python script all the things that you need.
[48:42] I need to copy and paste into a python script all these parts of this that you need, so, for example, we wouldn't need this — it's just to check something out, we wouldn't need this — it was just experimenting. This was just experimenting. We'd need this, right? So, what I did… is I went through and I wrote “hash pipe export” at the top of each cell that contains information that I'm going to need in my final script, and then — so they're the steps, right? And then, at the very bottom here, I've imported something called notebook2script from nbdev, and if I run that, and pass in the name of this notebook, that creates a file for me called app.py containing that script. So this is a nice easy way to — like when you're working with stuff that's expecting a script and not a notebook, like HuggingFace Spaces does. It's fine to just copy and paste into a text file if you like,
[49:42] but I really like this way of doing it because that way I can do all of my experimentation in a notebook, and when I'm done, I just have a cell at the bottom I just run and export it. How does it know to call it app.py? That's because there's a special thing at the top default export: default_exp, which says what python file name to create. So that's just a little trick that I use. So now we've got an app.py. We need to upload this to Gradio, how do we do that? You just… you just push it to Git. So… you can either do it with Visual Studio Code, or you can type “git commit”.
[50:28] And then “git push”. And once you've done that…if we change minimal to testing… I think this hopefully might still be running my previous model — because I didn't push it, and that way, we can see our crazy dog-cat. All right, so here it is, you can see it running in production. So now this is something that anybody can — if you set it to public — anybody can go here and check out your model, and so they can upload it. And so, here's my doggy.
[51:02] Yep, definitely a dog. Cat, yeah I think I might have trained this for an epoch or two less, so it's less confident. Yeah definitely a cat. Dog-cat… hey dog-cat… hmm, still thinks it's definitely a cat. Oh well, so weird. Okay, so, that is…Okay, so, that is an example of getting a simple model in production. There's a couple of questions from the forum, from the community.
[51:49] Okay so one person's asking: “what's the difference between a pytorch model and a fastai learner?” Okay, that's fine, we'll get to that shortly. Don't know if it'd be the lesson, it might be this lesson or the next lesson. And then somebody else asked, basically it's asking: “how many epochs do we train for?” So, as you train a model your error rate, as you can see, it improves.
[52:21] And so the question is: should I run more? Should I increase the number of epochs? This is doing three epochs, right? Here's my three epochs plus one to get started. Look, it's up to you, right? I mean, for this, is here saying: there's a one percent error. I'm… I'm okay with the one percent error, you know, if you want it to be better, then you could use more data augmentation, and you could train it for longer. If you train for long enough, as we'll learn about soon, and then… maybe the next lesson. If you train for long enough, your error rate actually starts getting worse. And, you'll see, we'll learn about why. So basically yeah, you can train until it's good enough or until you've run out of patience, or time, or run out of compute, or until you… or until the error rate starts getting worse.
[53:14] Okay, oh, and then in Colab, how do you grab your model? All you need to do in Colab is, after you've exported it, is, if you go into their file browser you'll actually see it here, right? And you can click download. It's a bit weird, it doesn't like, pop up a box saying where do you want to download it to, but instead this kind of progress circle thing pops up, and so depending on how big it is, and so forth, it can take a few minutes. And once that circle fills up then it'll, the browser thing will finally pop up and say, okay, you can save it. Okay so that's how you…
[53:57] that's how you actually grab your model. So as you can see, the step where you actually need a GPU, you can use these totally free resources: Colab, Kaggle, and there are other ones we'll talk about in future lessons. And then you can do everything else on your own computer, including the predictions, the predictions are fast, right? So, you really don't need to use a GPU for that, unless you're doing thousands of them. Okay, here it goes, now it's asking me to save it. Okay, so now one big issue is, we needed… to run it on our computer… we needed python and Jupyter Notebooks running on our computer, so how do you do that?
[54:41] Because this is where often people get in all kinds of trouble… “I'm trying to figure out how to get this all working.” So, the good news is we've actually got something that makes it very, very straightforward. It's called fastsetup. There's really only just one part of it you need, so let me show you…. it's… it's… actually a Git repository on Github. Github's the place where most Git repositories live. So if you go to Github fastai/fastsetup you'll see it. And so what you can do is you can now grab this whole repository, just by clicking here on code, and if you've got Github Desktop installed, click on open with Github Desktop and, as you'll see, it brings this up saying: okay I'm ready to save this for you, so I click clone, so it's making a copy of it. There we go…
[55:39] So, basically, once you've cloned it, you'll then find there's a file in there called setup-conda.sh… which, you know, the details don't really matter, it's pretty short, but, that's the thing that's going to install python for you. So at that point you can just run “./setup-conda.sh” and it'll run this installer. Now, if you've got Linux or Mac you've already got python on your machine. Don't use that python!!
[56:14] And the reason is because that python, it's called the system python, it's used by your computer to do computery stuff, right? It's actually… it's actually needed. You don't want to be messing with it, I promise you, like… it's, it always leads to disaster, always! You want your own development version of python, it's also going to make sure you've got the latest version, and all the libraries you want, and, by far, the best one for you is almost certainly going to be these conda based python distributions, so if you run setup conda you'll get the one that we recommend. And, the one we recommend at the moment is something called mambaforge. So basically once you run it, you'll find that you've now — you close your terminal and reopen it — you'll find you've now got one extra command, which is called mamba, and mamba lets you install stuff.
[57:10] So once you've run it, you'll be able to go “mamba install fastai”. And that's gonna… actually we should probably, I should mention this, actually more, a bit more detail about how to install it correctly. If we go to docs.fast.ai, installing. Yeah, okay, we actually want to do “conda install -c fastchan fastai”. So let's just copy and paste. Oh sorry, not actually… and then, the other thing I'll say is, instead of using conda, replace conda with mamba because nowadays it's much faster. So “mamba install -c fastchan fastai”.
[57:52] Now, this is going to install everything you need. It's going to install Pytorch, it's going to install Numpy, it's going to install fastai… and so forth, right? And… so obviously I've already got it. And then the other thing you'll want to do is install nbdev, so you can do exactly the same thing for nbdev. You don't have to, right? It's just that… but that'll install Jupyter for you, amongst other things.
[58:28] And so at that point you can now, you can now use Jupyter. And so the way Jupyter works is — you can see it over here. This is my… (I'll go ahead and close it so we can start again.) So basically, to use Jupyter you just type “jupyter notebook”, okay? And when you run it, it'll say: okay, we're now running a server for you, and so if you click on that hyperlink, it'll pop up this, okay?, which is exactly what you see me use all the time. Okay so, you know, that hopefully is enough to kind of get you started with python, and with Jupyter Notebook…
[59:17] The other way people tend to install software is using something called pip instead of mamba. Pretty much anything you can do with mamba you can also do with pip, but if you've got a GPU, pip isn't going to install things generally so that it works on your GPU, you have to install lots of other stuff, which is annoying, so, that's why I kind of tell people to use mamba, but you can use pip otherwise. Still a little bit of red. Please let us know how we can help you again.
[59:54] Okay so let's see how we’re going with our steps. I forgot I had these steps here to remind myself. We created a space, tick. We created a basic interface, tick. Okay we got git setup, we got conda set up, or mamba. So mamba and conda are the same thing, mamba is just a much faster version and we'll keep some notes on the course website because at the moment they're actually working on including the speed ups from mamba into conda. So at some point maybe it'll be fine to use conda again. At the moment conda is way too slow, so don't use it.
[1:00:26] Okay we've done dogs versus cats no problem. Yeah so we could also look at pet breeds (well yeah we'll briefly look at that.) Okay, we've used an exported learner, no problem. We’ve used nbdev, no problem. Oh! okay… try the api… alright this is interesting. So I think we can all agree, hopefully, that this is pretty cool that we can provide to anybody who wants to use it, for free, a real working model, and you know, with gradio there's actually, you know, a reasonable amount of flexibility, around like how you can make your website look, you know, using these various different widgets.
[1:01:22] It's not amazingly flexible, but it's flexible enough to kind of… it's really just for prototyping, right. So gradio has a lots of widgets and things that you can use. The other main platform at the moment that HuggingFace Spaces supports is called streamlit. streamlit is more flexible, I would say, than gradio - not quite as easy to get started with but you know, it's kind of that nice in between, I guess, so also a very good thing you know again mainly for, kind of, building prototypes. But at some point you're going to want to build more than a prototype. You want to build an app, right, and one of the things I really like about gradio in HuggingFace Spaces is… there's a button down here “view the api”. So we can actually create any app we want and the key point is that the thing
[1:02:27] that does the actual model predictions for us is going to be handled by HuggingFace Spaces Gradio, right, and then we can write a Javascript application that then talks to that. Now there's going to be two reactions here … Anybody who's done some front-end engineering is going to be like, “oh great, I can now literally create anything in the world, because I just write any code and I can do it and they'll be excited.” And a lot of data scientists might be going “uh oh… I have no idea how to use javascript, it's not in my, you know, inventory.”
[1:03:07] So this is again where I'm going to say: look, don't be too afraid of Javascript. I mean obviously one option here is just to kind of say, “hey I've got a model” and throw it over to the wall to your mate who does know Javascript and say please create a Javascript interface for me. But let me just give you a sense of how really not hard this actually is. So there's a… end point. There's now a url that's running with our model on it and if you pass it some data… an image… some image data… to this… to this url, it's going to return back the dictionary. So it's going to do exactly the same thing that this UI does but as an api, as a function we can call.
[1:03:58] And so it's got like, examples here of how to call it… so for example, i can actually… let me show you the api as an example using that minimal interface we had … because it's just going to be a bit simpler. So if I click curl and I copy that… copy that… and paste. So you can see there … (oh that's not a great example passing in hello world) …if I pass in Tanishq again, let's see how I'm going with his name Tanishq.
[1:04:37] Here you can see it returns back “Hello Tanishq!” So this is how these apis work, right. So we can use javascript to call the api and we've got some examples. So I've created a website, and here is my website “tinypets” and on this website as you can see it's not the most amazingly beautiful thing but it's a website. It's a start right, and up here I've got some examples, here we go, single file, click, choose file, click… and in this example I'm actually doing full pet classification. So I actually trained a model to classify breed, which we'll talk about more next week, rather than just dog versus cats. So let's pick a particular breed… and we run it…
[1:05:41] Oh! And there it is! Now not very amazing right, but the fact is that this is now a Javascript App means we have no restrictions about what we can do. And let's take a look at that HTML… that's it! It easily fits in a screen, right, and the basic steps are not crazy, right, it's basically… we create an import for our photo, we add an event listener that says when you change the photo call the read function, the read function says create a file reader, read the file and when you finished loading, call loaded, and then loaded says… fetch that… now that path there… is…
[1:06:39] that path there! Right! Except we're doing the full pets one. So this is basically just copied and pasted from their… from their sample. And then grab the JSON, and then grab from the data the first thing… the confidences… the label, and then set the HTML. So as you can see it's like (okay) if you haven't used javascript before these are all new things, right, but they're not… it's not harder than python, right, it's just… it's just another just another language to learn. And so from here you can start to build up, right!
[1:07:20] So for example we've created a multi-file version. So with the multi-file version, let me show you… multi-file… choose… so we can now click a few… so we've got a newfie, a ragdoll, a basset hound, and some kind of cat. I'm not much of a cat person. So we chose four files and bang they've all been classified! Apparently it's a Bengal! (I wouldn't know) Here's our newfoundland.
[1:07:52] So there's the multifile version, and if you look at the code, it's not much more, right, it's now just doing… it's getting all the files and mapping them to read, and now appending each one, so not much more code at all. And as you might have seen on our site here, there's a few more examples, which is some of the community during the week, has created their own versions. So this one here is… I think this is… the yeah… this is from one of the gradio guys... they called it “get to know your pet.” So if i choose a pet… I kind of… I really like this because it actually combines two models. So first of all it says, oh! It's a basset hound, and then it lets me type in and ask things about it. So i can see, “oh, what kind of tail does it have?...” search, and so that's now going to call an NLP model,
[1:08:56] which asks about this… it's a curved saber tail. There we go! “What maintenance does it need?” So again, like here you can kind of see how (oh) a basset hound’s ears must be cleaned inside and out frequently. So this is like combining models.So you can see this is something that you couldn't do with just a kind of a… a ready to go interface. And so the next thing i wanted to point out is how did we create the website? I showed you how to create an HTML file, but like how do you create those, and how do you make a website out of them? Well watch this! Let's… here's…. here's….
[1:09:52] here's the source code to our most basic version, okay! So i could just save this… there we go… Okay so we could open that with Visual Studio Code… and… what we could actually do is, we could… here it just is in explorer or mac & finder. I could just double click on it and here it is! It's a working app!!
[1:10:32] So you can see i don't need any software installed on my computer to use a Javascript App, right! It's a single file… I just run it in a browser… a browser is our complete execution environment. It's a got a debugger. It's got the whole thing. So here you can (yeah) here you can see it's… it's just calling out to this external hacking faces endpoint, so I can do it all sitting here on my computer. So once I've got my HTML file that's working fine on my computer, in VSCode, how do I then put it on the web, so that other people can use it?
[1:11:06] Again, the whole thing's free. There's a really cool thing called github pages, which basically will host your website for you. And because it's just Javascript, it'll… it'll all work just fine! The easiest way to create a github pages site in my opinion, is to use something called fastpages, which is a fast.ai thing! And… basically all you do… is you follow the setup process.
[1:11:43] So first it does it's… let's just go through it… so it says generate a copy by clicking on this link. So I click the link, all right, okay, give it a name… (I try to make everything public! I always think it's good good practice… you don't have to) …create repo, generating, okay, and then there's basically two more steps, takes about five minutes. We don't have five minutes, so I'll show you the one that I've already built, which is “fast.ai/tinypets” And so once it's done you'll basically end up with this empty site which, again, you just go “code” “open with github desktop” or “open with visual studio” whatever… so “open with github desktop”...
[1:12:37] or you can copy & paste this to your terminal… and so any one of those is going to get you this whole thing on your computer. You can save your HTML files there… push it back up to github. And what you'll find is we… we've… FastPages will show you the link to the website that is created for you. Now, the website that's created for you, you can make it look however you want using something called a “Theme”. So you'll see it's created a file called config.yml where you can pick a theme. So, in this case, I picked a theme called “alembic” for no particular reason.
[1:13:18] So Github Pages uses something called “Jekyll”, and so any “Jekyll” theme will, will basically work. So I picked out this theme, and so, as a result, when I now save things into this repo, they will automatically appear in this website, and the files automatically appear up here in this list. So, if you look at my index, that's the home page… the entire file is just this, right! The only slightly weird thing is at the top of every Github Pages file, you have to have three dashes, title, and layout, and three dashes. It's called “front meta”. And so, once you do that… and save it, it will appear in your website. So, something else I did then, I was like, okay, well… that's all very well that fast.ai has created this website, but I don't really like what it looks like. I want to create a different version.
[1:14:18] No worries. You can go to “fast.ai/tinypets” and click “Fork”. And, when you click “Fork”, it's going to create your own copy. So I did that under my personal account which is jph00. And look! Now I've got my own version of it, and now I can make changes here. So I made a few changes. One change I made was: I went to config.yml and I changed the theme… to “pages-themes/hacker”. So once you fork, one thing you have to do, which normally FastPages does for you, is you do have to go to “Settings”, and click “Pages”, and actually enable “Github Pages”. So you basically have to, by default, it's turned off, so here you'll just have to turn it on. So use the “Master Branch Root Save” and then it'll say, “No worries, it's ready to be published”. And so I changed the config.yml file to point at a different theme. And so, if you look at now… the jph's Tiny Pets…
[1:15:17] it's different! Okay? So it's got the same info, but it's much more “hackerish” because jph00 is a serious hacker, as you can tell from his website. So, anyway, look, it's a very brief taste of this kind of world of Javascript and websites and so forth, but I wanted to give you a sense of, like, you know, you don't need any money. You don't need any IDEs. You know, you don't really need much code to get started with writing your own web apps, and, thanks to hugging face spaces, you know, they'll even host your model for you and all you need to do is just have the magic string, as a thing to call. Okay!
[1:16:07] So… signing out: Hacker Jeremy Howard. Thanks very much for watching, and, in the next lesson, we're going to be digging into some natural language processing. We're going to be doing some of the same stuff, but we're going to be doing it with language rather than pictures. And we're going to be diving under the hood to see how these models actually work. We're going to learn about things like stochastic gradient descent, and we might even be having to brush off a little bit of calculus. I hope I haven't put you off by saying the “C” word.
[1:16:38] I will see you next time! Thanks all! Bye!
在 Vidleaf 工作台中開啟
搜尋逐字稿、選取行、複製帶時間戳的引文、翻譯。
署名
「課程 2:給程式設計師的實用深度學習 2022」由 Jeremy Howard(https://www.youtube.com/@howardjeremyp)製作,以 CC BY 4.0(https://creativecommons.org/licenses/by/4.0/)授權。來源影片:https://www.youtube.com/watch?v=F4tvM4Vb3A0。本頁是該影片的文字轉錄,並加入了段落分隔與時間戳記;創作者與 Vidleaf 無隸屬關係,也不為其背書。Vidleaf 加入了 AI 標點與 AI 生成的摘要;原始措辭仍可於本頁取得。
你是創作者或權利持有人嗎?請求更正或移除:copyright@vidleaf.app(參見 關於這些頁面)。
最後更新