Deep Learning with Fast AI and Google Colab
I started learning deep learning from the Fast AI course created by jeremy howard. I heard about this course a while back but couldn’t find the time to complete the course. The shift from a conventional bottom-up approach of learning to a top-down approach can be intimidating at first but as I progress through the course I have started to realise why this approach works. The course can be done on your local system but you will need a very powerful GPU running on your local machine to run the code. To make things easier google provides the colab platform to run your code on a machine where you can setup GPU runtime for free. I will walk you through the process of how to do it and also go through the notebook for lesson-1 from the course.
When you open google colab you will get the below screen.
Select GitHub option from the tabs. For URL type fastai/course-v3, for repository select fastai/course-v3 and select master as the branch. You will see multiple notebooks in the dropdown, select the lesson you are about to start.
Once you are inside the notebook, goto runtime tab and then select change runtime. This will open a pop-up window, in that select GPU as the hardware accelerator and then click save.
Before you start running anything in the notebook you need to run the below script inside the notebook.
!curl -s https://course.fast.ai/setup/colab | bash
This might trigger a warning saying that the code is not authored by google just select RUN ANYWAY.
To make a copy of the notebook just goto File tab and select make a copy in drive. The copied notebook will be saved in the folder colab notebook by default. This completes the setup and now you are ready to run the jupyter notebook using a GPU for free. Now lets go through the lesson-1 notebook.
First thing is to import the necessary fast ai libraries.
from fastai.vision import *
from fastai.metrics import error_rate
One thing that you will notice is that I am importing * which is a big no no for python practitioners but jeremy makes a very important point that when you are trying out new things while training a model it would be irritating to go back and forth to import required libraries. When we will deploy the code in production we can change it and just import the required libraries.
Now lets initialise the batch size as bs=64 we can change it to 16 if we get run out of memory error.
Now we will use fastai method untar_data to get the pets dataset.
path = untar_data(URLs.PETS); path
Now to get list of files or folders fastai uses path.ls(). This will list all the files and folders in the path. We will use the path to the images to get the file names.
Now we will extract the categories of the pets from the image name using internal fastai method and then normalise the images. We will also crop the images to 224*224 size. The ImageDataBunch does the job for us, we are using from_name_re method which lets us pass the reg-ex to pick the category from the image file name.
We will now display the normalised images.
Now we are ready to feed the data into the inbuilt cnn_learner of fastai. This has already trained weights on Image Net which makes our job a lot easier and we don’t have to worry about the defaults.
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
We are using resnet34 architecture because it works very well with image data. We will in future also use resnet50 and compare the results.
Earlier we used learn.fit() to fit our model and train it but recently we have found out that fit_one_cycle has a better performance.
When we train it for 4 cycles we see that the error is around 6.6%.
Now lets save our model so that if we modify it we can get it back if required.
We will now see how our model performed. Fastai provides a wonderful method plot_top_losses to get an idea of where our model went wrong.
I also plotted a confusion matrix but it was quite difficult to look at it and get an idea, luckily i had fastai to the rescue as it provides most_confused method which gives a much better idea of where the model found it difficult to predict.
Our model is working as expected so we can unfreeze it and train some more. Let’s try to find the perfect learning rate to train our model.
From the above graph it is quite evident that we can use the 10^-6 to 10^-4 to train our model. So let’s try it and see if the error rate goes down.
Wow! with around 5–6 lines of actual code we are able to train an image classifier with an accuracy of around 94%.
To get an idea of all the methods we have used and much more visit https://docs.fast.ai/.