Fast.ai v3 2019课程中文版笔记

Lesson 1 你的宠物

如何启动你的第一个GPU

0:00-0:47
ytcropper Lesson 0 How to get GPU running

  1. Lesson 0 How to get GPU running gpu
    如何启动你的第一个GPU
你需要做些什么准备

0:30-3:16
What else do you need to get started
你需要做些什么准备?

如何最大化利用课程视频与notebook

3:13-4:29
How to make the most out of these lesson videos and notebooks
如何最大化利用课程视频与notebook?

  • 从头看到尾,不要纠结概念细节
  • 一边看,一边跑代码
  • 试试实验代码
你可以期望的学习成就高度与关键学习资源

4:29-5:28
what you expect to be with fastai course

  • 成为世界级水准深度学习实践者,构建和训练具备达到甚至超越学术界state of art水平的模型
为什么我们要跟着Jeremy Howard学习

5:26-6:47
why should we learn from Jeremy Howard

  • 从事 ML 实践25年+
  • 起始于麦肯锡首位数据分析专员,然后进入大咨询领域
  • 创建并运行了多个初创企业
  • 成为Kaggle主席
  • 成为Kaggle排行第一级的参赛者
  • 创建了Enlitic, 史上第一个深度学习医疗公司
  • San Francisco大学教职人员
  • 与Rachel Thomas共同创建了fastai
  • 是务实非学术风格,聚焦于用深度学习做有用的事情
fast.ai让人人都成为深度学习高手的策略方针

6:40-7:26
How to make DL accessible to everyone to do useful things

  • 创建fastai library
    • 能用最简洁,最快速,最可靠的构建深度学习模型
  • 创建fast.ai系列课程
    • 帮助更多人免费便捷地学习
  • 将学术论文成果落地 fastai library
    • 将学术界最高水平的技术融入到fastai library中,让所有人便捷使用
  • 创建维护学习社区
    • 让深度学习实践者能够找到并帮助彼此
需要投入多少精力,会有怎样的收获

7:25-8:51
How much to invest and What I get out

  • 我们需要投入都少精力学习?
    • 看完课程视频至少需要14小时,完成代码实验整个课程至少需要70-80小时
  • 当然具体情况,因人而异
    • 有人全职学习
    • 也有人只看视频,不做作业,只需要获取课程概要
  • 如果跟玩整个课程,你将能够做到:
    • 在世界级水平实践深度学习,甚至训练出打败学术state of art水平的模型
    • 对任意图片数据集做分类
    • 对文本做情感分类
    • 预测连锁超市销售额
    • 创建类似Netflix的电影推荐系统
学习深度学习的必要基础是什么,以及常见的误解与成见

8:51-10:23
Prerequisites and False assumptions and claims on DL

  • 仅凭1年python和高中数学,你是无法学会深度学习的!
    • fast.ai让其不攻自破
  • 深度学习是黑箱,无法理解
    • 很多模型是可解释可理解的
    • 比如可以用可视化工具理解CNN参数的功能
  • 需要巨量数据才能做深度学习
    • 迁移学习不需要大量数据
    • 可分享的训练好的模型
  • 做前沿研究需要博士学位为前提
    • fastai 或则 keras libs 和MOOCs就足够了
    • Jeremy只有哲学学位
  • 好应用仅限于视觉识别领域
    • 优秀应用同样存在于speech, tabular data, time series领域
  • 需要大量GPU为前提
    • 一个云端GPU即可,甚至是免费的
    • 当然对于大型项目,的确需要多个GPU
  • 深度学习不是真正的AI
    • 我们不是在造人造大脑
    • 我们只是在用深度学习做造福世界的事情
完成第一课后你将能做些什么

10:22-11:04
What will you be able to do at the end of lesson 1

  • 创建你的图片数据集的分类器
  • 你甚至可以仅用30张图实现曲棍球和棒球的接近完美的分类器训练
fast.ai的教学哲学是什么

