Path: blob/master/00-is-it-a-bird-creating-a-model-from-your-own-data.ipynb
807 views
Is it a bird?
In 2015 the idea of creating a computer system that could recognise birds was considered so outrageously challenging that it was the basis of this XKCD joke:

But today, we can do exactly that, in just a few minutes, using entirely free resources!
The basic steps we'll take are:
Use DuckDuckGo to search for images of "bird photos"
Use DuckDuckGo to search for images of "forest photos"
Fine-tune a pretrained neural network to recognise these two groups
Try running this model on a picture of a bird and see if it works.
Step 1: Download images of birds and non-birds
Let's start by searching for a bird photo and seeing what kind of result we get. We'll start by getting URLs from a search:
...and then download a URL and take a look at it:
Now let's do the same with "forest photos":
Our searches seem to be giving reasonable results, so let's grab 200 examples of each of "bird" and "forest" photos, and save each group of photos to a different folder:
Step 2: Train our model
Some photos might not download correctly which could cause our model training to fail, so we'll remove them:
To train a model, we'll need DataLoaders, which is an object that contains a training set (the images used to create a model) and a validation set (the images used to check the accuracy of a model -- not used during training). In fastai we can create that easily using a DataBlock, and view sample images from it:
Here is what each of the DataBlock parameters means:
The inputs to our model are images, and the outputs are categories (in this case, "bird" or "forest").
To find all the inputs to our model, run the get_image_files function (which returns a list of all image files in a path).
Split the data into training and validation sets randomly, using 20% of the data for the validation set.
The labels (y values) is the name of the parent of each file (i.e. the name of the folder they're in, which will be bird or forest).
Before training, resize each image to 192x192 pixels by "squishing" it (as opposed to cropping it).
Now we're ready to train our model. The fastest widely used computer vision model is resnet18. You can train this in a few minutes, even on a CPU! (On a GPU, it generally takes under 10 seconds...)
fastai comes with a helpful fine_tune() method which automatically uses best practices for fine tuning a pre-trained model, so we'll use that.
Generally when I run this I see 100% accuracy on the validation set (although it might vary a bit from run to run).
"Fine-tuning" a model means that we're starting with a model someone else has trained using some other dataset (called the pretrained model), and adjusting the weights a little bit so that the model learns to recognise your particular dataset. In this case, the pretrained model was trained to recognise photos in imagenet, and widely-used computer vision dataset with images covering 1000 categories) For details on fine-tuning and why it's important, check out the free fast.ai course.
Step 3: Use our model (and build your own!)
Let's see what our model thinks about that bird we downloaded at the start:
Good job, resnet18. 😃
So, as you see, in the space of a few years, creating computer vision classification models has gone from "so hard it's a joke" to "trivially easy and free"!
It's not just in computer vision. Thanks to deep learning, computers can now do many things which seemed impossible just a few years ago, including creating amazing artworks, and explaining jokes. It's moving so fast that even experts in the field have trouble predicting how it's going to impact society in the coming years.
One thing is clear -- it's important that we all do our best to understand this technology, because otherwise we'll get left behind!
Now it's your turn. Click "Copy & Edit" and try creating your own image classifier using your own image searches!
If you enjoyed this, please consider clicking the "upvote" button in the top-right -- it's very encouraging to us notebook authors to know when people appreciate our work.