面向程序员的实用深度学习 2022,第 2 课——Jeremy Howard
本页标题、字幕、摘要和阅读笔记为已保存的 AI 译文,可能有误。时间戳与来源一致,可展开查看原始字幕,并对照原视频核对。
Jeremy Howard 逐步讲解如何使用笔记本、本地和 Kaggle GPU 以及面向程序员的 fastai 工作流来训练和部署实用的深度学习模型。
文字稿来源:创作者上传至 YouTube 的字幕。AI 标点由 Vidleaf 添加;原始措辞和时间戳锚点均保留。原始文字稿见下方。
AI 生成的摘要
本节课因大学教室预订问题而在家中录制,内容涵盖课程后勤与学生项目,随后进入将模型投入生产的话题。免费书籍可通过 fastbook 仓库或 course.fast.ai 获取,每章末尾都有测验,forums.fast.ai 为每节课设有官方主题帖。早期项目包括受损汽车分类器、胡须检测器以及一个已投入生产的食物分类器。在生产环节,笔记本用 ddg 替代 bing,因为 Bing API 需要 SDK 密钥。讲者建议先训练再清洗数据,解释了尺寸调整的选择以及作为数据增强的 aug_transforms,并指出由于仅约四个微调轮次,增强可能并非真正必要。混淆矩阵显示哪些案例较难,plot_top_losses 会呈现损失最高的项目,其中糟糕的损失可能来自错误且自信,或正确但不自信;ImageClassifierCleaner 按损失排序项目,以便对错误标注的项目重新标注或删除。实用建议包括在 4GB GPU 上关闭未使用的笔记本,因为 GPU 无法交换内存。部署方面,重点是使用 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 的回复。
来自“展示你的作品”的学生项目
将 Web 应用部署到生产环境
从模型到生产环境 Web 应用
- [6:20] 演讲者提出,有一天观众或许能创建自己的 Web 应用并将其投入生产。
- [6:27] 演讲者澄清“有一天”指的就是今天,并表示愿意现在就演示如何做到。
- [6:40] Suvash 发布的主题被描述为幸运的巧合,因为它恰好就是今天选定的主题。
将模型投入生产的第一步
数据清洗与 Jupyter notebook 导航
从问题定义到数据清洗
- [7:00] 在定义问题、查找并收集数据之后,下一步是数据清洗。
- [7:14] 本书第 2 章涵盖数据清洗,可以从课程中或本地克隆在 Colab 中打开。
- [7:35] course.fast.ai 讲解了如何运行这些 notebook,而第 2 章讲的是将内容投入生产。
- [7:46] 按 Shift + Enter 即可执行单元格。
Jupyter Notebook 扩展与目录
- [8:20] Navigate 菜单默认不会出现;它来自通过 pip 安装 Jupyter Notebook 扩展并重启 Jupyter。
- [8:45] Colab 已经有目录,因此该扩展主要适用于本地环境,在本地会出现 Nbextensions 标签页。
- [8:57] 在 Nbextensions 中点击目录(2)会得到一个方便的导航栏。
可折叠标题与键盘导航
- [9:07] 可折叠标题扩展添加了关闭和打开各部分的控件。
- [9:20] 右箭头移动到某个部分的末尾,左箭头移动到开头,便于在各部分之间移动。
- [9:35] 再次按左箭头会关闭某个部分,按右箭头则会打开它。
用于图像搜索的 Bing API
- [9:48] 本书使用 Bing API 来搜索图像。
用 DDG 替换 Bing 以及 Python 文档技巧
在 Notebook 中将 Bing 切换为 DDG
- [9:58] Notebook 将 "bing" 替换为 "ddg",因为 Bing API 需要获取 SDK 密钥。
- [10:05] 演讲者开玩笑说,深度学习最难的部分是搞清楚 Bing Azure 网站并把它弄好,而 ddg 不需要这样做。
- [10:21] Notebook 将分享在课程网站论坛上,唯一真正的改动是将 bing 替换为 ddg 并移除密钥。
复用上周的熊搜索示例
查找关于 Python、fastai 和 PyTorch 的信息
在清洗数据和图像增强之前进行训练
在清洗数据之前训练模型
- [12:48] 与通常建议先清洗数据相反,演讲者提议在清洗数据之前先训练模型,并会很快解释原因。
- [13:10] 使用 DataBlock 获取用于训练的数据加载器,show_batch 像之前一样显示批次。
调整尺寸选项:挤压、裁剪和填充
- [13:37] 调整尺寸可以用字符串或类似枚举的选项来指定,挤压可能会产生非常瘦的熊,但能保持整个图片可见。
- [14:12] 裁剪只保留图像的中心部分以获得更好的宽高比,但会丢失一些内容;而用零(黑色)填充则保留整个图像和正确的宽高比。
- [14:33] 不同的调整尺寸选择可能会产生不同质量的模型,但差异通常不大,因此不必过于担心。
RandomResizedCrop 与数据增强
- [14:49] RandomResizedCrop 每次会产生图像的不同部分,这对于在非常大的扫描图像上运行多个 epoch 很有用。
- [15:11] 在论坛上,有人想要在非常大的文档扫描中区分法语和德语文本,建议的 RandomResizedCrop 方法略有成效。
- [15:51] 每次从同一图像获得不同图片称为数据增强,aug_transforms 会应用旋转、挤压、扭曲、重新着色和饱和度变化。
- [16:32] 对于超过大约五到十个 epoch 的训练,通常推荐使用 RandomResizedCrop 和 aug_transforms,并将 mult 留空而不是设置为 2。
增强在内存中进行,而非副本
- [17:03] 针对 Alex 的问题,演讲者澄清图像不会被多次复制;每个 epoch 都会读取每张图像,并在训练期间实时在 RAM 中对其进行扭曲、裁剪和重新着色。
- [17:32] 计算机上不存储任何副本,但模型实际上看到了几乎无限多的略有不同的副本。
使用四个微调 epoch 进行训练运行
使用更多 Epoch 进行更长时间的训练
运行额外的 epoch
- [18:24] 运行更多 epoch 被描述为一种好方法,报告的结果低于百分之三。
训练熊分类器并读取其混淆矩阵
在 4GB 笔记本电脑 GPU 上训练
分类标签的混淆矩阵
- [18:59] 混淆矩阵只有在标签是类别时才有意义,它展示了模型犯了哪些类别错误。
- [19:15] 此时,模型两次将实际的灰熊误认为黑熊,两次将实际的黑熊误认为灰熊。
- [19:27] 模型从未把泰迪熊认错,这很合理,因为泰迪熊与两种熊看起来都差别很大。
使用混淆矩阵找出困难类别
- [19:39] 混淆矩阵通常能让人真切感受到哪些情况比较困难。
- [19:43] 在本书和课程使用的宠物数据集中,不同品种的混淆矩阵展示了哪些品种难以识别。
- [20:00] 讲者阅读了维基百科页面和宠物繁育报告中关于识别这些品种的内容,因为它们太难了,连专家都觉得困难。
- [20:10] 从课程中,讲者了解到黑熊和灰熊比想象中难区分得多,所以讲者甚至不会去尝试。
下一步:分类解释
- [20:21] 模型创建好后,下一个有趣的内容是分类解释。
使用 plot_top_losses 和 ImageClassifierCleaner 清理图像
解读 plot_top_losses 输出
- [20:42] plot_top_losses 显示损失最高的项目,而损失是对每条数据评估后衡量模型质量的指标。
- [21:03] 如果模型预测错误且非常自信,损失就很差;显示顺序是预测、实际、损失、概率。
- [21:16] 在示例中,模型在实际是黑熊时预测为灰熊,并且有 96% 的把握,因此专家需要检查是模型错了还是标签错了。
- [21:41] 有些高损失项目实际上是正确的,比如灰熊/灰熊和泰迪/泰迪,因为模型只有 66% 或 72% 的把握。
- [22:01] 糟糕的损失可能来自错误且自信,也可能来自正确但不自信。
使用 ImageClassifierCleaner 清理训练集
- [22:14] fast.ai 的 ImageClassifierCleaner 用于清理数据集中标注错误的项目。
- [22:24] ImageClassifierCleaner 会运行模型,这就是为什么要把 "learn" 传给它。
- [22:42] 选择泰迪熊后,它会显示训练集或验证集中标记为泰迪熊的项目,按损失排序,也就是按把握程度排序。
- [23:00] 对于被错误收集的条目,你可以选择正确的类别,或者如果它根本不该在那里,就点击删除。
- [23:14] 一些困难的例子包括两只泰迪熊挨在一起、一只看起来像袋熊的奇怪玩偶,以及一只在背景中很难看清的玩偶;如果前几个都没有问题,这个数据集大概就没问题。
清理验证集并应用更改
- [23:44] 验证集也以同样的方式清理,删除那些实际上不是熊或泰迪熊的条目。
- [24:20] 清理器会存储一个已更改条目的列表和一个已删除条目的列表。
- [24:26] 运行该单元格会删除标记为删除的索引处的文件,并将标记为更改的条目移动到新文件夹。
在清理数据之前构建模型
GPU 内存管理与视频学习方式
GPU 内存限制与交换
- [25:23] 只有一块 4GB 的 GPU 时,关闭并停止笔记本以释放内存非常重要。
- [25:37] 普通内存被占满会导致计算机将内存交换到硬盘,但 GPU 无法交换。
- [26:00] 当 GPU 内存耗尽时,你就没法继续了,所以请关闭未使用的笔记本,并在 GPU 上一次只使用一个任务,以避免内存耗尽。
观看编程视频的推荐方式
使用 HuggingFace Spaces 和 Gradio 部署模型
部署选项:Voilà 与 HuggingFace Spaces
- [27:18] 清理数据后,书中使用 Voilà 将模型投入生产,演讲者认为这相当不错。
- [27:29] 演讲者认为如今大多数人会觉得 HuggingFace Spaces 更有用,而今天的重点是 Gradio,它是可与之配合使用的工具之一。
Tanishq Abraham 的博客文章作为课程材料
- [27:42] 虽然书中没有关于此内容的章节,但课程助教 Tanishq Abraham 写了一篇全面的博客文章,涵盖了今天课程的所有内容,链接已发布在论坛和课程页面上。
- [28:07] 演讲者称赞 Tanishq 是 fastai 社区中最乐于助人、最坚韧、最体贴、最有耐心的人之一,并分享了一张他小时候作为神童与 Conan 的合影。
创建 HuggingFace Space
将 Git 和 GitHub Desktop 与 Spaces 配合使用
Git 作为 Spaces 背后的机制
- [30:14] Spaces 通过 Git 运作,这是大多数软件开发人员非常熟悉的工具,不过一些数据科学家可能不太了解。
- [30:24] Git 被描述为一个非常有用的工具,但演讲者并未详细讲解,而是转而快速介绍如何使用它。
GitHub Desktop 的优势
- [30:38] Git 可以通过 GitHub Desktop 使用,演讲者称其相当不错。
- [30:46] 即使是通过控制台使用 Git 的人也应该考虑 GitHub Desktop,因为有些事情用它做起来要快得多、容易得多。
通过 GitHub Desktop 还原意外提交
从终端使用 Git 和 Linux
各操作系统的终端选项
- [31:19] Git 主要通过控制台或终端使用,Linux 和 Mac 用户已经具备这些。
- [31:30] Windows 用户可以从 Microsoft Store 获取 Windows Terminal,演讲者每当出现终端时都会使用它。
通过 WSL 在 Windows 上运行 Ubuntu
- [31:59] 在 Windows 上,演讲者推荐使用 Ubuntu,因为 Windows 可以运行完整的 Linux 环境。
- [32:15] 安装 WSL 需要搜索 WSL 安装方法,以管理员身份运行 PowerShell,粘贴命令,等待约五分钟,然后重启。
演讲者为何偏爱 Windows 搭配 Linux
将 Gradio 应用部署到 HuggingFace Spaces
在终端中克隆起始仓库
- [33:37] 一旦有了终端,你就可以复制粘贴命令,而不必按照逐步点击的说明操作。
- [33:55] 粘贴并运行提供的命令会创建一个新目录,该目录最初是空的。
- [34:10] 然后说明要求你创建一个包含指定内容的文件。
在编辑器中打开文件夹
- [34:24] 在 Windows 上,输入 "explorer.exe ." 会在当前目录打开资源管理器。
- [34:42] 在 Mac、Linux 或 Windows 上,输入 "code ." 会打开 Visual Studio Code 并加载该文件夹。
- [34:58] VS Code 被描述为一款出色的 IDE,值得花几分钟看看教程。
- [35:04] 你创建一个 app.py 文件,其中包含说明所指定的内容。
将文件提交到 HuggingFace Spaces
- [35:30] 保存后,必须将文件提交到 HuggingFace Spaces。
- [35:37] 在 Visual Studio Code 中,你可以点击输入提交信息,点击对勾,它就会把提交发送到 HuggingFace Spaces。
测试构建好的 Gradio 应用
- [35:54] 回到 huggingface.co/spaces/jhp00/minimal,网站大约需要一分钟来构建。
- [36:15] 构建好的网站有一个 Gradio 界面,包含文本输入和文本输出,运行一个 greet() 函数并返回 "Hello {name}"。
- [36:40] 用名字 Tanishq 进行测试,展示了针对该输入产生的输出。
- [36:53] 这个应用并不怎么令人兴奋,但它现在是一个在生产环境中运行的应用了。
迈向深度学习模型的下一步
- [37:00] 下一步是把这个简单的应用变成一个在生产环境中运行的深度学习模型。
训练并导出 fastai 宠物分类器
在 Kaggle 中构建狗/猫分类器
- [37:10] 必须先获得一个深度学习模型,基本上得先训练一个。
- [37:29] Kaggle 和 Colab 示例做的事情相同:创建一个狗或猫分类器。
- [37:52] Kaggle 已经安装了 fastai,但笔记本会先安装它,以确保使用的是最新版本。
- [38:03] 宠物数据集通过用于 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 下载保存的模型
将模型移入 Spaces 应用目录
- [39:57] 下载的模型必须复制到与 HuggingFace Spaces 应用相同的目录中。
- [40:13] 在终端中,在 Mac 上输入 "open ." 或在 Windows 上输入 "explorer.exe ." 会在该目录中打开访达或资源管理器,以便将文件粘贴到那里。
在 Windows 上访问 Linux 文件的提示
- [40:36] 在 Windows 上,将 Linux 主目录固定到快速访问可以快速访问 Linux 文件。
- [40:51] 此变通方法在 Mac 上不需要,因为文件是集成的,而在 Windows 上它们表现得像两台独立的机器。
将模型放入测试 Space
- [41:04] 创建了一个名为 testing 的 Space,并将下载的 "model.pkl" 粘贴到其中。
使用保存的 fastai 学习器进行预测
加载保存的学习器和所需函数
- [41:41] 笔记本像之前一样导入 fastai 和 Gradio,并复制与标注时使用的完全相同的 is_cat 定义。
- [41:49] 标注中使用的任何外部函数都必须包含在内,因为学习器会引用它们,尽管它不会保存它们的源代码。
- [42:43] 不训练学习器,而是用保存的文件名调用 load_learner(),返回与训练结束时相同的学习器。
如何恢复 pickle 化的学习器
- [43:03] 训练好的学习器被冻结为 pickle 文件,保存到磁盘,传输到计算机,然后加载并解冻。
- [43:23] 解 pickle 后的学习器可用于任何用途,其方法之一是 .predict 方法。
预测速度和输出格式
构建 Gradio 猫狗分类器界面
为 Gradio 调整 predict 输出
- [44:28] Gradio 需要一个可调用的函数,因此 predict 返回预测字符串、其索引以及猫狗概率。
- [44:45] Gradio 期望一个将每个可能类别(狗或猫)映射到其概率的字典,该字典通过 zip 的 dict 构建。
- [45:12] Gradio 目前无法处理 PyTorch 张量或 Numpy 数组,因此每个值都必须转换为普通浮点数。
- [45:32] 对狗狗图片调用 classify_image() 会返回一个字典,其中狗为确定是,猫为确定不是。
创建并启动 Gradio 界面
- [45:51] Gradio 界面指定了要调用的函数、图像输入和标签输出,构成一个非常简单的界面。
- [46:16] 提供的示例包括一张狗、一张猫和一张“不知道”图片。
- [46:27] 启动后,界面运行在演讲者自己笔记本电脑上的一个 URL 上,尚未投入生产,并且响应基本是即时的。
“不知道”示例与重新训练的模型
- [46:55] “不知道”图片现在返回 100,而不是通常的大约 50-50,演讲者称这很令人沮丧。
- [47:13] 演讲者六岁的女儿通过 Google 图片找到了那张狗与猫混合的图片,之前的模型给出的结果几乎正好是 50-50。
- [47:53] 当天重新训练的模型现在确信它是猫,可能是因为训练计划略有不同或多了一个 epoch,演讲者说他本不该训练那么久。
停止界面与下一步
将笔记本导出为 app.py 并部署到 Gradio
使用 hash pipe export 标记要导出的单元格
- [48:35] 需要的代码必须复制到 Python 脚本中,而仅用于检查或实验的单元格可以省略。
- [48:54] 演讲者在每个包含最终脚本所需信息的单元格顶部写上“hash pipe export”来标记它们。
使用 nbdev 的 notebook2script 生成 app.py
- [49:13] 在 notebook 底部,讲者从 nbdev 导入了 notebook2script,并用 notebook 名称运行它来生成 app.py。
- [49:32] 这种方法适合那些需要脚本而非 notebook 的工具,例如 HuggingFace Spaces;复制粘贴到文本文件中同样可行。
- [49:42] 讲者更青睐这种方法,因为所有实验都可以在 notebook 中完成,只需在底部用一个单元格导出结果。
- [49:54] 输出文件名 app.py 由 notebook 顶部的 default_exp 设置决定。
通过 Git push 部署并在生产环境中测试
训练时长、导出模型与 GPU 使用
选择 epoch 数量
- [52:14] 随着训练进行,错误率会改善,因此问题在于是否要增加 epoch 数量。
- [52:26] 当前运行使用了三个 epoch 外加一个作为起步,这是否足够取决于用户。
- [52:32] 百分之一的错误率让讲者感到满意,但更多的数据增强和更长的训练还能进一步改善。
- [52:47] 如果训练时间足够长,错误率实际上会开始变差,这将在后续课程中解释。
- [52:58] 你可以一直训练到结果足够好、直到你失去耐心、时间或算力,或者直到错误率开始变差为止。
从 Colab 下载导出的模型
- [53:20] 在 Colab 中,导出模型后打开文件浏览器即可看到该文件。
- [53:30] 点击下载并不会立即打开保存对话框,而是出现一个进度圆圈。
- [53:43] 根据模型大小,进度圆圈可能需要几分钟,之后浏览器才会提示你保存文件。
训练与预测对 GPU 的需求
使用 fastsetup 设置 Python 和 Jupyter
从 GitHub 克隆 fastsetup
- [54:52] GitHub 上的 fastsetup Git 仓库让设置 Python 和 Jupyter Notebook 变得简单直接。
- [55:03] 访问 Github 上的 fastai/fastsetup,你可以通过 Code 按钮获取整个仓库,并用 Github Desktop 打开来克隆它。
使用 setup-conda.sh 安装 Python
- [55:44] 克隆之后,以 ./setup-conda.sh 运行 setup-conda.sh 文件即可安装 Python。
- [56:05] 在 Linux 或 Mac 上,不应使用系统自带的 python,因为电脑需要它,而乱动它总是会导致灾难。
- [56:32] 你需要自己专属的开发版 Python,带有最新版本和库,而基于 conda 的发行版几乎肯定是最佳选择。
- [56:49] 目前推荐的发行版是 mambaforge,它在你重新打开终端后增加了用于安装软件包的 mamba 命令。
使用 mamba 安装 fastai 和 nbdev
- [57:23] 根据 docs.fast.ai,正确的命令是 conda install -c fastchan fastai,但应把 conda 替换为 mamba,因为它快得多:mamba install -c fastchan fastai。
- [57:52] 这会安装所需的一切,包括 Pytorch、Numpy 和 fastai。
- [58:11] 用同样的方式安装 nbdev 是可选的,但它也会安装 Jupyter 等其他东西。
启动 Jupyter Notebook
- [58:42] 输入 jupyter notebook 会启动一个服务器,点击超链接即可打开课程中展示的界面。
GPU 环境下 mamba 与 pip 的对比
- [59:17] pip 几乎可以做 mamba 能做的任何事,但在有 GPU 的情况下,pip 通常不会安装能在 GPU 上正常工作的东西,需要大量其他烦人的安装,这就是推荐 mamba 的原因。
在 HuggingFace Sp 上使用 Gradio 和 Streamlit 部署模型
课程清单进度
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 宠物分类 Web 应用
单文件宠物品种分类器演示
- [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,并追加每个结果。
结合两个模型的社区示例
构建并托管一个简单的 JavaScript 网页应用
在本地运行单文件 JavaScript 应用
- [1:09:52] 最基本的版本是一个单一的 HTML 源文件,可以保存并在 Visual Studio Code 中打开,或在资源管理器或访达中双击打开。
- [1:10:28] 该文件无需安装任何软件即可作为可用的应用运行,因为浏览器就是完整的执行环境,包括调试器。
- [1:10:46] 该应用调用外部的 Hugging Face 端点,因此可以完全在计算机上本地运行。
通过 FastPages 托管在 GitHub Pages 上
- [1:11:06] GitHub Pages 免费托管网站,而且因为它只是 JavaScript,在那里运行良好。
- [1:11:22] 在演讲者看来,创建 GitHub Pages 站点最简单的方法是 FastPages,这是一个 fast.ai 项目。
- [1:11:43] 设置过程包括点击链接生成副本、为其命名并创建仓库,另外大约还有两个步骤,耗时约五分钟。
- [1:12:24] 设置完成后你会得到一个空站点,你可以用 GitHub Desktop 或 Visual Studio 打开它,或在终端中粘贴命令,然后保存 HTML 文件并将它们推送回 GitHub。
主题与 Jekyll 配置
- [1:12:49] FastPages 会显示所创建网站的链接,其外观可以通过主题来更改。
- [1:13:03] 一个 config.yml 文件让你选择主题;演讲者没有特别理由地选择了一个名为“alembic”的主题。
- [1:13:18] GitHub Pages 使用 Jekyll,因此任何 Jekyll 主题都可以使用,保存的文件会自动出现在网站和文件列表中。
- [1:13:38] 每个 GitHub Pages 文件都需要在顶部有“前置元数据”:三条短横线、标题、布局,以及三条短横线。
复刻并自定义站点
- [1:14:18] 要制作不同的版本,请前往 fast.ai/tinypets 并点击 Fork 创建你自己的副本;演讲者在账户 jph00 下完成了此操作。
- [1:14:38] 演讲者将 config.yml 中的主题更改为“pages-themes/hacker”。
- [1:14:47] 复刻后,你必须前往 Settings,点击 Pages,并启用 GitHub Pages(默认关闭),使用“Master Branch Root Save”。
- [1:15:09] 换了不同的主题后,jph 的 Tiny Pets 信息相同,但看起来更有黑客范儿。
零成本、无需重型工具即可上手
自然语言处理简介
即将介绍的自然语言处理主题与方法
前 3 段双语对照预览 · 免消耗额度
以下展示前 3 段已保存中文译文与英文原版字幕的逐段对照(零额度消耗):
大家好。欢迎来到第二课。感谢大家回来……环境稍有变化,我们大学那边出了点“行政问题”——有人预订了我们的教室——所以我现在在家里的书房讲课。很抱歉我身后没什么装饰。我其实对这节课非常非常兴奋。感觉像是回到了最初的日子,因为我们在做一些非常新、非常酷的东西,这些……
Hi everybody. Welcome to lesson two. Thanks for coming back… slight change of environment here, we had a bit of an “administrative issue” at our university — somebody booked our room — so I'm doing this from the study at home. so sorry about the lack of decorations behind me. I'm actually really really pumped about this lesson. It feels like going back to what things were like in the very early days, because we're doing some really new, really cool stuff, which…
你知道……这些东西以前真的没怎么出现在这样的课程里。所以我超级超级兴奋。非常感谢大家在第一课之后回来,希望回来是值得的。我觉得……我觉得你们会喜欢的!我确实,是的,我对此非常兴奋。现在请记住,这门课是与书配套的,所以一定要跟上。我的意思是,不是要在书里逐页跟读,因为我们从不同方向讲类似的内容……但是,也要读那本书,并记住这本书也是完全免费提供的。你可以去 fast.ai 的 fastbook 仓库查看 notebook,或者通过 course.fast.ai 在那里阅读——例如,通过 Colab。
you know… stuff that hasn't really been in courses like this before. So, I'm super super excited. So thanks a lot for coming back after lesson one, and I hope it's worth you coming back. I think… I think you're gonna love it! I am, yeah, I'm really excited about this. Now remember that, you know, the course goes with the book, so be sure that you're following. I mean, not following along in the book, because we're covering similar things in different directions…. but, like, read the book as well, and remember the book is entirely available for free as well. You can go to the fast.ai fastbook repo to see the notebooks, or through course.fast.ai you can read it there — for example, through Colab.
还要记住,这本书……我是说……书里有很多我们在课程中没有涵盖的内容,比如一些我觉得相当有趣的东西,关于神经网络的历史,其中一些还有非常有趣的个人故事,你读到这里就会知道。每章末尾都有测验。记住,在看视频之前先读测验是个不错的主意,所以如果你想读第二章的测验,你知道,然后再回来,这是个不错的主意。然后确保在看完视频、读完书的第二章之后,你能做那个测验。
And also remember that the book… I mean… the book has a lot of stuff that we didn't cover in the course like, you know, stuff I find pretty interesting, about the history of neural networks, some of which has some really interesting personal stories actually, as you'll read here. And at the end of each chapter there is a quiz. Remember it's not a bad idea before you watch the video to read the quiz, so if you want to read the Chapter Two quiz, you know, and then come back, that's not a bad idea. Then make sure that you can do the quiz after you've watched the video, and you've read chapter two of the book.
[0:00] 大家好。欢迎来到第二课。感谢大家回来……环境稍有变化,我们大学那边出了点“行政问题”——有人预订了我们的教室——所以我现在在家里的书房讲课。很抱歉我身后没什么装饰。我其实对这节课非常非常兴奋。感觉像是回到了最初的日子,因为我们在做一些非常新、非常酷的东西,这些……
[0:41] 你知道……这些东西以前真的没怎么出现在这样的课程里。所以我超级超级兴奋。非常感谢大家在第一课之后回来,希望回来是值得的。我觉得……我觉得你们会喜欢的!我确实,是的,我对此非常兴奋。现在请记住,这门课是与书配套的,所以一定要跟上。我的意思是,不是要在书里逐页跟读,因为我们从不同方向讲类似的内容……但是,也要读那本书,并记住这本书也是完全免费提供的。你可以去 fast.ai 的 fastbook 仓库查看 notebook,或者通过 course.fast.ai 在那里阅读——例如,通过 Colab。
[1:28] 还要记住,这本书……我是说……书里有很多我们在课程中没有涵盖的内容,比如一些我觉得相当有趣的东西,关于神经网络的历史,其中一些还有非常有趣的个人故事,你读到这里就会知道。每章末尾都有测验。记住,在看视频之前先读测验是个不错的主意,所以如果你想读第二章的测验,你知道,然后再回来,这是个不错的主意。然后确保在看完视频、读完书的第二章之后,你能做那个测验。
[2:07] 上周我没提到的一件事是,Radek——我上周提到过他——还写了一个非常酷的东西,叫 aiquizzes.com,这是一个专门针对这本书的测验网站。它实际上使用了基于重复的学习技巧,确保你永远不会忘记。所以,一定要去看看 aiquizzes.com。里面全是全新的问题,和书里的问题不一样,而且整理得非常好。所以也去看看 aiquizzes.com 吧。记住,除了 course.fast.ai,还有 forums.fast.ai。course.fast.ai 是你要去获取所有 notebook 链接、kaggle 相关内容等等的地方。你还会在 forums.fast.ai 上发现,每节课都有一个官方主题帖,里面有你需要的一切信息。通常论坛上会有更多一点的信息,我们尽量让课程
[3:09] 保持精简高效,而论坛则更详细一些。所以如果你发现——在这个例子中你要找的是第二课的官方主题帖,但这里是到目前为止的第一课官方主题帖。所以从第一课官方主题帖来看,在我录制它仅仅几天之后——我们甚至还没正式发布课程,所以只是那些实时跟着做的人——就已经有很多回复了,这可能会让人有点不知所措。
[3:40] 所以请注意,我的帖子底部有一个按钮,写着“总结此主题”。如果你点击它,你就只会看到得票最高的回复,这是一个非常好的方式,可以确保你抓住主要内容。所以,按钮在那里,这是你点击后它看起来的样子。你只会得到……你只会得到来自 fast.ai 的传奇人物如 Sanyam 和 Tanishq 的得票高的内容。所以希望你会发现这是使用论坛的一种有用方式。
[4:14] 所以这周很酷的事情之一是,我……如承诺的那样,发布了“展示你做了什么”的帖子,已经有很多人发帖了。我几天前截了这张图,已经远远超过 39 条回复了,如果我……如果我没记错的话。我很难决定要分享哪些,因为它们都太好了,所以我实际上决定走个简单路线,就挑了最早的几个。所以我只给你们看最早发布的那些,因为它们都太好了。所以第一个,最早发布的一个是损坏车辆分类器……看起来效果相当不错。我真的很喜欢创作者 Matt 对此说的话,就是……你知道,哇,运行这段代码有点不舒服,我还不太理解,但我就是在做。所以我想,是的,Matt 干得好,就这样……
[5:14] 光是动手去做就行。这就是入门的方式,一切都会变得清晰明了,别担心。很高兴看到下一个发布的实际上是一篇 fast pages 上的博客文章。——非常棒。看到——,所以只是描述一些东西,他们这周做的一些实验以及他们发现了什么。下一个是令人惊叹的胡子检测器,如果我理解正确的话,主要是因为从 bird 到 beard 只需把一个字母改成两个字母非常容易,而这个在寻找有胡子的先生们方面做得非常好。非常棒。然后这个是……
[5:51] 又上了一个台阶,这是一个完整的生产环境 Web 应用,用来对食物进行分类,这算是额外加分项。显然那个帖子里现在已经有 80 条回复了,谢谢你 Sanyam,非常酷。所以,你知道,显然……所以这实际上是 Suvash 创建的,他做这些课程已经有好几年了,我相信。所以你知道,有一天你也能创建你自己的 Web 应用并把它投入生产。当我说有一天的时候,更具体地说……就是今天!我实际上现在就要向你们展示如何做到这一点!所以 Suvash 把它发上去其实是个相当幸运的巧合,因为这正是我们今天要选的主题。
[6:46] 那么,我们如何把一个模型投入生产呢?第一步是……嗯……你差不多已经完成了第一步,对吧?第一步是……第一、二、三、四步……是弄清楚你想解决什么问题,弄清楚如何为它找到数据,去收集一些数据,等等。那么在你有了数据之后,第一步是什么呢?下一步是数据清洗,如果你翻到书的第 2 章,我现在就打开它……
[7:23] 这就是那本书……你可以直接从课程中在 Colab 里打开它,或者如果你把它克隆到了你的电脑上,或者随便什么,你可以在那里做。所以记住,course.fast.ai 会带你了解如何运行这些 notebook,所以你可以看到第 2 章全是关于把东西投入生产的,而这里是第 2 章。好的,记住我们按 Shift + Enter 来运行单元格,好吗?……来执行它们。所以我们要翻到书中开始清洗数据的那部分。
[7:59] 所以我点击 Navigate,然后我们往下走……收集数据……找到了!所以我们可以先快速复习一下。顺便提一下,很多人问我,我用什么小技巧能如此快速轻松地在 Jupyter notebook 中导航。你会看到其中一个非常好用的就是导航菜单,它实际上默认并不显示。所以如果你安装一个叫做 Jupyter Notebook Extensions 的东西——Jupyter……
[8:30] Notebook……扩展……然后你只需……你只需“pip install”它们,按照说明操作,然后重启 Jupyter。显然这……顺便说一下,Colab 已经有目录功能了,所以这只是针对你使用本地环境的情况,比如,你会看到这里出现这个“Nbextensions”的东西,如果你点击“目录(2)”,就会得到这个方便的导航栏。我另一个非常喜欢的是这里这个叫可折叠标题的功能,它给我提供了这些好用的小东西,用来关闭和展开。实际上这还不是最棒的部分。对我来说最棒的是,如果我按右箭头,它会跳到一个小节的末尾,如果我按左箭头,它会跳到一个小节的开头。所以就像,如果我想在各个小节之间移动,我只需按上左、下右、下右,非常方便,
[9:35] 如果你在这里再按一次左键,它就会折叠起来。在这里再按一次右键,它就会展开。这就是可折叠标题。总之,几个非常方便的东西,我们稍后今天还会更多地讨论如何设置你的 notebook。好的,你会注意到的一件事是,在书中,我们使用 Bing API 来搜索图像。我刚刚已经把“bing”替换成了“ddg”,因为 Bing API 需要获取一个 SDK 密钥,老实说,这就像深度学习中最难的事情,就是搞清楚 Bing Azure 网站并把它弄好。ddg 不需要!所以基本上完全一样……(而且你可以……)
[10:21] 我也会在课程网站的论坛上分享这个 notebook,但我基本上只是把 bing 替换成了 ddg,并去掉了密钥。所以就像我们上周做的那样,我们可以搜索东西,在书中我们做了一个熊检测器,因为在我写它的时候,我那时刚学步的孩子非常喜欢让我帮忙识别泰迪熊,我当然不想让她不小心抱到一只灰熊,所以我们在这里展示了如何搜索灰熊,就像上周一样。
[10:56] 一个循环遍历灰熊、黑熊和泰迪熊的东西,就像上周一样。去掉那些失败的,就像上周一样。论坛上有几个人问过的一个问题是?我如何找到关于基本上任何 Python 或 fastai 或 PyTorch 东西的更多信息。书里有几个提示。一个是,如果你在任何函数名旁边放两个问号,你实际上会得到它的完整源代码,同样地,如果你放一个问号,你会得到一个简短的……
[11:41] 你知道的……一点点信息。如果你安装了 nbdev(我想你需要的是 nbdev),那么你可以输入 doc,这可能会给你最重要的东西——一个直接指向文档的链接,在那里你可以找到更多信息,通常还会有示例。另外,如果你愿意,这里还有一个指向源代码的链接……
[12:13](我们用一个控件来做这个,好吗)……一个指向源代码的链接,这样你就可以跳来跳去。注意,在 GitHub 的源代码中,你可以点击某些东西并跳转到它们的定义。所以这是一种很好的方式,可以跳来跳去,准确理解发生了什么。好了,有很多获取帮助的好方法。但我向你承诺的是,我们现在要清理数据。所以我要告诉你一件你可能觉得非常惊讶的事。在你清理数据之前,你要训练一个模型。
[12:55] 现在,我知道这听起来和你可能听过一千遍的说法完全相反,那就是首先你……~(构建/你/训练)……你清理数据,然后你训练模型。但我要向你展示一些非常神奇的东西。首先我们要训练一个模型,你马上就会明白为什么。所以,要训练一个模型,就像之前一样,我们使用 DataBlock 来获取我们的数据加载器——书里有很多关于这里发生了什么的信息。好了……然后我们可以像往常一样调用 show_batch 来查看它们。
[13:33] 书里这里有一个小侧边栏,我快速提一下,是关于我们可以使用的不同调整大小的方法。我想我们上周简要提到过……我们可以挤压……上周我用了一个字符串,你可以用字符串或者我们这种类似枚举的东西。你可以看到,用挤压,你最终会得到一些非常瘦的熊,对吧?所以这是熊的真实尺寸、形状。在这里它变瘦了,但你可以看到现在我们有它所有的幼崽……(它们叫 clubs 吗?对,熊崽)……所以它挤压了,以确保我们能看到整个画面。这里也一样。这只原本在画面外,我们挤压了它,这家伙现在看起来瘦得奇怪,但我们可以看到整个东西。所以这就是挤压!
[14:12] 或者这里的这个是裁剪。它只裁剪了图像的中心,所以我们得到了更好的宽高比,但失去了一些东西。这是为了让我们能得到正方形图像。另一种方法是我们可以使用“填充”。所以你可以用各种不同的东西来填充。如果你用零填充,也就是黑色,你可以看到现在我们有完整的图像和正确的宽高比。所以这是我们可以做的另一种方式,你知道,不同的情况会导致不同质量的模型。你可以都试试,通常不会有太大的差别。
[14:44] 所以不用太担心。不过我要告诉你一个非常有意思的,就是 RandomResizedCrop。所以与其说 resize,我们可以说 RandomResizedCrop。如果我们这样做,你会看到每次都会得到图像的不同部分。所以,在这周……这周有人在论坛上问……我试着……(这是一个非常有意思的想法,结果证明它稍微有点用,)他们想识别法语和德语文本的图片。所以显然这不是你会用的常规方法,但只是做个实验,(我喜欢实验,所以,)他们有非常大的文档扫描件,想仅仅通过看图片来判断是法语还是德语,他们说图片太大了?该怎么办?我说,用 RandomResizedCrop,这样你就能抓取图像的不同部分,这非常好,因为你可以跑很多很多
[15:44] 个 epoch,每次得到稍微不同的图片。所以这是一个非常好的技术。这种每次从同一张图像得到不同图片的想法叫做“数据增强”。再说一次,我不会详细讲数据增强,因为书里有,但我就在这里快速指出,如果你用这个叫“aug_transforms”的东西,也就是增强变换,这里我把它们乘以了二,——所以我让它们超级大,这样你能更容易看到。你可以看到这些泰迪熊被旋转、压扁、扭曲、重新着色、饱和度调整,所有这些让每张图片都不同。一般来说,如果你要训练超过大约五到十个 epoch,除非你要解决的问题超级简单,大多数时候你大概都会想这么做,你大概会想用 RandomResizedCrop 和这些“aug_transforms”。不要把 mult=2,
[16:49] 就把它留空。我放在那里只是为了让你们看得更清楚。这里我们观众中的 Alex 问了一个有意思的问题:“这是……这是把图像复制多份吗?……做这样的事?……还是这样?”答案是“不,我们不是在复制图像。实际发生的是,图像……所以每个 epoch,每张图像都会被读取。而这里发生的是……是……有点像……在内存中,在 RAM 中,这个……图像被‘扭曲’了,对吧?它被……我们在裁剪它、重新着色等等。所以这是模型训练期间实时发生的过程。所以,你的电脑上不会存储任何副本,但实际上几乎就像有无限多稍微不同的副本,因为那就是模型最终看到的东西。所以,希望这能解释清楚,Alex,以及其他人,
[17:52] 所以这是个好问题!好的,那么我们已经有了……我们要使用 RandomResizeCrop,我们要使用增强变换,这样我们就能从中得到 DataLoaders,然后我们就可以继续训练我们的模型了。这大约需要一分钟。在这个例子中,我们只做了四个 epoch 的微调,我们稍后会在课程中讨论为什么这里显示的是五个,但主要是四个 epoch 的微调。所以我们可能并不真的需要 RandomResizedCrop 和 aug_transforms,因为 epoch 太少了,但是,你知道,如果你想运行更多的 epoch,这是一个好方法。低于百分之三!错误率!那很好!好的,所以记住我说过我们要在清理之前先训练一个模型?
[18:36] 好的,那我们继续训练它。那么在训练进行的同时……它正在我的笔记本电脑上运行,那台电脑只有 4GB 的 GPU,相当基础,但足够开始了。在训练进行的同时,我们来看看下一个。所以我们要看的第一个东西是混淆矩阵,而混淆矩阵这个东西……它只有在你的标签是类别时才有意义,对吧!
[19:09] 它说的是你犯了什么样的类别错误。所以这显示的是我们目前得到的模型……有两次实际上是一只灰熊,而它认为是黑熊,还有两次实际上是一只黑熊,而它认为是灰熊,并且没有一次把泰迪熊弄错,这很合理,对吧,因为泰迪熊看起来和这两种都相当不同。在很多情况下,当你看到这个时,它会让你真正感受到,好吧,哪些是难的?对吧?所以例如,如果你使用我们在书和课程中经常玩的 pets 数据集,这个针对不同宠物品种的分类[**]矩阵,你知道,真的会告诉你哪些是难以识别的,而我实际上还去查阅了维基百科页面和宠物繁育报告,了解如何识别这些特定类型,因为它们太难了,甚至专家也觉得很难。
[20:10] 实际上,我在做这门课程时学到的一件事是……黑熊和灰熊比我意识到的要难区分得多……所以我甚至都不打算尝试!但我要给你们展示的是,我们能用这个模型做的一件非常有趣的事:现在我们已经创建了这个分类解释对象,我们用它来做混淆矩阵,我们可以调用 plot_top_losses。我们可以说绘制最高损失,这非常有意思。它的作用是告诉我们“损失”最高的地方。如果你们还记得上一课的内容,损失就是那个衡量指标——衡量我们模型有多好——我们在每次运行一条数据后计算出来的。如果我们预测错了,而且非常自信,损失就会“很差”。
[21:11] 关于那个预测。所以这里有一个例子,我们预测了……(这里的顺序是:预测、实际、损失、概率)。……我们预测是灰熊,实际是黑熊,而且我们96%确定(我们的模型)它是灰熊。现在,我对熊的了解还不够多,无法判断是模型犯了错,还是这实际上就是一张灰熊的照片……但所以,专家显然会回去检查这些……
[21:41] 对吧?现在,你会注意到这里有几个……有灰熊/灰熊、泰迪熊/泰迪熊……它们实际上是正确的,对吧?那么为什么这个损失“差”呢?明明是正确的?原因是……因为它不够自信。它只有66%的置信度,对吧?所以这里有一个泰迪熊……它只有72%的置信度。对吧?所以损失差可以有两种情况:要么是错了但很自信,要么是对了但不自信。现在这之所以非常有帮助,是因为我们可以使用一个叫做 fast.ai ImageClassifierCleaner 的工具来清理数据集中标签错误的数据。
[22:24] 所以当我们使用 ImageClassifierCleaner 时,它实际上会运行我们的模型。这就是为什么我们要把“learn”传给它,对吧?我提到过我对黑熊和灰熊了解不多,但我对泰迪熊很了解,所以我会选择泰迪熊。如果我点击泰迪熊,它现在会显示训练集中(你可以选择训练集或验证集)所有被标记为泰迪熊的东西,而这里非常重要的是:它们按损失排序,也就是按置信度排序,对吧?所以我可以滚动浏览前几个,检查它们是否正确,对吧?哦,这里有一个错误,对吧?所以当我发现一个被错误收集的样本时,我可以要么把它……如果它属于错误的类别,我可以选择正确的类别……或者,如果它根本不应该在那里,我就点击删除。所以这里我就继续点击删除,对吧?
[23:14] 所以你可以看到,其中一些难以识别是有原因的,比如这里有两个泰迪熊,我想这让人困惑,因为不常见。这个看起来有点奇怪,几乎像一只袋熊。这里有非常多的泰迪熊。这个可能只是从背景中有点难以看清,但除此之外,它们看起来没问题。……好,所以我们只需浏览前几个,如果你在第一年里没有发现任何问题,那你大概就没问题了。这样我们就清理了训练集,接下来也清理验证集。这是那个有问题的,我不知道为什么它会有问题,但就这样吧,我们快速滚动浏览一下……
[23:59] 好吧,我不太确定那是一只熊,所以我直接把它删掉。它是个泰迪什么的……但是,你知道,这是个问题。好吧,那也不是泰迪熊……所以你明白思路了吧?做完这些之后,效果是……清理器现在已经存储了我们修改过的列表和删除过的列表,所以我们现在可以运行这个单元格。它会遍历我们说要删除的所有索引,并删除那些文件。然后它会遍历我们设置为修改的所有项,把它们移到新文件夹,好了,完成!
[24:45] 所以,这不仅仅是用于图像模型的东西。它其实……是一个几乎没人知道和使用的非常强大的技术,那就是,在开始数据清理之前,总是先构建一个模型,找出数据中难以识别的项目,并找出模型能帮你发现的数据问题。然后当你看到它们时,你会说“好吧,我看到了我们遇到的这类问题”,你可能会找到更好的方法来收集下一个数据集,或者找到自动化部分清理工作的方法等等。
[25:23] 好的,这就是数据清理,由于我只有一块 4GB 的 GPU,所以关闭并停止对我来说非常重要,因为这会释放内存。所以重要的是要知道……在你的电脑上,普通内存其实不会真的被填满,因为如果你用掉太多内存,会发生的是你的电脑会开始(这叫做)交换,基本上就是把那部分内存保存到硬盘上以便以后使用。GPU 无法交换。
[26:00] 块 GPU,当显存耗尽时,就到此为止了,你就完了。所以你需要确保关闭那些正在使用 GPU 但你并未使用的 notebook,并且真正在 GPU 上一次只运行一样东西,否则你几乎肯定会耗尽内存。所以我们已经看到最初几个红色开始出现了。所以记得要提问,而关于黄色部分,重要的是要知道,当你看视频时,我并不是要求你运行所有这些代码,好吗?这个想法是让你先观看,然后回头暂停,随着进度推进。或者你可以直接停下来、尝试、再停下来、再尝试。我非常喜欢——而且很多学生也非常喜欢——观看这些视频的方法是,先完全不碰键盘地看完整段内容,了解视频讲的是什么,然后回到开头再看一遍并跟着做。这样,在每一个点上你都知道自己在
[27:00] 做什么,你知道接下来会发生什么。这实际上可以为你节省一些时间。这是一种有点不寻常的方式,因为显然在现实生活中的讲座里你没法这样做,你没法把教授倒回去让他们再说一遍,但在这里这是个好方法。那么既然我们已经清理了数据,我们要如何把它投入生产呢?在书里我们用了叫做 Voilà 的东西。它……它相当不错!但实际上,我认为你们大多数人如今可能会觉得更有用的是另一个东西,叫做 HuggingFace Spaces,而且有几样东西你可以配合它使用。今天我们要看的是叫做 Gradio 的东西。
[27:42] 书里并没有关于这个的章节,但这没关系,因为 Tanishq Abraham——他实际上是这门课的助教之一——写了一篇很棒的博客文章,几乎涵盖了我们今天要讲的所有内容。论坛和课程页面上都有那篇文章的链接,所以如果你愿意,这相当于书里的一章。如果我不停下来好好表扬一下 Tanishq,那就是我的失职了,原因有两个:第一,他是 fastai 社区里最有帮助的人之一,他已经在社区里待了好几年,极其坚韧、周到且有耐心;第二,因为我有一张他几年前和 Conan 的绝佳合影,当时他还是著名的神童,所以现在你知道著名的神童长大后会发生什么了——他们成为了更有名的 fastai 社区成员和深度学习专家。
[28:41] 所以你一定要看看这个他给柯南讲笑话的视频。我觉得他现在其实还只有18岁,这大概不是很多年前的事。所以非常感谢你,Tanishq,感谢你在社区里的所有帮助。也很抱歉用你九岁时的照片让你难堪——其实我并不抱歉,哈哈。好了,现在的问题是,做 Gradio 和 HuggingFace Spaces 其实上手很容易。好,我们从 HuggingFace Spaces 页面开始——我们在课程论坛里给了链接。我们要把一个模型投入生产,也就是把我们训练好的模型,基本上复制到这个 HuggingFace Spaces 服务器上,并为它写一个用户界面。
[29:29] 那么……开始吧!创建新 space。好,你可以直接去说,好吧……显然你要注册……整个东西都是免费的。基本上我在这整门课里给你展示的所有东西,你都可以免费做,这是好消息。好,给它起个名字,创建个最简单的就行。我一直用 Apache 许可证,因为这意味着别人可以很容易地使用你的工作,但你不用太担心专利问题。就像我说的,有几个不同的产品可以搭配使用,我们要用 Gradio,也是免费的。
[30:04] 如果你把它设为公开,你就可以分享它,这对学生来说总是个好主意,尤其是……真正建立起你的作品集。好,我们完成了。我们创建了一个 space。现在,下一步做什么?Spaces 是通过 Git 工作的,现在大多数软件开发者都会非常熟悉 Git。有些数据科学家可能不熟悉。所以 Git 是一个非常非常有用的工具。我不打算详细讲它,但我们快速了解一下怎么用它,好吗?现在,Git 你可以通过一个叫 Github Desktop 的东西来用,它其实相当不错,甚至那些通过控制台使用 Git 的人可能也应该考虑用 Github Desktop,因为有些事用它快得多也容易得多,事实上我今天跟我的朋友 Hamil 聊天,我说,哦,救命,我不小心误提交了这两个东西,恢复它最简单的方法是什么,而他以前在 Github 工作过,
[31:09] 我还以为他会给出什么花哨的控制台命令,结果他说:“哦,你应该用 GitHub Desktop,直接点一下就行了。”哦!这主意真不错。所以这……这确实很实用。但大多数时候,我们还是在控制台、在终端里使用 Git。如果你是 Linux 用户或 Mac 用户,你已经有终端了,非常简单,不用担心。如果你是 Windows 用户,我有个好消息:如今 Windows 有一个很棒的终端,叫 Windows Terminal,你可以从 Microsoft Store 获取,所以实际上每次你看到我用终端,我用的就是那个 Windows Terminal,运行得非常好。天知道你为什么想要它,配上这些花里胡哨的颜色,不过,就这样吧。
[31:54] 那么,你想在终端里运行什么呢?显然,如果你用的是 Linux 或 Mac,你已经配置好了 shell。在 Windows 上,你几乎肯定想用 Ubuntu。所以,信不信由你,Windows 实际上可以运行完整的 Linux 环境,要做到这一点,只需输入一行命令,就是这个……所以,如果你去(直接谷歌搜索 WSL install)以管理员身份运行 PowerShell,粘贴那条命令,等大约五分钟,重启,就完成了。
[32:30] 你现在有了一个完整的 Linux 环境。我提到这个的原因之一是,我现在要向你展示如何在你自己的机器上做事,所以这有点像进入更高一级的极客领域,一些数据科学家可能不太熟悉。所以你知道,不要害怕终端,你会……我觉得你会……发现它真的很有帮助,而且远没有你想象的那么可怕。我特别要说的是,对我来说,我选择使用 Windows,因为我能得到所有好用的 Windows 图形界面应用,可以在屏幕上画画、做演示,而且我还有一个完整的 Linux 环境。那个 Linux 环境会使用我的 GPU 等等,所以对我来说,我的首选是 Windows,我的第二选择——差得不多,我真的很喜欢——是 Linux。Mac 稍微难一点,但仍然可用。在 Mac 上有些事会稍微棘手一些,但你应该没问题。
[33:37] 好的,那么,不管你手头有什么,此时你已经有了一个可用的终端。所以,在你的终端里……(使用终端的一大好处是你不用遵循一大堆“点这里、点这里、点这里”的说明。你只需复制粘贴即可。)所以我这就……你只需复制这个,然后转到你的终端,粘贴进去,运行它,做完之后,你会发现你现在有了一个目录。这个新目录一开始是空的,然后他们会告诉你,好,继续创建一个包含这些内容的文件。那么,当我们身处 Windows 上的 Linux 环境,或者 Mac 上的终端,或者随便什么环境时,如何创建一个包含这些内容的文件呢?在 Windows 上,你只需输入“explorer.exe .”,它就会在这里打开资源管理器,或者更好的做法是,在 Mac、Linux 或 Windows 上……
[34:42] 所以,是的,无论你用的是哪种电脑,你只需输入“code .”,它就会弹出 Visual Studio Code 并打开你的文件夹。然后你就可以继续……(如果你以前没用过 VS Code,花几分钟读一些教程是非常值得的,它是一个很棒的 IDE)。……然后,你就可以按照他们告诉你的,创建一个 app.py 文件,app.py 文件,里面包含他们让你放进去的内容。就在这里,这里。
[35:22] 好了,我们快完成了。所以你现在可以继续保存它,然后你需要把它提交到 Gradio……(不是 Gradio……)到 HuggingFace Spaces。所以一个非常简单的方法就是,在 Visual Studio 本身里,你只需点击这里,它就会给你一个地方让你输入消息,然后你点勾,它就会帮你把它发送到 HuggingFace Spaces。做完之后,你就可以回到……回到……你之前所在的同一个网站:“huggingface.co/spaces/jhp00/minimal”,你会发现现在——构建你的网站大约需要一分钟。而且它……
[36:15] 它正在构建的网站将有一个 Gradio 界面,包含一个文本输入、一个文本输出,并且会对输入运行一个名为“greet()”的函数,而我的名为“greet()”的函数将返回“Hello {name}”。所以这就是它要做的事。现在它开始了,我们来试试!我们来向 Tanishq 问好。我不太擅长记住他名字怎么拼,我想应该是这样的。
[36:50] 这样就可以了,你可以看到它已经为我们的输入给出了输出。所以这个应用不算很惊艳,但公平地说,我们现在已经有一个在生产环境中运行的应用了,对吧?现在,我说过我们会有一个在生产环境中运行的深度学习模型。所以现在我们要迈出下一步,把它变成一个深度学习模型。好的,首先我们需要一个深度学习模型。获取深度学习模型有几种不同的方式,但基本上我们得训练一个。所以,我准备了几个示例:一个 Kaggle 示例和一个 Colab 示例。也许我会快速把两个都展示给你。它们其实是一样的。我就来创建一个猫狗分类器。好的,这是我们的 Kaggle 模型,我点击编辑,这样你就能看到它在编辑视图中的样子。
[37:52] 现在,Kaggle 已经预装了 fastai,但我总是先执行这一句,以确保我们用的是最新版本。然后显然是导入一些东西,我们……所以我们要获取 pets 数据集,一个用来判断是不是猫的函数,这就是我们给 ImageDataLoaders 用的标注函数——记住这只是做 DataBlocks 的另一种方式,算是一种简写。然后我们创建 learner,并对其进行微调。好的,这些都是我们之前见过的东西。所以在 Kaggle 中,每个 notebook 都有一个编辑视图(就是你刚才看到的)和一个阅读视图。
[38:27] 所以你可以分享你的 notebook——如果你想的话。然后任何人都可以阅读阅读视图,如你所见。所以在这里,你可以看到它展示了运行时的结果。我训练了它,花了……你知道,Kaggle 上的 GPU 比大多数现代 GPU 慢一些,但仍然足够快,我的意思是,大概花了五分钟。最后这里还有一点你之前没见过的内容,就是我用了 learn.export 并给它指定了一个名字。
[39:02] 现在,这会创建一个包含我们训练好的模型的文件,而且……就这些!创建这个文件是你唯一需要 GPU 的地方,对吧?所以你在 Kaggle 或 Colab 上做这件事。这是 Colab 上完全一样的内容……你可以看到“pip install”、is_cat、我们的数据、ImageDataLoaders,我这里还加了一个 show_batch,只是为了好玩。创建我的 learner,然后导出。所以,在等待的时候,我可能就直接把它跑起来。
[39:36] Kaggle 的一个好处是,一旦你运行并保存了它,就可以转到数据标签页,基本上你保存的任何东西都会出现在这里,看,这就是:model.pickle,对吧?所以现在我可以直接下载它,然后它就会被下载到我的下载文件夹里。接着我需要把它复制到我的 HuggingFace Spaces 应用所在的同一个目录中。现在,我的 HuggingFace Spaces 应用正在我的终端里打开着,在 Mac 上你可以输入“open .”,在 Windows 上你可以输入“explorer.exe .”,这样就会在那个目录里打开你的访达或资源管理器。然后你就可以把你下载的那个东西粘贴到这个目录里。顺便说一句,在 Windows 上我会做一件我觉得非常有帮助的事,就是把我 Linux 下的主目录
[40:43] 固定到我的快速访问里,这样我在 Windows 上就总能直接跳到我的 Linux 文件。这在 Mac 上基本不用担心,因为一切都是集成的,但在 Windows 上它们就像是两台独立的机器。好,那么……我们来做……我创建了一个叫 testing 的 Space,我下载了我的“model.pkl”并把它粘贴到了 testing 里。所以现在我们需要知道如何对已保存的模型进行预测?
[41:23] 我们为此准备了一个 notebook……好,我们为此准备了一个 notebook,那么……我将带你了解如何使用我们训练好的模型来进行预测。hash pipe 有一些奇怪的地方,我稍后会解释,现在先忽略它们。我们像往常一样导入 fastai。我们像之前一样导入 Gradio。然后我们复制进来和之前完全相同的 is_cat 定义。这很重要,你在标注中用到的任何外部函数都需要在这里也包含进来,因为那个 learner 会引用这些函数,明白吗?它会保存……那个 learner 会保存你模型的所有内容,但它不包含函数的源代码,所以你需要把这些函数保留在身边。
[42:12] 那么,我们来试着运行一下。比如,我刚刚抓取了一张……(你可能在我的资源管理器里看到了)我刚刚在那里放了一张狗的照片。于是我们可以用这张狗的照片创建一个 Python 图像库图像,把它稍微缩小一点,这样它就不会占满我的整个屏幕,然后这里就有一张狗的照片了。那么,我们如何预测它是狗还是猫呢?嗯,非常简单,我们所要做的就是……不是训练一个学习器,而是使用 load_learner()。我们传入保存的文件名,它就会返回一个学习器。这个学习器与你训练结束时得到的学习器完全相同。所以这里就是 Colab,对吧?我们刚刚一直在训练一个学习器,所以在训练结束时,就有一个已经训练好的学习器,于是我们……我们把它在时间上冻结起来,也就是所谓的 pickle 文件——这是 Python 的一个概念。它就像一个被冻结的对象,我们把它保存到磁盘,传输到
[43:17] 我们的电脑上,现在又把它加载了回来,并且把它解冻了。这就是我们反序列化后的学习器,现在我们可以对它做任何想做的事。那么其中一件事……学习器拥有的一个方法就是点 predict 方法,所以,如果我运行它,你可以看到,即使在我的笔记本电脑上,它基本上也是瞬间完成的。事实上我们可以看看它花了多长时间。如果你——在 Jupyter 中,以百分号开头的东西叫做魔法命令,它们是 Jupyter 的特殊功能,比如,有一个东西可以查看某件事花了多长时间。
[43:51] 好了,那么它花了 54 毫秒来判断这不是一只猫。所以它返回两样东西:它是猫吗?(以字符串形式);它是猫吗?(以零或一的形式),然后是它是狗的概率,以及它是猫的概率。所以是猫吗?这个问题的零(假)和一(真)的概率,所以肯定是狗。那么我们现在想创建一个 Gradio 界面,基本上包含这些信息。
[44:28] 所以 Gradio 要求我们给它一个函数,它会去调用这个函数,这就是我们的函数。我们要调用 predict,正如我们所说,它返回三样东西:预测结果是一个字符串;它的索引;以及它是狗还是猫的概率。而 Gradio 想要的是,它想要得到一个字典,其中包含每个可能的类别——在这个例子中是狗或猫——以及每个类别的概率。所以,如果你以前没怎么用过 Python,那么 zip 的字典可能是你没见过的东西。
[45:04] 非常实用的小技巧,值得一看。如果你之前没见过 map,也一样,总之,就在这里。目前 Gradio 有一个稍微烦人的地方,就是它不处理 Pytorch 张量,你可以看到这里 Pytorch 返回的不是普通数字,而是张量。它甚至不返回 Numpy 数组。事实上 radio 也处理不了 Numpy,所以我们得把所有东西都改成普通浮点数,这就是这里做的全部事情,就是把每一个都转成浮点数。所以,举例来说,如果我现在用我们的小狗图片调用 classify_image(),我们得到一个字典:狗,是的,绝对是;猫,绝对不是。
[45:45] 现在我们已经有了这些,就可以继续创建一个 Gradio 界面了。Gradio 界面就是这样的东西:我们说明要调用哪个函数来获得输出,输入是什么——在这个例子中我们说,哦,输入是一张图片——。所以去看看 Gradio 文档吧,它可以是各种东西,比如网络摄像头图片、文本,或者你知道的各种东西。给它一个要放入的形状,输出只是一个标签,所以我们要创建一个非常非常简单的界面。我们还可以提供一些示例,所以这里有一只狗、一只猫,还有一个“不知道”——我稍后会告诉你——你会在这里看到有一只狗、一只猫,和一个“不知道”。
[46:27] 所以一旦我启动它,它就会说,好的,现在运行在这个 URL 上。所以如果我打开它;你现在可以看到,就像 Suvash 一样,我们有了自己的——还没上生产环境,但运行在我们自己的机器上——分类器。让我们检查一下:狗,所以你可以点击它并上传一张,或者直接选择示例……它们来了!所以它运行在我自己的笔记本电脑上,基本上是瞬间完成。我真的得给你讲讲这里这家伙的故事,这就是那个“不知道”。
[47:01] 提交,等等,为什么它显示 100?通常这里会显示类似 50 50。真扫兴,这个模型……把我的整个故事都搞砸了。上次我训练这个模型,然后在……“不知道”上运行它,它说,它说几乎正好是 50 50。而我们找到这张图片的方式是……我给我六岁的女儿看了,她说:“爸爸你在干什么?”,我说,我在写代码,“你在写什么代码?”哦,你知道的,狗猫分类器。她看了看,她的第一个问题是:“我能用一下你的键盘吗”,然后她去了谷歌,她说:狗和猫混在一起叫什么。就好像狗和猫混在一起这种东西根本不存在。总之,她去了图片标签页,找到了这张图片,然后她说:“看,有一只狗和猫混在一起。”
[47:47] 她说:“拿那个爸爸来跑一下!拿那个跑一下!”我跑了之后结果就像:50-50。它完全不知道那是狗还是猫。现在,这个模型我今天刚重新训练过,现在它确定那是猫。所以你看,我想我用了一个稍微不同的训练计划之类的,或者多给了它一个 epoch,总之,那是一只狗和猫,但显然它是猫。我猜它就是猫。它大概是对的。我不该训练它那么久。好,这就是我们的界面。现在,它实际上在运行,所以你得点击停止按钮才能让它停下来,否则你在 notebook 里什么都做不了。好,现在我们要把它变成一个 Python 脚本。那么,把它变成 Python 脚本的一种方式,就是把所有你需要的东西复制粘贴到一个 Python 脚本里。
[48:42] 我需要把这里面你需要的所有部分都复制粘贴到一个 Python 脚本里,所以,比如说,我们不需要这个——它只是用来检查某些东西的,我们不需要这个——它只是做实验。这只是做实验。我们需要这个,对吧?所以,我所做的是……我逐一遍历,在每个包含我最终脚本所需信息的单元格顶部写上“hash pipe export”,然后——所以这些就是步骤,对吧?然后,在最下面这里,我从 nbdev 导入了一个叫 notebook2script 的东西,如果我运行它,并传入这个 notebook 的名字,它就会为我创建一个叫 app.py 的文件,包含那个脚本。所以这是一种很好、很简单的方式——比如当你处理的东西需要的是脚本而不是 notebook 时,就像 HuggingFace Spaces 那样。如果你愿意,直接复制粘贴到一个文本文件里也没问题,
[49:42] 但我真的很喜欢这种方式,因为这样我就可以在 notebook 里做所有的实验,做完之后,我只需要在底部有一个单元格,运行一下就能导出它。它怎么知道要叫 app.py 呢?那是因为顶部有一个特殊的东西 default export:default_exp,它说明了要创建什么 Python 文件名。所以这只是我用的一点小技巧。现在我们已经有了一个 app.py。我们需要把它上传到 Gradio,怎么做呢?你只要……你只要把它推送到 Git 就行了。所以……你可以用 Visual Studio Code 来做,或者输入“git commit”。
[50:28] 然后执行“git push”。完成之后,……如果我们把 minimal 改成 testing,……我想这个可能还在运行,我之前的模型——因为我没有推送它,这样我们就能看到我们那只疯狂的狗猫了。好了,它就在这里,你可以看到它在生产环境中运行。所以现在这是任何人都可以——,如果你把它设为公开——,任何人都可以到这里来查看你的模型,然后他们可以上传它。那么,这是我的狗狗。
[51:02] 没错,绝对是狗。猫,对,我想我可能少训练了一两个 epoch,所以它没那么自信。对,绝对是猫。狗猫……嘿,狗猫……嗯,还是认为它绝对是猫。唉,真奇怪。好了,那么,这就是……好了,那么,这就是把一个简单模型投入生产的一个例子。论坛上、社区里有几个问题。
[51:49] 好,有人问:“pytorch 模型和 fastai learner 有什么区别?”好,没问题,我们很快就会讲到。不确定是不是这一课,可能是这一课或下一课。然后另一个人问的,基本上是:“我们要训练多少个 epoch?”所以,当你训练模型时,错误率,如你所见,会改善。
[52:21] 所以问题是:我应该多跑一些吗?我应该增加 epoch 的数量吗?这里跑的是三个 epoch,对吧?这是我开始用的三个 epoch 加一。看,这取决于你,对吧?我的意思是,对于这个,这里显示:有百分之一的错误率。我……我对百分之一的错误率还算满意,你知道,如果你想让它更好,那你可以用更多的数据增强,并且训练更长时间。如果你训练得足够久,我们很快就会学到,然后……也许是下一课。如果你训练得足够久,你的错误率实际上会开始变差。而且,你会看到,我们会学到原因。所以基本上,是的,你可以一直训练到它足够好,或者直到你失去耐心、或时间、或算力用完,或者直到你……或者直到错误率开始变差。
[53:14] 好,哦,那么在 Colab 里,你怎么拿到你的模型?在 Colab 里你只需要,在导出之后,如果你进入他们的文件浏览器,你实际上会在这里看到它,对吧?然后你可以点击下载。有点奇怪,它不会像弹出个框问你要下载到哪里,而是弹出这种进度圆圈的东西,所以取决于它有多大等等,可能要花几分钟。一旦那个圆圈填满,然后浏览器的东西最终会弹出来说,好,你可以保存它了。好,那就是你如何……
[53:57] 这就是你实际获取模型的方式。所以如你所见,真正需要 GPU 的那一步,你可以使用这些完全免费的资源:Colab、Kaggle,还有其他一些我们会在后续课程中讲到的。然后其他所有事情都可以在你自己的电脑上完成,包括预测,预测很快,对吧?所以你真的不需要为此使用 GPU,除非你要做成千上万次。好,来了,现在它要求我保存。好,那么现在一个大问题是,我们需要……在我们的电脑上运行它……我们需要在电脑上运行 Python 和 Jupyter Notebooks,那你怎么做呢?
[54:41] 因为这里往往是人们陷入各种麻烦的地方……“我想弄清楚怎么让这一切跑起来。”好消息是,我们其实有一个东西能让这件事变得非常简单。它叫 fastsetup。你真正需要的其实只有其中一部分,所以让我给你看看……它……它……其实是 Github 上的一个 Git 仓库。Github 是大多数 Git 仓库所在的地方。所以如果你访问 Github 上的 fastai/fastsetup,你就会看到它。那么你可以做的是,现在就可以获取整个仓库,只需点击这里的 code,如果你安装了 Github Desktop,点击 open with Github Desktop,如你所见,它会弹出提示:好,我准备好为你保存这个了,于是我点击 clone,它就在复制一份。好了……
[55:39] 所以基本上,克隆之后,你会发现在里面有一个叫 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,安装。是的,好的,我们实际上想要执行“conda install -c fastchan fastai”。所以我们直接复制粘贴。哦抱歉,不是真的……然后,我要说的另一件事是,不要用 conda,把 conda 换成 mamba,因为现在它快得多。所以“mamba install -c fastchan fastai.”。
[57:52] 现在,这会安装你所需的一切。它会安装 Pytorch,它会安装 Numpy,它会安装 fastai……等等,对吧?而且……所以显然我已经装好了。然后你还要做另一件事是安装 nbdev,这样你可以对 nbdev 做完全相同的事情。你不一定非得这么做,对吧?只是……但那会顺便为你安装 Jupyter 以及其他东西。
[58:28] 所以到那时你就可以,你现在就可以使用 Jupyter 了。Jupyter 的工作方式是——你可以在这里看到。这是我的……(我先把它关掉,这样我们可以重新开始。)所以基本上,要使用 Jupyter,你只需输入“jupyter notebook”,好吗?当你运行它时,它会说:好的,我们现在为你运行一个服务器,所以如果你点击那个超链接,它会弹出这个,好吗?,这正是你一直看到我使用的东西。好的,所以,你知道,希望这足以让你开始使用 python 和 Jupyter Notebook……
[59:17] 人们安装软件的另一种方式是用一个叫 pip 的东西,而不是 mamba。几乎任何你能用 mamba 做的事情,你也可以用 pip 做,但如果你有 GPU,pip 一般不会安装能在你 GPU 上运行的东西,所以那,它能在你的 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……就这些!它很容易就能放进一屏里,对吧,而且基本步骤也不算复杂,对吧,基本上就是……我们为照片创建一个 import,添加一个事件监听器,说当你更换照片时调用 read 函数,read 函数说创建一个 file reader,读取文件,当你完成加载时,调用 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 网站最简单的方法是使用一个叫 fastpages 的东西,它是 fast.ai 的一个项目!而且……基本上你要做的就是……按照设置流程操作。
[1:11:43] 所以首先它会做它的……我们就直接过一遍……它说通过点击这个链接来生成一个副本。所以我点击这个链接,好的,没问题,给它起个名字……(我尽量把所有东西都设为公开!我一直觉得这是个好习惯……你不必这样做)……创建仓库,生成中,好的,然后基本上还有两个步骤,大约需要五分钟。我们没有五分钟,所以我给你展示我已经构建好的那个,也就是“fast.ai/tinypets”。所以一旦完成,你基本上会得到这个空站点,同样,你只需点击“代码”“用 github desktop 打开”,或者“用 visual studio 打开”,随便哪个……所以“用 github desktop 打开”……
[1:12:37] 或者你可以复制 & 粘贴到你的终端……所以其中任何一种方式都会把整个东西放到你的电脑上。你可以在那里保存你的 HTML 文件……再推送回 github。你会发现我们……我们……,FastPages 会给你展示为你创建的网站的链接。现在,为你创建的网站,你可以用叫做“主题”的东西让它看起来随心所欲。所以你会看到它创建了一个叫 config.yml 的文件,你可以在那里选一个主题。所以,在这个例子里,我选了一个叫“alembic”的主题,没什么特别的原因。
[1:13:18] 所以 Github Pages 使用一种叫“Jekyll”的东西,所以任何“Jekyll”主题基本上都能用。所以我选了这个主题,因此,当我现在把东西保存到这个仓库时,它们会自动出现在这个网站上,文件也会自动出现在上面的这个列表里。所以,如果你看我的 index,那就是主页……整个文件就只是这个,对吧!唯一稍微有点奇怪的是,在每个 Github Pages 文件的顶部,你必须有三个短横线、title 和 layout,再来三个短横线。这叫做“front meta”。所以,一旦你这样做……并保存,它就会出现在你的网站上。所以,然后我还做了另一件事,我想,好吧……fast.ai 创建了这个网站固然很好,但我并不太喜欢它的样子。我想创建一个不同的版本。
[1:14:18] 不用担心。你可以访问“fast.ai/tinypets”并点击“Fork”。点击“Fork”后,它会创建你自己的副本。所以,我在我的个人账户 jph00 下做了这个操作。看!现在我有了自己的版本,现在我可以在这里做修改了。所以我做了一些改动。其中一个改动是:我进入 config.yml,把主题改成了“pages-themes/hacker”。所以一旦你 fork 了,有一件事你必须做,通常 FastPages 会帮你做,那就是你必须进入“Settings”,点击“Pages”,然后实际启用“Github Pages”。基本上你必须这样做,默认情况下它是关闭的,所以这里你只需要把它打开。使用“Master Branch. Root Save”。然后它会显示“不用担心,已经准备好发布了”。所以我改了 config.yml 文件来指向不同的主题。所以,如果你现在看……jph 的 Tiny Pets……
[1:15:17] 它不一样了!对吧?所以它有相同的信息,但更加“黑客范”,因为 jph00 是一个严肃的黑客,从它的网站就能看出来。所以,总之,看,这是对 Javascript 和网站等等这个世界的一个非常简短的体验,但我想让你感受到,你知道,你不需要任何钱。你不需要任何 IDE。你知道,你其实不需要太多代码就能开始编写你自己的 Web 应用,而且,多亏了 Hugging Face Spaces,你知道,他们甚至会为你托管你的模型,你只需要有那个魔法字符串,作为一个可调用的东西。好的!
[1:16:07] 那么……下线了:黑客 Jeremy Howard。非常感谢你的观看,在下一课中,我们将深入研究一些自然语言处理。我们将做类似的事情,但我们将用语言而不是图片来做。我们将深入底层,看看这些模型实际上是如何工作的。我们将学习随机梯度下降之类的东西,我们甚至可能需要温习一点微积分。我希望我说出那个“微”字没有把你吓跑。
[1:16:38] 下次见!谢谢大家!再见!
查看英文原始字幕
[0:00] Hi everybody. Welcome to lesson two. Thanks for coming back… slight change of environment here, we had a bit of an “administrative issue” at our university — somebody booked our room — so I'm doing this from the study at home. so sorry about the lack of decorations behind me. I'm actually really really pumped about this lesson. It feels like going back to what things were like in the very early days, because we're doing some really new, really cool stuff, which…
[0:41] you know… stuff that hasn't really been in courses like this before. So, I'm super super excited. So thanks a lot for coming back after lesson one, and I hope it's worth you coming back. I think… I think you're gonna love it! I am, yeah, I'm really excited about this. Now remember that, you know, the course goes with the book, so be sure that you're following. I mean, not following along in the book, because we're covering similar things in different directions… but, like, read the book as well, and remember the book is entirely available for free as well. You can go to the fast.ai fastbook repo to see the notebooks, or through course.fast.ai you can read it there — for example, through Colab.
[1:28] And also remember that the book… I mean… the book has a lot of stuff that we didn't cover in the course like, you know, stuff I find pretty interesting about the history of neural networks, some of which has some really interesting personal stories actually, as you'll read here. And at the end of each chapter there is a quiz. Remember it's not a bad idea before you watch the video to read the quiz, so if you want to read the Chapter Two quiz, you know, and then come back, that's not a bad idea. Then make sure that you can do the quiz after you've watched the video, and you've read chapter two of the book.
[2:07] Something I didn't mention last week is there's also a very cool thing that Radek — who I mentioned last week — has written, called aiquizzes.com which is a site specifically for quizzes about the book. And it actually uses repetitive based learning techniques to make sure that you never forget. So, do check out aiquizzes.com. It's all brand new questions, they're different to the ones in the book, and they're really nicely curated, and put together. So check out aiqquizzes.com as well. Remember, as well as course.fast.ai, there's also forums.fast.ai. So course.fast.ai is where you want to go to get, you know, links to all the notebooks, and kaggle stuff, and all that stuff. You'll also find on forums.fast.ai every lesson has an official topic, you know, with all the information you'll need. Generally there'll be a bit more info on the forums, we try to keep the course
[3:09] lean and mean, and the forums are a bit more detailed. So if you find — in this case you are gonna look for the Lesson Two official topic but here's the Lesson One official topic so far. So from the Lesson One official topic, already, after just a few days since I recorded it — we haven't even launched the course so it's just the people doing it live — there's already a lot of replies, and that can get pretty overwhelming.
[3:40] So be aware that there's a button at the bottom of my post that says summarize this topic. And if you hit that, then you'll just see the most upvoted replies, and that's a really good way to just make sure that you hit on the main stuff. So there's the button, and here's what it looks like after you hit it. You'll just get… you'll just get the upvoted stuff from fast.ai legends like Sanyam and Tanishq. So hopefully you'll find that a useful way to use the forum.
[4:14] So one of the cool things about this week is I… as promised, put up the “show us what you've made” post, and already a lot of people have posted. I took this screenshot a few days ago, it's way above 39 replies already, if I'm… if I remember correctly. I had a lot of trouble deciding which ones to share because they're all so good, so I've actually decided to kind of, you know, go the easy route, and I just picked the firsts. So I'm just going to show you the first ones that were posted because they're all so good. So the first, the very very first one to be posted is a damaged car classifier… so that worked out pretty well — it looks like. And I really liked what Matt, the creator, said about this, is that… you know, wow, it's a bit uncomfortable to run this code, I don't really understand yet but I'm just doing it. And so I'm like, yeah, good on you Matt for just…
[5:14] for just doing it. That's the way to get started, it's all going to make sense, don't worry. Very nice to see that the next one posted was actually a blog post in fast pages — very nice to see — so just describing some stuff, some experiments that they ran over the week and what did they find. Next one was the amazing beard detector which if I understand correctly was mainly because it's very easy to get from bird to beard by just changing one letter to two, and this is doing a very good job of finding gentlemen with beards. Very nice. And then this one is…
[5:51] another level again, it's a whole in-production web app to classify food, which is kind of like extra credit . Apparently we're up to 80 replies now in that thread, thank you Sanyam, very cool. So, you know, obviously… so this was actually created by Suvash who's been doing the courses for a few years now, I believe. And so you know, one day you too might be able to create your very own web app and put it in production. And when I say one day, more specifically… today! I'm actually going to show you how to do this right now! So it's actually quite a lucky coincidence that Suvash put this up there because it's exactly the topic that we're going to pick today.
[6:46] So, how do we go about putting a model in production? Step one, is… well… you've kinda done step one, right? Step one is… step one, two, three, four… is to figure out what problem you want to solve, figure out how to find the data for it, go gather some data, and so forth. So what's the kind of first step after you've got your data? The next step is data cleaning, and if you go to Chapter 2 of the book, which I'm going to go ahead and open up now…
[7:23] So here is the book… so you can open it in Colab directly from the course, or if you've cloned it to your computer, or whatever, you can… you can do it there. So remember, course.fast.ai will run you through exactly how to run these notebooks, and so you can see Chapter 2 is all about putting stuff in production, and so here is Chapter 2. All right, and so remember we hit Shift + Enter to run cells, okay? …to execute them. And so we're going to go to the part of the book where we start cleaning the data.
[7:59] So I'll click on Navigate, and we'll go down here… gathering data… there we are! So we could do a quick bit of revision first. By the way, I will mention, a lot of people ask me what are the little tricks I use for getting around a Jupyter notebook so quickly and easily. One of the really nice ones as you'll see is this navigate menu, which actually doesn't appear by default. So if you install something called Jupyter Notebook Extensions - Jupyter…
[8:30] Notebook… Extensions… and so you just… you just “pip install” them, follow the instructions and then restart Jupyter. Obviously this… Colab already has a table of contents by the way so this is just if you're using something local, for example, then you'll see here that this “Nbextensions” thing will appear, and if you click on “Table of Contents (2)” that gives you this handy navigation bar. The other thing I really like is this one here called collapsible headings, and that's the one which gives me these nice little things here, to close, and open up. And actually that's not even the best part. The best part for me is if I hit the right arrow it goes to the end of a section, and if I hit the left arrow it goes to the start of a section. So it's like, if I want to move around sections I just press up left, down right, down right, very handy,
[9:35] and if you hit left again when you're here it 'll close it up. Hit right again here, open it up. So that's collapsible headings. Anyway, a couple of really handy things, and we'll be talking a lot more about getting your notebook set up today in a moment. Okay, so one thing you'll notice is in the book, we use the Bing API for searching images. I've just gone ahead, and replaced “bing” with “ddg” because the Bing API requires getting an SDK key which honestly, it’s like the hardest thing in deep learning is figuring out the Bing Azure website, and getting that sorted out. ddg doesn't! So it's basically exactly the same… (and you can…)
[10:21] I'll share this notebook as well, on the course website in the forum but all I've basically done is I've replaced bing with ddg, and got rid of the key. So then, just like we did last week, we can search for things, and so in the book we did a bear detector because at the time I wrote it my then toddler was very interested in me helping identify teddy bears, and I certainly didn't want her accidentally cuddling a grizzly bear, so we show how here how we can search for grizzly bears, just like last week.
[10:56] Something that loops through grizzly bears, black bears, and teddy bears, just like last week. Get rid of the ones that failed, just like last week. And one thing a few people have asked on the forum is how do i find out more information about basically any python or fastai or pytorch thing. There's a few tips here in the book. One is that if you put a double question mark next to any function name you'll actually get the whole source code for it, and by the same token if you put a single question mark you'll get a brief…
[11:41] you know… a little bit of information. If you've got nbdev installed (I think it's nbdev you need) then you can type doc, and that will give you perhaps most importantly a link straight to the documentation where you can find out more information and generally there will be examples as well. And also a link here to the source code, if you want to…
[12:13] (let's do that with a control okay) …a link to the source code and that way you can jump around. And notice that in github in the source code you can click on things and jump to their definition. So it's kind of a nice way of skipping around to understand exactly what's going on. Okay, so lots of great ways of getting help. But what I promised you is that we're going to now clean the data. So I'm going to tell you something that you might find really surprising. Before you clean the data, you train a model.
[12:55] Now, I know that's going to sound really backwards to what you've probably heard a thousand times, which is that first you… ~(build/you/train) …you clean your data and then you train your model. But I'm going to show you something really amazing. First we're going to train a model and you'll see why in a moment. So to train a model, just like before, we use a DataBlock to grab our data loaders — there's lots of information here in the book about what's going on here. There we go… and so then we can call show_batch to see them as per usual.
[13:33] There's a little side bar here in the book, I'll quickly mention, which is about the different ways we can resize. I think we briefly mentioned it last week… we can squish… last week I used a string, you can use a string or this kind of enum like thing that we have. You can see with a squish you can end up with some very thin bears, right? So this is the real size, the shape, of the bear. Here its become thin but you can see now we've got all of its cubs… (are they called clubs? yeah bear cubs) …so it squished it to make sure we can see the whole picture. Same one here. This one was out of the picture, we squished it and this guy now looks weirdly thin, but we can see the whole thing. So that's squishing!
[14:12] Or else this one here is cropping. It's cropped out just the center of the image so we get a better aspect ratio but we lose some stuff. This is so we can get square images. And the other approach is we can use “pad.” And so you can pad with various different things. If you pad with zeros, which is black, you can see here now we've got the whole image and the correct aspect ratio. So that's another way we can do it and you know, different situations, you know, result in different quality models. You can try them, all it doesn't normally make too big a difference.
[14:44] So I wouldn't worry about it too much. I'll tell you one though that is very interesting is RandomResizedCrop. So instead of saying resize, we can say RandomResizedCrop. And if we do that you'll see we get a different bit of an image every time. So, during the week… this week somebody asked on the forum… I'm trying to… (this is a really interesting idea, which it turned out worked slightly) they wanted to recognize pictures of French and German texts. So obviously this is not the normal way you would do that, but just for a bit of experiment (and I love experiments, so) they had very big scans of documents and they wanted to figure out whether it was french or german just by looking at images and they said the pictures were too big what should I do? I said, use RandomResizedCrop and that way you'd grab different bits of the image, and this is very nice because you could run lots and lots
[15:44] of epochs and get slightly different pictures each time. So this is a very good technique. And this idea of getting different pictures each time from the same image is called “data augmentation.” And again, I'm not going to go into too much detail about data augmentation, because it's in the book, but i'll just quickly, you know, point out here that if you use this thing called “aug_transforms” so augmentation transforms, and here I have multiplied them by two – so I've made them super big so you can see them more easily. You can see that these teddies are getting turned, and squished, and warped, and recolored, and saturated, all this stuff to make every picture different. And generally speaking if you're training for more than about five or ten epochs, which you'll probably want to do most of the time unless you've got a super easy problem to solve, you'll probably want to use RandomResizedCrop and these “aug_transforms”. Don't put the mult=2,
[16:49] just leave that empty. I'm just putting it there so you can see them more clearly. So I've got an interesting question here from Alex in our audience, which is: “Is this… is this copying the image multiple times? …doing something like this? …or something like this?” and the answer is “no we're not copying the image. What happens is that image… so each epoch, every image gets read. And what happens here is though… is… is kind of… in memory, in RAM, this… the image is being “warped”, right? it's being… we're cropping it and recoloring it, and so forth. So it's a real time process that's happening during model training. So, there's no copies being stored on your computer, but effectively it's almost like there's infinitely slightly different copies because that's what the model ends up seeing. So, I hope that makes sense Alex, and everybody else,
[17:52] so that's a great question! Okay, so we've got… we're going to use RandomResizeCrop, we're going to use augmentation transforms, so that we can get at DataLoaders from that and then we can go ahead and train our model. It takes about a minute. In this case we only did four epochs of fine tuning, and we'll talk about why there's five here later in the course, but four main epochs of fine tuning. So we probably didn't really need RandomResizedCrop and aug_transforms because there's so few epochs, but you know, if you want to run more epochs this is a good approach. Under three percent error! that's good! Okay, so remember I said we're going to train a model before we clean?
[18:36] Okay so let's go ahead and train it. So while that's training… so it's running on my laptop, which only has a 4GB GPU, it's pretty basic, but it's enough to get started. While that's training we'll take a look at the next one. So the first thing we're going to look at is the confusion matrix and the confusion matrix is something that… it only is meaningful for when your labels are categories, right!
[19:09] And what it says is how/what category errors are you making. And so this is showing that the model that we've got at this point… there were two times when there was actually a grizzly bear and it thought it was a black bear, and there was two times when there was actually a black bear, and it thought it was a grizzly bear, and there was no times that it got teddies wrong, which makes sense right because teddies do look quite different to both. In a lot of situations when you look at this, it'll kind of give you a real sense of, like okay, well what are the hard ones? right? So for example if you use the pets data set that we quite often play with in the book and the course, this classification [**] matrix for different breeds of pets, you know, really shows you which ones are difficult to identify, and I've actually gone in, and like, read wikipedia pages, and pet breeding reports about how to identify these particular types, because they're so difficult, and even experts find it difficult.
[20:10] And one of the things I've learned from doing the course actually is… black bears and grizzly bears are much harder to pick apart than I had realized... so I'm not even going to try! But I'll show you, the really interesting thing we can do with this model, is that now we've created this classification interpretation object, which we use for confusion matrix, we can say plot_top_losses. We can say plot top losses, and this is very interesting. What it does is, it tells us the places where the “loss” is the highest. Now if you remember from the last lesson, the loss is that measurement – of how good our model is – that we take after each time we run through an item of data. A loss will be “bad” if we predict wrongly, and we're very confident
[21:11] about that prediction. So here's an example where we predicted… (here's the order here: prediction, actual loss, probability) …where we predicted grizzly and it was actually a black, and we were 96% sure (our model was) that it's a grizzly. Now, I don't know enough about bears to know whether the model made a mistake or whether this actually is a picture of a grizzly bear… but so, an expert would obviously go back and check those out…
[21:41] right? Now, you'll notice a couple here... it's got grizzly/grizzly, teddy/teddy… they're actually correct, right? So why is this loss “bad”? When it's correct? And the reason is… because it wasn't very confident. It was only 66% confident, right? So here's a teddy… it's only 72% confident right? So you can have a bad loss, either by being wrong and confident, or being right and unconfident. Now the reason that's really helpful, is that now we can use something called the fast.ai ImageClassifierCleaner to clean up the ones that are wrongly labeled in our data set.
[22:24] So when we use the ImageClassifierCleaner it actually runs our models. That's why we pass it “learn”, right? And I mentioned that I don't know much about black bears and grizzly bears but I do know a lot about teddy bears, so I'll pick teddy bears. And if I click teddy bears it's now showing me all the things in the training set (you can pick training or valid) that were marked as teddy bears and here's what's really important: they're ordered by loss, so they're ordered by confidence, right? So I can scroll through just the first few and check they're correct, right? And oh, here's a mistake, right? So when I find one that was wrongly gathered I can either, put it…if it's in the wrong category, I can choose the correct category… or, if it shouldn't be there at all, I click delete. So here I'll go ahead and click delete, right?
[23:14] So you can see some reasons that some of these are hard, like for example here's two teddies, which is just, I guess, confusing since it doesn't see that often. This one here is a bit weird looking, it looks almost like a wombat. This is an awful lot of teddies. This one maybe is just a bit hard to see from the background, but these, otherwise, they look fine… Fine, so we just look through the first few and if you don't see any problem or problems in the first year, you're probably fine. So that's cleaned up our training set, let's clean up our validation set as well. So here's that one it had trouble with, I don't know why it had trouble with that one but so be it and we'll have a quick scroll through…
[23:59] Okay I'm not really sure that's a bear so I'm just going to go ahead and delete it. It's a teddy something… but, you know, it's a problem. Okay, that's not a teddy either… So you see the idea, right? So after we've done that, what that does is… the cleaner has now stored a list of the ones that we changed and the list of the ones we deleted, so we can now go ahead and run this cell. And so that's going to go through a list of all of the indexes that we said to delete, and it will delete those files. And we'll go through all the ones we set to change, and it will move them to the new folder, there we go, done!
[24:45] So, this is like, not just something for image models. It's just… it's actually… a really powerful technique that almost nobody knows about and uses, which is, before you start data cleaning, always build a model to find out what things are difficult to recognize in your data and to find the things that the model can help you find data problems. And then as you see them, you'll kind of say “okay, I see the kinds of problems we're having” and you might find better ways to gather the next data set, or you might find ways to kind of automate some of the cleaning and so forth.
[25:23] Okay, so, that is data cleaning and since I only have a 4GB GPU it's very important for me to close and halt, because that will free up the memory. So it's important to know… on your computer your normal RAM doesn't really get filled up, because if you use up too much RAM, what will happen is that instead your computer will start (it's called) swapping, which is basically to save that RAM onto the hard disk to use it later. GPUs can't swap.
[26:00] GPUs, when they run out of RAM, that's it, you're done. So you need to make sure that you close any notebooks that are using the GPU that you're not using and really only use one thing at a time on the GPU, otherwise you're almost certainly run out of memory. So we've got the first few reds starting to appear. So remember to ask and, in terms of the yellows, it's important to know, as you watch the video, I'm not asking you to run all this code, okay? The idea is to kind of watch it, and then go back and pause, you know, as you go along. Or you can just stop, try, stop, try. The approach I really like — and a lot of students really like — for watching these videos is to actually watch the entire thing without touching the keyboard, to get a sense of what the video is about, and then go back to the start and watch it again and follow along. That way, at every point you know what it is you're
[27:00] doing, you know what's going to happen next. That can actually save you some time. It's a bit of an unusual way because, obviously like, real life lectures you can't do that, you can't rewind the professor and get them to say it again, but it's a good way to do it here. So now that we've cleaned our data, how are we going to put it into production? Well in the book we use something called Voilà. And it's… it's pretty good! But there's actually something that I think most of you are probably going to find a lot more useful nowadays, which is something called HuggingFace Spaces, and there's a couple of things you can use with that. We're going to look at something called Gradio today.
[27:42] And there isn't a chapter about this in the book but that doesn't matter because Tanishq Abraham — who's actually one of the TAs in the course — has written a fantastic blog post about really everything we're going to cover today. So there's a link to that from the forum and from the course page, so this is like the equivalent of the chapter of the book, if you like. And I would be remiss if I didn't stop for a moment and call out Tanishq in a big way for two reasons: the first is he is one of the most helpful people in the fastai community, he's been around quite a few years, incredibly tenacious, thoughtful and patient; and also because I have this fantastic picture of him a few years ago with Conan when he was a famous child prodigy, so now you know what happens to famous child prodigies when they grow up – they became even more famous fastai community members and deep learning experts.
[28:41] So you should definitely check out this video of him telling jokes to Conan. I think he's still only 18 actually, this is probably not that many years ago. So thank you very much Tanishq for all your help in the community and sorry for embarrassing you with that picture of you as a nine-year-old — I'm not really, haha. Okay, now, the thing is for doing Gradio and HuggingFace Spaces, well, it's easy enough to start. Okay, we start over here on the HuggingFace Spaces page — which we've linked to from the forum in the course. And we're going to put a model in production, where we're going to take the model we trained, and we are going to basically copy it to this HuggingFace Spaces server and write a user interface for it.
[29:29] So… let's go! Create new space. Okay, so, you can just go ahead and say, all right… so obviously you sign up… the whole thing's free. Basically everything I'm showing you in this entire course, you can do for free, that's the good news. Okay, so, give it a name, just create something minimal. I always use the Apache license because it means other people can use your work really easily but you don't have to worry too much about patents. As I say there's a few different products you can use with it, we're going to use Gradio, also free.
[30:04] If you make it public then you can share it, which is always a good idea when you're a student, particularly to… to really be building up that portfolio. Okay, so we're done. We've created a space. Now, what do we do next? Well, Spaces works through Git, now, most software developers will be very familiar with Git. Some data scientists might not be. And so Git is a very, very useful tool. I'm not going to talk about it in detail, but let's kind of quickly learn about how to use it, right? Now, Git, you can use it through something called Github Desktop which is actually pretty great and even people who use Git through the console should probably be considering using Github Desktop as well, because some things are just much faster and easier in it, in fact I was talking to my friend Hamil today and I was like, oh, help, I've accidentally committed this two things by mistake, what's the easiest way to revert it, and he used to work at Github
[31:09] and I thought he was going to have some fancy console command and he's like “oh, you should use Github Desktop and you can just click on it.” Oh! That's a great idea. So that's… that's useful. But most of the time we do use Git from the console, from the terminal. If you're a Linux user or a Mac user you've already got a terminal, very straightforward, no worries. If you're a Windows user I've got good news, nowadays Windows has a terrific terminal. It's called Windows Terminal, you get it from the Microsoft Store, so in fact every time you see me using a terminal, I'm actually using that Windows Terminal, works very well. God knows why you'd want it to, with all these ridiculous colors but, there you go.
[31:54] Now, what do you want to be running inside your terminal? Obviously, if you're in Linux or Mac you've already got a shell set up. In Windows, you almost certainly want to use Ubuntu. So Windows, believe it or not, can actually run a full Linux environment and to do it, is right typing a single line, which is this… So, if you go to (just google for WSL install) run PowerShell as administrator, paste that command, wait about five minutes reboot, you're done.
[32:30] You now have a complete Linux environment. Now, one of the reasons I'm mentioning this is, I'm going to show you how to do stuff on your own machine now, and, so, this is like going to a bit of an extra level of geekery, which some data scientists may be less familiar with. So you know, don't be worried about the terminal, you're gonna.. I think you're gonna… find it really helpful and much less scary than you expect. And, I particularly say, like for me, I choose to use Windows and that's because I get, you know, all the nice Windows GUI apps and I can draw on my screen, and do presentations, and I have a complete Linux environment as well. And that Linux environment uses my GPU and everything, so for me, you know, my first choice is to use Windows, my second choice, by not very much, really like it, would be to use Linux. Mac is a little bit harder, but it's still usable. Some things are a little bit trickier on Mac but, you should be fine.
[33:37] Okay, so, whatever you've got, at this point you've now got a terminal available. And so, in your terminal… (one of the really nice things about using a terminal is you don't have to follow lots of instructions about click here, click here, click here. You just copy and paste things.) So I'm just going to… you just copy this, and you go over to your terminal, and you paste it in, and you run it, and after you do that, you'll find that you've now got a directory. And so that new directory initially is empty and they tell you, okay, go ahead and create a file with this in it. Okay, so, how do you create a file with that in it, when we're in here, in our Linux environment on Windows, or in the terminal on Mac, or whatever? Well, all you do in Windows, if you just type “explorer.exe .”, it'll open up explorer here, or better still, on either Mac, or Linux, or Windows…
[34:42] So, yeah, regardless of what computer (type of computer on), you can just type “code .” and it will pop up Visual Studio Code and open up your folder. And so then you can just go ahead and… (if you haven't used VS Code before it's really well worth taking a few minutes to read some tutorials, it's a really great IDE) …and so you can go ahead and create an app.py file, like they tell you to, app.py file, containing what they told you to put in it. Here it is, here.
[35:22] All right, we're nearly there. So you can now go ahead and save that, and then you need to commit it, to Gradio… (not Gradio…) to HuggingFace Spaces. So one really easy way is just, in Visual Studio itself, you can just click here, and that'll give you a place where you type a message, and you hit tick, and it'll send it off to HuggingFace Spaces for you. So once you've done that, you can then go to… back to… the exact same website you're in before: “huggingface.co/spaces/jhp00/minimal”, and what you'll find now is that — it'll take about a minute, to build your website. And it's…
[36:15] the website it's building is going to have a Gradio interface with a text input, a text output, and it's going to run a function called “greet()” on the input, and my function called “greet()” will return “Hello {name}”. So that's what it's going to do. Now there it goes, let's try it! We'll say hello to Tanishq. I'm not always very good at remembering how to spell his name, I think it's like that.
[36:50] And there you go, so you can see it's put the output, for our input. So not a very exciting app but we now have, to be fair, an app running in production, right? Now, I told you we'd have a deep learning model running in production so now we have to take the next step which is to turn this into a deep learning model. All right, so, first we're going to need a deep learning model. And there's a few different ways we can get ourselves a deep learning model, but basically we're gonna have to train one. So, I've got a couple of examples: I've got a Kaggle example and a Colab example. Maybe I'll quickly show you both. They're gonna be the same thing. And I'm just going to create a dog or a cat classifier. Okay, so here's our Kaggle model, I'll click on edit so you can actually see what it looks like in edit view.
[37:52] Now, Kaggle already has fastai installed but I always put this first, just to make sure we've got the latest version. And obviously import stuff, we… So we're going to grab the pets data set, a function to check whether it's a cat, that's our labeling function for our ImageDataLoaders — remember this is just another way of doing DataBlocks, it's like a little shorthand. And we create our learner, and we fine tune it. Okay, so, that's all stuff we've seen before. So, in Kaggle every notebook has an edit-view (which is what you just saw) and a reader-view.
[38:27] And so you can share your notebook — if you want to. And then anybody can read the reader-view, as you see. And so, here, you can see, it shows you what happened when I ran it. And so, I trained it, it took… you know, the GPUs on Kaggle are a bit slower than most modern GPUs but they're still fast enough, I mean, it takes five minutes. And there's one bit at the end here, which you haven't seen before, which is I got learn.export and I give it a name.
[39:02] Now, that's going to create a file containing our trained model, and… that's the only thing! Creating this file is the only thing you need a GPU for, right? So you do that on Kaggle, or on Colab. So here's exactly the same thing on Colab… you can see “pip install”, is_cat, our data, ImageDataLoaders, so I've got a show_batch here as well, just for fun. Create my learner and then export. So, while we wait I might go ahead and just run that.
[39:36] One nice thing about Kaggle is once you've run it and saved it, you can then go to the data tab and, here is basically anything you've saved, it's going to appear here, and here it is: model.pickle, right? So now I can go ahead and download that and that will then be downloaded to my downloads folder. And then I need to copy it into the same directory that my HuggingFace Spaces App is in. Now my HuggingFace Spaces App is currently open in my terminal, on Mac you can type “open .” or in Windows you can type “explorer.exe .” and that'll bring up your finder or explorer in that directory. And so then you can just paste that, that thing you downloaded into this directory. Something by the way, in Windows I do, which I find really helpful is, I actually grab my… my home directory in Linux
[40:43] and I pin it to my Quick Access and that way I can always jump in Windows straight to my Linux files. Not really something you have to worry about on Mac because it's all kind of integrated, but on Windows they're like, kind of like, two separate machines. Okay, so… Let's do… so, I created a Space called testing and I downloaded my “model.pkl” and I pasted it into testing. So now we need to know how do we do predictions on a saved model?
[41:23] So we've got a notebook for that… Okay, so we've got a notebook for that and… so I'm going to take you through how we use a model that we've trained to make predictions. There's a few funny things with hash pipe which I'll explain in a moment, just ignore those for now. So we import fastai as usual. We import Gradio as we did before. And we copy in the exact same is_cat definition we had before. That's important, any external functions that you used in your labeling, need to be included here as well because that learner refers to those functions, okay? It saves... that learner saves everything about your model, but it doesn't have the source code of the function, so you need to keep those with you.
[42:12] So, let's try running this. So, for example, I just grabbed… (as you might have seen in my explorer) I just popped a dog picture there. And so we can create a python image library image from that dog, turn it into a slightly smaller one so it doesn't overwhelm my whole screen and there is a picture of a dog. So, how do we make predictions of whether that's a dog or a cat? Well, it's very simple, all we do is… instead of training a learner, we use load_learner(). We pass in the file name that we saved, and that returns a learner. This learner is exactly the same as the learner you get when you finish training. So here we are, here's Colab, right? We've just been training a learner, so at the end of that, there's a learner that's been trained, and so we… We kind of froze it in time, something called a pickle file — which is a python concept. It's like a frozen object, we saved it to disk, we transferred
[43:17] it to our computer, and we've now loaded it, and we've now unfreezed thought it. This is our unpickled learner and we can now do whatever we like with that. So one of the things that… One of the methods that a learner has is a dot predict method, so, if I run it, you can see, even on my laptop, it's basically ~(instance) instant. In fact we can see how long it took. If you — in Jupyter things that start with percent are called magics, they're special Jupyter things, so for example, there's a thing to see how long something takes.
[43:51] There you go, okay, so it took 54 milliseconds to figure out that this is not a cat. So it's returning two things: is it a cat? (as a string); is it a cat? (as a zero or one), and then the probability that it's a dog, and the probability that it's a cat. So the probability of zero (false) and one (true) of is it a cat? so definitely a dog. So we now want to create a Gradio interface which basically has this information.
[44:28] So Gradio requires us to give it a function that it's going to call, so here's our function. So we're going to call predict, and that returns as we said three things: the prediction is a string; the index of that; and the probabilities of whether it's a dog or a cat. And what Gradio wants, is it wants to get back a dictionary containing each of the possible categories — which in this case is dog or cat — and the probability of each one. So, if you haven't done much python before, a dict of a zip may be something you haven't seen.
[45:04] Very handy little idiom well worth checking out. Ditto if you haven't seen map before, anyway, here it is. One slightly annoying thing about Gradio at the moment is that it doesn't handle Pytorch Tensors, you can see here Pytorch is not returning normal numbers, it's returning Tensors. It's not even returning Numpy Arrays. In fact radio can't handle Numpy either, so we have to change everything just to a normal float, so that's all that this is doing, it's changing each one to a float. So, for example, if I now call classify_image() with our doggy image, we get back a dictionary of a dog, yes definitely; cat, definitely not.
[45:45] So now we've got all that we can go ahead and create a Gradio interface. So Gradio interface is something where we say, well, what function do you call to get the output, what is the input — in this case we say, oh, the input is an image — and so check out the Gradio docs, it can be all kinds of things like a webcam picture, or a text, or you know, all kinds of things. Give it a shape that it's going to put it into, the output's just going to be a label so we're going to create very, very simple interface. And we can also provide some examples, and so there's a dog, a cat, and a dunno — which I'll tell you about in a moment — which you'll see here there's a dog, and a cat, and a don't know.
[46:27] So once I launch it, it says, okay, that's now running on this url. So if I open that up; you can see now, we have just like Suvash, we have our own — not yet in production, but running on our own box — classifier. So let's check: dog, so you can click it and upload one, or just choose the examples… Here they are! So it's running on my own laptop, basically instant. And I really have to tell you the story about this guy here, this is the dunno.
[47:01] Submit, wait, why is it saying 100? Normally this says like 50 50. That's a bummer, this model's got… messed up my whole story. So last time I trained this model, and I ran it on the… dunno, it said, it said like it's almost exactly 50 50. And the way we found this picture is… I showed my six-year-old daughter, she's like: “what are you doing dad?”, like, I'm coding, “what are you coding?” Oh, you know, dog cat classifier. She checks it out and her first question is: “can I take your keyboard for a moment”, and she goes to google, and she's like: what is a dog mixed with a cat called. Like, there's no such thing as a dog mixed with a cat. Anyway, she goes to the images tab, and finds this picture, and she's like: “look there's a dog mixed with a cat”.
[47:47] She said: “run it on that dad!, run it on that!” And I ran it and it was like: 50-50. It had no idea if it's a dog or a cat. Now, this model I just retrained today, now it's sure it's a cat. So there you go, I think I used a slightly different training schedule or something, or gave it an extra epoch, anyway, so that's a dog-cat, but apparently it's a cat. I guess it is a cat. It's probably right. I shouldn't have trained it for as long. Okay, so there's our interface. Now, that's actually running, so you actually have to click the start button to stop it running so otherwise you won't be able to do anything else in your notebook. Okay, so, now we have to turn that into a python script. So, one way to turn it into a python script would be to copy and paste into a python script all the things that you need.
[48:42] I need to copy and paste into a python script all these parts of this that you need, so, for example, we wouldn't need this — it's just to check something out, we wouldn't need this — it was just experimenting. This was just experimenting. We'd need this, right? So, what I did… is I went through and I wrote “hash pipe export” at the top of each cell that contains information that I'm going to need in my final script, and then — so they're the steps, right? And then, at the very bottom here, I've imported something called notebook2script from nbdev, and if I run that, and pass in the name of this notebook, that creates a file for me called app.py containing that script. So this is a nice easy way to — like when you're working with stuff that's expecting a script and not a notebook, like HuggingFace Spaces does. It's fine to just copy and paste into a text file if you like,
[49:42] but I really like this way of doing it because that way I can do all of my experimentation in a notebook, and when I'm done, I just have a cell at the bottom I just run and export it. How does it know to call it app.py? That's because there's a special thing at the top default export: default_exp, which says what python file name to create. So that's just a little trick that I use. So now we've got an app.py. We need to upload this to Gradio, how do we do that? You just… you just push it to Git. So… you can either do it with Visual Studio Code, or you can type “git commit”.
[50:28] And then “git push”. And once you've done that…if we change minimal to testing… I think this hopefully might still be running my previous model — because I didn't push it, and that way, we can see our crazy dog-cat. All right, so here it is, you can see it running in production. So now this is something that anybody can — if you set it to public — anybody can go here and check out your model, and so they can upload it. And so, here's my doggy.
[51:02] Yep, definitely a dog. Cat, yeah I think I might have trained this for an epoch or two less, so it's less confident. Yeah definitely a cat. Dog-cat… hey dog-cat… hmm, still thinks it's definitely a cat. Oh well, so weird. Okay, so, that is…Okay, so, that is an example of getting a simple model in production. There's a couple of questions from the forum, from the community.
[51:49] Okay so one person's asking: “what's the difference between a pytorch model and a fastai learner?” Okay, that's fine, we'll get to that shortly. Don't know if it'd be the lesson, it might be this lesson or the next lesson. And then somebody else asked, basically it's asking: “how many epochs do we train for?” So, as you train a model your error rate, as you can see, it improves.
[52:21] And so the question is: should I run more? Should I increase the number of epochs? This is doing three epochs, right? Here's my three epochs plus one to get started. Look, it's up to you, right? I mean, for this, is here saying: there's a one percent error. I'm… I'm okay with the one percent error, you know, if you want it to be better, then you could use more data augmentation, and you could train it for longer. If you train for long enough, as we'll learn about soon, and then… maybe the next lesson. If you train for long enough, your error rate actually starts getting worse. And, you'll see, we'll learn about why. So basically yeah, you can train until it's good enough or until you've run out of patience, or time, or run out of compute, or until you… or until the error rate starts getting worse.
[53:14] Okay, oh, and then in Colab, how do you grab your model? All you need to do in Colab is, after you've exported it, is, if you go into their file browser you'll actually see it here, right? And you can click download. It's a bit weird, it doesn't like, pop up a box saying where do you want to download it to, but instead this kind of progress circle thing pops up, and so depending on how big it is, and so forth, it can take a few minutes. And once that circle fills up then it'll, the browser thing will finally pop up and say, okay, you can save it. Okay so that's how you…
[53:57] that's how you actually grab your model. So as you can see, the step where you actually need a GPU, you can use these totally free resources: Colab, Kaggle, and there are other ones we'll talk about in future lessons. And then you can do everything else on your own computer, including the predictions, the predictions are fast, right? So, you really don't need to use a GPU for that, unless you're doing thousands of them. Okay, here it goes, now it's asking me to save it. Okay, so now one big issue is, we needed… to run it on our computer… we needed python and Jupyter Notebooks running on our computer, so how do you do that?
[54:41] Because this is where often people get in all kinds of trouble… “I'm trying to figure out how to get this all working.” So, the good news is we've actually got something that makes it very, very straightforward. It's called fastsetup. There's really only just one part of it you need, so let me show you…. it's… it's… actually a Git repository on Github. Github's the place where most Git repositories live. So if you go to Github fastai/fastsetup you'll see it. And so what you can do is you can now grab this whole repository, just by clicking here on code, and if you've got Github Desktop installed, click on open with Github Desktop and, as you'll see, it brings this up saying: okay I'm ready to save this for you, so I click clone, so it's making a copy of it. There we go…
[55:39] So, basically, once you've cloned it, you'll then find there's a file in there called setup-conda.sh… which, you know, the details don't really matter, it's pretty short, but, that's the thing that's going to install python for you. So at that point you can just run “./setup-conda.sh” and it'll run this installer. Now, if you've got Linux or Mac you've already got python on your machine. Don't use that python!!
[56:14] And the reason is because that python, it's called the system python, it's used by your computer to do computery stuff, right? It's actually… it's actually needed. You don't want to be messing with it, I promise you, like… it's, it always leads to disaster, always! You want your own development version of python, it's also going to make sure you've got the latest version, and all the libraries you want, and, by far, the best one for you is almost certainly going to be these conda based python distributions, so if you run setup conda you'll get the one that we recommend. And, the one we recommend at the moment is something called mambaforge. So basically once you run it, you'll find that you've now — you close your terminal and reopen it — you'll find you've now got one extra command, which is called mamba, and mamba lets you install stuff.
[57:10] So once you've run it, you'll be able to go “mamba install fastai”. And that's gonna… actually we should probably, I should mention this, actually more, a bit more detail about how to install it correctly. If we go to docs.fast.ai, installing. Yeah, okay, we actually want to do “conda install -c fastchan fastai”. So let's just copy and paste. Oh sorry, not actually… and then, the other thing I'll say is, instead of using conda, replace conda with mamba because nowadays it's much faster. So “mamba install -c fastchan fastai”.
[57:52] Now, this is going to install everything you need. It's going to install Pytorch, it's going to install Numpy, it's going to install fastai… and so forth, right? And… so obviously I've already got it. And then the other thing you'll want to do is install nbdev, so you can do exactly the same thing for nbdev. You don't have to, right? It's just that… but that'll install Jupyter for you, amongst other things.
[58:28] And so at that point you can now, you can now use Jupyter. And so the way Jupyter works is — you can see it over here. This is my… (I'll go ahead and close it so we can start again.) So basically, to use Jupyter you just type “jupyter notebook”, okay? And when you run it, it'll say: okay, we're now running a server for you, and so if you click on that hyperlink, it'll pop up this, okay?, which is exactly what you see me use all the time. Okay so, you know, that hopefully is enough to kind of get you started with python, and with Jupyter Notebook…
[59:17] The other way people tend to install software is using something called pip instead of mamba. Pretty much anything you can do with mamba you can also do with pip, but if you've got a GPU, pip isn't going to install things generally so that it works on your GPU, you have to install lots of other stuff, which is annoying, so, that's why I kind of tell people to use mamba, but you can use pip otherwise. Still a little bit of red. Please let us know how we can help you again.
[59:54] Okay so let's see how we’re going with our steps. I forgot I had these steps here to remind myself. We created a space, tick. We created a basic interface, tick. Okay we got git setup, we got conda set up, or mamba. So mamba and conda are the same thing, mamba is just a much faster version and we'll keep some notes on the course website because at the moment they're actually working on including the speed ups from mamba into conda. So at some point maybe it'll be fine to use conda again. At the moment conda is way too slow, so don't use it.
[1:00:26] Okay we've done dogs versus cats no problem. Yeah so we could also look at pet breeds (well yeah we'll briefly look at that.) Okay, we've used an exported learner, no problem. We’ve used nbdev, no problem. Oh! okay… try the api… alright this is interesting. So I think we can all agree, hopefully, that this is pretty cool that we can provide to anybody who wants to use it, for free, a real working model, and you know, with gradio there's actually, you know, a reasonable amount of flexibility, around like how you can make your website look, you know, using these various different widgets.
[1:01:22] It's not amazingly flexible, but it's flexible enough to kind of… it's really just for prototyping, right. So gradio has a lots of widgets and things that you can use. The other main platform at the moment that HuggingFace Spaces supports is called streamlit. streamlit is more flexible, I would say, than gradio - not quite as easy to get started with but you know, it's kind of that nice in between, I guess, so also a very good thing you know again mainly for, kind of, building prototypes. But at some point you're going to want to build more than a prototype. You want to build an app, right, and one of the things I really like about gradio in HuggingFace Spaces is… there's a button down here “view the api”. So we can actually create any app we want and the key point is that the thing
[1:02:27] that does the actual model predictions for us is going to be handled by HuggingFace Spaces Gradio, right, and then we can write a Javascript application that then talks to that. Now there's going to be two reactions here … Anybody who's done some front-end engineering is going to be like, “oh great, I can now literally create anything in the world, because I just write any code and I can do it and they'll be excited.” And a lot of data scientists might be going “uh oh… I have no idea how to use javascript, it's not in my, you know, inventory.”
[1:03:07] So this is again where I'm going to say: look, don't be too afraid of Javascript. I mean obviously one option here is just to kind of say, “hey I've got a model” and throw it over to the wall to your mate who does know Javascript and say please create a Javascript interface for me. But let me just give you a sense of how really not hard this actually is. So there's a… end point. There's now a url that's running with our model on it and if you pass it some data… an image… some image data… to this… to this url, it's going to return back the dictionary. So it's going to do exactly the same thing that this UI does but as an api, as a function we can call.
[1:03:58] And so it's got like, examples here of how to call it… so for example, i can actually… let me show you the api as an example using that minimal interface we had … because it's just going to be a bit simpler. So if I click curl and I copy that… copy that… and paste. So you can see there … (oh that's not a great example passing in hello world) …if I pass in Tanishq again, let's see how I'm going with his name Tanishq.
[1:04:37] Here you can see it returns back “Hello Tanishq!” So this is how these apis work, right. So we can use javascript to call the api and we've got some examples. So I've created a website, and here is my website “tinypets” and on this website as you can see it's not the most amazingly beautiful thing but it's a website. It's a start right, and up here I've got some examples, here we go, single file, click, choose file, click… and in this example I'm actually doing full pet classification. So I actually trained a model to classify breed, which we'll talk about more next week, rather than just dog versus cats. So let's pick a particular breed… and we run it…
[1:05:41] Oh! And there it is! Now not very amazing right, but the fact is that this is now a Javascript App means we have no restrictions about what we can do. And let's take a look at that HTML… that's it! It easily fits in a screen, right, and the basic steps are not crazy, right, it's basically… we create an import for our photo, we add an event listener that says when you change the photo call the read function, the read function says create a file reader, read the file and when you finished loading, call loaded, and then loaded says… fetch that… now that path there… is…
[1:06:39] that path there! Right! Except we're doing the full pets one. So this is basically just copied and pasted from their… from their sample. And then grab the JSON, and then grab from the data the first thing… the confidences… the label, and then set the HTML. So as you can see it's like (okay) if you haven't used javascript before these are all new things, right, but they're not… it's not harder than python, right, it's just… it's just another just another language to learn. And so from here you can start to build up, right!
[1:07:20] So for example we've created a multi-file version. So with the multi-file version, let me show you… multi-file… choose… so we can now click a few… so we've got a newfie, a ragdoll, a basset hound, and some kind of cat. I'm not much of a cat person. So we chose four files and bang they've all been classified! Apparently it's a Bengal! (I wouldn't know) Here's our newfoundland.
[1:07:52] So there's the multifile version, and if you look at the code, it's not much more, right, it's now just doing… it's getting all the files and mapping them to read, and now appending each one, so not much more code at all. And as you might have seen on our site here, there's a few more examples, which is some of the community during the week, has created their own versions. So this one here is… I think this is… the yeah… this is from one of the gradio guys... they called it “get to know your pet.” So if i choose a pet… I kind of… I really like this because it actually combines two models. So first of all it says, oh! It's a basset hound, and then it lets me type in and ask things about it. So i can see, “oh, what kind of tail does it have?...” search, and so that's now going to call an NLP model,
[1:08:56] which asks about this… it's a curved saber tail. There we go! “What maintenance does it need?” So again, like here you can kind of see how (oh) a basset hound’s ears must be cleaned inside and out frequently. So this is like combining models.So you can see this is something that you couldn't do with just a kind of a… a ready to go interface. And so the next thing i wanted to point out is how did we create the website? I showed you how to create an HTML file, but like how do you create those, and how do you make a website out of them? Well watch this! Let's… here's…. here's….
[1:09:52] here's the source code to our most basic version, okay! So i could just save this… there we go… Okay so we could open that with Visual Studio Code… and… what we could actually do is, we could… here it just is in explorer or mac & finder. I could just double click on it and here it is! It's a working app!!
[1:10:32] So you can see i don't need any software installed on my computer to use a Javascript App, right! It's a single file… I just run it in a browser… a browser is our complete execution environment. It's a got a debugger. It's got the whole thing. So here you can (yeah) here you can see it's… it's just calling out to this external hacking faces endpoint, so I can do it all sitting here on my computer. So once I've got my HTML file that's working fine on my computer, in VSCode, how do I then put it on the web, so that other people can use it?
[1:11:06] Again, the whole thing's free. There's a really cool thing called github pages, which basically will host your website for you. And because it's just Javascript, it'll… it'll all work just fine! The easiest way to create a github pages site in my opinion, is to use something called fastpages, which is a fast.ai thing! And… basically all you do… is you follow the setup process.
[1:11:43] So first it does it's… let's just go through it… so it says generate a copy by clicking on this link. So I click the link, all right, okay, give it a name… (I try to make everything public! I always think it's good good practice… you don't have to) …create repo, generating, okay, and then there's basically two more steps, takes about five minutes. We don't have five minutes, so I'll show you the one that I've already built, which is “fast.ai/tinypets” And so once it's done you'll basically end up with this empty site which, again, you just go “code” “open with github desktop” or “open with visual studio” whatever… so “open with github desktop”...
[1:12:37] or you can copy & paste this to your terminal… and so any one of those is going to get you this whole thing on your computer. You can save your HTML files there… push it back up to github. And what you'll find is we… we've… FastPages will show you the link to the website that is created for you. Now, the website that's created for you, you can make it look however you want using something called a “Theme”. So you'll see it's created a file called config.yml where you can pick a theme. So, in this case, I picked a theme called “alembic” for no particular reason.
[1:13:18] So Github Pages uses something called “Jekyll”, and so any “Jekyll” theme will, will basically work. So I picked out this theme, and so, as a result, when I now save things into this repo, they will automatically appear in this website, and the files automatically appear up here in this list. So, if you look at my index, that's the home page… the entire file is just this, right! The only slightly weird thing is at the top of every Github Pages file, you have to have three dashes, title, and layout, and three dashes. It's called “front meta”. And so, once you do that… and save it, it will appear in your website. So, something else I did then, I was like, okay, well… that's all very well that fast.ai has created this website, but I don't really like what it looks like. I want to create a different version.
[1:14:18] No worries. You can go to “fast.ai/tinypets” and click “Fork”. And, when you click “Fork”, it's going to create your own copy. So I did that under my personal account which is jph00. And look! Now I've got my own version of it, and now I can make changes here. So I made a few changes. One change I made was: I went to config.yml and I changed the theme… to “pages-themes/hacker”. So once you fork, one thing you have to do, which normally FastPages does for you, is you do have to go to “Settings”, and click “Pages”, and actually enable “Github Pages”. So you basically have to, by default, it's turned off, so here you'll just have to turn it on. So use the “Master Branch Root Save” and then it'll say, “No worries, it's ready to be published”. And so I changed the config.yml file to point at a different theme. And so, if you look at now… the jph's Tiny Pets…
[1:15:17] it's different! Okay? So it's got the same info, but it's much more “hackerish” because jph00 is a serious hacker, as you can tell from his website. So, anyway, look, it's a very brief taste of this kind of world of Javascript and websites and so forth, but I wanted to give you a sense of, like, you know, you don't need any money. You don't need any IDEs. You know, you don't really need much code to get started with writing your own web apps, and, thanks to hugging face spaces, you know, they'll even host your model for you and all you need to do is just have the magic string, as a thing to call. Okay!
[1:16:07] So… signing out: Hacker Jeremy Howard. Thanks very much for watching, and, in the next lesson, we're going to be digging into some natural language processing. We're going to be doing some of the same stuff, but we're going to be doing it with language rather than pictures. And we're going to be diving under the hood to see how these models actually work. We're going to learn about things like stochastic gradient descent, and we might even be having to brush off a little bit of calculus. I hope I haven't put you off by saying the “C” word.
[1:16:38] I will see you next time! Thanks all! Bye!
在 Vidleaf 工作台中打开
搜索转录文本、选择行、复制带时间戳的引文、翻译。
署名
“第 2 课:面向程序员的实用深度学习 2022”由 Jeremy Howard(https://www.youtube.com/@howardjeremyp)创作,采用 CC BY 4.0(https://creativecommons.org/licenses/by/4.0/)许可。源视频:https://www.youtube.com/watch?v=F4tvM4Vb3A0。本页面是视频的文本转录,添加了段落分隔和时间戳;创作者与 Vidleaf 无关联,也不为其背书。Vidleaf 添加了 AI 标点和 AI 生成的摘要;原始措辞仍可在本页面上查看。
您是创作者或权利持有人吗?请求更正或移除:copyright@vidleaf.app(参见关于这些页面)。
最后更新于