Image classifier for Indian Men/Women — Fast.ai Deep Learning Course — Part 1 — V3 using Colab
( TEST YOUR MODEL WITH CUSTOM IMAGE)
I wanted to see how i can use smaller datasets with Deep learning techniques for solving practical problems. I trained a model with 60 urban indian men and women images .The validation set contained 10 images each of women and men. I wanted to see how accurate my model was by testing with rural indian men/women.
- DATA COLLECTION
I downloaded 60 images of urban men and women images from Google images and organised the data as shown below. The valid folder had 10 images of men and women ( i tried downloading black and white rural men/women images)
Indian_faces_dataset
|-- train
|-- man (60 images)
|-- woman (50 images)
|-- valid
|-- man (10 images)
|-- woman (10 images)
You can download the dataset at ….
2. Upload data to your Google drive
Sign in to your Google drive using your gmail id. Upload the data. I uploaded it to drive/Mydrive/Fastaidata/Indian_faces_dataset
2. Google Colab
Sign in using your gmail account to Google Colab. Open a new Python3 Notebook.
Set up your free GPU in Colab : Just follow Edit > Notebook settings or Runtime>Change runtime type and select GPU as Hardware accelerator.
Check whether GPU is enabled in your notebook -
3. Set up fast ai version 1 in Google Colab
!pip install torch_nightly -f https://download.pytorch.org/whl/nightly/cu92/torch_nightly.html!pip install fastai
4. Importing Libraries
%reload_ext autoreload
%autoreload 2
%matplotlib inlinefrom fastai import *
from fastai.vision import *
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
5. Mounting Google Drive locally to access data / files from your Drive:
from google.colab import drive
drive.mount('/content/drive/')
When you run the code above, you will get a link. Click the link . Select ‘Allow’. Copy Authorisation code and paste into Enter Authorisation text box and press Enter.
6. Data preprocessing
path = '/content/drive/My Drive/FastaiData/Indian_faces_dataset'
path;
np.random.seed(2)tfms = get_transforms(do_flip=False)
#default bs=64, set image size=100 to run successfully on colab
data = ImageDataBunch.from_folder(path,ds_tfms=tfms, size=100)
data.show_batch(rows=3, figsize=(10,10))
Few images from your training dataset will be shown on 3 rows
7. Building/Training/ Evaluating the model
learn = create_cnn(data, models.resnet34, metrics=error_rate)learn.fit_one_cycle(6)
We get the following output. We can see the error decreases as the number of epoch rises.
Total time: 00:09
epoch train_loss valid_loss error_rate
1 0.976905 1.010379 0.550000 (00:01)
2 0.742076 0.861343 0.450000 (00:01)
3 0.612918 0.711301 0.400000 (00:01)
4 0.529260 0.636116 0.350000 (00:01)
5 0.446259 0.614106 0.350000 (00:01)
6 0.398564 0.598918 0.250000 (00:01)
Save the model .
learn.save('stage-a')
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_top_losses(9, figsize=(15,11))
interp.most_confused(min_val=2)
interp.plot_confusion_matrix()
8. Unfreezing the model and training all layers .
Total time: 00:08
epoch train_loss valid_loss error_rate
1 0.263781 1.136744 0.400000 (00:01)
2 0.184603 1.689964 0.350000 (00:01)
3 0.140850 4.449215 0.350000 (00:01)
4 0.122480 5.085160 0.400000 (00:01)
5 0.110812 3.682216 0.300000 (00:01)
We can see that the error is not improving. Hence we are going to load the model saved as ‘stage-a’ and tune it by changing the learning rates
9. Fine Tuning
learn.lr_find()
learn.recorder.plot()
We can see that the loss reduces from 1e-6 to 1e-3. Let’s use 1e-6 to 1e-3 as the learning rates and see how our model performs.
learn.fit_one_cycle(3, max_lr=slice(1e-6,1e-3))
The output
Total time: 00:05
epoch train_loss valid_loss error_rate
1 0.009076 2.324049 0.300000 (00:01)
2 0.032253 1.738601 0.250000 (00:01)
3 0.026521 1.630889 0.250000 (00:01)
With three epochs we are able to achieve 75% accuracy. Lets check our model against custom images and see how well it predicts.
10. Uploading a custom image.
Download an image that you want to test on your model from google images. Save it in your computer. For example, i have saved an image as testing_image1.jpeg . Upload the image to your drive in the folder where you have the dataset. Then run the code below. This loads the image in filename.
filename = '/content/drive/My Drive/FastaiData/Indian_faces_dataset/testing_image1.jpg'
11. Testing your model with a custom image
img = open_image(filename)
losses = img.predict(learn)
learn.data.classes[losses.argmax()]
It prints out the prediction as
'woman'
12. Checking/ Verifying
You can print your image to verify if the prediction made by your model is right. To print your image type the code below.
img=mpimg.imread(filename)
imgplot = plt.imshow(img)
plt.show()
Quite surprised at the predictions. The accuracy of this model has to be improved. With very little effort, with a very small dataset using the resnet34 model and fastai library, we have got a reasonably good model.
My notebook is available at https://github.com/bhuvanakundumani/ruralvsurbanindianfaces.git