11:04-12:31
What is fastai learning philosophy

  • 从代码开始,然后在尝试理解理论
  • 不需要在博士最后一年才开始真正写代码做项目
  • 创造多个模型,研究模型内部结构,掌握良好的深度学习实践者的直觉
  • 用Jupyter notebooks做大量代码实验
如何像专业人士一样使用jupyter notebook

12:30-13:17 *
How to use Jupyter notebook as a pro

  • 从最基础的快捷键开始,每天学习3-5个快捷键,每天用,很快就上手了
什么是Jupyter magics

13:17-14:00
What are Jupyter magics

  • 任何library的改动,将被自动加载
  • 所有作图会在cell中完成和展示
  • 适用于所有的Notebook实验
  • 听写kernel中查看代码
什么是fastai库以及如何使用

14:00-17:56
What are fastai lib and how to use it

  • fast.ai 课程和机构名称
  • fastai 库的名称
  • 依赖的library
    • pytorch is easier and more powerful than tensorflow
  • fastai 支持四大领域应用
    • CV, NLP, Tabular, Collaborative Filtering
  • 两种 * 的使用都支持
    • from fastai import * (个人评论:但实际作用几乎为零)
    • from fastai.vision import * 引入所需工具

个人评论:代码探索发现 from fastai import *并无真用途

学术和Kaggle数据,CatsDogs 与 Pets数据的异同

17:56-21:08
Academic vs Kaggle Datasets, CatsDogs vs Pets dataset
两大数据集来源
- 学术和Kaggle
学术数据集特点?
- 学术人员耗费大量时间精力收集处理
- 用来解决富有挑战的问题
- 对比不同方法或模型的表现效果,从而凸显新方法的突破性表现
- 不断攀登和发表学术最优水平
为什么这些数据集有帮助?
- 提供强大的比较基准
- 排行榜与学界最优表现
- 从而得知你的模型的好坏程度
记住要注明对数据集和论文的引用
- 同时学习了解数据集创建的背景和方法
你的宠物数据集问题的难度
- 猫狗大战相比较是非常简单的问题
- 二元分类,全部猜狗也有50%准确率
- 而且猫狗之间差异大,特征比较简单
- 刚开始做猫狗大战竞赛时,80%已经是行业顶级水平
- 如今我们的模型几乎做到预测无误
- 宠物数据集要求识别37不同种的猫狗
- 猜一种只会有1/37正确率
- 因为不同种猫和不同种狗之间差异小,特征难度更高
- 我们要做的是细微特征分类fine-grain classification

如何下载数据集

21:07-23:56
How to download dataset with fastai
AWS 在云端为fast.ai所需数据集提供免费告诉下载
我们去Kernel中看untar_data的用法

如何进入图片文件夹以及查看里面的文件

23:56-25:49
How to access image folders and check filenames inside
我们去Kernel中看Path,ls,get_image_files的用法

如何从文件名中获取标注

25:40-27:53
How to get the labels of dataset
如何理解用regular expression 提取label,见笔记
运行代码理解其用法,见kernel

为什么以及如何选择图片大小规格

27:48-29:15
Why and how to pick the image size for DataBunch

  • Why do we need to set the image size?
    • every image has its size
    • GPU needs images with same shape, size to run fast
  • what shape we usually create?
    • in part 1, square shape, most used
    • in part 2, learn rectangle shape, with nuanced difference
  • What size value work most of the time generally?
    • size = 224
  • What special about fastai? *
    • teach us the best and most used techniques to improve performance
  • make the all the decisions for us if necessary, such as size=224 now
什么是DataBunch

29:15-29:56
What is a DataBunch
What does DataBunch contain
- training Dataset
- images and labels
- texts and labels
- tabular data and labels
- etc
- validation Dataset
- testing Dataset (optional)

如何normalize DataBunch

29:56-30:19
What does normalize do to DataBunch

to make data about the same size with same mean and std

