FastAI Multi-label image classification

FastAI Multi-label image classificationLearn how to work with multi-label dataGilbert TannerBlockedUnblockFollowFollowingFeb 20Figure 1: Planet Amazon datasetThe FastAI library allows us to build models using only a few lines of code.

Furthermore it implements some of the newest state-of-the-art technics taken from research papers that allow you to get state-of-the-art results on almost any type of problem.

This article is my second article covering how to use the FastAI library.

In this article, we will learn how to do multi-label image classification on the Planet Amazon satellite dataset and what differences there are between single- and multi-label classification.

If you don’t know the FastAI library and organization yet, I would highly encourage you to read my last article which gives you some basic information about the FastAi research lab and shows you how to use the FastAi vision module to build an animal detector.

If you prefer a visual tutorial you can check out my FastAi videos.

Getting dataThe first thing we need to do is download the Planet Amazon satelitte dataset from Kaggle.

After this is done we can read in the provided training dataframe using pandas.

Figure 2: training dataframeWe have two columns.

The image_name column which contains the names of the images — without there suffix — and the tags column which contains the labels for each image separated by spaces.

This quick look at the dataframe provides us with enough information to load in the data but before we will to that we will specify which transformations we want to apply to the images.

Because these are satellite images it doesn’t matter what direction they have and so we can enable both do_flip — enabled by default — and flip_vert.

We will also play around with the lighting by tuning the max_lighting parameter, change the zoom by adjusting the max_zoom parameter and disable warp by setting max_warp to 0.

Now that we have our transforms we can load in the data using FastAIs data block api, which provides a lot of different classes and methods for loading in datasets.

Now that we have an ImageItemList we just need to apply our transforms, convert our data in a databunch object and normalize it.

We didn’t do the whole process in a single step because we will first of train on a smaller image size — 128x128px — and then switch to the original size, which is 256x256px.

This is a little trick which was introduced by Jeremy Howard in the Practical Deep Learning for Coders course.

It doesn’t only help train the network faster but it also helps to prevent overfitting.

For more information you should defenetly check out the course, which is completely free.

No that we have our data we can visualize a random batch of images using the show_batch method.

Figure 3: Random data batchTraining modelThe model creation process is just like for a “normal” single-label image classification problem.

Therefore we can just us FastAIs create_cnn method, which can be used to create a convolutional neural network.

The method needs two arguments, the data and the architecture, but also supports many other parameters that can be used to customize the model for a given problem.

One of these other parameters is called metrics.

Metrics basically are all the “scores” printed out whilst training.

The Planet Amazon competition uses the F2-Score to determine the ranking so we will specify the F2-Score and accuracy as our metrics.

But because we are working with multi-label data we can’t just use the normal accuracy and F2-score methods, which are for single label problems but we rather need to set a threshold which determines if the image contains a class.

Normally we would need to find the right value for the threshold but for this problem Jeremy Howard already did that so we will just use the same threshold as he used.

The partial method just creates another method that calles the passed one with a fixed parameter (threshold=0.

2 in our case).

Now that we have our model we can just follow the standard FastAI transfer learning process which we looked into in more depth in my last article so if you want more information about this process you can either check out the article or the first few lessons of the course(specifically lesson 1 and 2).

The only difference is that this time we will perform this process twice.

Once for the 128x128px images and then with the 256x256px images.

Figure 4: Learning rate plotFigure 5: Training outputThe above code only trained the top layers — which were specifically added for our problem — and it didn’t train any of the convolutional layers.

In order to train these we need to call unfreeze.

Now that we have a pretty good model it’s time to change the image size from 128 to 256px.

This can be done by creating a new databunch object and placing it inside learn.

data.

Now the exact same training process can be executed once again.

Lastly we can export our model using the .

export method which saves everything needed in an pkl file.

Submitting to KaggleThe testing data for this competition is split into two separate folders called test-jpg and test-jpg-additional.

We will load the data from each folder using the ImageItemList class, get our predictions calling the get_preds method of our model and then we need to loop through the predictions and check if the prediction is bigger than our threshold.

Lastly we will save the results in a dataframe.

Figure 6: Submission dataframeThe created csv-file can now be submitted to Kaggle which can be done by either going to the competition page and submitting it there directly.

Figure 7: Kaggle submit resultsOr by using the cli.

Recommended ReadingsFastAI Image ClassificationUse the FastAI deep learning library to classify imagestowardsdatascience.

comConclusionThe FastAi library is a high level library build on PyTorch which allows for easy prototyping and gives you access to a lot of state-of-the-art methods/techniques.

The vision module allows us to build Convolutional Neural Networks for different problems without changing the underlying code-pipeline.

If you liked this article consider subscribing to my Youtube Channel and following me on social media.

The code covered in this article is available as a Github Repository.

If you have any questions, recommendations or critiques, I can be reached via Twitter or the comment section.

.

. More details

Leave a Reply