🤯 fast.ai & Dog Breed Image Recognition via Transfer Learning

🤯 fast.ai & Dog Breed Image Recognition via Transfer Learning

Deep learning. Transfer learning. Mind-blowing! How is fast.ai not EVERYWHERE?!

I have been working with neural networks for a little while now.  When I was introduced to the practice, I fell in love immediately. So I have been immersing myself all day every day, completing and compiling my own projects and learning from as many sources as I can find. Recently, one project I was working on led to a brief reference to the work at fast.ai. I was immediately intrigued, but I had no idea how amazed I would be when I took a deeper look into what these Aussies are up to!

I am IN LOVE with this framework.  It is a heavenly blessing to a deep learning fanatic like me. Here is one reason why: I spend a GREAT deal of time writing functions to help me streamline my model training and evaluation work, as well as countless other tasks involved in the deep learning workflow. It can get messy, especially when there are 6 different libraries I am working with and trying to remember which format my data is in where, etc. Well, the angelic humans at fast.ai have not only streamlined all that for me so that I can work with a completely organized system and analyze pretty much any data you could throw at me in minutes, but it all free and open. It is also all documented more thoroughly and cleanly than anything I have ever seen.

fast.ai also has free educational materials for learning their system, and it is so easy to use and dive into the fun parts of this work without having to sift through learning a whole host of new syntax formats and ways of doing things. They literally just took the whole process and made it into a bullet point list of steps and impeccable functions that I can apply to any project my limitless imagination can come up with.


External versions of this project: PDF  |  Jupyter  |  TikTok

In this dataset, we have folders of dog breeds, 10 different breeds and many photos for each breed, which will be split up as 80% for training and 20% for validation, or testing.

Now, the first thing I would like to joyfully point out is...do you see how little I have to import? That's it. Fast.ai does all my PyTorch and all those imports that can fill a page or more of my screen. Here, I have all of 5 lines.

It is easy, breezy, amazing that this one little line, and POOF! I have my dataset, locked and downloaded! Sorry...weird gun reference. My bad.

Now, usually when working with images, you can have screens full of code to just get the images in place, organized, applying transforms to the images, blah blah blah! But not with fast.ai. This is it.

Here I am defining the blocks of data that I will be using. I have my images and then the labels, or categories they belong to, the dog breeds. Then I also split my data into training and validation, and I tell it where to find the labels for the training process. BOOM!

This is where I define what transforms should be applied pre-processing, i.e. resize all photos to be of the same size so the model can work with them, and then the batch transformations that will be applied to all images.

The images are originally categorized by breed, but the labels for the breeds are these n02087394 style codes. To me, the human working with this data, that is not a fun way to label the dog breds. So I will createa dictionary below to make this more human-readable.

Here, I am translating the codes to dog breed labels for training and predicting.

And here, we can see that the labels have now been changed to acutal dog breeds. I now am telling the model where to find those labels when it is time for training.

And this is where I construct the datablock-building process and put it all together for training.

Here is an example from a batch of photos of the dogs and their labels that the model will learn from.

Now, I am defining the model. I am using transfer learning here, meaning I am using a model that is been pre-trained and is known to be robust at image recognition. I will rely on its pre-trained knowledge for a large amount of image recognition here, but then I will fine tune this model with my training data below.

And here is the training, or fine tuning, process. And with only 7 epochs, the model is almost 94% accurate on the validation data at discerning the dog breeds from the images. 93.82% accurate to be exact. I don't know about you, but I am impressed!

And here are some examples of the extremely accurate model predicting the dog breeds, which we humans can visually verify are correct. You can see I have coded it so that it prints the predicted label from the model, and then the images seem to be just as predicted! What a smart little model!

So there you have it! This is just a glimpse at the power of fast.ai. But I hope it has achieved my goal of at least grabbing your interest. I look forward to presenting more in the very near future. So check back soon for more high accuracy percentages and awesome deep learning projects!