如果图片大小不是224会怎样

30:19 - 31:50
What to do if size is not 224
* get_transforms function will make the size so
- data looks zoomed
- center-cropping
- resizing
- padding
- these techniques will be used in data augmentation

normalize 图片意味着什么

31:50-33:01
What does it mean to normalize images

* all pixel start 0 to 255
* but some channels are very bright and other not, vary a lot
* if all channels don’t have mean 0 and std 1
* models may be hard to train well
为什么图片尺寸是224而不是256

33:01- 33:34
Why 224 not 256 as power of 2
- because final layer of model is 7x7
- so 224 is better than 256
- more in later

如何查看图片和标注

33:34-35:06
How to check the real images and labels
* to be a really good practitioner is to look at your data
* how to look at your images
* data.show_batch(rows=3, figsize=(7,6))
* how to look at your labels
* print(data.classes
- what is data.c of DataBunch
* number of classes for classification problem
* not for regression and other problems

如何构建一个CNN模型

35:06-37:25
How to build a CNN learner/model
what is a Learner?
- things can learn to fit the data/model
what is ConvLearner?
- to create convolution NN
- ConvLearner is replaced by create_cnn *
what is needed to make such a model?
- required
- DataBunch
- Model: resnet34 or resnet50
- metrics = is from kwargs *
How to pick between resnet34 and resnet50?
- always start with smaller one
- then see whether bigger is better
What is metrics
- things to print out during training
- e.g., error_rate

为什么要用训练好的模型的框架与参数

37:25-40:03
Why use a pretrained model (framework and parameters) for your CNN? in other words, What is transfer learning?
* the model resnet34 will be automatically downloaded if not already so
* what exactly is downloaded
* pretrained model with weights trained with ImageNet dataset
* why a pretrained model is useful?
* such model can recognize 1000 categories
* not the 37 cats and dogs,
* but know quit a lot about cats and dogs
* what is transfer learning?
* take a model which already can do something very well (1000 objects)
* make it do your thing well (37 cats dogs species)
* also need thousands times less data to train your model

什么是过拟合?为什么我们的模型很难过拟合?

40:03-41:40
what is overfitting? why wouldn’t the model cheating?
How do we know the model is not cheating?
- not learn the patterns to tell cricket from baseball
- but only member those specific objects in the images
How to avoid cheating
- use validation set which your model doesn’t see when training
what use validation set for?
- use validation set to plot metrics to check how good model is fairly
Where is validation set?
- automatically and directly baked into the DataBunch
- to enforce the best practice, so it is impossible to not use it

如何用最优的技术来训练模型?

41:40-44:33
How to train the model with the best technique
we can use function fit, but always better to use fit_one_cycle
What is the big deal of fit_one_cycle?
- a paper released in 2018
- more accurate and faster than any previous approach
- ::fastai incorporates the best current techniques *::
keyboard shortcut for functions
* tab to use autocomplete for possible functions
* shift + tab to display all args for the function
how to pick the best number of epochs for training?
- learn how to tune epochs (4) in later lessons
- I don’t remember it had been discussed in later 6 lessons *
- not too many, otherwise easy overfit

如何了解模型的好坏?

44:28 - 46:42
How to find out how good is your model

  • How to find out the state of art result of 2012 paper?
    • Section of Experiment, check on accuracy
    • Oxford, nearly 60% accuracy
  • What is our result
    • 96% accuracy
如何放大课程效果?

46:47-48:41
How to get the most out of this course
What is the most occurred mistake or regret?
- spend too much on the concept and theory
- spend too little time on notebooks and codes
What your most important skill is about
- understanding what goes in
- and what comes out

fastai的业界口碑以及与kera的对比

48:41-52:53
The popularity of fastai library
* Why we say fastai library becomes very popular and important
* major cloud support fastai
* many researchers start to use fastai
* what is the best way of understand fastai software well?
* docs.fast.ai
* How fastai compare with keras
* codes are much shorter
* keras has 31 lines which you need to make a lot of decisions
* fastai has 5 lines which make the decisions for you
* accuracy is much higher
* training time is much less
* cutting edge researches use fastai to build models
* “the ImageNet moment” for NLP done with fastai
* github: “towards Natural Language Semantic code search”
* Where on the forum people talking about papers?
* Deep Learning section

学员能够用fast.ai所学完成的项目

52:45-65:51
What students achieved with fastai and this course
Sarah Hooker
- first course student, economics (no background in coding)
- delta analytics to detect chainsaw to prevent rainforest
- google brain researcher and publish papers
- go to Africa to setup the first DL research center
- dig deep into the course and Deep learning book

Christine Mcleavey Payne
- 2018 year student
- openAI
- Clara: a neural net music generator
- background: math and …. too much to mention
- pick one project and do it really well and make it fantastic

Alexandre Cadrin
- can tell MIT X-ray chest model is overfitting
- bring deep learning into your industry and expertise

Melissa Fabros
- English literature degree, became Kiva engineer
- help Kiva (micro-lending) to recognize faces to reduce gender and racial bias

Karthik and envision
- after the course started a startup named envision
- help blind people use phone to see ahead of you

Jeremy helped a small student team
- to beat google team in ImageNet competition

Helena Saren? @glagoli…?
- combine her own artistic skills with image generator
- style transfer

a student as Splunk engineer to detect fraud

Francisco and Language Model Zoo at the forum
- use NLP to do different languages with different students

Don’t feel intimidated and ask for help and contribute

为什么选择ResNet 而非Inception作为模型框架和已训练的参数

66:00 - 67:57
Why use Resnet rather than Inception
* DAWNBench on ImageNet classification
* “Resnet is good enough” for top 5 places
* edge computing
* but the most flexible way is let your model on cloud talk with your mobile app
* inception is memory intensive and not resilient

如何保存训练好的模型

67:57-68:43
How to save a trained model
What is inside the trained model?
- updated weights
why do we need to save model?
- keep working and updating the previous weights
how to save a model?
- learn.save("stage-1")
where will be the model be located?
- in the same fold where data is

如何画出损失值最高的数据/图?

68:43-73:22
how to plot top losses examples/images
How to create model interpreter?
- interp = ClassificationInterpretation.from_learner(learn)
Why plot the high loss?
- to find out our high prob predictions are wrong
- they are the defect of our model
How to plot top losses using the interpreter?
- interp.plot_top_losses(9, figsize=(15,11))
How to read the output of the plotting and numbers?
- doc(interp.plot_top_losses) -> doc & source
What does those numbers on the plotting mean?
- prediction, actual, loss, prob of actual (not prediction)
Why fastai source code is very easy to read?
- intension when writing it
- don’t be afraid to read the source
Why it is useful to see top loss images
- figure out where is the weak spot
- error analysis * (Ng)

如何找出模型混淆度最高的图片?

73:20 -74:39
How to find out the most confused images of our model
* why we need the confusion matrix to interpret the model?
* when not to use interp.plot_confusion_matrix(figsize=(12,12), dpi=60)?
* when to use interp.most_confused(min_val=2)?

如何用微调改进模型?

74:37 - 76:26
How to improve our model
What is the default way of training?
- add a few layers at the end
- only train or update weights for the last few layers
What is the benefit of the default way?
- less likely to overfit
- much faster
How to train the whole model?
- unfreeze the model learn.unfreeze()
- train the entire model learn.fit_one_cycle(1)
why it is easy to ruin the model by training the whole model?
- learning rate is more likely to set too large for earlier layers *
- to understand it please see the next question

CNN模型在学习些什么?以及为什么直接用全模型学习效果不佳?

76:24 - 82:32
what is CNN actually learning and why previous full model training didn’t work
what is the plot of layer 1?
- coefficients? weights? filters
- finding some basic shapes
what are the plots of layer 2?
- 16 filters
- each filter is good at finding one type of pattern
What are the plots of layer3?
- 12 filters
- each is more complex patterns
What are inside plots of layer4 and layer 5?
- filters to find out even more complex patterns using previous layer patterns
Which layer’s filter pattern can be improved?
- less likely for layer 1
- maybe not layer 4-5
- probably much later layers should be changed to some extent
Why the previous full model train won’t work?
- the same learning rate is applied to earlier and later layers

训练全模型的正确方式是什么

82:32- 86:55
How to train the whole model in the right way
How we go back to the unbroken model by full training?
- load the backup model
- learn.load('stage-1');
How to find the best learning rate?
- to find the fastest learning rate value
- learn.lr_find()
How to plot the result of learning rate finding?
- learn.recorder.plot()
how to read the learning rate plot?
- learn.unfreeze()
- learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))
- how to find the lowest/fastes learning rate?
- find the lr value before loss get worse
- how to find the highest/slowest learning rate?
- 10x smaller than original learning rate
- how to give learning rate value to middle layers?
- distribute values equally to other middle layers
Why you can’t win Kaggle easily?
- many fastai alumni compete on Kaggle
- this is the first thing they will try out

