Building a Flower Classifier using Fast.ai

Using ResNet-34

Aakanksha NS
Oct 30, 2018 · 3 min read
Image for post
Image for post

To build our flower classifier, we are going to use the Flowers Recognition dataset provided by Kaggle. The pictures are divided into five classes: daisy, tulip, rose, sunflower, dandelion. For each class there are about 800 photos.

Dividing data into Training and Validation Sets

Since our data is not already segregated into training and validation sets, we are going to divide them on our own using the ‘os’ and ‘shutil’ modules. (Assuming current path of dataset is data/flowers-kaggle)

Importing packages and setting batch Size

Now that our training and validation sets are in place, let’s import the necessary packages and set our batch size. We are going to work with the fastai V1 library which sits on top of Pytorch 1.0.

bs = 64

Looking at the Data

We always need to understand very well what the problem is and what the data looks like before we can figure out how to solve it. Taking a look at the data means understanding how the data directories are structured, what the labels are and what some sample images look like.

Your output should look like this:

Image for post
Image for post

Training: resnet34

We are now going to train our model using pretrained weights from the resnet34 convolutional neural network model. We will train for 4 epochs (4 cycles through all our data).

Image for post
Image for post

Congratulations! Our model is now trained and you can see its accuracy is more than 90%.

Testing our classifier on custom input

We can test our classifier by passing it any image of our choice and seeing how well it works. For this, we need to create a folder called test and put our custom file in it. To see the classifier’s output , we first get its predictions on the image for all classes , pick the class which has the highest prediction and then return the class label of that class:

Input image:

Image for post
Image for post
Image for post
Image for post

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Aakanksha NS

Written by

MSDS University of San Francisco | CSE NIT Calicut https://www.linkedin.com/in/aakanksha-ns/

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Aakanksha NS

Written by

MSDS University of San Francisco | CSE NIT Calicut https://www.linkedin.com/in/aakanksha-ns/

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store