Introduction To Deep Learning 🤖 — Chapter 3

Improving Your Machine Learning Model

satyabrata pal
ML and Automation
5 min readOct 10, 2020

--

This is a sequel to Chapter 1 & Chapter 2

Run this code as a kaggle kernel.

All The code for Chapter 1, Chapter 2 and Chapter 3 is available at github.

Flashback📽️

In the previous chapter we created a model to detect if there is a waffle or ice-cream in a given image. We created the model but while scrutinizing that we found that there was one image in particular which was wrongly classified as ice-cream with a very high probability.

👈This image.

So, we talked to our “domain experts” 😜 and we concluded that this should belong to ice-cream and is wrongly labeled as waffles.

Now, we need to change the label of this image from waffle to ice-cream.

Fastai provides an API to do this. This class is ImageClassifierCleaner and the following code gives you a nice GUI to view the images which you may want to change.

The above widget won’t actually change the labels for you. It would return the index which you want to change.

For example, the below image is what should be re-labeled as an ice-cream. So, we go ahead and change the label in the drop down to “ice-cream”.

At this point we can grab the index like so.

clean.change()

see this also gives us the new category to which we want to change the index 26 which was originally labeled as waffle.

There’s one more trick. If you do this then it gives you the filepath for that image👇

clean.fns[26]

This can then be used to create a function to actually move the file to the directory where all the 🍨🍨🍨 are stored. To do this I have a nifty function to do just that.

There is nothing exotic in the above function. You take in the “clean” object and access the change() method of this object. After this use the shutil module in python to move the required file to the required folder.

Does, this do any good? 🤔

There’s one way to find it. Let’s re-train the network.

learn.fine_tune(4)

🤩 our score actually improved.

The above steps which we did above i.e. looking into the results of the model, verifying where it got confused and then cleaning the data to correct potential confusion candidates is one example of the “Human-A.I.” feedback loop.

This is an example which demonstrates the importance of keep a human in the loop who can monitor an A.I system and provide feedback to improve the system. This is also a way to build responsible, ethical and less “dangerous” cognitive systems.

Did you notice something? We built our model first and then cleaned our data. This way of doing things is proposed by Jeremy Howard in the fastai courses.

The claim is that by building the model first we get an idea of what we can achieve with that model and the current state of data. Then we can use the same model to identify pain areas in the data and then improve it.

Does this claim holds? Yes! it definitely does. The improved scores are the proof. Aren’t they?

At this point we have a good model. I will save it so that I can take this model to a different machine and then use it to do prediction on unseen data.

To save the model use the export function provided by fastai. This saves the model’s weight and training data in a file which can then be moved around and used for predictions.

Note that if you are using kaggle then you ill have to set the path to the “kaggle working directory” because fastai by default saves the model in the present working directory.

Is This Model Useful?

This is the question which the consumer of your product will always ask. So, how do we make our model meaningful for the end user? How to make a minimum viable product to pitch our idea to the end user?

We will explore all these questions in Chapter-4.

How To Show Your Support To The Publication🤗

Creating a content requires a lot of research, planning, writing and rewriting . This is important because I want to deliver practical content to you without any fluff.

If you like my work and my content and want to support me then the following are the ways to show your support →

  • If you like my work then click on this link to Buy me a coffee.
  • Buy my deep learning course at udemy. Just click on the course link in the show notes and get awesome discount on my deep learning course.
  • Subscribe to my publication and share it across so that more people can discover it.
  • Subscribe and share my podcast “SimpleAI” on google podcast or any other podcast player of your choice. Don’t forget to give it a 5 star.
  • Subscribe to my newsletter.

--

--

satyabrata pal
ML and Automation

A QA engineer by profession, ML enthusiast by interest, Photography enthusiast by passion and Fitness freak by nature