如何用更大的模型来改进效果

86:55-91:00
How to improve model with more layers
to use ResNet50 instead of ResNet34
- data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=299, bs=bs//2).normalize(imagenet_stats)
- learn = create_cnn(data, models.resnet50, metrics=error_rate)
what to do when GPU memory is tight?
- due to model is too large and take too much GPU memory
- less 8 GPU memory can’t run ResNet50
How to fix it?
- shrink the batch_size when creating the DataBunch
How good is 4% error rate for Pets dataset?
- compare to CatsDogs 3% error rate
- 4% for 37 similar looking species is extraordinary
Why ResNet50 still use the same lr range from ResNet34? *
- the lr plot looks different from that of ResNet34
- but why we still use the following code
- learn.fit_one_cycle(3, max_lr=slice(1e-6,1e-4))
- problem asked on formum
How to use most confused images to demonstrate model is already quite good?
- check out the most confused images online
- see whether human can’t tell the difference neither
- if so, then model is doing good enough
- it teaches you to become a domain expert

生成DataBunch的不同方式有哪些

91:35-95:10
Different ways to put your data into DataBunch
How to use MNIST sample dataset?
* path = untar_data(URLs.MNIST_SAMPLE); path
How to create DataBunch while labels on folder names?
- data = ImageDataBunch.from_folder(path, ds_tfms=tfms, size=26)
How to check the images and labels?
How to read from CSV?
- df = pd.read_csv(path/'labels.csv')
How to create DataBunch while labels in CSV file?
- data = ImageDataBunch.from_csv(path, ds_tfms=tfms, size=28)
How to create DataBunch while labels in dataframe?
- data = ImageDataBunch.from_df(path, df, ds_tfms=tfms, size=24)
How to create DataBunch while labels in filename?
- data = ImageDataBunch.from_name_re(path, fn_paths, pat=pat, ds_tfms=tfms, size=24)
How to create DataBunch while labels in filename using function?
- data = ImageDataBunch.from_name_func(path, fn_paths, ds_tfms=tfms, size=24, label_func = lambda x: '3' if '/3/' in str(x) else '7')
How to create DataBunch while labels in a list?
- labels = [('3' if '/3/' in str(x) else '7') for x in fn_paths]
- data = ImageDataBunch.from_lists(path, fn_paths, labels=labels, ds_tfms=tfms, size=24)

如何最大化利用fastai文档

95:10-97:28
How to make the most out of documents
- To experiment the doc notebook
- How do I do better?

关于fastai与多GPU和3D数据的问题

97:28-98:09
QA on fastai with multi-GPU, 3D data

一些有趣的项目

98:09-end
An interesting and inspiring project
how to transform mouse moment into images
then train it with CNN

5 Likes