Building a Flower Classifier using Fast.ai
Using ResNet-34
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.
from fastai import *
from fastai.vision import *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:
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).
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: