コーダーのための実践的深層学習2022、レッスン2 — ジェレミー・ハワード
このページのタイトル、字幕、要約、読書メモは保存済みの AI 翻訳です。誤りが含まれる場合があります。タイムスタンプは原文と同じです。原文の字幕を開き、元の動画と照合できます。
ジェレミー・ハワードが、ノートブック、ローカルおよびKaggleのGPU、コーダー向けのfastaiワークフローを用いて、実践的な深層学習モデルのトレーニングとデプロイを解説します。
文字起こしの出典:YouTubeの手動字幕。VidleafによるAI句読点付与。元の文言とタイムスタンプのアンカーは保持されています。元の文字起こしは以下でご覧いただけます。
AI生成の要約
このレッスンは、大学の部屋予約の問題により自宅から収録され、コースの運営と学生プロジェクトを扱った後、モデルを本番環境に導入する話に移ります。無料の書籍はfastbookリポジトリまたはcourse.fast.aiで入手でき、各章はクイズで終わり、forums.fast.aiにはレッスンごとの公式トピックがあります。初期のプロジェクトには、損傷した車の分類器、ひげ検出器、本番環境で稼働する食品分類器がありました。本番環境向けに、ノートブックはBing APIにSDKキーが必要なためbingをddgに置き換えています。講演者はデータをクリーニングする前にトレーニングすることを提案し、リサイズの選択肢とデータ拡張としての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] 公式トピック投稿の下部にある「このトピックを要約」ボタンは、SanyamやTanishqによるものなど、最も高く評価された返信のみを表示します。
「作ったものを見せてください」からの学生プロジェクト
- [4:14] 「作ったものを見せてください」の投稿にはすでに39件をはるかに超える返信があり、最初に投稿されたプロジェクトが共有のために選ばれました。どれも非常に優れていたためです。
- [4:49] 最初の投稿は損傷した車の分類器でした。その作成者であるMattは、まだ理解していないコードを実行するのは不快に感じると述べ、講師はそれを始める正しい方法だと支持しました。
- [5:20] 他の初期の投稿には、毎週の実験を説明するfast pagesのブログ投稿や、素晴らしいひげ検出器がありました。これは「bird」から一文字を二文字に変えるだけで簡単にたどり着けます。
- [5:51] 別のプロジェクトは、食べ物を分類するための実際に稼働中のWebアプリ全体で、追加課題として説明されました。そのスレッドは80件の返信に達したと報告され、Sanyamの功績とされています。
Webアプリを本番環境にデプロイする
モデルから本番環境のWebアプリへ
- [6:20] 講演者は、いつか視聴者が自分自身のWebアプリを作り、それを本番環境に置くかもしれないと示唆しています。
- [6:27] 講演者は「いつか」とは今日のことだと明確にし、今すぐその方法を実演しようと申し出ます。
- [6:40] Suvashが投稿したトピックは、今日選ばれたトピックとまさに同じであるため、幸運な偶然だと説明されています。
モデルを本番環境に置く最初のステップ
データクリーニングとJupyterノートブックのナビゲーション
問題定義からデータクリーニングへ
- [7:00] 問題を定義し、データを見つけて収集した後、次のステップはデータクリーニングです。
- [7:14] コースから、またはローカルクローンからColabで開ける本の第2章では、データクリーニングを扱っています。
- [7:35] course.fast.aiではノートブックの実行方法を説明しており、第2章は本番環境への配置についてです。
- [7:46] セルはShift + Enterを押すことで実行されます。
Jupyterノートブックの拡張機能と目次
- [8:20] ナビゲートメニューはデフォルトでは表示されません。これはpip経由でJupyter Notebook Extensionsをインストールし、Jupyterを再起動することで表示されます。
- [8:45] Colabにはすでに目次があるため、この拡張機能は主にローカル環境向けで、そこではNbextensionsタブが表示されます。
- [8:57] Nbextensionsの目次(2)をクリックすると、便利なナビゲーションバーが表示されます。
折りたたみ可能な見出しとキーボードナビゲーション
- [9:07] 折りたたみ見出し拡張機能は、セクションを閉じたり開いたりするためのコントロールを追加します。
- [9:20] 右矢印キーでセクションの末尾へ、左矢印キーで先頭へ移動できるため、セクション間を簡単に移動できます。
- [9:35] もう一度左を押すとセクションが閉じ、右を押すと開きます。
画像検索用のBing API
- [9:48] この本では画像検索にBing APIを使用しています。
BingをDDGに置き換える方法とPythonドキュメントのヒント
ノートブックでBingからDDGへの切り替え
- [9:58] Bing APIはSDKキーの取得が必要なため、ノートブックでは「bing」を「ddg」に置き換えています。
- [10:05] 講演者は、深層学習で最も難しいのはBing Azureのウェブサイトを理解して設定することだと冗談を言い、ddgではそれが不要だと述べています。
- [10:21] ノートブックはコースのウェブサイトのフォーラムで共有され、実際の変更点はbingをddgに置き換えてキーを削除するだけです。
先週から再利用されているクマ検索の例
- [10:33] 本と同様に、この例ではグリズリーベア、クロクマ、テディベアを検索します。これは講演者の当時幼児だったお子さんがテディベアを識別することに関心を持っていたことがきっかけです。
- [10:56] ループはグリズリーベア、クロクマ、テディベアを順に処理し、先週と同様に失敗したものは破棄します。
Python、fastai、PyTorchの情報を見つける方法
- [11:14] フォーラムでよくある質問は、Python、fastai、PyTorchに関する情報をどこで見つけるかというもので、この本ではいくつかのヒントを提供しています。
- [11:24] 関数名の後に疑問符を二つ付けるとソースコード全体が表示され、一つ付けると簡単な情報が表示されます。
- [11:48] nbdevがインストールされている場合、docと入力するとドキュメントへのリンクが直接表示されます。通常は例とソースコードへのリンクが含まれています。
- [12:13] GitHubのソースコードでは、項目をクリックするとその定義にジャンプできるため、コードをあちこち見て理解するのに便利な方法です。
クリーニングと画像拡張の前のトレーニング
データをクリーニングする前にモデルをトレーニングする
- [12:48] まずデータをクリーニングするという一般的なアドバイスとは逆に、講演者はデータをクリーニングする前にモデルをトレーニングすることを提案し、その理由をまもなく説明します。
- [13:10] DataBlockを使用してトレーニング用のデータローダーを取得し、これまでと同様にshow_batchでバッチを表示します。
サイズ変更のオプション:squish、crop、pad
- [13:37] サイズ変更は文字列またはenum風のオプションで指定でき、squishを使うと画像全体を表示したまま非常に細長いクマが生成されることがあります。
- [14:12] cropは画像の中央のみを保持してアスペクト比を改善しますが一部の内容が失われ、ゼロ(黒)でpadすると画像全体と正しいアスペクト比が保持されます。
- [14:33] サイズ変更の選択によって得られるモデルの品質は異なることがありますが、通常その差は大きくないため、あまり気にする必要はありません。
RandomResizedCropとデータ拡張
- [14:49] RandomResizedCropは毎回画像の異なる部分を切り出すため、非常に大きなスキャンで多数のエポックを実行するのに便利です。
- [15:11] フォーラムで、非常に大きな文書スキャン内のフランス語とドイツ語のテキストを区別したいという要望があり、提案されたRandomResizedCropのアプローチはわずかに効果がありました。
- [15:51] 同じ画像から毎回異なる画像を得ることをデータ拡張と呼び、aug_transformsは回転、squish、warp、色変更、彩度変更を適用します。
- [16:32] およそ五〜十エポックを超えて学習する場合は、RandomResizedCropとaug_transformsが一般に推奨され、multは2に設定するのではなく空のままにします。
拡張はメモリ内で行われ、コピーは作成されない
- [17:03] Alexの質問に答えて、話者は画像が複数回コピーされるわけではないと説明します。各エポックで全ての画像を読み込み、学習中にリアルタイムでRAM内でwarp、crop、色変更を行います。
- [17:32] コンピュータ上にコピーは保存されませんが、モデルは事実上ほぼ無限にわずかに異なるコピーを見ることになります。
四つのファインチューニングエポックでの学習実行
より多くのエポックで長く学習する
追加エポックの実行
- [18:24] より多くのエポックを実行することは良いアプローチと説明され、結果は三パーセント未満と報告されています。
クマ分類器の学習と混同行列の確認
4GBのラップトップGPUでの学習
カテゴリラベルの混同行列
- [18:59] 混同行列はラベルがカテゴリである場合にのみ意味を持ち、モデルがどのカテゴリの誤りを犯すかを示します。
- [19:15] この時点で、モデルは実際のグリズリーベアをクロクマと二度誤認し、実際のクロクマをグリズリーベアと二度誤認しました。
- [19:27] モデルはテディを一度も間違えませんでした。テディはどちらのクマの種類ともかなり異なって見えるので、これは理にかなっています。
混同行列を使って難しいクラスを見つける
- [19:39] 混同行列は、どのケースが難しいかを実感させてくれることがよくあります。
- [19:43] 書籍とコースで使われているペットのデータセットでは、異なる品種の混同行列が、どの品種の識別が難しいかを示します。
- [20:00] 話者は、これらの種類の識別に関するWikipediaのページやペット繁殖の報告書を読みました。専門家でも難しいほど、これらは識別が困難だからです。
- [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しかない場合、メモリを解放するためにノートブックを閉じて停止することが非常に重要です。
- [25:37] 通常のRAMがいっぱいになると、コンピュータはRAMをハードディスクにスワップしますが、GPUはスワップできません。
- [26:00] GPUのRAMが不足するとそこで終わりなので、使っていないノートブックを閉じ、GPU上では一度に一つのものだけを使ってメモリ不足を避けましょう。
コーディング動画の推奨視聴方法
HuggingFace SpacesとGradioでモデルをデプロイする
デプロイの選択肢: VoilàとHuggingFace Spaces
- [27:18] データをクリーンアップした後、本書ではVoilàを使ってモデルを本番環境に導入しており、話者はそれをかなり良いと考えています。
- [27:29] 話者は、今日ではほとんどの人にとってHuggingFace Spacesの方が便利だと考えており、今日はそれと一緒に使えるツールの一つであるGradioに焦点を当てます。
Tanishq Abrahamのブログ記事を教材として使用
- [27:42] 本書にはこの章はありませんが、コースTAのTanishq Abrahamが今日のレッスンのすべてを網羅した包括的なブログ記事を書いており、フォーラムとコースページからリンクされています。
- [28:07] 講演者はTanishqをfastaiコミュニティで最も助けになり、粘り強く、思慮深く、忍耐強い人々の一人として称賛し、コナンと一緒にいる神童時代の写真を共有します。
HuggingFace Spaceの作成
SpacesでGitとGitHub Desktopを使用する
Spacesの背後にある仕組みとしてのGit
- [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では、Windowsが完全なLinux環境を実行できるため、発表者はUbuntuの使用を勧めています。
- [32:15] WSLのインストールは、WSL installを検索し、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インターフェースがあり、「Hello {name}」を返すgreet()関数を実行します。
- [36:40] Tanishq という名前でテストすると、入力に対して生成される出力が表示されます。
- [36:53] このアプリはあまり刺激的ではありませんが、今では本番環境で動作するアプリになっています。
深層学習モデルへの次のステップ
- [37:00] 次のステップは、このシンプルなアプリを本番環境で動作する深層学習モデルに変えることです。
fastai のペット分類器の学習とエクスポート
Kaggle で犬/猫分類器を構築する
- [37:10] まず深層学習モデルを入手する必要があり、基本的には学習させる必要があります。
- [37:29] Kaggle と Colab の例は同じことを行います。犬か猫の分類器を作成します。
- [37:52] Kaggle にはすでに fastai がインストールされていますが、最新バージョンが使われるようにノートブックが最初にインストールします。
- [38:03] ペットのデータセットは、ImageDataLoaders 用の is_cat ラベリング関数で読み込まれ、DataBlocks を行うための略記法として説明されています。
- [38:13] 学習器が作成され、以前に見た手順を使ってファインチューニングされます。
Kaggle の編集ビューとリーダービュー
- [38:22] すべての Kaggle ノートブックには編集ビューとリーダービューがあり、共有されたノートブックは誰でもリーダービューで読むことができます。
- [38:42] 学習には約五分かかりました。Kaggle の GPU は最新の GPU より少し遅いですが、それでも十分に高速です。
学習済みモデルのエクスポート
- [38:56] 最後に、learn.export に名前を付けて、学習済みモデルを含むファイルを作成します。
- [39:07] このファイルの作成だけが GPU を必要とする作業なので、Kaggle または Colab で行います。
Colab でワークフローを繰り返す
- [39:12] Colab 版も同じ手順を使います。pip install、is_cat、データ、ImageDataLoaders、おまけの show_batch、そして学習器の作成とエクスポートです。
Kaggle モデルを HuggingFace Spaces にエクスポートする
Kaggle から保存済みモデルをダウンロードする
- [39:36] Kaggle ノートブックを実行して保存すると、保存されたファイルがデータタブに表示されます。
- [39:47] 保存されたファイル model.pickle は、データタブからダウンロードフォルダにダウンロードできます。
モデルを Spaces アプリのディレクトリに移動する
- [39:57] ダウンロードしたモデルは、HuggingFace Spaces アプリと同じディレクトリにコピーする必要があります。
- [40:13] ターミナルで、Mac では「open .」、Windows では「explorer.exe .」と入力すると、そのディレクトリで Finder またはエクスプローラーが開くので、そこにファイルを貼り付けられます。
Linux ファイルにアクセスするための Windows のヒント
- [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 がそれらを参照するため含める必要がありますが、learner はそのソースコードを保存しません。
- [42:43] learner をトレーニングする代わりに、保存されたファイル名を指定して load_learner() を呼び出すと、トレーニング終了時と同一の learner が返されます。
ピクル化された learner が復元される仕組み
- [43:03] トレーニング済みの learner はピクルファイルとしてその時点で凍結され、ディスクに保存され、コンピュータに転送された後、読み込まれて解凍されました。
- [43:23] ピクルを解凍した learner はあらゆる用途に使え、そのメソッドの一つに .predict メソッドがあります。
予測の速度と出力形式
Gradioの犬猫分類インターフェースを構築する
Gradio用にpredictの出力を調整する
- [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画像は通常のおよそ50-50ではなく100を返すようになり、話者はそれを残念だと言っています。
- [47:13] 話者の六歳の娘がGoogle画像検索で犬と猫が混ざった写真を見つけ、以前のモデルはそれにほぼ正確に50-50を与えました。
- [47:53] その日に再トレーニングされたモデルは今では猫だと確信しており、おそらくわずかに異なるトレーニングスケジュールか追加のエポックによるもので、話者はそんなに長くトレーニングすべきではなかったと言っています。
インターフェースを停止して次のステップへ
ノートブックを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によるデプロイと本番環境でのテスト
- [50:08] app.pyはGitにプッシュすることでGradioにアップロードされます。Visual Studio Code経由か、「git commit」の後に「git push」と入力する方法があります。
- [50:34] プッシュする前は、実行中のアプリがまだ以前のモデルを表示している場合があり、話者は犬と猫の例でそれを示しています。
- [50:51] 公開設定にすると、誰でもアプリにアクセスして画像をアップロードし、モデルを確認できます。
- [51:02] モデルは犬と猫を正しく識別しますが、話者は一、二エポック少なく学習したため確信度が下がったと考えており、犬と猫の画像は依然として間違いなく猫と分類されます。
学習時間、モデルのエクスポート、GPUの使用
エポック数の選択
- [52:14] 学習が進むにつれてエラー率は改善するため、エポック数を増やすべきかが問題になります。
- [52:26] 現在の実行では、開始するために三エポックに一を加えた数を使用しており、それで十分かどうかはユーザー次第です。
- [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 Spacesで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] 基本的な手順は、写真のインポートを作成し、写真が変更されたときに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にマップし、各結果を追加します。
二つのモデルを組み合わせたコミュニティの例
- [1:08:14] このサイトには、その週にコミュニティが作成したより多くの例へのリンクがあり、その中にはgradioの関係者の一人による「get to know your pet」というものもあります。
- [1:08:34] この例は二つのモデルを組み合わせています。まずペットをバセットハウンドと識別し、次にユーザーがそれについて質問を入力できるようにします。
- [1:08:45] どんな尾を持っているかなどの質問はNLPモデルを呼び出し、曲がったサーベルテールだと答えました。
- [1:09:11] どんな手入れが必要か尋ねると、アプリはバセットハウンドの耳は頻繁に内側も外側も掃除しなければならないと返しました。
- [1:09:17] 話者はこれを、すぐに使えるインターフェースではできなかった方法でモデルを組み合わせていると強調しています。
シンプルな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サイトを作成する最も簡単な方法は、話者の意見では、fast.aiプロジェクトのFastPagesです。
- [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ファイルの先頭には「フロントメタ」が必要です。三つのダッシュ、タイトル、レイアウト、そして三つのダッシュです。
サイトのフォークとカスタマイズ
- [1:14:18] 別のバージョンを作るには、fast.ai/tinypetsにアクセスしてForkをクリックし、自分のコピーを作成します。話者はアカウントjph00でこれを行いました。
- [1:14:38] 話者はconfig.ymlのテーマを「pages-themes/hacker」に変更しました。
- [1:14:47] フォークした後は、設定に移動し、Pagesをクリックして、デフォルトでオフになっているGitHub Pagesを「Master Branch Root Save」で有効にする必要があります。
- [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リポジトリでノートブックを見られますし、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リポジトリでノートブックを見られますし、course.fast.aiからも読めます——たとえばColab経由などで。
[1:28] それから本のことも覚えておいてください…つまり…本にはコースで扱わなかったこともたくさん載っています。たとえば、私がかなり面白いと思うニューラルネットワークの歴史についてなどで、その中にはここで読むとわかるのですが、実に興味深い個人的な物語もあります。そして各章の終わりにはクイズがあります。動画を見る前にクイズを読んでおくのは悪くないアイデアです。たとえば第二章のクイズを読んでから戻ってくる、というのは悪くないでしょう。そして動画を見て、本の第二章を読んだ後に、クイズが解けることを確認してください。
[2:07] 先週お伝えしなかったことなのですが、先週紹介したRadekが作ったとてもクールなものがあり、aiquizzes.comという、この本に関するクイズ専用のサイトです。反復学習の手法を使って、決して忘れないようにしています。ぜひaiquizzes.comをチェックしてみてください。すべて新しい問題で、本に載っているものとは異なり、とても丁寧に厳選してまとめられています。ですのでaiquizzes.comもぜひ見てみてください。覚えておいてほしいのは、course.fast.aiだけでなく、forums.fast.aiもあるということです。course.fast.aiは、ノートブックやKaggleのリンクなど、そういったものへのリンクを入手するために行く場所です。forums.fast.aiでは、各レッスンに公式トピックがあり、必要な情報がすべて載っています。一般的にはフォーラムの方がもう少し詳しい情報があり、コースは
[3:09]は無駄がなくて効率的で、フォーラムはもう少し詳しいです。ですから、見つけたら — この場合はレッスン二の公式トピックを探すことになりますが、ここにこれまでのレッスン一の公式トピックがあります。このレッスン一の公式トピックから、私が収録してからほんの数日で — コースはまだ公開していないので、ライブで参加している人たちだけですが — すでにたくさんの返信があり、かなり圧倒されるかもしれません。
[3:40] 私の投稿の下部に「このトピックを要約する」というボタンがあることに注意してください。それを押すと、最も高評価の返信だけが表示され、主要な内容を押さえるのにとても良い方法です。ボタンはこちらで、押した後の表示はこんな感じです。fast.aiのSanyamやTanishqのような伝説的な人たちの高評価の投稿だけが表示されます。フォーラムを使うのに役立つ方法だと思います。
[4:14] 今週のクールなことの一つは、約束通り「作ったものを見せてください」という投稿を上げたことで、すでにたくさんの人が投稿しています。このスクリーンショットは数日前に撮ったもので、覚えている限りではすでに39件以上の返信をはるかに超えています。どれもとても良いので、どれを共有するか決めるのにかなり苦労しました。なので、簡単な方法を選んで、最初のものだけを選ぶことにしました。最初に投稿されたものだけをお見せします。どれもとても良いからです。最初の、一番最初に投稿されたのは、損傷した車の分類器です…うまくいったようです。そして、作成者のMattがこれについて言ったことがとても気に入りました。つまり…「わあ、このコードを実行するのは少し不安で、まだよく理解していないけれど、とにかくやっている」と。それで私は、Mattよくやったね、とにかく…
[5:14]、ただやってみただけです。それが始める方法です。すべて理解できるようになりますから、心配しないでください。次に投稿されたのが、実はfast pagesのブログ記事だったのを見るのはとても嬉しいです。— とてもいいですね。— 見てみると、週の間に彼らが行ったいくつかの実験や、何を見つけたかなどについて説明しています。次は驚くべきひげ検出器で、私の理解が正しければ、主に鳥からひげへと一文字を二文字に変えるだけでとても簡単にできるからで、これはひげを生やした紳士を見つけるのに非常に良い仕事をしています。とてもいいですね。そして次はこれです。…
[5:51]、また一段上がって、食べ物を分類する本番環境のウェブアプリで、一種の追加課題のようなものです。どうやらそのスレッドには今80件の返信があるようです、Sanyamありがとう、とてもクールです。ですから、ご存知の通り、明らかに… これは実は、もう何年もこのコースをやっているSuvashが作ったものです。ですから、いつかあなたも自分だけのウェブアプリを作って本番環境に置けるようになるかもしれません。そして「いつか」と言いましたが、より具体的には…今日です!実は今からその方法をお見せします!Suvashがこれを投稿してくれたのは本当に幸運な偶然で、まさに今日取り上げるトピックだからです。
[6:46] では、どうやってモデルを本番環境に置くのでしょうか?ステップ一は…そうですね…ステップ一はもうやったようなものですよね?ステップ一は…ステップ一、二、三、四…は、どんな問題を解決したいのかを明確にし、そのためのデータをどう見つけるかを考え、データを集めに行く、などです。では、データを手に入れた後の最初のステップは何でしょう?次のステップはデータクリーニングです。本の第2章を開くと、今から開きますが…
[7:23] では本はこちらです…コースから直接Colabで開くこともできますし、コンピュータにクローンした場合はそこでできます。思い出してください、course.fast.aiでこれらのノートブックの実行方法が正確に説明されています。第2章は本番環境に置くことについてで、こちらが第2章です。よろしいですか、セルを実行するにはShift + Enterを押すことを覚えておいてください。では、データのクリーニングを始める部分に進みます。
[7:59] では「Navigate」をクリックして、ここを下に進みます…データの収集…ありました!まずは簡単に復習しましょう。ところで、Jupyterノートブックをこんなに素早く簡単に操作するためのちょっとしたコツは何かとよく聞かれます。とても便利なものの一つが、ご覧の通りこのナビゲートメニューですが、実はデフォルトでは表示されません。Jupyter Notebook Extensionsというものをインストールすると…Jupyter…
[8:30] Notebook… 拡張機能… そしてそれを「pip install」して、指示に従ってからJupyterを再起動するだけです。もちろんこれは… ちなみにColabにはすでに目次があるので、これは例えばローカルで使っている場合の話で、ここにこの「Nbextensions」というものが表示され、「Table of Contents (2)」をクリックすると、この便利なナビゲーションバーが表示されます。もう一つ私がとても気に入っているのは、ここにある「collapsible headings」というもので、これがあるとここにあるような、閉じたり開いたりできる素敵な小さなものが使えます。そして実はそれだけが最高の部分ではありません。私にとって最高なのは、右矢印を押すとセクションの終わりに移動し、左矢印を押すとセクションの始まりに移動できることです。つまり、セクション間を移動したいときは、上左、下右、下右と押すだけで、とても便利です。
[9:35] そしてここでまた左を押すと閉じます。ここでまた右を押すと開きます。これがcollapsible headingsです。とにかく、いくつか本当に便利なものがあり、今日はノートブックの設定についてもっとたくさんお話しします。さて、気づくことの一つは、本では画像検索にBing APIを使っていることです。私はすでに「bing」を「ddg」に置き換えました。なぜなら、Bing APIはSDKキーを取得する必要があり、正直なところ、ディープラーニングで最も難しいことの一つは、Bing Azureのウェブサイトを理解してそれを整理することだからです。ddgは違います!なので基本的にまったく同じです…(そして…)
[10:21] このノートブックもコースのウェブサイトのフォーラムで共有しますが、私が基本的にやったのはbingをddgに置き換え、キーを削除しただけです。それで、先週やったのと同じように、物を検索できます。本ではクマ検出器を作りました。なぜなら、書いた当時、私の幼い子どもがテディベアを識別するのを手伝うことに非常に興味を持っていて、誤ってハイイログマを抱きしめてほしくないと強く思ったからです。そこで先週と同じように、ここでハイイログマを検索する方法を示します。
[10:56] 先週と同じように、ハイイログマ、ツキノワグマ、テディベアをループするものです。先週と同じように、失敗したものを取り除きます。そしてフォーラムで何人かが尋ねたことの一つは、基本的に任意のPythonやfastaiやPyTorchのものについて、もっと情報を見つけるにはどうすればいいか、ということです。本にはいくつかのヒントがあります。一つは、任意の関数名の横に二重の疑問符を付けると、その完全なソースコードが得られるということです。同じように、一つの疑問符を付けると、簡単な…
[11:41] ご存じのとおり…少しだけ情報があります。nbdevがインストールされている場合(必要なのはnbdevだと思います)、docと入力すると、おそらく最も重要なこととして、ドキュメントへのリンクが直接表示されます。そこでより詳しい情報を見つけることができ、通常は例も用意されています。また、必要であればソースコードへのリンクもあります。…
[12:13](これをコントロールでやってみましょう、いいですね)…ソースコードへのリンクがあり、そのようにして飛び回ることができます。そしてGitHubのソースコードでは、項目をクリックしてその定義にジャンプできることに気づくでしょう。つまり、何が起きているのかを正確に理解するためにあちこち見て回る、なかなか良い方法です。はい、助けを得る素晴らしい方法がたくさんあります。しかし私がお約束したのは、これからデータをクリーンアップするということです。そこで、皆さんがとても驚くかもしれないことをお伝えします。データをクリーンアップする前に、モデルを訓練するのです。
[12:55] さて、これは皆さんがおそらく何千回も聞いたことのある話とはまったく逆に聞こえるでしょう。つまり、まず…(構築する/あなたが/訓練する)…データをクリーンアップしてから、モデルを訓練するという話です。しかし、とても驚くべきことをお見せします。まずモデルを訓練します。その理由はすぐにわかります。モデルを訓練するには、以前と同じようにDataBlockを使ってデータローダーを取得します。ここで何が起きているかについては、この本にたくさんの情報があります。はい、できました…そして通常どおり、show_batchを呼び出してそれらを見ることができます。
[13:33] この本にはここに小さなサイドバーがあり、簡単に触れておくと、サイズ変更のさまざまな方法についてです。先週も簡単に触れたと思います…squish(押しつぶし)ができます…先週は文字列を使いましたが、文字列でも、このような列挙型のようなものを使うこともできます。squishを使うと、とても細いクマになってしまうことがあるのがわかりますね。これがクマの実際のサイズ、形です。ここでは細くなってしまいましたが、わかるように、今度はその子どものクマたちがすべて入っています…(cubsと呼ばれますよね?ええ、bear cubsです)…つまり、全体像が見えるように押しつぶしたのです。ここも同じです。これは画像からはみ出していたので、押しつぶしました。そしてこのクマは奇妙に細く見えますが、全体を見ることができます。これがsquish(押しつぶし)です!
[14:12] あるいは、こちらのこれはクロッピング(切り抜き)です。画像の中央だけを切り抜いているので、より良いアスペクト比が得られますが、一部の情報は失われます。これは正方形の画像を得るためです。そしてもう一つの方法は「pad」(パディング)を使うことです。パディングにはさまざまなものを使えます。ゼロ(黒)でパディングすると、ここでわかるように、画像全体と正しいアスペクト比が得られます。これがもう一つの方法で、ご存じのとおり、状況によって異なる品質のモデルが得られます。試してみることができますが、通常はそれほど大きな違いは生じません。
[14:44] なので、あまり気にしなくて大丈夫です。でも、とても面白いものを一つ紹介しますね。それは RandomResizedCrop です。つまり、resize と言う代わりに RandomResizedCrop と言えるわけです。そうすると、毎回画像の違う部分が得られるのがわかります。それで、今週…今週、誰かがフォーラムで質問していました…私は…(これは本当に面白いアイデアで、実際に少しうまくいったのですが、)フランス語とドイツ語のテキストの写真を認識したいとのことでした。明らかにこれは普通のやり方ではありませんが、ちょっとした実験として(私は実験が大好きなので、)とても大きな文書のスキャンがあって、画像を見るだけでフランス語かドイツ語かを判別したいそうで、写真が大きすぎると言っていました。どうすればいいですか?と。私は、RandomResizedCrop を使って、そうすれば画像の違う部分を切り取れると答えました。これはとても良い方法で、たくさんの
[15:44] エポックを回して毎回少しずつ違う画像を得られます。なので、これはとても良いテクニックです。そして、同じ画像から毎回違う画像を得るというこのアイデアは「データ拡張」と呼ばれます。繰り返しますが、データ拡張についてあまり詳しくは説明しません。本に載っていますから。ただここで簡単に指摘しておくと、「aug_transforms」、つまり拡張変換を使うと、ここではそれらを二倍にしています – とても大きくして見やすくしているのです。これらのテディベアが回転したり、押しつぶされたり、歪んだり、色が変わったり、彩度が変わったりして、すべての画像が違うものになっているのがわかります。一般的に言って、約五から十エポック以上訓練するなら、解くのが超簡単な問題でもない限りほとんどの場合そうしたいでしょうが、RandomResizedCrop とこれらの「aug_transforms」を使いたくなるでしょう。mult=2 は入れないでください。
[16:49] そこは空のままにしておいてください。見やすくするために置いているだけです。さて、観客のアレックスから面白い質問があります。「これは…これは画像を複数回コピーしているのですか?…こういうことを?…それともこういうこと?」という質問で、答えは「いいえ、画像をコピーしているわけではありません。何が起きるかというと、その画像は…各エポックで、すべての画像が読み込まれます。そしてここで起きているのは…ある意味…メモリ上、RAM 上で、その画像が「歪められて」いるのです、そうです、切り取られて色を変えられたりしているのです。つまり、モデル訓練中にリアルタイムで行われる処理なのです。なので、コンピュータにコピーが保存されることはありませんが、実質的にはほぼ無限に少しずつ違うコピーがあるようなもので、モデルは結局それを見ることになるのです。アレックス、そして皆さん、これで理解してもらえたら嬉しいです。
[17:52] というわけで、それは素晴らしい質問ですね!では、ええと、私たちは… RandomResizeCropを使い、データ拡張の変換を使います。そうすればそこからDataLoaderを取得でき、その後モデルのトレーニングに進めます。およそ一分かかります。このケースではファインチューニングを四エポックだけ行いました。なぜここに五があるのかはコースの後半で話しますが、メインのファインチューニングは四エポックです。なので、エポック数がとても少ないので、RandomResizedCropやaug_transformsは実際には必要なかったかもしれません。でも、もっとエポックを回したいなら、これは良いアプローチです。三パーセント未満のエラー!これは良いですね!では、モデルをトレーニングしてからクリーニングすると言ったのを覚えていますか?
[18:36] では、トレーニングを進めましょう。トレーニングしている間に… 私のラップトップで動いていて、4GBのGPUしかありません。かなり基本的なものですが、始めるには十分です。トレーニング中に、次のものを見てみましょう。まず最初に見るのは混同行列です。混同行列は、ラベルがカテゴリである場合にのみ意味を持ちますよね!
[19:09] そしてそれが示すのは、どのカテゴリのエラーをしているかです。これが示しているのは、この時点でのモデルが… 実際にはグリズリーベアだったのに、クロクマだと思ったのが二回あり、実際にはクロクマだったのに、グリズリーベアだと思ったのが二回あり、テディを間違えたことは一度もありませんでした。これは理にかなっていますよね。テディは両方とかなり見た目が違いますから。多くの状況でこれを見ると、なるほど、難しいのはどれだろう?という実感が得られますよね?例えば、本やコースでよく使うペットのデータセットを使うと、異なるペットの品種のこの分類[**]行列は、どれが識別しにくいかを本当に示してくれます。私は実際にウィキペディアのページや、ペットの繁殖レポートを読んで、これらの特定の種類をどう識別するかを調べました。なぜならそれらはとても難しく、専門家でも難しいと感じるからです。
[20:10] 実はこのコースを受講して学んだことの一つに…クロクマとハイイログマは、私が思っていたよりもずっと見分けるのが難しいということがあります…なので、私も見分けようとはしません!でも、このモデルでできる本当に面白いことをお見せしましょう。今、混同行列に使うこの分類解釈オブジェクトを作成したので、plot_top_losses を呼び出せます。plot top losses と言うことができ、これはとても興味深いです。これが行うのは、"loss" が最も高い場所を教えてくれることです。前回のレッスンを覚えていれば、loss とは、モデルがどれだけ優れているかの指標で、データの項目を処理するたびに記録するものです。予測が間違っていて、しかも自信満々であれば、loss は "悪い" ものになります。
[21:11] その予測についてです。ここに例があります。予測は…(ここの順序は、予測、実際、loss、確率です)…予測がハイイログマで、実際はクロクマだったのに、モデルはそれがハイイログマだと 96% 確信していたものです。さて、私はクマについて十分な知識がないので、モデルが間違えたのか、それともこれが本当にハイイログマの写真なのかは分かりません…でも、専門家なら明らかに戻って確認するでしょう…
[21:41] ですよね?さて、ここにいくつか気づくでしょう…ハイイログマ/ハイイログマ、テディ/テディ…これらは実際には正しいのです、よね?では、なぜこの loss は "悪い" のでしょうか?正しいのに?その理由は…あまり自信がなかったからです。確信度は 66% だけでした、よね?ここにテディがあります…確信度は 72% だけです、よね?つまり、間違っていて自信がある場合、または正しくて自信がない場合のどちらでも、悪い loss になり得るのです。これがとても役立つ理由は、fast.ai の ImageClassifierCleaner を使って、データセット内の誤ってラベル付けされたものを修正できるからです。
[22:24] そこで ImageClassifierCleaner を使うと、実際にモデルが実行されます。だから "learn" を渡すのです、よね?そして、私はクロクマとハイイログマについてはあまり知らないと言いましたが、テディベアについてはよく知っています。なのでテディベアを選びましょう。テディベアをクリックすると、トレーニングセット(トレーニングか検証を選べます)の中でテディベアとマークされたものがすべて表示されます。ここが本当に重要な点です。それらは loss 順、つまり確信度順に並んでいます、よね?だから最初のいくつかをスクロールして正しいか確認できます、よね?ああ、ここに間違いがあります、よね?誤って集められたものを見つけたら、間違ったカテゴリにある場合は正しいカテゴリを選ぶか、そもそもそこにあるべきでない場合は削除をクリックします。では、ここで削除をクリックしましょう、よね?
[23:14] ですから、これらのいくつかが難しい理由がいくつかわかります。例えば、ここにテディが二つありますが、これはあまり見かけないので、混乱するかもしれません。こちらのものは少し奇妙に見え、ほとんどウォンバットのように見えます。これはテディがかなりたくさんあります。これは背景から少し見えにくいかもしれませんが、それ以外は問題なさそうです。… はい、ですから最初のいくつかを見て、最初の年で問題が見つからなければ、おそらく大丈夫です。これでトレーニングセットがきれいになったので、検証セットもきれいにしましょう。こちらが問題があったものです。なぜ問題があったのかはわかりませんが、仕方ありません。そして、ざっとスクロールしていきます…
[23:59] うーん、これはクマではないと確信できないので、そのまま削除します。テディ何とか…ですが、ご存知の通り、問題です。はい、これもテディではありません…。ですから、考え方はわかりますよね?これをやった後、何が起こるかというと…クリーナーが、変更したもののリストと削除したもののリストを保存したので、このセルを実行できます。そして、削除すると言ったすべてのインデックスのリストを処理し、それらのファイルを削除します。そして、変更すると設定したすべてのものを処理し、新しいフォルダに移動します。はい、完了です!
[24:45] ですから、これは画像モデルだけのものではありません。これはただ…実際には…ほとんど誰も知らずに使っていない、非常に強力なテクニックです。つまり、データクリーニングを始める前に、常にモデルを構築して、データの中で認識が難しいものを見つけ、モデルがデータの問題を見つけるのを助けられるものを見つけるのです。そして、それらを見ると、「なるほど、こういう種類の問題があるのか」と言い、次のデータセットを集めるより良い方法を見つけたり、クリーニングの一部を自動化する方法を見つけたりするかもしれません。
[25:23] はい、これがデータクリーニングです。私のGPUは4GBしかないので、閉じて停止することが非常に重要です。それによりメモリが解放されます。ですから知っておくことが重要です…あなたのコンピュータでは、通常のRAMは実際にはいっぱいになりません。なぜなら、RAMを使いすぎると、代わりにコンピュータが(スワッピングと呼ばれる)処理を開始し、基本的にそのRAMをハードディスクに保存して後で使うからです。GPUはスワップできません。
[26:00] 台のGPUは、RAMを使い果たしたらそれで終わりです。ですから、使っていないGPUを使っているノートブックは必ず閉じてください。そしてGPU上では本当に一度に一つのことだけを使うようにしてください。そうしないと、ほぼ確実にメモリ不足になります。さて、最初のいくつかの赤い表示が出始めました。ですから質問することを忘れないでください。そして黄色に関しては、動画を見ながら重要なのは、私がこのコードを全部実行してほしいと言っているわけではないということです。考え方は、まず動画を見て、それから戻って一時停止する、進めながらそうする、というものです。あるいは止めて試して、止めて試して、でも構いません。私がとても気に入っていて、多くの学生もとても気に入っている動画の見方は、まずキーボードに触れずに全体を通して見て、この動画が何についてなのかを把握し、それから最初に戻ってもう一度見ながらついていく、という方法です。そうすれば、どの時点でも自分が何をしているのか
[27:00] 次に何が起こるのかが分かります。これは実際に時間の節約になります。現実の講義では教授を巻き戻して同じことをもう一度言ってもらうことはできませんから、明らかに少し変わった方法ですが、ここでは良いやり方です。さて、データをきれいにしたので、それをどうやって本番環境に載せるのでしょうか。本ではVoilàというものを使っています。そして、それは…かなり良いです!でも実は、今の皆さんのほとんどがずっと便利だと感じるだろうものがあり、それはHuggingFace Spacesというものです。それにはいくつか使えるものがあります。今日はGradioというものを見ていきます。
[27:42] そして本にはこれについての章はありませんが、それは問題ではありません。なぜならTanishq Abraham——実はこのコースのTAの一人——が、今日扱うことのほぼすべてについて素晴らしいブログ記事を書いているからです。フォーラムとコースページからそのリンクがありますので、いわば本の章に相当するものです。そして、二つの理由からTanishqを大いに称賛するために少し立ち止まらないのは失礼でしょう。一つ目は、彼はfastaiコミュニティで最も助けになる人の一人で、かなり何年も前からいて、信じられないほど粘り強く、思慮深く、忍耐強いこと。もう一つは、数年前に彼が有名な天才少年だった頃のConanとの素晴らしい写真を持っているからです。これで、有名な天才少年が大人になるとどうなるか分かりましたね——彼らはさらに有名なfastaiコミュニティのメンバーであり深層学習の専門家になるのです。
[28:41] なので、彼がコナンにジョークを言っているこの動画はぜひチェックしてみてください。彼はまだ18歳くらいだと思います。実際、そんなに何年も前のことではないでしょう。というわけで、タニシュク、コミュニティでの助けに本当に感謝しています。そして、九歳の頃の写真で恥をかかせてしまってごめんなさい——本当は謝ってないですけどね、ハハ。さて、ここからが本題ですが、GradioとHuggingFace Spacesを使うのは、まあ、始めるのは簡単です。では、HuggingFace Spacesのページから始めましょう——コースのフォーラムからリンクを貼っています。そしてモデルを本番環境にデプロイします。訓練したモデルを取って、基本的にそれをこのHuggingFace Spacesのサーバーにコピーし、ユーザーインターフェースを書くわけです。
[29:29] では…始めましょう!新しいスペースを作成します。さあ、そのまま進めて、こう言えばいいんです…もちろんサインアップします…全部無料です。このコース全体でお見せするものは基本的にすべて無料でできます、それが良い知らせです。では、名前を付けて、最小限のものを作りましょう。私はいつもApacheライセンスを使います。なぜなら他の人があなたの作品をとても簡単に使えるようになるからです。でも特許についてあまり心配する必要はありません。言ったように、これと一緒に使える製品はいくつかありますが、私たちはGradioを使います。これも無料です。
[30:04] 公開設定にすれば共有できます。学生のうちは特に、ポートフォリオをしっかり築いていくためにも、これはいつも良いアイデアです。さて、これで完了です。スペースを作成しました。では次に何をしましょうか?SpacesはGitを通じて動きます。ほとんどのソフトウェア開発者はGitに非常に慣れているでしょう。データサイエンティストの中にはそうでない人もいるかもしれません。Gitは非常に非常に便利なツールです。詳しくは話しませんが、使い方をざっと学びましょう。GitはGithub Desktopというもので使えます。これは実はかなり素晴らしくて、コンソールでGitを使う人でもGithub Desktopの使用を検討すべきでしょう。なぜなら、いくつかのことはこちらの方がずっと速く簡単にできるからです。実際、今日友人のハミルと話していて、ああ、助けて、誤ってこの二つのものをコミットしてしまった、元に戻す一番簡単な方法は何?と聞いたら、彼は以前Githubで働いていて、
[31:09] そして、彼は何か凝ったコンソールコマンドを使うのかと思っていたら、こう言うんです。「ああ、GitHub Desktop を使えばいいよ、クリックするだけでできるから」。ああ!それはいいアイデアですね。というわけで、それは…それは便利です。でも、ほとんどの場合、私たちはコンソールから、ターミナルから Git を使います。Linux ユーザーや Mac ユーザーなら、すでにターミナルがあるのでとても簡単で、心配いりません。Windows ユーザーならいい知らせがあります。最近の Windows には素晴らしいターミナルがあります。Windows Terminal といって、Microsoft Store から入手できます。なので実際、私がターミナルを使っているのを見かけたら、それは Windows Terminal を使っているんです。とてもよく動きます。なぜそうしたいのか神のみぞ知りますが、こんな馬鹿げた色ばかりで、まあ、そういうことです。
[31:54] さて、ターミナルの中で何を動かしたいですか?もちろん、Linux や Mac を使っているなら、すでにシェルが設定されています。Windows なら、ほぼ間違いなく Ubuntu を使いたいでしょう。信じられないかもしれませんが、Windows は完全な Linux 環境を実際に動かすことができ、そのためにはたった一行入力するだけです。それがこれです… なので、(WSL install でググってください) PowerShell を管理者として実行し、そのコマンドを貼り付けて、五分ほど待って再起動すれば、完了です。
[32:30] これで完全な Linux 環境が手に入りました。さて、私がこれを話している理由の一つは、これから自分のマシンで作業する方法をお見せするからです。なので、これは少しギーク度が上がる話で、データサイエンティストの中にはあまり馴染みがないかもしれません。でも、ターミナルを怖がらないでください。きっと…きっと…とても役に立つと分かるでしょうし、思っているよりずっと怖くないはずです。特に私の場合、私は Windows を使うことを選んでいます。なぜなら、素敵な Windows の GUI アプリがすべて使えて、画面に描画したり、プレゼンをしたりでき、さらに完全な 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] よし、もうすぐです。これを保存して、次にHuggingFace Spacesにコミットする必要があります(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がインストールされていますが、最新版であることを確認するために、いつもこれを最初に実行します。そして当然、インポートなども行います。ペットのデータセットを取得し、猫かどうかを判定する関数を使います。これがImageDataLoadersのラベリング関数です。これはDataBlocksの別の書き方で、ちょっとした省略記法のようなものだと覚えておいてください。そしてlearnerを作成し、ファインチューニングします。ここまではすべて以前に見た内容です。Kaggleでは、すべてのノートブックに編集ビュー(今見たもの)とリーダービューがあります。
[38:27] そして、必要ならノートブックを共有できます。そうすれば、ご覧の通り、誰でもリーダービューを読むことができます。ここでは、実行したときに何が起きたかが表示されています。学習させたところ、時間がかかりました。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] そのためのノートブックがあります…では、そのためのノートブックがあり、トレーニング済みのモデルを使って予測を行う方法を説明します。hash pipeには少し変わった点がありますが、後で説明します。今は無視してください。いつものようにfastaiをインポートします。前と同じようにGradioもインポートします。そして、前に使ったのとまったく同じis_catの定義をコピーします。これは重要です。ラベリングで使用した外部関数はすべてここにも含める必要があります。なぜなら、そのlearnerがそれらの関数を参照するからです。learnerはモデルに関するすべてを保存しますが、関数のソースコードは保存しないので、それらを手元に置いておく必要があります。
[42:12] では、これを実行してみましょう。例えば、ちょうど今…(エクスプローラーで見たかもしれませんが)犬の画像をそこに置きました。そして、その犬からPythonイメージライブラリの画像を作成し、画面全体を圧迫しないように少し小さめのものに変換すると、犬の画像があります。では、どうやってそれが犬か猫かを予測するのでしょうか?実はとても簡単で、やることは…学習器を訓練する代わりに、load_learner()を使います。保存したファイル名を渡すと、それが返ります。学習器です。この学習器は、訓練を終えたときに得られる学習器とまったく同じです。ここがColabですよね?ちょうど学習器を訓練してきたところなので、その最後には訓練済みの学習器があります。そして私たちは…それを時間の中で凍結させたような、pickleファイルと呼ばれるもの——Pythonの概念です。凍結されたオブジェクトのようなもので、ディスクに保存し、転送して
[43:17] コンピュータに移し、今それを読み込み、そして解凍したと考えています。これが私たちの解凍された学習器で、これで好きなことができるようになりました。では、その一つとして…学習器が持つメソッドの一つにドットpredictメソッドがあり、実行すると、私のラップトップ上でもほぼ瞬時であることがわかります。実際、どれくらいかかったかもわかります。Jupyterでは、パーセントで始まるものはマジックと呼ばれる特別なJupyterの機能で、例えば、何かにどれくらい時間がかかるかを見るものがあります。
[43:51] ほら、できました。これが猫ではないと判断するのに54ミリ秒かかりました。つまり二つのことを返しています。猫ですか?(文字列として)、猫ですか?(ゼロか一として)、そして犬である確率と猫である確率です。つまり、猫ですか?のゼロ(偽)と一(真)の確率で、間違いなく犬です。では、この情報を基本的に持つGradioインターフェースを作りたいと思います。
[44:28] Gradioは呼び出す関数を渡す必要があるので、これがその関数です。predictを呼び出し、先ほど言ったように三つのものを返します。予測は文字列、そのインデックス、そして犬か猫かの確率です。Gradioが求めているのは、考えられる各カテゴリ——この場合は犬か猫——とそれぞれの確率を含む辞書を返すことです。もしPythonをあまりやったことがなければ、zipの辞書は見たことがないかもしれません。
[45:04] とても便利な小さなイディオムで、ぜひ確認する価値があります。mapをまだ見たことがない人も同様です。とにかく、これがその内容です。現時点でのGradioの少し面倒な点は、Pytorchのテンソルを扱えないことです。ここで分かるように、Pytorchは通常の数値を返しておらず、テンソルを返しています。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。それが犬なのか猫なのか全く分かりませんでした。さて、今日再トレーニングしたこのモデルは、今では猫だと確信しています。というわけで、少し違うトレーニングスケジュールを使ったか何かで、あるいはエポックを一つ余分に与えたのか、ともかく、それは犬猫判別なのですが、どうやら猫のようです。猫なんでしょうね。おそらく正しいのでしょう。そんなに長くトレーニングすべきではなかったですね。さて、これが私たちのインターフェースです。今、実際に実行中なので、停止するにはスタートボタンをクリックする必要があります。そうしないと、ノートブックで他のことが何もできません。さて、これをPythonスクリプトに変換する必要があります。Pythonスクリプトに変換する一つの方法は、必要なものをすべてPythonスクリプトにコピー&ペーストすることです。
[48:42] このうち必要な部分をすべてPythonスクリプトにコピー&ペーストする必要があります。例えば、これは必要ありません — 何かを確認するだけのものです。これも必要ありません — ただの実験でした。これもただの実験でした。これは必要ですよね?そこで私がしたのは…各セルを順に見て、最終的なスクリプトで必要になる情報を含むセルの先頭に「hash pipe export」と書きました。そして、これらがそのステップですよね?そして、一番下で、nbdevからnotebook2scriptというものをインポートしました。それを実行して、このノートブックの名前を渡すと、そのスクリプトを含むapp.pyというファイルが作成されます。これは簡単で便利な方法です — HuggingFace Spacesのようにスクリプトを期待していてノートブックを期待しないものに取り組むときに。お好みでテキストファイルにコピー&ペーストするだけでも構いませんが、
[49:42] でも私はこの方法がとても気に入っています。なぜなら、実験はすべてノートブックで行い、終わったら一番下のセルを実行してエクスポートするだけだからです。どうやってapp.pyと呼ぶことを知るのかって?それは先頭にある特別なもの、default export: default_exp のおかげで、作成するPythonファイル名を指定しているからです。これは私が使っているちょっとした小技です。さて、これでapp.pyができました。これをGradioにアップロードする必要がありますが、どうやるのでしょうか?ただ…Gitにプッシュするだけです。ですから…Visual Studio Codeで行うこともできますし、「git commit」と入力することもできます。
[50:28] そして「git push」です。それができたら、…minimalをtestingに変えると、…おそらくこれはまだ動いていると思います。前回のモデルはプッシュしていなかったので。そうすれば、あの奇妙な犬猫を見られます。では、こちらです。本番環境で動いているのがわかります。これで誰でも、公開設定にすれば、誰でもここに来てあなたのモデルを確認でき、アップロードもできます。では、これが私のワンちゃんです。
[51:02] うん、間違いなく犬です。猫、そうですね、これはエポックを二つほど少なく学習させたのかもしれません。だから自信が低いのです。うん、間違いなく猫です。犬猫…ねえ犬猫…うーん、まだ間違いなく猫だと思っています。まあいいや、本当に変ですね。では、これが…はい、これがシンプルなモデルを本番環境に導入する例です。フォーラムやコミュニティからいくつか質問が来ています。
[51:49] では、ある人がこう尋ねています。「pytorchモデルとfastaiのlearnerの違いは何ですか?」はい、大丈夫です。それについてはすぐに触れます。このレッスンか次のレッスンになるかはわかりません。そして別の人が、基本的にはこう尋ねています。「エポックはいくつ学習するのですか?」モデルを学習するにつれて、ご覧のとおり、エラー率は改善します。
[52:21] そこで質問は、もっと実行すべきか?エポック数を増やすべきか?ということです。これは三エポック行っていますよね?これが始めるための三エポックと一です。見てください、それはあなた次第ですよね?つまり、これについては、ここで言っているのは、エラーが一パーセントあるということです。私は…一パーセントのエラーで構いません。もっと良くしたいなら、データ拡張を増やし、より長く学習させればよいのです。十分長く学習させると、すぐに学ぶように、そして…おそらく次のレッスンで。十分長く学習させると、実際にエラー率が悪化し始めます。そして、その理由も学びます。つまり基本的には、十分に良くなるまで、または忍耐や時間や計算資源が尽きるまで、あるいは…エラー率が悪化し始めるまで学習させればよいのです。
[53:14] はい、あ、それからColabでモデルをどうやって取得するか?Colabで必要なのは、エクスポートした後に、ファイルブラウザに入ると、実際にここに表示されますよね?そしてダウンロードをクリックできます。少し変で、どこにダウンロードするか尋ねるボックスがポップアップするのではなく、このような進行状況の円が表示され、サイズなどによって数分かかることがあります。その円が埋まると、ようやくブラウザのポップアップが表示され、保存できるようになります。はい、これがその方法です…
[53:57] これが実際にモデルを取得する方法です。ご覧のとおり、実際にGPUが必要なステップでは、Colab、Kaggle、そして今後のレッスンで説明するその他の完全に無料のリソースを利用できます。それ以外のすべては自分のコンピュータで実行できます。予測も含めてです。予測は高速ですよね?ですから、何千件も行うのでなければ、予測にGPUを使う必要はまったくありません。さて、いよいよです。今、保存するように求められています。さて、ここで大きな問題が一つあります。自分のコンピュータで実行するには、PythonとJupyter Notebookがコンピュータ上で動いている必要がありました。どうすればいいのでしょうか?
[54:41] なぜなら、ここでよく多くの人がさまざまなトラブルに巻き込まれるからです…。「これを全部動かす方法をどうにかして見つけようとしているんだ」と。良いニュースは、これを非常に簡単にしてくれるものがあるということです。それはfastsetupと呼ばれています。必要なのはそのほんの一部分だけなので、お見せしましょう…。それは…実は…GitHub上のGitリポジトリです。GitHubはほとんどのGitリポジトリが置かれている場所です。GitHubでfastai/fastsetupにアクセスすれば見つかります。そこで、ここでコードをクリックするだけで、このリポジトリ全体を取得できます。GitHub Desktopがインストールされていれば、「Open with GitHub Desktop」をクリックすると、ご覧のとおり、こう表示されます。「はい、これを保存する準備ができました」と。そこで「Clone」をクリックすると、コピーが作成されます。はい、できました…
[55:39] 基本的に、クローンしたら、その中に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] ソフトウェアをインストールするもう一つの方法は、mamba の代わりに pip と呼ばれるものを使うことです。mamba でできることのほとんどは pip でもできますが、GPU をお持ちの場合、pip はたいてい GPU で動くようにインストールしてくれないので、GPU で動かすにはほかにいろいろインストールしなければならず、それが面倒なんです。だから私は mamba を使うように勧めているわけですが、それ以外なら pip を使ってもかまいません。まだ少し赤が残っていますね。どうお手伝いできるか、ぜひまたお知らせください。
[59:54] では、手順がどう進んでいるか見てみましょう。自分への備忘録としてここに手順を置いていたのを忘れていました。スペースを作成しました、チェック。基本的なインターフェースを作成しました、チェック。よし、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で私が本当に気に入っていることの一つは…ここに「APIを表示」というボタンがあります。ですので実際に好きなアプリを作ることができ、重要な点は、
[1:02:27] 実際のモデル予測を行ってくれる部分はHuggingFace SpacesのGradioが処理します。そして、それと通信するJavascriptアプリケーションを書くことができます。ここで二つの反応があるでしょう…フロントエンドエンジニアリングをやったことがある人は、「おお、素晴らしい、コードを書くだけで何でも作れるようになった」と興奮するでしょう。そして多くのデータサイエンティストは「うわ…Javascriptの使い方が全く分からない、自分の持ち札にない」と思うかもしれません。
[1:03:07] ここでまた言いたいのは、Javascriptをあまり恐れないでくださいということです。もちろん一つの選択肢は、「モデルがあるよ」と言って、Javascriptを知っている友人に丸投げして「Javascriptのインターフェースを作ってくれ」と頼むことです。しかし、これが実際にどれほど難しくないかを少しお見せしましょう。エンドポイントがあります。今、私たちのモデルが動いているURLがあります。そこにデータ…画像…画像データをこのURLに渡すと、辞書を返してくれます。つまり、このUIがやっていることと全く同じことを、APIとして、呼び出せる関数として行うのです。
[1:03:58] そしてここに、呼び出し方の例がいくつかあります…例えば、実際にできます…先ほどの最小限のインターフェースを使って、APIの例をお見せしましょう…その方が少し簡単だからです。では、curlをクリックして、これをコピーして…コピーして…貼り付けます。するとほら、そこに…(ああ、hello worldを渡すのは良い例ではありませんね)…もし、Tanishqを再度渡してみると、彼の名前Tanishqでどうなるか見てみましょう。
[1:04:37][] ここで「Hello Tanishq!」と返ってくるのがわかります。これがこれらのAPIの仕組みですよね。JavaScriptを使ってAPIを呼び出せて、いくつか例もあります。では、ウェブサイトを作りました。これが私のウェブサイト「tinypets」です。このウェブサイトはご覧の通り、ものすごく美しいわけではありませんが、ウェブサイトです。始まりですよね。そしてここにいくつか例があります。ほら、単一ファイル、クリック、ファイルを選択、クリック…そしてこの例では、実際に完全なペット分類を行っています。つまり、犬か猫かだけでなく、品種を分類するモデルを実際に訓練しました。それについては来週もっと話します。では、特定の品種を選んで…実行してみましょう…
[1:05:41][] おっ!出ました!あまりすごくはないですよね。でも事実は、これが今やJavaScriptアプリなので、できることに制限がないということです。そしてそのHTMLを見てみましょう…これだけです!簡単に画面に収まりますよね。基本的な手順はそれほど複雑ではありません。基本的には…写真のインポートを作成し、写真を変更したときにread関数を呼び出すイベントリスナーを追加します。read関数はファイルリーダーを作成し、ファイルを読み込み、読み込みが完了したらloadedを呼びます。そしてloadedは…それをfetchします…さて、そこのパスは…
[1:06:39][] そこのパスです!そう!ただし、完全なペットのものを行っています。なので、これは基本的に彼らの…彼らのサンプルからコピー&ペーストしただけです。そしてJSONを取得し、データから最初のもの…信頼度…ラベルを取得し、HTMLを設定します。ご覧の通り、(まあ)JavaScriptを使ったことがないなら、これらはすべて新しいことですよね。でも、Pythonより難しいわけではありません。ただ…ただ別の言語を学ぶだけです。そしてここから構築を始められます!
[1:07:20][] 例えば、マルチファイル版を作りました。マルチファイル版では、お見せしましょう…マルチファイル…選択…いくつかクリックできます…ニューファンドランド、ラグドール、バセットハウンド、そして何かの猫がいます。私はあまり猫好きではありません。四つのファイルを選んで、ドン!すべて分類されました!どうやらベンガルだそうです!(私にはわかりません)こちらがニューファンドランドです。
[1:07:52] 複数ファイル版もあります。コードを見ると、それほど多くはありません。今はすべてのファイルを取得して読み込み用にマッピングし、それぞれを追加しているだけなので、コードはほとんど増えていません。そして、こちらのサイトでご覧になったかもしれませんが、さらにいくつかの例があります。これは今週コミュニティの人たちが作った独自のバージョンです。こちらの例は…これは…そう…これはgradioの方の一人が作ったもので、「あなたのペットを知ろう」と呼ばれています。ペットを選ぶと…私はこれがとても気に入っています。なぜなら二つのモデルを組み合わせているからです。まず「ああ!バセットハウンドだ」と言い、それから入力してそのペットについて質問できます。「ああ、どんなしっぽをしているの?…」と検索すると、NLPモデルを呼び出します。
[1:08:56] それがこの…湾曲したサーベル状のしっぽだと答えます。できました!「どんな手入れが必要?」ここでもまた、バセットハウンドの耳は内外を頻繁に掃除しなければならないことがわかります。これはモデルを組み合わせた例です。つまり、これは既製のインターフェースだけではできなかったことです。次にお伝えしたいのは、どうやってウェブサイトを作ったかです。HTMLファイルの作り方はお見せしましたが、どうやってそれらを作り、どうやってウェブサイトにするのか?見てください!では…こちらが…こちらが…
[1:09:52] こちらが最も基本的なバージョンのソースコードです!これを保存して…できました…これをVisual Studio Codeで開けます…そして実際にできるのは…ここではエクスプローラーやMacの&ファインダーにあります。ダブルクリックするだけで、ほら!動くアプリです!!
[1:10:32] お分かりのように、JavaScriptアプリを使うのにコンピューターにソフトウェアをインストールする必要はありません!単一のファイルで…ブラウザで実行するだけです…ブラウザが完全な実行環境です。デバッガーもあります。すべて揃っています。ここで…ここでご覧のように…外部のhacking facesエンドポイントを呼び出しているだけなので、すべて自分のコンピューター上でできます。コンピューター上で問題なく動くHTMLファイルができたら、VSCodeで、どうやってそれをウェブに公開して他の人が使えるようにするのでしょうか?
[1:11:06] 繰り返しますが、すべて無料です。github pagesというとても便利なものがあり、基本的にあなたのウェブサイトをホストしてくれます。JavaScriptだけなので、すべて問題なく動きます!私の意見では、github pagesサイトを作る最も簡単な方法は、fast.aiのfastpagesというものを使うことです!そして基本的にやることは、セットアップ手順に従うだけです。
[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、そして三つのダッシュを置く必要があることです。「フロントメタ」と呼ばれます。そうすれば…保存すると、ウェブサイトに表示されます。それからもう一つしたのは、こう思ったんです、なるほど…fast.aiがこのウェブサイトを作ったのは結構ですが、見た目があまり好きではありません。別のバージョンを作りたいな、と。
[1:14:18] 心配いりません。「fast.ai/tinypets」にアクセスして「Fork」をクリックしてください。そして「Fork」をクリックすると、あなた自身のコピーが作成されます。そこで、私は自分の個人アカウント jph00 でそれをやりました。見てください!これで自分専用のバージョンができ、ここで変更を加えられるようになりました。それでいくつか変更を加えました。一つ目の変更は、config.yml に移動してテーマを「pages-themes/hacker」に変えたことです。フォークしたら、通常は 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] それでは…失礼します。ハッカーのジェレミー・ハワードでした。ご視聴いただきありがとうございました。次のレッスンでは、自然言語処理を掘り下げていきます。同じようなことをしますが、画像ではなく言語で行います。そして、これらのモデルが実際にどう動いているのか、内部を覗いていきます。確率的勾配降下法のようなことについて学び、微積分もちょっとだけ払拭しなければならないかもしれません。「C」の言葉を言って、皆さんを引き留めてしまっていなければいいのですが。
[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」by 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 を推奨するものでもありません。Vidleaf は AI による句読点の付加と AI 生成の要約を加えました。元の文言はこのページで引き続きご覧いただけます。
あなたは制作者または権利者ですか? 訂正または削除を依頼する: copyright@vidleaf.app (これらのページについてをご覧ください)。
最終更新