Created on 10-30-201811:43 PM - edited 09-16-202201:44 AM
I've just stared the third edition of the fastai course with
Jeremy Howard. There’s been a lot of buzz around fast.ai and how the non-profit
is making deep learning accessible to hundreds of thousands of developers. The latest mention was in the Economist: https://www.economist.com/business/2018/10/27/new-schemes-teach-the-masses-to-build-ai.
For me the most exciting part of the course is learning how to get cutting
edge results (in the 90%+ accuracy range) with just a few lines of code using fastai
library methods that have best practices baked in.
Below, I'll present a few lines of code that allow you to
quickly classify different breeds of cats and dogs. You may recall that
distinguishing between cats and dogs was big just a few years ago but now
it's too easy. Thus, we’re using the Oxford-IIIT Pet Dataset http://www.robots.ox.ac.uk/~vgg/data/pets/.
The code example references the latest fastai
library v1.0.x built on top of PyTorch. See the github repo for more details https://github.com/fastai.
So let’s get started!
First, import prerequisite libraries
from fastai import *
from fastai.vision import *
Set training batch size to 64
bs = 64
Note: if your GPU is running out of memory, set a smaller
batch size, e.g. 32 or 16.
Assuming path
points to your dataset of pet images, where the image labels (representing type of
pet breed) are the folder name, we use a handy data preparation method ImageDataBunch.
We set our validation set to 20% and transform all the
images to size 224. (The size 224 is set as a multiplier of 7 which is optimal
for the ResNet-34 model used in this example.)
data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2,
ds_tfms=get_transforms(), size=224)
Also, let’s normalize our dataset
data.normalize(imagenet_stats)
And preview our training data
data.show_batch(rows=3, figsize=(7,8))
Now we’re ready to train the model. We’re using a pre-trained convolutional
neural net ResNet-34. (To learn more about convolutional neural networks see https://cs231n.github.io/convolutional-networks/.)
Of course, we could further fine tune this model and adjust the weights across all 34 layers. We could also replace ResNet-34 with a larger model, e.g. ResNet-50.
You can check the Stanford Deep Learning benchmark site https://dawn.cs.stanford.edu/benchmark/ for the top performing models. (As of Sep 2018 ResNet-50 was the top one.) If you do decide to use ResNet-50 for your training, make sure to set image size to 320.
Also, for ResNet-50 your GPU should have at least 11GB of memory.
If you want to learn more about the fast.ai course, here's the link: https://course.fast.ai/