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.
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,
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.